WorldView.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.jxmapviewer.JXMapViewer;
  11. import org.jxmapviewer.OSMTileFactoryInfo;
  12. import org.jxmapviewer.painter.CompoundPainter;
  13. import org.jxmapviewer.painter.Painter;
  14. import org.jxmapviewer.viewer.GeoPosition;
  15. import org.jxmapviewer.viewer.TileFactoryInfo;
  16. import org.jxmapviewer.viewer.WaypointPainter;
  17. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphManager;
  18. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyEdge;
  19. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyNode;
  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. public static HashSet<CustomWaypoint> waypoints;
  53. /**
  54. * the waypoints represented as an ordered list
  55. */
  56. public static ArrayList<CustomWaypoint> waypointsAsList;
  57. /*
  58. * All edges in the WorldView
  59. */
  60. public static HashSet<MyEdge> edges;
  61. public 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<MyEdge>();
  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. // update listener
  135. else {
  136. internMapViewer.removeMouseListener(mapClickListener);
  137. mapClickListener = new CustomMapClickListener(internMapViewer);
  138. internMapViewer.addMouseListener(mapClickListener);
  139. }
  140. internMapViewer.repaint();
  141. // try to load OpenStreesMap, when errors occur, throw and handle
  142. // Exceptions
  143. URL osmWebPage;
  144. try {
  145. // try to connect to OpenStreetMap server
  146. osmWebPage = new URL(info.getBaseURL());
  147. URLConnection connection = osmWebPage.openConnection();
  148. connection.connect();
  149. } catch (MalformedURLException e) {
  150. e.printStackTrace();
  151. }
  152. }
  153. /**
  154. * centers map, so that all waypoints are shown
  155. *
  156. * @param positions
  157. */
  158. public static void showAllWaypoints(HashSet<GeoPosition> positions) {
  159. ArrayList<Point2D> points = new ArrayList<Point2D>(positions.size());
  160. internMapViewer.setZoom(1);
  161. if (positions.size() == 1) {
  162. internMapViewer.setCenterPosition(positions.iterator().next());
  163. return;
  164. }
  165. Double defaultLat = null;
  166. Double defaultLong = null;
  167. Boolean onLineLat = true;
  168. Boolean onLineLong = true;
  169. // geoPositions are all on one line -> JXMapViewer2 method doesnt work
  170. for (GeoPosition geoPos : positions) {
  171. if (defaultLat == null && defaultLong == null) {
  172. defaultLat = geoPos.getLatitude();
  173. defaultLong = geoPos.getLongitude();
  174. }
  175. // there is a geoPosition with a different Latitude then the last
  176. // one
  177. if (!defaultLat.equals(geoPos.getLatitude())) {
  178. onLineLat = false;
  179. }
  180. // there is a geoPosition with a different Longitude then the last
  181. // one
  182. if (!defaultLong.equals(geoPos.getLongitude())) {
  183. onLineLong = false;
  184. }
  185. }
  186. // geoPositions all have the same Latitude
  187. if (onLineLat && !onLineLong) {
  188. HashSet<GeoPosition> newPositions = new HashSet<GeoPosition>();
  189. newPositions.addAll(positions);
  190. GeoPosition newGeoPos = new GeoPosition(positions.iterator().next().getLatitude() + 0.00001,
  191. positions.iterator().next().getLongitude());
  192. newPositions.add(newGeoPos);
  193. internMapViewer.calculateZoomFrom(newPositions);
  194. } else if (onLineLong && !onLineLat) {
  195. // geoPositions all have the same Longitude
  196. HashSet<GeoPosition> newPositions = new HashSet<GeoPosition>();
  197. newPositions.addAll(positions);
  198. GeoPosition newGeoPos = new GeoPosition(positions.iterator().next().getLatitude(),
  199. positions.iterator().next().getLongitude() + 0.00001);
  200. newPositions.add(newGeoPos);
  201. internMapViewer.calculateZoomFrom(newPositions);
  202. } else {
  203. // geoPositions have different Latitude and Longitude
  204. internMapViewer.calculateZoomFrom(positions);
  205. }
  206. positions.forEach((geoPos) -> points.add(internMapViewer.convertGeoPositionToPoint(geoPos)));
  207. double minX = Double.MAX_VALUE;
  208. double maxX = Double.MIN_VALUE;
  209. double minY = Double.MAX_VALUE;
  210. double maxY = Double.MIN_VALUE;
  211. for (Point2D p : points) {
  212. if (p.getX() < minX) {
  213. minX = p.getX();
  214. }
  215. if (p.getX() > maxX) {
  216. maxX = p.getX();
  217. }
  218. if (p.getY() < minY) {
  219. minY = p.getY();
  220. }
  221. if (p.getY() > maxY) {
  222. maxY = p.getY();
  223. }
  224. }
  225. Rectangle2D rect = new Rectangle2D(minX, minY, maxY - minY, maxX - minX);
  226. double xPos = rect.getMinX() + rect.getHeight() / 2;
  227. double yPos = rect.getMinY() + rect.getWidth() / 2;
  228. Point2D center = new Point2D.Double(xPos, yPos);
  229. internMapViewer.setCenterPosition(internMapViewer.convertPointToGeoPosition(center));
  230. }
  231. /**
  232. * Initialize HashSets with data from graph
  233. *
  234. * @param nodePositions
  235. * Read node data to create GeoPositions of all nodes
  236. * @param waypoints
  237. * Read node data to create CustomWaypoints with deviceTypes
  238. */
  239. public static void fetchGraphData() {
  240. GraphManager man = GraphDisplayManager.getGraphManager(Layer.UNDERLAY);
  241. // add all edges from the Graph to the HashSet
  242. for (MyEdge edge : man.getGraph().<MyEdge>getEdgeSet()) {
  243. edges.add(edge);
  244. }
  245. // fetch all needed data from nodes
  246. for (MyNode node : man.getGraph().<MyNode>getEachNode()) {
  247. if (node.hasAttribute("lat") && node.hasAttribute("long")) {
  248. // Fetch all geo-data from nodes
  249. Double latitude = node.getAttribute("lat");
  250. Double longitude = node.getAttribute("long");
  251. GeoPosition geoPos = new GeoPosition(latitude.doubleValue(), longitude.doubleValue());
  252. nodePositions.add(geoPos);
  253. // Create waypoints with device type dependent pictures
  254. String deviceType = (String) node.getAttribute("typeofDevice");
  255. URL resource = getDeviceTypeURL(deviceType);
  256. // create a new waypoint with the node information
  257. CustomWaypoint waypoint = new CustomWaypoint(node.getAttribute("ui.label"), node.getId(), resource,
  258. deviceType, geoPos);
  259. waypoints.add(waypoint);
  260. waypointsAsList.add(waypoint);
  261. }
  262. }
  263. }
  264. /**
  265. * get the png URL based on the device.type of nodes
  266. *
  267. * @param deviceType
  268. * @return
  269. */
  270. public static URL getDeviceTypeURL(String deviceType) {
  271. URL image = MainApp.class
  272. .getResource("/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/symbol_icons/" + deviceType + ".png");
  273. if (image == null) {
  274. return MainApp.class
  275. .getResource("/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/symbol_icons/not_found.png");
  276. }
  277. else {
  278. return image;
  279. }
  280. }
  281. public static HashSet<CustomWaypoint> getWaypoints() {
  282. return waypoints;
  283. }
  284. public static HashSet<GeoPosition> getNodePositions() {
  285. return nodePositions;
  286. }
  287. public static HashSet<MyEdge> getEdges() {
  288. return edges;
  289. }
  290. public static ArrayList<CustomWaypoint> getWaypointsAsArrayList() {
  291. return waypointsAsList;
  292. }
  293. }