Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* вычисление координат станций с помощью force-based алгоритма
- *
- * @author nastia.malysheva
- */
- import java.sql.*;
- import java.util.*;
- class Station {
- private int esr;
- private boolean fixed;
- private double x, y;
- private double fx, fy;
- Station(int esr, double x, double y, boolean fixed) {
- this.esr = esr;
- this.x = x;
- this.y = y;
- this.fixed = fixed;
- System.out.println(this);
- }
- boolean isFixed() {
- return fixed;
- }
- int getEsr() {
- return esr;
- }
- public double getFx() {
- return fx;
- }
- public double getFy() {
- return fy;
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- public void addF(double fx, double fy) {
- this.fx += fx;
- this.fy += fy;
- }
- public void addXY(double x, double y) {
- this.x += x;
- this.y += y;
- }
- public void zeroF() {
- fx = 0;
- fy = 0;
- }
- public String toString() {
- return "id: " + esr + ", x: " + x + " y: " + y;
- }
- }
- //список станций
- class Stations extends ArrayList<Station> {
- void add(int esr, double x, double y, boolean fixed) {
- super.add(new Station(esr, x, y, fixed));
- }
- void zeroForces() {
- for (Station s : this) {
- s.zeroF();
- }
- }
- }
- //граф связей станций
- class Connections extends HashMap<Integer, List<Integer>> {
- public void put(int esrFrom, int esrTo) {
- if (containsKey(esrFrom)) {
- get(esrFrom).add(esrTo);
- } else {
- List<Integer> list = new ArrayList<Integer>();
- list.add(esrTo);
- put(esrFrom, list);
- }
- }
- public boolean isConnected(int esrFrom, int esrTo) {
- if (containsKey(esrFrom)) {
- if (get(esrFrom).contains(esrTo)) {
- return true;
- }
- }
- if (containsKey(esrTo)) {
- if (get(esrTo).contains(esrFrom)) {
- return true;
- }
- }
- return false;
- }
- }
- public class StationsCoord {
- private static Stations stations = new Stations();
- private static Connections connections = new Connections();
- public static final int XMIN = 6400000;
- public static final int YMIN = 5600000;
- public static final int DX = 900000;
- public static final int DY = 500000;
- //iterationsNum - количество итераций force-based алгоритма
- //чем больше, тем выше качество, но ниже производительность
- public static void calculate(int iterationsNum) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String connectionUrl = "jdbc:mysql://localhost/skat?user=root&password=0Y2uiPCt2ARj";
- Connection dbConn = DriverManager.getConnection(connectionUrl);
- String sql = "select esr, fixed, latitude, longitude from stations";
- ResultSet rs = dbConn.createStatement().executeQuery(sql);
- //координаты станций должны быть уникальны
- Set<Long> setUniq = new HashSet<Long>();
- Random rnd = new Random();
- int esr;
- double x, y;
- boolean fixed;
- long key;
- while (rs.next()) {
- esr = Integer.parseInt(rs.getString("esr"));
- //fixed - у таких станций координаты заданы вручную
- if (rs.getString("fixed") != null) {
- fixed = true;
- } else {
- fixed = false;
- }
- //если в БД нет координат, они задаются случайно
- if (rs.getString("latitude") == null || rs.getString("longitude") == null) {
- while (true) {
- x = rnd.nextInt(DX) + XMIN;
- y = rnd.nextInt(DY) + YMIN;
- key = (long) (x * 10000000L + y);
- if (!setUniq.contains(key)) {
- setUniq.add(key);
- break;
- }
- x /= 100000;
- y /= 100000;
- }
- if (fixed) {
- fixed = false;
- }
- } else {
- x = (int) (Double.parseDouble(rs.getString("latitude")) * 100000);
- y = (int) (Double.parseDouble(rs.getString("longitude")) * 100000);
- key = (long) (x * 10000000L + y);
- setUniq.add(key);
- }
- stations.add(esr, x, y, fixed);
- }
- sql = "select esrfrom, esrto from nsi_run_list";
- rs = dbConn.createStatement().executeQuery(sql);
- while (rs.next()) {
- int esrFrom = Integer.parseInt(rs.getString("esrfrom"));
- int esrTo = Integer.parseInt(rs.getString("esrto"));
- connections.put(esrFrom, esrTo);
- }
- //начало работы алгоритма
- double kCoulomb = 0.00005;
- double kHooke = 0.1;
- for (int step = 0; step < iterationsNum; step++) {
- stations.zeroForces();
- for (Station stationFrom : stations) {
- for (Station stationTo : stations) {
- if (stationFrom.getEsr() == stationTo.getEsr() || stationTo.isFixed()) {
- continue;
- }
- double dx = stationTo.getX() - stationFrom.getX();
- double dy = stationTo.getY() - stationFrom.getY();
- double dist = Math.sqrt(dx * dx + dy * dy);
- double force = kCoulomb / (dist * dist);
- if (connections.isConnected(stationFrom.getEsr(), stationTo.getEsr())) {
- force += -kHooke * (dist - 0.01);
- }
- stationTo.addF(force * dx / dist, force * dy / dist);
- }
- }
- for (Station s : stations) {
- s.addXY(s.getFx(), s.getFy());
- }
- }
- for (Station s : stations) {
- sql = "update stations set coord=GeomFromWKB(Point(" +
- s.getX() + "," + s.getY() + "),4326)," +
- " latitude=" + s.getX() + ", longitude=" + s.getY() +
- " where esr=" + s.getEsr();
- dbConn.createStatement().executeUpdate(sql);
- }
- } catch (SQLException e) {
- System.out.println("SQL Exception: " + e.toString());
- } catch (ClassNotFoundException cE) {
- System.out.println("Class Not Found Exception: " + cE.toString());
- }
- }
- static public void String toString() {
- return stations.toString();
- }
- public static void main(String[] args) {
- StationsCoord.calculate(10000);
- System.out.println(StationsCoord);
- }
- }
Add Comment
Please, Sign In to add comment