WorldView.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 javafx.geometry.Rectangle2D;
  25. public class WorldView {
  26. /*
  27. * intern map viewer
  28. */
  29. public static JXMapViewer internMapViewer;
  30. /*
  31. * edgePainter of overlayPainter
  32. */
  33. public static EdgePainter edgePainter;
  34. /*
  35. * waypointPointer of overlayPainter
  36. */
  37. public static WaypointPainter<CustomWaypoint> waypointPainter;
  38. /*
  39. * mapClickListener, used to only initialize the Listener once
  40. */
  41. public static CustomMapClickListener mapClickListener;
  42. /*
  43. * GUIController with UI elements
  44. */
  45. public static GUIController controller;
  46. /*
  47. * All waypoints in the WorldView
  48. */
  49. public static HashSet<CustomWaypoint> waypoints;
  50. /**
  51. * the waypoints represented as an ordered list
  52. */
  53. public static ArrayList<CustomWaypoint> waypointsAsList;
  54. /*
  55. * All edges in the WorldView
  56. */
  57. public static HashSet<MyEdge> edges;
  58. public static HashSet<GeoPosition> nodePositions;
  59. /*
  60. * All painter in symbolLayer stored in a list
  61. */
  62. public static List<Painter<JXMapViewer>> painters;
  63. /**
  64. * private constructor to avoid instantiation
  65. */
  66. private WorldView() {
  67. }
  68. /**
  69. * initialize attributes internMapViewer and controller
  70. *
  71. * @param mapViewer
  72. * @param guiController
  73. */
  74. public static void initAttributes(JXMapViewer mapViewer, GUIController guiController) {
  75. internMapViewer = mapViewer;
  76. controller = guiController;
  77. }
  78. /**
  79. * load map elements based on current underlay graph
  80. *
  81. * @throws IOException
  82. */
  83. public static void loadWorldView() throws IOException {
  84. nodePositions = new HashSet<GeoPosition>();
  85. waypoints = new HashSet<CustomWaypoint>();
  86. edges = new HashSet<MyEdge>();
  87. waypointsAsList = new ArrayList<CustomWaypoint>();
  88. // Get GeoPositions of nodes and get all waypoints created
  89. fetchGraphData();
  90. MapViewFunctions.initializeWaypointImages();
  91. // Create a line for all edges
  92. edgePainter = new EdgePainter(edges);
  93. // Create a waypoint painter that takes all the waypoints
  94. waypointPainter = new WaypointPainter<CustomWaypoint>();
  95. waypointPainter.setWaypoints(waypoints);
  96. waypointPainter.setRenderer(new CustomWaypointRenderer());
  97. // Create a compound painter that uses all painters
  98. painters = new ArrayList<Painter<JXMapViewer>>();
  99. painters.add(edgePainter);
  100. painters.add(waypointPainter);
  101. CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
  102. // Create a TileFactoryInfo for OpenStreetMap
  103. TileFactoryInfo info = new OSMTileFactoryInfo();
  104. CustomTileFactory tileFactory = new CustomTileFactory(info);
  105. if (!internMapViewer.getTileFactory().equals(tileFactory)) {
  106. internMapViewer.setTileFactory(tileFactory);
  107. }
  108. // Use 8 threads in parallel to load the tiles
  109. tileFactory.setThreadPoolSize(8);
  110. showAllWaypoints(nodePositions);
  111. internMapViewer.setOverlayPainter(painter);
  112. // set listener the first time
  113. if (mapClickListener == null) {
  114. mapClickListener = new CustomMapClickListener(internMapViewer);
  115. // "click on waypoints" listener
  116. internMapViewer.addMouseListener(mapClickListener);
  117. }
  118. // update listener
  119. else {
  120. internMapViewer.removeMouseListener(mapClickListener);
  121. mapClickListener = new CustomMapClickListener(internMapViewer);
  122. internMapViewer.addMouseListener(mapClickListener);
  123. }
  124. internMapViewer.repaint();
  125. // try to load OpenStreesMap, when errors occur, throw and handle
  126. // Exceptions
  127. URL osmWebPage;
  128. try {
  129. // try to connect to OpenStreetMap server
  130. osmWebPage = new URL(info.getBaseURL());
  131. URLConnection connection = osmWebPage.openConnection();
  132. connection.connect();
  133. } catch (MalformedURLException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. /**
  138. * centers map, so that all waypoints are shown
  139. *
  140. * @param positions
  141. */
  142. public static void showAllWaypoints(HashSet<GeoPosition> positions) {
  143. ArrayList<Point2D> points = new ArrayList<Point2D>(positions.size());
  144. internMapViewer.setZoom(1);
  145. internMapViewer.calculateZoomFrom(positions);
  146. positions.forEach((geoPos) -> points.add(internMapViewer.convertGeoPositionToPoint(geoPos)));
  147. double minX = Double.MAX_VALUE;
  148. double maxX = Double.MIN_VALUE;
  149. double minY = Double.MAX_VALUE;
  150. double maxY = Double.MIN_VALUE;
  151. for (Point2D p : points) {
  152. if (p.getX() < minX) {
  153. minX = p.getX();
  154. }
  155. if (p.getX() > maxX) {
  156. maxX = p.getX();
  157. }
  158. if (p.getY() < minY) {
  159. minY = p.getY();
  160. }
  161. if (p.getY() > maxY) {
  162. maxY = p.getY();
  163. }
  164. }
  165. Rectangle2D rect = new Rectangle2D(minX, minY, maxY - minY, maxX - minX);
  166. double xPos = rect.getMinX() + rect.getHeight() / 2;
  167. double yPos = rect.getMinY() + rect.getWidth() / 2;
  168. Point2D center = new Point2D.Double(xPos, yPos);
  169. internMapViewer.setCenterPosition(internMapViewer.convertPointToGeoPosition(center));
  170. }
  171. /**
  172. * Initialize HashSets with data from graph
  173. *
  174. * @param nodePositions
  175. * Read node data to create GeoPositions of all nodes
  176. * @param waypoints
  177. * Read node data to create CustomWaypoints with deviceTypes
  178. */
  179. public static void fetchGraphData() {
  180. GraphManager man = GraphDisplayManager.getGraphManager(Layer.UNDERLAY);
  181. // add all edges from the Graph to the HashSet
  182. for (MyEdge edge : man.getGraph().<MyEdge>getEdgeSet()) {
  183. edges.add(edge);
  184. }
  185. // fetch all needed data from nodes
  186. for (MyNode node : man.getGraph().<MyNode>getEachNode()) {
  187. if (node.hasAttribute("lat") && node.hasAttribute("long")) {
  188. // Fetch all geo-data from nodes
  189. Double latitude = node.getAttribute("lat");
  190. Double longitude = node.getAttribute("long");
  191. GeoPosition geoPos = new GeoPosition(latitude.doubleValue(), longitude.doubleValue());
  192. nodePositions.add(geoPos);
  193. // Create waypoints with device type dependent pictures
  194. String deviceType = (String) node.getAttribute("typeofDevice");
  195. URL resource = getDeviceTypeURL(deviceType);
  196. // create a new waypoint with the node information
  197. CustomWaypoint waypoint = new CustomWaypoint(node.getAttribute("ui.label"), node.getId(), resource,
  198. deviceType, geoPos);
  199. waypoints.add(waypoint);
  200. waypointsAsList.add(waypoint);
  201. }
  202. }
  203. }
  204. /**
  205. * get the png URL based on the device.type of nodes
  206. *
  207. * @param deviceType
  208. * @return
  209. */
  210. public static URL getDeviceTypeURL(String deviceType) {
  211. URL image = MainApp.class
  212. .getResource("/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/symbol_icons/" + deviceType + ".png");
  213. if (image == null) {
  214. return MainApp.class
  215. .getResource("/de/tu_darmstadt/informatik/tk/scopviz/ui/mapView/symbol_icons/not_found.png");
  216. }
  217. else {
  218. return image;
  219. }
  220. }
  221. public static HashSet<CustomWaypoint> getWaypoints() {
  222. return waypoints;
  223. }
  224. }