WorldView.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui.mapView;
  2. import java.awt.geom.Point2D;
  3. import java.io.IOException;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.util.ArrayList;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import org.graphstream.graph.Edge;
  11. import org.graphstream.graph.Node;
  12. import org.jxmapviewer.JXMapViewer;
  13. import org.jxmapviewer.OSMTileFactoryInfo;
  14. import org.jxmapviewer.painter.CompoundPainter;
  15. import org.jxmapviewer.painter.Painter;
  16. import org.jxmapviewer.viewer.GeoPosition;
  17. import org.jxmapviewer.viewer.TileFactoryInfo;
  18. import org.jxmapviewer.viewer.WaypointPainter;
  19. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphManager;
  20. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  21. import de.tu_darmstadt.informatik.tk.scopviz.main.MainApp;
  22. import de.tu_darmstadt.informatik.tk.scopviz.ui.GUIController;
  23. import de.tu_darmstadt.informatik.tk.scopviz.ui.GraphDisplayManager;
  24. import de.tu_darmstadt.informatik.tk.scopviz.ui.OptionsManager;
  25. import javafx.geometry.Rectangle2D;
  26. import javafx.scene.control.Alert;
  27. import javafx.scene.control.Alert.AlertType;
  28. public class WorldView {
  29. /*
  30. * intern map viewer
  31. */
  32. public static JXMapViewer internMapViewer;
  33. /*
  34. * edgePainter of overlayPainter
  35. */
  36. public static EdgePainter edgePainter;
  37. /*
  38. * waypointPointer of overlayPainter
  39. */
  40. public static WaypointPainter<CustomWaypoint> waypointPainter;
  41. /*
  42. * mapClickListener, used to only initialize the Listener once
  43. */
  44. public static CustomMapClickListener mapClickListener;
  45. /*
  46. * GUIController with UI elements
  47. */
  48. public static GUIController controller;
  49. /*
  50. * All waypoints in the WorldView
  51. */
  52. private static HashSet<CustomWaypoint> waypoints;
  53. /**
  54. * the waypoints represented as an ordered list
  55. */
  56. private static ArrayList<CustomWaypoint> waypointsAsList;
  57. /*
  58. * All edges in the WorldView
  59. */
  60. private static HashSet<Edge> edges;
  61. private static HashSet<GeoPosition> nodePositions;
  62. /*
  63. * All painter in symbolLayer stored in a list
  64. */
  65. public static List<Painter<JXMapViewer>> painters;
  66. /**
  67. * private constructor to avoid instantiation
  68. */
  69. private WorldView() {
  70. }
  71. /**
  72. * initialize attributes internMapViewer and controller
  73. *
  74. * @param mapViewer
  75. * @param guiController
  76. */
  77. public static void initAttributes(JXMapViewer mapViewer, GUIController guiController) {
  78. internMapViewer = mapViewer;
  79. controller = guiController;
  80. }
  81. /**
  82. * load map elements based on current underlay graph
  83. *
  84. * @throws IOException
  85. */
  86. public static void loadWorldView() throws IOException {
  87. nodePositions = new HashSet<GeoPosition>();
  88. waypoints = new HashSet<CustomWaypoint>();
  89. edges = new HashSet<Edge>();
  90. waypointsAsList = new ArrayList<CustomWaypoint>();
  91. // Get GeoPositions of nodes and get all waypoints created
  92. fetchGraphData();
  93. // underlay is empty
  94. if (waypoints.size() == 0) {
  95. Alert alert = new Alert(AlertType.WARNING);
  96. alert.setTitle("Warning");
  97. alert.setHeaderText("Underlay Empty");
  98. alert.setContentText("The referenced Underlay-Graph has no nodes to visualize");
  99. alert.showAndWait();
  100. GeoPosition defaultGeoPos = new GeoPosition(OptionsManager.getDefaultLat(), OptionsManager.getDefaultLong());
  101. nodePositions.add(defaultGeoPos);
  102. CustomWaypoint defaultWaypoint = new CustomWaypoint("", "", getDeviceTypeURL(""), "", defaultGeoPos);
  103. waypoints.add(defaultWaypoint);
  104. waypointsAsList.add(defaultWaypoint);
  105. }
  106. MapViewFunctions.initializeWaypointImages();
  107. // Create a line for all edges
  108. edgePainter = new EdgePainter(edges);
  109. // Create a waypoint painter that takes all the waypoints
  110. waypointPainter = new WaypointPainter<CustomWaypoint>();
  111. waypointPainter.setWaypoints(waypoints);
  112. waypointPainter.setRenderer(new CustomWaypointRenderer());
  113. // Create a compound painter that uses all painters
  114. painters = new ArrayList<Painter<JXMapViewer>>();
  115. painters.add(edgePainter);
  116. painters.add(waypointPainter);
  117. CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
  118. // Create a TileFactoryInfo for OpenStreetMap
  119. TileFactoryInfo info = new OSMTileFactoryInfo();
  120. if (!internMapViewer.getTileFactory().equals(null)) {
  121. CustomTileFactory tileFactory = new CustomTileFactory(info);
  122. internMapViewer.setTileFactory(tileFactory);
  123. }
  124. // Use 8 threads in parallel to load the tiles
  125. ((CustomTileFactory) internMapViewer.getTileFactory()).setThreadPoolSize(8);
  126. showAllWaypoints(nodePositions);
  127. internMapViewer.setOverlayPainter(painter);
  128. // set listener the first time
  129. if (mapClickListener == null) {
  130. mapClickListener = new CustomMapClickListener(internMapViewer);
  131. // "click on waypoints" listener
  132. internMapViewer.addMouseListener(mapClickListener);
  133. }
  134. internMapViewer.repaint();
  135. // try to load OpenStreesMap, when errors occur, throw and handle
  136. // Exceptions
  137. URL osmWebPage;
  138. try {
  139. // try to connect to OpenStreetMap server
  140. osmWebPage = new URL(info.getBaseURL());
  141. URLConnection connection = osmWebPage.openConnection();
  142. connection.connect();
  143. } catch (MalformedURLException e) {
  144. e.printStackTrace();
  145. }
  146. }
  147. /**
  148. * centers map, so that all waypoints are shown
  149. *
  150. * @param positions
  151. */
  152. public static void showAllWaypoints(HashSet<GeoPosition> positions) {
  153. ArrayList<Point2D> points = new ArrayList<Point2D>(positions.size());
  154. internMapViewer.setZoom(1);
  155. if (positions.size() == 1) {
  156. internMapViewer.setCenterPosition(positions.iterator().next());
  157. return;
  158. }
  159. Double defaultLat = null;
  160. Double defaultLong = null;
  161. Boolean onLineLat = true;
  162. Boolean onLineLong = true;
  163. // geoPositions are all on one line -> JXMapViewer2 method doesnt work
  164. for (GeoPosition geoPos : positions) {
  165. if (defaultLat == null && defaultLong == null) {
  166. defaultLat = geoPos.getLatitude();
  167. defaultLong = geoPos.getLongitude();
  168. }
  169. // there is a geoPosition with a different Latitude then the last
  170. // one
  171. if (!defaultLat.equals(geoPos.getLatitude())) {
  172. onLineLat = false;
  173. }
  174. // there is a geoPosition with a different Longitude then the last
  175. // one
  176. if (!defaultLong.equals(geoPos.getLongitude())) {
  177. onLineLong = false;
  178. }
  179. }
  180. // geoPositions all have the same Latitude
  181. if (onLineLat && !onLineLong) {
  182. HashSet<GeoPosition> newPositions = new HashSet<GeoPosition>();
  183. newPositions.addAll(positions);
  184. GeoPosition newGeoPos = new GeoPosition(positions.iterator().next().getLatitude() + 0.00001,
  185. positions.iterator().next().getLongitude());
  186. newPositions.add(newGeoPos);
  187. internMapViewer.calculateZoomFrom(newPositions);
  188. } else if (onLineLong && !onLineLat) {
  189. // geoPositions all have the same Longitude
  190. HashSet<GeoPosition> newPositions = new HashSet<GeoPosition>();
  191. newPositions.addAll(positions);
  192. GeoPosition newGeoPos = new GeoPosition(positions.iterator().next().getLatitude(),
  193. positions.iterator().next().getLongitude() + 0.00001);
  194. newPositions.add(newGeoPos);
  195. internMapViewer.calculateZoomFrom(newPositions);
  196. } else {
  197. // geoPositions have different Latitude and Longitude
  198. internMapViewer.calculateZoomFrom(positions);
  199. }
  200. positions.forEach((geoPos) -> points.add(internMapViewer.convertGeoPositionToPoint(geoPos)));
  201. double minX = Double.MAX_VALUE;
  202. double maxX = Double.MIN_VALUE;
  203. double minY = Double.MAX_VALUE;
  204. double maxY = Double.MIN_VALUE;
  205. for (Point2D p : points) {
  206. if (p.getX() < minX) {
  207. minX = p.getX();
  208. }
  209. if (p.getX() > maxX) {
  210. maxX = p.getX();
  211. }
  212. if (p.getY() < minY) {
  213. minY = p.getY();
  214. }
  215. if (p.getY() > maxY) {
  216. maxY = p.getY();
  217. }
  218. }
  219. Rectangle2D rect = new Rectangle2D(minX, minY, maxY - minY, maxX - minX);
  220. double xPos = rect.getMinX() + rect.getHeight() / 2;
  221. double yPos = rect.getMinY() + rect.getWidth() / 2;
  222. Point2D center = new Point2D.Double(xPos, yPos);
  223. internMapViewer.setCenterPosition(internMapViewer.convertPointToGeoPosition(center));
  224. }
  225. /**
  226. * Initialize HashSets with data from graph
  227. *
  228. * @param nodePositions
  229. * Read node data to create GeoPositions of all nodes
  230. * @param waypoints
  231. * Read node data to create CustomWaypoints with deviceTypes
  232. */
  233. public static void fetchGraphData() {
  234. GraphManager man = GraphDisplayManager.getGraphManager(Layer.UNDERLAY);
  235. // add all edges from the Graph to the HashSet
  236. for (Edge edge : man.getGraph().getEdgeSet()) {
  237. edges.add(edge);
  238. }
  239. // fetch all needed data from nodes
  240. for (Node node : man.getGraph().getEachNode()) {
  241. if (node.hasAttribute("lat") && node.hasAttribute("long")) {
  242. // Fetch all geo-data from nodes
  243. Double latitude = node.getAttribute("lat");
  244. Double longitude = node.getAttribute("long");
  245. GeoPosition geoPos = new GeoPosition(latitude.doubleValue(), longitude.doubleValue());
  246. nodePositions.add(geoPos);
  247. // Create waypoints with device type dependent pictures
  248. String deviceType = (String) node.getAttribute("typeofDevice");
  249. URL resource = getDeviceTypeURL(deviceType);
  250. // create a new waypoint with the node information
  251. CustomWaypoint waypoint = new CustomWaypoint(node.getAttribute("ui.label"), node.getId(), resource,
  252. deviceType, geoPos);
  253. waypoints.add(waypoint);
  254. waypointsAsList.add(waypoint);
  255. }
  256. }
  257. }
  258. /**
  259. * get the png URL based on the device.type of nodes
  260. *
  261. * @param deviceType
  262. * @return
  263. */
  264. public static URL getDeviceTypeURL(String deviceType) {
  265. URL image = MainApp.class
  266. .getResource("/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/symbol_icons/" + deviceType + ".png");
  267. if (image == null) {
  268. return MainApp.class
  269. .getResource("/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/symbol_icons/not_found.png");
  270. }
  271. else {
  272. return image;
  273. }
  274. }
  275. public static HashSet<CustomWaypoint> getWaypoints() {
  276. return waypoints;
  277. }
  278. public static HashSet<GeoPosition> getNodePositions() {
  279. return nodePositions;
  280. }
  281. public static HashSet<Edge> getEdges() {
  282. return edges;
  283. }
  284. public static ArrayList<CustomWaypoint> getWaypointsAsArrayList() {
  285. return waypointsAsList;
  286. }
  287. }