Browse Source

Create new drawing using Mapsforge.

Mikael Capelle 6 years ago
parent
commit
d5af267227
32 changed files with 1225 additions and 379 deletions
  1. 42
    0
      .classpath
  2. 0
    6
      .project
  3. BIN
      libs/kxml2-2.3.0.jar
  4. BIN
      libs/mapsforge-core-0.9.1-javadoc.jar
  5. BIN
      libs/mapsforge-core-0.9.1.jar
  6. BIN
      libs/mapsforge-map-0.9.1-javadoc.jar
  7. BIN
      libs/mapsforge-map-0.9.1.jar
  8. BIN
      libs/mapsforge-map-awt-0.9.1-javadoc.jar
  9. BIN
      libs/mapsforge-map-awt-0.9.1.jar
  10. BIN
      libs/mapsforge-map-reader-0.9.1-javadoc.jar
  11. BIN
      libs/mapsforge-map-reader-0.9.1.jar
  12. BIN
      libs/mapsforge-poi-0.9.1-javadoc.jar
  13. BIN
      libs/mapsforge-poi-0.9.1.jar
  14. BIN
      libs/mapsforge-poi-awt-0.9.1-javadoc.jar
  15. BIN
      libs/mapsforge-poi-awt-0.9.1.jar
  16. BIN
      libs/mapsforge-themes-0.9.1-javadoc.jar
  17. BIN
      libs/mapsforge-themes-0.9.1.jar
  18. BIN
      libs/svg-salamander-1.0-javadoc.jar
  19. BIN
      libs/svg-salamander-1.0.jar
  20. 2
    8
      src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentGraphicObserver.java
  21. 77
    25
      src/main/org/insa/base/MainWindow.java
  22. 285
    0
      src/main/org/insa/base/Samples.java
  23. 314
    0
      src/main/org/insa/drawing/BasicDrawing.java
  24. 1
    1
      src/main/org/insa/drawing/BasicGraphPalette.java
  25. 1
    1
      src/main/org/insa/drawing/BlackAndWhiteGraphPalette.java
  26. 93
    153
      src/main/org/insa/drawing/Drawing.java
  27. 1
    1
      src/main/org/insa/drawing/GraphPalette.java
  28. 237
    0
      src/main/org/insa/drawing/MapViewDrawing.java
  29. 0
    132
      src/main/org/insa/drawing/graph/GraphDrawing.java
  30. 0
    52
      src/main/org/insa/drawing/graph/PathDrawing.java
  31. 2
    0
      src/main/org/insa/graph/io/BinaryGraphReader.java
  32. 170
    0
      src/main/org/insa/graph/io/BinaryGraphReaderV2.java

+ 42
- 0
.classpath View File

@@ -23,5 +23,47 @@
23 23
 			<attribute name="maven.pomderived" value="true"/>
24 24
 		</attributes>
25 25
 	</classpathentry>
26
+	<classpathentry kind="lib" path="libs/mapsforge-core-0.9.1.jar">
27
+		<attributes>
28
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-core-0.9.1-javadoc.jar!/"/>
29
+		</attributes>
30
+	</classpathentry>
31
+	<classpathentry kind="lib" path="libs/mapsforge-map-0.9.1.jar">
32
+		<attributes>
33
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-0.9.1-javadoc.jar!/"/>
34
+		</attributes>
35
+	</classpathentry>
36
+	<classpathentry kind="lib" path="libs/mapsforge-map-awt-0.9.1.jar">
37
+		<attributes>
38
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-awt-0.9.1-javadoc.jar!/"/>
39
+		</attributes>
40
+	</classpathentry>
41
+	<classpathentry kind="lib" path="libs/mapsforge-map-reader-0.9.1.jar">
42
+		<attributes>
43
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-reader-0.9.1-javadoc.jar!/"/>
44
+		</attributes>
45
+	</classpathentry>
46
+	<classpathentry kind="lib" path="libs/mapsforge-poi-0.9.1.jar">
47
+		<attributes>
48
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-0.9.1-javadoc.jar!/"/>
49
+		</attributes>
50
+	</classpathentry>
51
+	<classpathentry kind="lib" path="libs/mapsforge-poi-awt-0.9.1.jar">
52
+		<attributes>
53
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-awt-0.9.1-javadoc.jar!/"/>
54
+		</attributes>
55
+	</classpathentry>
56
+	<classpathentry kind="lib" path="libs/mapsforge-themes-0.9.1.jar">
57
+		<attributes>
58
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-themes-0.9.1-javadoc.jar!/"/>
59
+		</attributes>
60
+	</classpathentry>
61
+	<classpathentry kind="lib" path="libs/kxml2-2.3.0.jar"/>
62
+	<classpathentry kind="lib" path="libs/svg-salamander-1.0.jar">
63
+		<attributes>
64
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/svg-salamander-1.0-javadoc.jar!/"/>
65
+		</attributes>
66
+	</classpathentry>
67
+	<classpathentry kind="lib" path="libs/mapsforge-map-writer-0.9.1-jar-with-dependencies.jar"/>
26 68
 	<classpathentry kind="output" path="target/classes"/>
27 69
 </classpath>

+ 0
- 6
.project View File

@@ -10,14 +10,8 @@
10 10
 			<arguments>
11 11
 			</arguments>
12 12
 		</buildCommand>
13
-		<buildCommand>
14
-			<name>org.eclipse.m2e.core.maven2Builder</name>
15
-			<arguments>
16
-			</arguments>
17
-		</buildCommand>
18 13
 	</buildSpec>
19 14
 	<natures>
20
-		<nature>org.eclipse.m2e.core.maven2Nature</nature>
21 15
 		<nature>org.eclipse.jdt.core.javanature</nature>
22 16
 	</natures>
23 17
 </projectDescription>

BIN
libs/kxml2-2.3.0.jar View File


BIN
libs/mapsforge-core-0.9.1-javadoc.jar View File


BIN
libs/mapsforge-core-0.9.1.jar View File


BIN
libs/mapsforge-map-0.9.1-javadoc.jar View File


BIN
libs/mapsforge-map-0.9.1.jar View File


BIN
libs/mapsforge-map-awt-0.9.1-javadoc.jar View File


BIN
libs/mapsforge-map-awt-0.9.1.jar View File


BIN
libs/mapsforge-map-reader-0.9.1-javadoc.jar View File


BIN
libs/mapsforge-map-reader-0.9.1.jar View File


BIN
libs/mapsforge-poi-0.9.1-javadoc.jar View File


BIN
libs/mapsforge-poi-0.9.1.jar View File


BIN
libs/mapsforge-poi-awt-0.9.1-javadoc.jar View File


BIN
libs/mapsforge-poi-awt-0.9.1.jar View File


BIN
libs/mapsforge-themes-0.9.1-javadoc.jar View File


BIN
libs/mapsforge-themes-0.9.1.jar View File


BIN
libs/svg-salamander-1.0-javadoc.jar View File


BIN
libs/svg-salamander-1.0.jar View File


+ 2
- 8
src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentGraphicObserver.java View File

@@ -4,7 +4,6 @@ import java.awt.Color;
4 4
 import java.util.ArrayList;
5 5
 
6 6
 import org.insa.drawing.Drawing;
7
-import org.insa.drawing.graph.GraphDrawing;
8 7
 import org.insa.graph.Node;
9 8
 
10 9
 public class WeaklyConnectedComponentGraphicObserver implements WeaklyConnectedComponentObserver {
@@ -15,27 +14,22 @@ public class WeaklyConnectedComponentGraphicObserver implements WeaklyConnectedC
15 14
 
16 15
 	// Drawing + Graph drawing
17 16
 	private Drawing drawing;
18
-	private GraphDrawing gdrawing;
19 17
 	
20 18
 	// Current index color
21
-	private int cindex = 0;
19
+	private int cindex = -1;
22 20
 	
23 21
 	public WeaklyConnectedComponentGraphicObserver(Drawing drawing) {
24 22
 		this.drawing = drawing;
25
-		this.gdrawing = new GraphDrawing(drawing);
26
-		this.drawing.setAutoRepaint(true);
27 23
 	}
28 24
 
29 25
 	@Override
30 26
 	public void notifyStartComponent(Node curNode) {
31
-		this.drawing.setColor(COLORS[cindex]);
32 27
 		cindex = (cindex + 1) % COLORS.length;
33 28
 	}
34 29
 
35 30
 	@Override
36 31
 	public void notifyNewNodeInComponent(Node node) {
37
-		this.gdrawing.drawPoint(node.getPoint(), 5);
38
-		this.drawing.repaint();
32
+		this.drawing.drawMarker(node.getPoint(), COLORS[cindex]);
39 33
 	}
40 34
 
41 35
 	@Override

+ 77
- 25
src/main/org/insa/base/MainWindow.java View File

@@ -2,6 +2,7 @@ package org.insa.base;
2 2
 
3 3
 import java.awt.BorderLayout;
4 4
 import java.awt.Color;
5
+import java.awt.Component;
5 6
 import java.awt.Dimension;
6 7
 import java.awt.event.ActionEvent;
7 8
 import java.awt.event.ActionListener;
@@ -11,6 +12,8 @@ import java.awt.event.MouseEvent;
11 12
 import java.awt.event.WindowAdapter;
12 13
 import java.awt.event.WindowEvent;
13 14
 import java.awt.geom.NoninvertibleTransformException;
15
+import java.io.DataInputStream;
16
+import java.io.DataOutputStream;
14 17
 import java.io.File;
15 18
 import java.io.IOException;
16 19
 import java.io.OutputStream;
@@ -47,15 +50,17 @@ import org.insa.algo.shortestpath.ShortestPathSolution;
47 50
 import org.insa.algo.weakconnectivity.WeaklyConnectedComponentGraphicObserver;
48 51
 import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsAlgorithm;
49 52
 import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsData;
53
+import org.insa.drawing.BasicDrawing;
54
+import org.insa.drawing.BlackAndWhiteGraphPalette;
50 55
 import org.insa.drawing.Drawing;
51
-import org.insa.drawing.graph.BlackAndWhiteGraphPalette;
52
-import org.insa.drawing.graph.GraphDrawing;
53
-import org.insa.drawing.graph.PathDrawing;
56
+import org.insa.drawing.MapViewDrawing;
54 57
 import org.insa.graph.Graph;
55 58
 import org.insa.graph.Node;
56 59
 import org.insa.graph.Path;
57 60
 import org.insa.graph.Point;
61
+import org.insa.graph.io.AbstractGraphReader;
58 62
 import org.insa.graph.io.BinaryGraphReader;
63
+import org.insa.graph.io.BinaryGraphReaderV2;
59 64
 import org.insa.graph.io.BinaryPathReader;
60 65
 import org.insa.graph.io.MapMismatchException;
61 66
 import org.insa.graph.io.Openfile;
@@ -132,7 +137,8 @@ public class MainWindow extends JFrame {
132 137
         		}
133 138
         		Point lonlat;
134 139
 			try {
135
-				lonlat = drawing.getLongitudeLatitude(evt);
140
+				// TODO: Fix
141
+				lonlat = ((BasicDrawing)drawing).getLongitudeLatitude(evt);
136 142
 			} 
137 143
 			catch (NoninvertibleTransformException e) {
138 144
 				// Should never happens in "normal" circumstances... 
@@ -142,7 +148,7 @@ public class MainWindow extends JFrame {
142 148
 			
143 149
 			Node node = graph.findClosestNode(lonlat);
144 150
 			
145
-			new GraphDrawing(drawing).drawPoint(node.getPoint(), 10, Color.BLUE);
151
+			drawing.drawMarker(node.getPoint(), Color.BLUE);
146 152
         		points.add(node);
147 153
         		if (points.size() == nTargetPoints) {
148 154
         			callable.call(points);
@@ -176,6 +182,9 @@ public class MainWindow extends JFrame {
176 182
 	private Drawing drawing;
177 183
 	private DrawingClickListener clickAdapter;
178 184
 	
185
+	// Main panel.
186
+	private JSplitPane mainPanel;
187
+	
179 188
 	// List of item for the top menus.
180 189
 	private JMenuItem openMapItem;
181 190
 
@@ -219,13 +228,17 @@ public class MainWindow extends JFrame {
219 228
 		});
220 229
 
221 230
 		// Create graph area
222
-		JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
231
+		mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
223 232
 
224
-		drawing = new Drawing();
233
+		BasicDrawing drawing = new BasicDrawing();
234
+		// MapViewDrawing drawing = new MapViewDrawing();
235
+		
236
+		Component drawingComponent = drawing;
237
+		this.drawing = drawing;
225 238
 		
226 239
 		// Click adapter
227 240
 		this.clickAdapter = new DrawingClickListener();
228
-		drawing.addMouseListener(this.clickAdapter);
241
+		// drawing.addMouseListener(this.clickAdapter);
229 242
 
230 243
 		JTextArea infoPanel = new JTextArea();
231 244
 		infoPanel.setMinimumSize(new Dimension(200, 50));
@@ -236,14 +249,14 @@ public class MainWindow extends JFrame {
236 249
 		this.logStream = new JOutputStream(infoPanel);
237 250
 		this.printStream = new PrintStream(this.logStream);
238 251
 
239
-		sp.setResizeWeight(0.8);
252
+		mainPanel.setResizeWeight(0.8);
240 253
 		// sp.setEnabled(false);
241
-		sp.setDividerSize(5);
254
+		mainPanel.setDividerSize(5);
242 255
 
243
-		sp.setBackground(Color.WHITE);
244
-		sp.add(drawing);
245
-		sp.add(new JScrollPane(infoPanel));		
246
-		this.add(sp, BorderLayout.CENTER);
256
+		mainPanel.setBackground(Color.WHITE);
257
+		mainPanel.add(drawingComponent);
258
+		mainPanel.add(new JScrollPane(infoPanel));	
259
+		this.add(mainPanel, BorderLayout.CENTER);
247 260
 		
248 261
 		// Top Panel
249 262
 		this.add(createTopPanel(), BorderLayout.NORTH);		
@@ -299,7 +312,7 @@ public class MainWindow extends JFrame {
299 312
 			public void run() {
300 313
 				ShortestPathSolution solution = spAlgorithm.run();
301 314
 				if (solution != null && solution.isFeasible()) {
302
-					new PathDrawing(drawing).drawPath(solution.getPath());
315
+					drawing.drawPath(solution.getPath());
303 316
 				}
304 317
 			}
305 318
 		});
@@ -317,30 +330,38 @@ public class MainWindow extends JFrame {
317 330
 			public void actionPerformed(ActionEvent e) {
318 331
 				JFileChooser chooser = new JFileChooser();
319 332
 				FileNameExtensionFilter filter = new FileNameExtensionFilter(
320
-						"Map & compressed map files", "map", "map.gz");
333
+						"Map & compressed map files", "map", "map2", "map.gz");
321 334
 				chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
322 335
 				chooser.setFileFilter(filter);
323 336
 				if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
324 337
 					launchThread(new Runnable() {
325 338
 						@Override
326 339
 						public void run() {
327
-							BinaryGraphReader reader;
340
+							String path = chooser.getSelectedFile().getAbsolutePath();
341
+							DataInputStream stream;
328 342
 							try {
329
-								reader = new BinaryGraphReader(
330
-										Openfile.open(chooser.getSelectedFile().getAbsolutePath()));
343
+								stream = Openfile.open(path);
331 344
 							} catch (IOException e1) {
332 345
 								JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file.");
333 346
 								return ;
334 347
 							}
348
+							AbstractGraphReader reader;
349
+							if (path.endsWith(".map2")) {
350
+								reader = new BinaryGraphReaderV2(stream);
351
+							}
352
+							else {
353
+								reader = new BinaryGraphReader(stream);
354
+							}
335 355
 							try {
336 356
 								graph = reader.read();
337 357
 							}
338 358
 							catch (Exception exception) {
339 359
 								JOptionPane.showMessageDialog(MainWindow.this, "Unable to read graph from the selected file.");
360
+								exception.printStackTrace(System.out);
340 361
 								return ;
341 362
 							}
342 363
 							drawing.clear();
343
-							new GraphDrawing(drawing).drawGraph(graph);
364
+							drawing.drawGraph(graph);
344 365
 
345 366
 							for (JMenuItem item: graphLockItems) {
346 367
 								item.setEnabled(true);
@@ -384,7 +405,7 @@ public class MainWindow extends JFrame {
384 405
 						JOptionPane.showMessageDialog(MainWindow.this, "Unable to read path from the selected file.");
385 406
 						return ;
386 407
 					}
387
-					new PathDrawing(drawing).drawPath(currentPath);
408
+					drawing.drawPath(currentPath);
388 409
 				}
389 410
 			}
390 411
 		});
@@ -418,9 +439,13 @@ public class MainWindow extends JFrame {
418 439
 				launchThread(new Runnable() {
419 440
 					@Override
420 441
 					public void run() {
442
+						if (!(drawing instanceof BasicDrawing)) {
443
+							BasicDrawing tmp = new BasicDrawing();
444
+							mainPanel.setLeftComponent(tmp);
445
+							drawing = tmp;
446
+						}
421 447
 						drawing.clear();
422
-						drawing.setAutoRepaint(true);
423
-						new GraphDrawing(drawing).drawGraph(graph);				
448
+						drawing.drawGraph(graph);	
424 449
 					}
425 450
 				});
426 451
 			}
@@ -435,18 +460,45 @@ public class MainWindow extends JFrame {
435 460
 				launchThread(new Runnable() {
436 461
 					@Override
437 462
 					public void run() {
463
+						if (!(drawing instanceof BasicDrawing)) {
464
+							BasicDrawing tmp = new BasicDrawing();
465
+							mainPanel.setLeftComponent(tmp);
466
+							drawing = tmp;
467
+						}
438 468
 						drawing.clear();
439
-						drawing.setAutoRepaint(true);
440
-						new GraphDrawing(drawing, new BlackAndWhiteGraphPalette()).drawGraph(graph);				
469
+						drawing.drawGraph(graph, new BlackAndWhiteGraphPalette());			
441 470
 					}
442 471
 				});
443 472
 			}
444 473
 		});
445 474
 		graphLockItems.add(drawGraphBWItem);
475
+		JMenuItem drawGraphMapsforgeItem = new JMenuItem("Redraw (Map)", KeyEvent.VK_M);
476
+		drawGraphMapsforgeItem.setAccelerator(KeyStroke.getKeyStroke(
477
+				KeyEvent.VK_M, ActionEvent.ALT_MASK));
478
+		drawGraphMapsforgeItem.addActionListener(new ActionListener() {		
479
+			@Override
480
+			public void actionPerformed(ActionEvent e) {
481
+				launchThread(new Runnable() {
482
+					@Override
483
+					public void run() {
484
+						if (!(drawing instanceof MapViewDrawing)) {
485
+							MapViewDrawing tmp = new MapViewDrawing();
486
+							mainPanel.setLeftComponent(tmp);
487
+							drawing = tmp;
488
+						}
489
+						drawing.clear();
490
+						drawing.drawGraph(graph, new BlackAndWhiteGraphPalette());			
491
+					}
492
+				});
493
+			}
494
+		});
495
+		graphLockItems.add(drawGraphMapsforgeItem);
446 496
 
447 497
 		JMenu graphMenu = new JMenu("Graph");
448 498
 		graphMenu.add(drawGraphItem);
449 499
 		graphMenu.add(drawGraphBWItem);
500
+		graphMenu.addSeparator();
501
+		graphMenu.add(drawGraphMapsforgeItem);
450 502
 
451 503
 		// Algo menu
452 504
 		JMenu algoMenu = new JMenu("Algorithms");

+ 285
- 0
src/main/org/insa/base/Samples.java View File

@@ -0,0 +1,285 @@
1
+/*
2
+ * Copyright 2010, 2011, 2012, 2013 mapsforge.org
3
+ * Copyright 2014 Christian Pesch
4
+ * Copyright 2014 Ludwig M Brinckmann
5
+ * Copyright 2014-2018 devemux86
6
+ * Copyright 2017 usrusr
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify it under the
9
+ * terms of the GNU Lesser General Public License as published by the Free Software
10
+ * Foundation, either version 3 of the License, or (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
13
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License along with
17
+ * this program. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+package org.insa.base;
20
+
21
+import org.insa.graph.Arc;
22
+import org.insa.graph.Graph;
23
+import org.insa.graph.Path;
24
+import org.insa.graph.io.BinaryGraphReader;
25
+import org.insa.graph.io.BinaryPathReader;
26
+import org.insa.graph.io.Openfile;
27
+import org.mapsforge.core.graphics.Color;
28
+import org.mapsforge.core.graphics.GraphicFactory;
29
+import org.mapsforge.core.graphics.Paint;
30
+import org.mapsforge.core.graphics.Style;
31
+import org.mapsforge.core.model.BoundingBox;
32
+import org.mapsforge.core.model.LatLong;
33
+import org.mapsforge.core.model.MapPosition;
34
+import org.mapsforge.core.model.Point;
35
+import org.mapsforge.core.util.LatLongUtils;
36
+import org.mapsforge.core.util.Parameters;
37
+import org.mapsforge.core.util.Utils;
38
+import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
39
+import org.mapsforge.map.awt.util.AwtUtil;
40
+import org.mapsforge.map.awt.util.JavaPreferences;
41
+import org.mapsforge.map.awt.view.MapView;
42
+import org.mapsforge.map.datastore.MapDataStore;
43
+import org.mapsforge.map.datastore.MultiMapDataStore;
44
+import org.mapsforge.map.layer.Layers;
45
+import org.mapsforge.map.layer.cache.TileCache;
46
+import org.mapsforge.map.layer.debug.TileCoordinatesLayer;
47
+import org.mapsforge.map.layer.debug.TileGridLayer;
48
+import org.mapsforge.map.layer.download.TileDownloadLayer;
49
+import org.mapsforge.map.layer.download.tilesource.OpenStreetMapMapnik;
50
+import org.mapsforge.map.layer.download.tilesource.TileSource;
51
+import org.mapsforge.map.layer.hills.DiffuseLightShadingAlgorithm;
52
+import org.mapsforge.map.layer.hills.HillsRenderConfig;
53
+import org.mapsforge.map.layer.hills.MemoryCachingHgtReaderTileSource;
54
+import org.mapsforge.map.layer.overlay.Polyline;
55
+import org.mapsforge.map.layer.renderer.TileRendererLayer;
56
+import org.mapsforge.map.model.MapViewPosition;
57
+import org.mapsforge.map.model.Model;
58
+import org.mapsforge.map.model.common.PreferencesFacade;
59
+import org.mapsforge.map.reader.MapFile;
60
+import org.mapsforge.map.rendertheme.InternalRenderTheme;
61
+
62
+import java.awt.Dimension;
63
+import java.awt.event.WindowAdapter;
64
+import java.awt.event.WindowEvent;
65
+import java.io.File;
66
+import java.io.IOException;
67
+import java.util.ArrayList;
68
+import java.util.Arrays;
69
+import java.util.List;
70
+import java.util.UUID;
71
+import java.util.prefs.Preferences;
72
+
73
+import javax.swing.JFrame;
74
+import javax.swing.JOptionPane;
75
+import javax.swing.WindowConstants;
76
+
77
+public final class Samples {
78
+    private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
79
+    private static final boolean SHOW_DEBUG_LAYERS = false;
80
+    private static final boolean SHOW_RASTER_MAP = false;
81
+
82
+    private static final String MESSAGE = "Are you sure you want to exit the application?";
83
+    private static final String TITLE = "Confirm close";
84
+
85
+    /**
86
+     * Starts the {@code Samples}.
87
+     *
88
+     * @param args command line args: expects the map files as multiple parameters
89
+     *             with possible SRTM hgt folder as 1st argument.
90
+     * @throws Exception 
91
+     */
92
+    public static void main(String[] args) throws Exception {
93
+    	
94
+        // Multithreaded map rendering
95
+        Parameters.NUMBER_OF_THREADS = 2;
96
+
97
+        // Square frame buffer
98
+        Parameters.SQUARE_FRAME_BUFFER = false;
99
+
100
+        HillsRenderConfig hillsCfg = null;
101
+        File demFolder = getDemFolder(args);
102
+        if (demFolder != null) {
103
+            MemoryCachingHgtReaderTileSource tileSource = new MemoryCachingHgtReaderTileSource(demFolder, new DiffuseLightShadingAlgorithm(), AwtGraphicFactory.INSTANCE);
104
+            tileSource.setEnableInterpolationOverlap(true);
105
+            hillsCfg = new HillsRenderConfig(tileSource);
106
+            hillsCfg.indexOnThread();
107
+            args = Arrays.copyOfRange(args, 1, args.length);
108
+        }
109
+
110
+        List<File> mapFiles = getMapFiles(args);
111
+        final MapView mapView = createMapView();
112
+        final BoundingBox boundingBox = addLayers(mapView, mapFiles, hillsCfg);
113
+        
114
+        // addAPath(mapView);
115
+
116
+        final PreferencesFacade preferencesFacade = new JavaPreferences(Preferences.userNodeForPackage(Samples.class));
117
+
118
+        final JFrame frame = new JFrame();
119
+        frame.setTitle("Mapsforge Samples");
120
+        frame.add(mapView);
121
+        frame.pack();
122
+        frame.setSize(new Dimension(800, 600));
123
+        frame.setLocationRelativeTo(null);
124
+        frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
125
+        frame.addWindowListener(new WindowAdapter() {
126
+            @Override
127
+            public void windowClosing(WindowEvent e) {
128
+                int result = JOptionPane.showConfirmDialog(frame, MESSAGE, TITLE, JOptionPane.YES_NO_OPTION);
129
+                if (result == JOptionPane.YES_OPTION) {
130
+                    mapView.getModel().save(preferencesFacade);
131
+                    mapView.destroyAll();
132
+                    AwtGraphicFactory.clearResourceMemoryCache();
133
+                    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
134
+                }
135
+            }
136
+
137
+            @Override
138
+            public void windowOpened(WindowEvent e) {
139
+                final Model model = mapView.getModel();
140
+                model.init(preferencesFacade);
141
+                if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) {
142
+                    byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, model.displayModel.getTileSize());
143
+                    model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
144
+                }
145
+            }
146
+        });
147
+        frame.setVisible(true);
148
+    }
149
+    
150
+    private static void addAPath(MapView mapView) throws Exception {
151
+
152
+    		Graph gr = (new BinaryGraphReader(Openfile.open("Maps/midip.map"))).read();
153
+    		Path path = (new BinaryPathReader(Openfile.open("Paths/chemin_0x400_119963_96676.path"))).readPath(gr);
154
+    		
155
+    		Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
156
+    		paintStroke.setColor(Color.GREEN);
157
+    		paintStroke.setStrokeWidth(3);
158
+    		paintStroke.setStyle(Style.STROKE);        
159
+
160
+        Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
161
+
162
+        for (Arc arc: path.getArcs()) {
163
+        		ArrayList<org.insa.graph.Point> points = arc.getPoints();
164
+        		for (int i = 0; i < points.size(); ++i) {
165
+        			line.getLatLongs().add(new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
166
+        		}
167
+        }
168
+        
169
+        
170
+        mapView.getLayerManager().getLayers().add(line);
171
+    }
172
+
173
+    private static BoundingBox addLayers(MapView mapView, List<File> mapFiles, HillsRenderConfig hillsRenderConfig) {
174
+        Layers layers = mapView.getLayerManager().getLayers();
175
+
176
+        int tileSize = SHOW_RASTER_MAP ? 256 : 512;
177
+
178
+        // Tile cache
179
+        TileCache tileCache = AwtUtil.createTileCache(
180
+                tileSize,
181
+                mapView.getModel().frameBufferModel.getOverdrawFactor(),
182
+                1024,
183
+                new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
184
+
185
+        final BoundingBox boundingBox;
186
+        if (SHOW_RASTER_MAP) {
187
+            // Raster
188
+            mapView.getModel().displayModel.setFixedTileSize(tileSize);
189
+            TileSource tileSource = OpenStreetMapMapnik.INSTANCE;
190
+            TileDownloadLayer tileDownloadLayer = createTileDownloadLayer(tileCache, mapView.getModel().mapViewPosition, tileSource);
191
+            layers.add(tileDownloadLayer);
192
+            tileDownloadLayer.start();
193
+            mapView.setZoomLevelMin(tileSource.getZoomLevelMin());
194
+            mapView.setZoomLevelMax(tileSource.getZoomLevelMax());
195
+            boundingBox = new BoundingBox(LatLongUtils.LATITUDE_MIN, LatLongUtils.LONGITUDE_MIN, LatLongUtils.LATITUDE_MAX, LatLongUtils.LONGITUDE_MAX);
196
+        } else {
197
+            // Vector
198
+            mapView.getModel().displayModel.setFixedTileSize(tileSize);
199
+            MultiMapDataStore mapDataStore = new MultiMapDataStore(MultiMapDataStore.DataPolicy.RETURN_ALL);
200
+            for (File file : mapFiles) {
201
+                mapDataStore.addMapDataStore(new MapFile(file), false, false);
202
+            }
203
+            TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore, mapView.getModel().mapViewPosition, hillsRenderConfig);
204
+            layers.add(tileRendererLayer);
205
+            boundingBox = mapDataStore.boundingBox();
206
+        }
207
+
208
+        // Debug
209
+        if (SHOW_DEBUG_LAYERS) {
210
+            layers.add(new TileGridLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
211
+            layers.add(new TileCoordinatesLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
212
+        }
213
+
214
+        return boundingBox;
215
+    }
216
+
217
+    private static MapView createMapView() {
218
+        MapView mapView = new MapView();
219
+        mapView.getMapScaleBar().setVisible(true);
220
+        if (SHOW_DEBUG_LAYERS) {
221
+            mapView.getFpsCounter().setVisible(true);
222
+        }
223
+
224
+        return mapView;
225
+    }
226
+
227
+    @SuppressWarnings("unused")
228
+    private static TileDownloadLayer createTileDownloadLayer(TileCache tileCache, MapViewPosition mapViewPosition, TileSource tileSource) {
229
+        return new TileDownloadLayer(tileCache, mapViewPosition, tileSource, GRAPHIC_FACTORY) {
230
+            @Override
231
+            public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
232
+                System.out.println("Tap on: " + tapLatLong);
233
+                return true;
234
+            }
235
+        };
236
+    }
237
+
238
+    private static TileRendererLayer createTileRendererLayer(TileCache tileCache, MapDataStore mapDataStore, MapViewPosition mapViewPosition, HillsRenderConfig hillsRenderConfig) {
239
+        TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
240
+            @Override
241
+            public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
242
+                System.out.println("Tap on: " + tapLatLong);
243
+                return true;
244
+            }
245
+        };
246
+        tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT);
247
+        return tileRendererLayer;
248
+    }
249
+
250
+    private static File getDemFolder(String[] args) {
251
+        if (args.length == 0) {
252
+            throw new IllegalArgumentException("missing argument: <mapFile>");
253
+        }
254
+
255
+        File demFolder = new File(args[0]);
256
+        if (demFolder.exists() && demFolder.isDirectory() && demFolder.canRead()) {
257
+            return demFolder;
258
+        }
259
+        return null;
260
+    }
261
+
262
+    private static List<File> getMapFiles(String[] args) {
263
+        if (args.length == 0) {
264
+            throw new IllegalArgumentException("missing argument: <mapFile>");
265
+        }
266
+
267
+        List<File> result = new ArrayList<>();
268
+        for (String arg : args) {
269
+            File mapFile = new File(arg);
270
+            if (!mapFile.exists()) {
271
+                throw new IllegalArgumentException("file does not exist: " + mapFile);
272
+            } else if (!mapFile.isFile()) {
273
+                throw new IllegalArgumentException("not a file: " + mapFile);
274
+            } else if (!mapFile.canRead()) {
275
+                throw new IllegalArgumentException("cannot read file: " + mapFile);
276
+            }
277
+            result.add(mapFile);
278
+        }
279
+        return result;
280
+    }
281
+
282
+    private Samples() {
283
+        throw new IllegalStateException();
284
+    }
285
+}

+ 314
- 0
src/main/org/insa/drawing/BasicDrawing.java View File

@@ -0,0 +1,314 @@
1
+package org.insa.drawing;
2
+
3
+import java.awt.BasicStroke;
4
+import java.awt.Color;
5
+import java.awt.Graphics;
6
+import java.awt.Graphics2D;
7
+import java.awt.Image;
8
+import java.awt.event.MouseEvent;
9
+import java.awt.geom.NoninvertibleTransformException;
10
+import java.awt.geom.Point2D;
11
+import java.awt.image.*;
12
+import java.util.ArrayList;
13
+import java.util.Iterator;
14
+
15
+import javax.swing.JPanel;
16
+
17
+import org.insa.graph.Arc;
18
+import org.insa.graph.Graph;
19
+import org.insa.graph.Node;
20
+import org.insa.graph.Path;
21
+import org.insa.graph.Point;
22
+
23
+/**
24
+ *   Cette implementation de la classe Dessin produit vraiment un affichage
25
+ *   (au contraire de la classe DessinInvisible).
26
+ */
27
+
28
+public class BasicDrawing extends JPanel implements Drawing {
29
+
30
+	/**
31
+	 * 
32
+	 */
33
+	private static final long serialVersionUID = 96779785877771827L;
34
+	
35
+	// Default path color.
36
+	public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255);
37
+
38
+	// Default palette.
39
+	public static final GraphPalette DEFAULT_PALETTE = new BasicGraphPalette();
40
+	
41
+	// Default marker width
42
+	private static final int DEFAULT_MARKER_WIDTH = 10;
43
+	
44
+	// 
45
+	private final Graphics2D gr;
46
+
47
+	private double long1, long2, lat1, lat2;
48
+	
49
+	// Width and height of the image
50
+	private final int width, height;
51
+	
52
+	private Image image;
53
+	private ZoomAndPanListener zoomAndPanListener;
54
+	
55
+	/**
56
+	 * Create a new BasicDrawing.
57
+	 * 
58
+	 */
59
+	public BasicDrawing() {
60
+		
61
+		this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2);
62
+		this.addMouseListener(zoomAndPanListener);
63
+		this.addMouseMotionListener(zoomAndPanListener);
64
+		this.addMouseWheelListener(zoomAndPanListener);
65
+		
66
+		this.width = 2000;
67
+		this.height = 1600;
68
+		
69
+		BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR);
70
+		
71
+		this.image = img;
72
+		this.gr = img.createGraphics();
73
+		
74
+		this.zoomAndPanListener.setCoordTransform(this.gr.getTransform());
75
+
76
+		this.long1 = -180;
77
+		this.long2 = 180;
78
+		this.lat1  = -90;
79
+		this.lat2  = 90;
80
+
81
+		this.clear();
82
+		this.repaint();
83
+
84
+	}
85
+
86
+	@Override
87
+	public void paintComponent(Graphics g1) {
88
+		Graphics2D g = (Graphics2D)g1;
89
+		g.clearRect(0, 0, getWidth(), getHeight());
90
+		g.setTransform(zoomAndPanListener.getCoordTransform());
91
+		g.drawImage(image, 0, 0, this);
92
+	}
93
+
94
+	protected void setBB(double long1, double long2, double lat1, double lat2) {	
95
+
96
+		if (long1 > long2 || lat1 > lat2) {
97
+			throw new Error("DessinVisible.setBB : mauvaises coordonnees.");
98
+		}
99
+				
100
+		this.long1 = long1;
101
+		this.long2 = long2;
102
+		this.lat1= lat1;
103
+		this.lat2 = lat2;
104
+				
105
+		double scale = 1 / Math.max(this.width / (double)this.getWidth(),  this.height / (double)this.getHeight());
106
+		
107
+		this.zoomAndPanListener.getCoordTransform().setToIdentity();
108
+		this.zoomAndPanListener.getCoordTransform().translate((this.getWidth() - this.width * scale) / 2, 
109
+				(this.getHeight() - this.height * scale) / 2);
110
+		this.zoomAndPanListener.getCoordTransform().scale(scale, scale);
111
+		this.zoomAndPanListener.setZoomLevel(0);
112
+		this.repaint();
113
+		
114
+	}
115
+
116
+	private int projx(double lon) {
117
+		return (int)(width * (lon - this.long1) / (this.long2 - this.long1)) ;
118
+	}
119
+
120
+	private int projy(double lat) {
121
+		return (int)(height * (1 - (lat - this.lat1) / (this.lat2 - this.lat1))) ;
122
+	}
123
+	
124
+	/**
125
+	 * Return the longitude and latitude corresponding to the given
126
+	 * position of the MouseEvent.
127
+	 * 
128
+	 * @param event
129
+	 * 
130
+	 * @return
131
+	 */
132
+	public Point getLongitudeLatitude(MouseEvent event) throws NoninvertibleTransformException {
133
+		// Get the point using the inverse transform of the Zoom/Pan object, this gives us
134
+		// a point within the drawing box (between [0, 0] and [width, height]).
135
+		Point2D ptDst = this.zoomAndPanListener.getCoordTransform().inverseTransform(event.getPoint(), null);
136
+		
137
+		// Inverse the "projection" on x/y to get longitude and latitude.
138
+		double lon = ptDst.getX();
139
+		double lat = ptDst.getY();
140
+		lon = (lon / this.width) * (this.long2 - this.long1) + this.long1;
141
+		lat = (1 - lat / this.height) * (this.lat2 - this.lat1) + this.lat1;
142
+		
143
+		// Return a new point.
144
+		return new Point(lon, lat);			
145
+	}
146
+	
147
+	protected void setWidth(int width) {
148
+		this.gr.setStroke(new BasicStroke(width));
149
+	}
150
+
151
+	protected void setColor(Color col) {
152
+		this.gr.setColor(col);
153
+	}
154
+	
155
+	@Override
156
+	public void clear() {
157
+		this.gr.setColor(Color.WHITE);
158
+		this.gr.fillRect(0, 0, this.width, this.height);
159
+	}
160
+
161
+	@Override
162
+	public void drawLine(Point from, Point to) {
163
+		int x1 = this.projx(from.getLongitude()) ;
164
+		int x2 = this.projx(to.getLongitude()) ;
165
+		int y1 = this.projy(from.getLatitude()) ;
166
+		int y2 = this.projy(to.getLatitude()) ;
167
+
168
+		gr.drawLine(x1, y1, x2, y2) ;
169
+		this.repaint();	
170
+	}
171
+
172
+	@Override
173
+	public void drawLine(Point from, Point to, int width) {
174
+		setWidth(width);
175
+		drawLine(from, to);		
176
+	}
177
+
178
+	@Override
179
+	public void drawLine(Point from, Point to, int width, Color color) {
180
+		setWidth(width);
181
+		setColor(color);	
182
+		drawLine(from, to);
183
+	}
184
+	
185
+	@Override
186
+	public void drawMarker(Point point) {
187
+		drawPoint(point, DEFAULT_MARKER_WIDTH, this.gr.getColor());
188
+	}
189
+
190
+
191
+	@Override
192
+	public void drawMarker(Point point, Color color) {
193
+		setColor(color);
194
+		drawMarker(point);
195
+	}
196
+	
197
+	@Override
198
+	public void drawPoint(Point point, int width, Color color) {
199
+		setWidth(width);
200
+		setColor(color);
201
+		int x = this.projx(point.getLongitude()) - DEFAULT_MARKER_WIDTH / 2;
202
+		int y = this.projy(point.getLatitude()) - DEFAULT_MARKER_WIDTH / 2;
203
+		gr.fillOval(x, y, DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH);
204
+		this.repaint();
205
+	}
206
+	
207
+	/**
208
+	 * Draw the given arc.
209
+	 * 
210
+	 * @param arc Arc to draw.
211
+	 * @param palette Palette to use to retrieve color and width for arc,
212
+	 * or null to use current settings.
213
+	 */
214
+	public void drawArc(Arc arc, GraphPalette palette) {
215
+		ArrayList<Point> pts = arc.getPoints();
216
+		if (!pts.isEmpty()) {
217
+			if (palette != null) {
218
+				setColor(palette.getColorForType(arc.getInfo().getType()));
219
+				setWidth(palette.getWidthForType(arc.getInfo().getType()));
220
+			}
221
+			Iterator<Point> it1 = pts.iterator();
222
+			Point prev = it1.next();
223
+			while (it1.hasNext()) {
224
+				Point curr = it1.next();
225
+				drawLine(prev, curr);
226
+				prev = curr;
227
+			}
228
+		}
229
+	}
230
+	
231
+	/**
232
+	 * Initialize the drawing for the given graph.
233
+	 * 
234
+	 * @param graph
235
+	 */
236
+	public void initialize(Graph graph) {
237
+		double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY, 
238
+				maxLon = Double.NEGATIVE_INFINITY, maxLat = Double.NEGATIVE_INFINITY;
239
+		for (Node node: graph.getNodes()) {
240
+			Point pt = node.getPoint();
241
+			if (pt.getLatitude() < minLat) {
242
+				minLat = pt.getLatitude();
243
+			}
244
+			if (pt.getLatitude() > maxLat) {
245
+				maxLat = pt.getLatitude();
246
+			}
247
+			if (pt.getLongitude() < minLon) {
248
+				minLon = pt.getLongitude();
249
+			}
250
+			if (pt.getLongitude() > maxLon) {
251
+				maxLon = pt.getLongitude();
252
+			}
253
+		}
254
+		
255
+		double deltaLon = 0.02 * (maxLon - minLon),
256
+				deltaLat = 0.02 * (maxLat - minLat);
257
+
258
+		setBB(minLon - deltaLon, maxLon + deltaLon, 
259
+			minLat - deltaLat, maxLat + deltaLat);
260
+	}
261
+
262
+	@Override
263
+	public void drawGraph(Graph graph, GraphPalette palette) {
264
+		clear();
265
+		initialize(graph);
266
+		for (Node node: graph.getNodes()) {
267
+			for (Arc arc: node.getSuccessors()) {
268
+				drawArc(arc, palette);
269
+			}
270
+		}
271
+	}
272
+
273
+	@Override
274
+	public void drawGraph(Graph graph) {
275
+		drawGraph(graph, DEFAULT_PALETTE);		
276
+	}
277
+
278
+	@Override
279
+	public void drawPath(Path path, Color color, boolean markers) {
280
+		setColor(color);
281
+		setWidth(2);
282
+		for (Arc arc: path.getArcs()) {
283
+			drawArc(arc, null);
284
+		}
285
+		if (markers) {
286
+			drawMarker(path.getOrigin().getPoint(), color);
287
+			drawMarker(path.getDestination().getPoint(), color);
288
+		}
289
+	}
290
+	
291
+	@Override
292
+	public void drawPath(Path path, Color color) {
293
+		drawPath(path, color, true);
294
+	}
295
+
296
+	@Override
297
+	public void drawPath(Path path) {
298
+		drawPath(path, DEFAULT_PATH_COLOR);
299
+	}
300
+	
301
+	@Override
302
+	public void drawPath(Path path, boolean markers) {
303
+		drawPath(path, DEFAULT_PATH_COLOR, markers);
304
+	}
305
+	
306
+	@SuppressWarnings("unused")
307
+	private void putText(Point point, String txt) {
308
+		int x = this.projx(point.getLongitude());
309
+		int y = this.projy(point.getLatitude());
310
+		gr.drawString(txt, x, y);
311
+		this.repaint();	
312
+	}
313
+
314
+}

src/main/org/insa/drawing/graph/BasicGraphPalette.java → src/main/org/insa/drawing/BasicGraphPalette.java View File

@@ -1,4 +1,4 @@
1
-package org.insa.drawing.graph;
1
+package org.insa.drawing;
2 2
 
3 3
 import java.awt.Color;
4 4
 

src/main/org/insa/drawing/graph/BlackAndWhiteGraphPalette.java → src/main/org/insa/drawing/BlackAndWhiteGraphPalette.java View File

@@ -1,4 +1,4 @@
1
-package org.insa.drawing.graph;
1
+package org.insa.drawing;
2 2
 
3 3
 import java.awt.Color;
4 4
 

+ 93
- 153
src/main/org/insa/drawing/Drawing.java View File

@@ -1,179 +1,119 @@
1 1
 package org.insa.drawing;
2 2
 
3
-import java.awt.BasicStroke;
4 3
 import java.awt.Color;
5
-import java.awt.Graphics;
6
-import java.awt.Graphics2D;
7
-import java.awt.Image;
8
-import java.awt.event.MouseEvent;
9
-import java.awt.geom.NoninvertibleTransformException;
10
-import java.awt.geom.Point2D;
11
-import java.awt.image.*;
12
-
13
-import javax.swing.JPanel;
14 4
 
5
+import org.insa.graph.Graph;
6
+import org.insa.graph.Path;
15 7
 import org.insa.graph.Point;
16 8
 
17
-/**
18
- *   Cette implementation de la classe Dessin produit vraiment un affichage
19
- *   (au contraire de la classe DessinInvisible).
20
- */
21
-
22
-public class Drawing extends JPanel {
9
+public interface Drawing {
10
+	
11
+	/**
12
+	 * Clear the drawing.
13
+	 */
14
+	public void clear();
23 15
 
24 16
 	/**
17
+	 * Draw a line between the two given points with the default color
18
+	 * and width.
25 19
 	 * 
20
+	 * @param from
21
+	 * @param to
26 22
 	 */
27
-	private static final long serialVersionUID = 96779785877771827L;
23
+	public void drawLine(Point from, Point to);
28 24
 	
29
-	private final Graphics2D gr;
30
-
31
-	private double long1, long2, lat1, lat2;
25
+	/**
26
+	 * Draw a line between the two given points with the default color
27
+	 * and the given width.
28
+	 * 
29
+	 * @param from
30
+	 * @param to
31
+	 * @param width
32
+	 */
33
+	public void drawLine(Point from, Point to, int width);
32 34
 	
33
-	// Width and height of the image
34
-	private final int width, height;
35
+	/**
36
+	 * Draw a line between the two given points with the given color
37
+	 * and the given width.
38
+	 * 
39
+	 * @param from
40
+	 * @param to
41
+	 * @param width
42
+	 * @param color
43
+	 */
44
+	public void drawLine(Point from, Point to, int width, Color color);
35 45
 	
36
-	private Image image;
37
-	private ZoomAndPanListener zoomAndPanListener;
46
+	/**
47
+	 * Draw a marker at the given point with the default color.
48
+	 * 
49
+	 * @param point
50
+	 */
51
+	public void drawMarker(Point point);
38 52
 	
39
-	public boolean autoRepaint = true;
40
-
41 53
 	/**
42
-	 *  Cree et affiche une nouvelle fenetre de dessin.
54
+	 * Draw the given point with the given color.
55
+	 * 
56
+	 * @param point
43 57
 	 */
44
-	public Drawing() {
45
-		
46
-		this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2);
47
-		this.addMouseListener(zoomAndPanListener);
48
-		this.addMouseMotionListener(zoomAndPanListener);
49
-		this.addMouseWheelListener(zoomAndPanListener);
50
-		
51
-		this.width = 2000;
52
-		this.height = 1600;
53
-		
54
-		BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR);
55
-		
56
-		this.image = img;
57
-		this.gr = img.createGraphics();
58
-		
59
-		this.zoomAndPanListener.setCoordTransform(this.gr.getTransform());
60
-
61
-		this.long1 = -180;
62
-		this.long2 = 180;
63
-		this.lat1  = -90;
64
-		this.lat2  = 90;
65
-
66
-		this.clear();
67
-		this.repaint();
68
-
69
-	}
58
+	public void drawMarker(Point point, Color color);
59
+	
60
+	/**
61
+	 * Draw a point width the given width and color. Do not use this to mark location,
62
+	 * use drawMarker.
63
+	 * 
64
+	 * @param point
65
+	 * @param width
66
+	 * @param color
67
+	 */
68
+	public void drawPoint(Point point, int width, Color color);
70 69
 
71
-	@Override
72
-	public void paintComponent(Graphics g1) {
73
-		Graphics2D g = (Graphics2D)g1;
74
-		g.clearRect(0, 0, getWidth(), getHeight());
75
-		g.setTransform(zoomAndPanListener.getCoordTransform());
76
-		g.drawImage(image, 0, 0, this);
77
-	}
70
+	/**
71
+	 * Draw the given graph using the given palette.
72
+	 * 
73
+	 * @param graph
74
+	 * @param palette
75
+	 */
76
+	public void drawGraph(Graph graph, GraphPalette palette);
78 77
 	
79
-	public void setAutoRepaint(boolean autoRepaint) {
80
-		this.autoRepaint = autoRepaint;
81
-	}
78
+	/**
79
+	 * Draw the given graph using a default palette specific to the implementation.
80
+	 * 
81
+	 * @param graph
82
+	 */
83
+	public void drawGraph(Graph graph);
82 84
 	
83
-	protected void doAutoPaint() {
84
-		if (autoRepaint) {
85
-			this.repaint();
86
-		}
87
-	}
85
+	/**
86
+	 * Draw a path using the given color.
87
+	 * 
88
+	 * @param path
89
+	 * @param color
90
+	 * @param markers Show origin and destination markers.
91
+	 */
92
+	public void drawPath(Path path, Color color, boolean markers);
88 93
 	
89
-	public void setWidth(int width) {
90
-		this.gr.setStroke(new BasicStroke(width));
91
-	}
92
-
93
-	public void setColor(Color col) {
94
-		this.gr.setColor(col);
95
-	}
96
-
97
-	public void clear() {
98
-		this.gr.setColor(Color.WHITE);
99
-		this.gr.fillRect(0, 0, this.width, this.height);
100
-	}
101
-
102
-	public void setBB(double long1, double long2, double lat1, double lat2) {	
103
-
104
-		if (long1 > long2 || lat1 > lat2) {
105
-			throw new Error("DessinVisible.setBB : mauvaises coordonnees.");
106
-		}
107
-		
108
-		this.long1 = long1;
109
-		this.long2 = long2;
110
-		this.lat1= lat1;
111
-		this.lat2 = lat2;
112
-				
113
-		double scale = 1 / Math.max(this.width / (double)this.getWidth(),  this.height / (double)this.getHeight());
114
-		
115
-		this.zoomAndPanListener.getCoordTransform().setToIdentity();
116
-		this.zoomAndPanListener.getCoordTransform().translate((this.getWidth() - this.width * scale) / 2, 
117
-				(this.getHeight() - this.height * scale) / 2);
118
-		this.zoomAndPanListener.getCoordTransform().scale(scale, scale);
119
-		this.zoomAndPanListener.setZoomLevel(0);
120
-		this.repaint();
121
-		
122
-	}
123
-
124
-	private int projx(double lon) {
125
-		return (int)(width * (lon - this.long1) / (this.long2 - this.long1)) ;
126
-	}
127
-
128
-	private int projy(double lat) {
129
-		return (int)(height * (1 - (lat - this.lat1) / (this.lat2 - this.lat1))) ;
130
-	}
94
+	/**
95
+	 * Draw a path using the given color with markers.
96
+	 * 
97
+	 * @param path
98
+	 * @param color
99
+	 */
100
+	public void drawPath(Path path, Color color);
131 101
 	
132 102
 	/**
133
-	 * Return the longitude and latitude corresponding to the given
134
-	 * position of the MouseEvent.
103
+	 * Draw a path using a default color specific to the implementation
135 104
 	 * 
136
-	 * @param event
137 105
 	 * 
138
-	 * @return
106
+	 * @param path
107
+	 * @param markers Show origin and destination markers.
139 108
 	 */
140
-	public Point getLongitudeLatitude(MouseEvent event) throws NoninvertibleTransformException {
141
-		// Get the point using the inverse transform of the Zoom/Pan object, this gives us
142
-		// a point within the drawing box (between [0, 0] and [width, height]).
143
-		Point2D ptDst = this.zoomAndPanListener.getCoordTransform().inverseTransform(event.getPoint(), null);
144
-		
145
-		// Inverse the "projection" on x/y to get longitude and latitude.
146
-		double lon = ptDst.getX();
147
-		double lat = ptDst.getY();
148
-		lon = (lon / this.width) * (this.long2 - this.long1) + this.long1;
149
-		lat = (1 - lat / this.height) * (this.lat2 - this.lat1) + this.lat1;
150
-		
151
-		// Return a new point.
152
-		return new Point(lon, lat);			
153
-	}
154
-
155
-	public void drawLine(Point from, Point to) {
156
-		int x1 = this.projx(from.getLongitude()) ;
157
-		int x2 = this.projx(to.getLongitude()) ;
158
-		int y1 = this.projy(from.getLatitude()) ;
159
-		int y2 = this.projy(to.getLatitude()) ;
160
-
161
-		gr.drawLine(x1, y1, x2, y2) ;
162
-		this.doAutoPaint();
163
-	}
164
-
165
-	public void drawPoint(Point point, int width) {
166
-		int x = this.projx(point.getLongitude()) - width / 2;
167
-		int y = this.projy(point.getLatitude()) - width / 2;
168
-		gr.fillOval(x, y, width, width);
169
-		this.doAutoPaint();
170
-	}
171
-
172
-	public void putText(Point point, String txt) {
173
-		int x = this.projx(point.getLongitude());
174
-		int y = this.projy(point.getLatitude());
175
-		gr.drawString(txt, x, y);
176
-		this.doAutoPaint();	
177
-	}
178
-
109
+	public void drawPath(Path path, boolean markers);
110
+	
111
+	/**
112
+	 * Draw a path using a default color specific to the implementation
113
+	 * 
114
+	 * 
115
+	 * @param path
116
+	 */
117
+	public void drawPath(Path path);
118
+	
179 119
 }

src/main/org/insa/drawing/graph/GraphPalette.java → src/main/org/insa/drawing/GraphPalette.java View File

@@ -1,4 +1,4 @@
1
-package org.insa.drawing.graph;
1
+package org.insa.drawing;
2 2
 
3 3
 import java.awt.Color;
4 4
 

+ 237
- 0
src/main/org/insa/drawing/MapViewDrawing.java View File

@@ -0,0 +1,237 @@
1
+package org.insa.drawing;
2
+
3
+import java.awt.Color;
4
+import java.io.File;
5
+import java.io.FileNotFoundException;
6
+import java.util.ArrayList;
7
+import java.util.HashMap;
8
+import java.util.Map;
9
+import java.util.UUID;
10
+import java.util.prefs.Preferences;
11
+
12
+import org.insa.graph.Arc;
13
+import org.insa.graph.Graph;
14
+import org.insa.graph.Path;
15
+import org.insa.graph.Point;
16
+import org.mapsforge.core.graphics.GraphicFactory;
17
+import org.mapsforge.core.graphics.Paint;
18
+import org.mapsforge.core.graphics.Style;
19
+import org.mapsforge.core.model.BoundingBox;
20
+import org.mapsforge.core.model.LatLong;
21
+import org.mapsforge.core.model.MapPosition;
22
+import org.mapsforge.core.util.LatLongUtils;
23
+import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
24
+import org.mapsforge.map.awt.util.AwtUtil;
25
+import org.mapsforge.map.awt.util.JavaPreferences;
26
+import org.mapsforge.map.awt.view.MapView;
27
+import org.mapsforge.map.datastore.MapDataStore;
28
+import org.mapsforge.map.layer.Layers;
29
+import org.mapsforge.map.layer.cache.TileCache;
30
+import org.mapsforge.map.layer.hills.HillsRenderConfig;
31
+import org.mapsforge.map.layer.overlay.Marker;
32
+import org.mapsforge.map.layer.overlay.Polyline;
33
+import org.mapsforge.map.layer.renderer.TileRendererLayer;
34
+import org.mapsforge.map.model.DisplayModel;
35
+import org.mapsforge.map.model.MapViewPosition;
36
+import org.mapsforge.map.model.Model;
37
+import org.mapsforge.map.model.common.Observer;
38
+import org.mapsforge.map.model.common.PreferencesFacade;
39
+import org.mapsforge.map.reader.MapFile;
40
+import org.mapsforge.map.rendertheme.InternalRenderTheme;
41
+import org.mapsforge.map.rendertheme.XmlRenderTheme;
42
+
43
+import com.google.common.util.concurrent.SettableFuture;
44
+
45
+public class MapViewDrawing extends MapView implements Drawing {
46
+
47
+	/**
48
+	 * 
49
+	 */
50
+	private static final long serialVersionUID = 8606967833704938092L;
51
+	
52
+	// Default path color.
53
+	public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244);
54
+	
55
+	// Graphic factory.
56
+    private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
57
+    
58
+    // Default tile size.
59
+    private static final int DEFAULT_TILE_SIZE = 512;
60
+	
61
+	// Tile size.
62
+	int tileSize;
63
+
64
+	public MapViewDrawing() {
65
+		setBackground(Color.WHITE);
66
+		getMapScaleBar().setVisible(true);
67
+		this.tileSize = DEFAULT_TILE_SIZE;
68
+		DisplayModel model = getModel().displayModel;
69
+        model.setFixedTileSize(tileSize);
70
+        model.setBackgroundColor(convertColor(Color.WHITE));
71
+	}
72
+	
73
+	protected int convertColor(Color color) {
74
+		return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), 
75
+				color.getGreen(), color.getBlue());
76
+	}
77
+	
78
+	private Paint createPaintStroke(int width, Color color) {
79
+		Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
80
+		paintStroke.setStyle(Style.STROKE);
81
+		if (width != 0) {
82
+			paintStroke.setStrokeWidth(width);
83
+		}
84
+		if (color != null) {
85
+			paintStroke.setColor(convertColor(color));
86
+		}
87
+		return paintStroke;
88
+	}
89
+	
90
+	/**
91
+	 * 
92
+	 * @param color
93
+	 * @return
94
+	 */
95
+	private static File getMapsforgeFileFromGraph(Graph graph) {
96
+		// TODO: Find a way to change this...
97
+		Map<Integer, String> idToNames = new HashMap<Integer, String>();
98
+		idToNames.put(0x100, "insa");
99
+		idToNames.put(0x101, "insa");
100
+		idToNames.put(0x110, "paris");
101
+		idToNames.put(0x200, "mayotte");
102
+		idToNames.put(0x250, "newzealand");
103
+		idToNames.put(0x300, "reunion");
104
+		idToNames.put(0x400, "midip");
105
+		idToNames.put(0x410, "morbihan");
106
+		
107
+		File file = null;
108
+		if (idToNames.containsKey(graph.getMapId())) {
109
+			file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg");
110
+		}
111
+		return file;
112
+	}
113
+	
114
+	protected LatLong convertPoint(Point point) {
115
+		return new LatLong(point.getLatitude(), point.getLongitude());
116
+	}
117
+	
118
+    private static TileRendererLayer createTileRendererLayer(TileCache tileCache, MapDataStore mapDataStore, MapViewPosition mapViewPosition, HillsRenderConfig hillsRenderConfig) {
119
+        TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
120
+            @Override
121
+            public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY, org.mapsforge.core.model.Point tapXY) {
122
+                System.out.println("Tap on: " + tapLatLong);
123
+                return true;
124
+            }
125
+        };
126
+        XmlRenderTheme renderTheme = InternalRenderTheme.DEFAULT;
127
+        tileRendererLayer.setXmlRenderTheme(renderTheme);
128
+        return tileRendererLayer;
129
+    }
130
+		
131
+	@Override
132
+	public void clear() {
133
+		getLayerManager().getLayers().clear();
134
+		repaint();
135
+	}
136
+
137
+	@Override
138
+	public void drawLine(Point from, Point to) {
139
+		drawLine(from, to, 0, null);
140
+	}
141
+
142
+	@Override
143
+	public void drawLine(Point from, Point to, int width) {
144
+		drawLine(from, to, width, null);
145
+	}
146
+
147
+	@Override
148
+	public void drawLine(Point from, Point to, int width, Color color) {
149
+		Paint paintStroke = createPaintStroke(width, color);
150
+        Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
151
+        line.getLatLongs().add(convertPoint(from));
152
+        line.getLatLongs().add(convertPoint(to));
153
+        getLayerManager().getLayers().add(line);	
154
+       }
155
+	
156
+	@Override
157
+	public void drawMarker(Point point) {
158
+		drawMarker(point, null);
159
+	}
160
+
161
+	@Override
162
+	public void drawMarker(Point point, Color color) {
163
+		Marker marker = new Marker(convertPoint(point), GRAPHIC_FACTORY.createBitmap(10, 20), 1, 2);
164
+		getLayerManager().getLayers().add(marker);
165
+	}
166
+	
167
+	@Override
168
+	public void drawPoint(Point point, int width, Color color) {
169
+		// TODO: Maybe do something?
170
+	}
171
+
172
+	@Override
173
+	public void drawGraph(Graph graph, GraphPalette palette) {
174
+		
175
+		File graphFile = getMapsforgeFileFromGraph(graph);
176
+
177
+        // Tile cache
178
+        TileCache tileCache = AwtUtil.createTileCache(
179
+        		tileSize, getModel().frameBufferModel.getOverdrawFactor(),
180
+            1024, new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
181
+        
182
+        // Layers
183
+        Layers layers = getLayerManager().getLayers();
184
+
185
+        MapDataStore mapDataStore = new MapFile(graphFile);
186
+        TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore, 
187
+        		getModel().mapViewPosition, null);
188
+        layers.add(tileRendererLayer);
189
+        BoundingBox boundingBox = mapDataStore.boundingBox();
190
+		
191
+        final PreferencesFacade preferencesFacade = new JavaPreferences(Preferences.userNodeForPackage(MapViewDrawing.class));
192
+        final Model model = getModel();
193
+        model.init(preferencesFacade);
194
+        if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) {
195
+            byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, model.displayModel.getTileSize());
196
+            model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
197
+        }
198
+	}
199
+
200
+	@Override
201
+	public void drawGraph(Graph graph) {
202
+		drawGraph(graph, null);
203
+	}
204
+
205
+	@Override
206
+	public void drawPath(Path path, Color color, boolean markers) {
207
+		Paint paintStroke = createPaintStroke(5, DEFAULT_PATH_COLOR);
208
+		Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
209
+        for (Arc arc: path.getArcs()) {
210
+        		ArrayList<Point> points = arc.getPoints();
211
+        		for (int i = 0; i < points.size(); ++i) {
212
+        			line.getLatLongs().add(new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
213
+        		}
214
+        }
215
+        getLayerManager().getLayers().add(line);
216
+        if (markers) {
217
+        		drawMarker(path.getOrigin().getPoint());
218
+        		drawMarker(path.getDestination().getPoint());
219
+        }
220
+	}
221
+	
222
+	@Override
223
+	public void drawPath(Path path, Color color) {
224
+		drawPath(path, color, true);
225
+	}
226
+
227
+	@Override
228
+	public void drawPath(Path path) {
229
+		drawPath(path, DEFAULT_PATH_COLOR, true);
230
+	}
231
+
232
+	@Override
233
+	public void drawPath(Path path, boolean markers) {
234
+		drawPath(path, DEFAULT_PATH_COLOR, markers);
235
+	}
236
+	
237
+}

+ 0
- 132
src/main/org/insa/drawing/graph/GraphDrawing.java View File

@@ -1,132 +0,0 @@
1
-package org.insa.drawing.graph;
2
-
3
-import java.awt.Color;
4
-import java.util.ArrayList;
5
-import java.util.Iterator;
6
-
7
-import org.insa.drawing.Drawing;
8
-import org.insa.graph.Arc;
9
-import org.insa.graph.Graph;
10
-import org.insa.graph.Node;
11
-import org.insa.graph.Point;
12
-
13
-public class GraphDrawing {
14
-
15
-	// Drawing
16
-	private Drawing drawing;
17
-
18
-	// Palette
19
-	private GraphPalette palette;
20
-	
21
-	public GraphDrawing(Drawing drawing) {
22
-		this.drawing = drawing;
23
-		this.palette = new BasicGraphPalette();
24
-	}
25
-	
26
-	public GraphDrawing(Drawing drawing, GraphPalette palette) {
27
-		this.drawing = drawing;
28
-		this.palette = palette;
29
-	}
30
-
31
-	public void drawLine(Point p1, Point p2) {
32
-		drawing.drawLine(p1, p2);
33
-	}
34
-	
35
-	public void drawPoint(Point p) {
36
-		drawPoint(p, palette.getDefaultPointWidth());
37
-	}
38
-	
39
-	public void drawPoint(Point p, int width) {
40
-		drawing.drawPoint(p, width);
41
-	}
42
-	
43
-	public void drawPoint(Point p, int width, Color c) {
44
-		drawing.setColor(c);
45
-		drawing.drawPoint(p, width);
46
-	}
47
-
48
-	/**
49
-	 * Draw the given arc with automatic color and width depending
50
-	 * on the road type.
51
-	 * 
52
-	 * @param arc Arc to draw.
53
-	 */
54
-	public void drawArc(Arc arc) {
55
-		drawArc(arc, true);
56
-	}
57
-	
58
-	/**
59
-	 * Draw the given arc.
60
-	 * 
61
-	 * @param arc Arc to draw.
62
-	 * @param autoColorAndWidth Set to true to set color and width based
63
-	 * on the road type of the arc.
64
-	 */
65
-	public void drawArc(Arc arc, boolean autoColorAndWidth) {
66
-		ArrayList<Point> pts = arc.getPoints();
67
-		if (!pts.isEmpty()) {
68
-			if (autoColorAndWidth) {
69
-				drawing.setColor(palette.getColorForType(arc.getInfo().getType()));
70
-				drawing.setWidth(palette.getWidthForType(arc.getInfo().getType()));
71
-			}
72
-			Iterator<Point> it1 = pts.iterator();
73
-			Point prev = it1.next();
74
-			while (it1.hasNext()) {
75
-				Point curr = it1.next();
76
-				drawLine(prev, curr);
77
-				prev = curr;
78
-			}
79
-		}
80
-	}
81
-	
82
-	/**
83
-	 * Initialize the drawing for the given graph.
84
-	 * 
85
-	 * @param graph
86
-	 */
87
-	public void initialize(Graph graph) {
88
-		double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY, 
89
-				maxLon = Double.NEGATIVE_INFINITY, maxLat = Double.NEGATIVE_INFINITY;
90
-		for (Node node: graph.getNodes()) {
91
-			Point pt = node.getPoint();
92
-			if (pt.getLatitude() < minLat) {
93
-				minLat = pt.getLatitude();
94
-			}
95
-			if (pt.getLatitude() > maxLat) {
96
-				maxLat = pt.getLatitude();
97
-			}
98
-			if (pt.getLongitude() < minLon) {
99
-				minLon = pt.getLongitude();
100
-			}
101
-			if (pt.getLongitude() > maxLon) {
102
-				maxLon = pt.getLongitude();
103
-			}
104
-		}
105
-		
106
-		double deltaLon = 0.02 * (maxLon - minLon),
107
-				deltaLat = 0.02 * (maxLat - minLat);
108
-
109
-		drawing.setBB(minLon - deltaLon, maxLon + deltaLon, 
110
-				minLat - deltaLat, maxLat + deltaLat);
111
-	}
112
-
113
-	
114
-	/**
115
-	 * Clear the drawing and draw the given graph on the drawing.
116
-	 * 
117
-	 * @param graph Graph to draw.
118
-	 */
119
-	public void drawGraph(Graph graph) {
120
-
121
-		drawing.clear();
122
-		
123
-		initialize(graph);
124
-
125
-		for (Node node: graph.getNodes()) {
126
-			for (Arc arc: node.getSuccessors()) {
127
-				drawArc(arc);
128
-			}
129
-		}
130
-	}
131
-
132
-}

+ 0
- 52
src/main/org/insa/drawing/graph/PathDrawing.java View File

@@ -1,52 +0,0 @@
1
-package org.insa.drawing.graph;
2
-
3
-import java.awt.Color;
4
-
5
-import org.insa.drawing.Drawing;
6
-import org.insa.graph.Arc;
7
-import org.insa.graph.Path;
8
-
9
-public class PathDrawing {
10
-	
11
-	// Default color
12
-	public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255);
13
-	
14
-	// Drawing
15
-	private Drawing drawing;
16
-	private GraphDrawing graphDrawing;
17
-	
18
-	/**
19
-	 * @param drawing
20
-	 */
21
-	public PathDrawing(Drawing drawing) {
22
-		this.drawing = drawing;
23
-		this.graphDrawing = new GraphDrawing(drawing);
24
-	}
25
-	
26
-	/**
27
-	 * Draw the given path with the given color.
28
-	 * 
29
-	 * @param path
30
-	 * @param color
31
-	 */
32
-	public void drawPath(Path path, Color color) {
33
-		this.graphDrawing.drawPoint(path.getFirstNode().getPoint(), 4, color);
34
-		this.drawing.setColor(color);
35
-		this.drawing.setWidth(2);
36
-		for (Arc arc: path.getArcs()) {
37
-			this.graphDrawing.drawArc(arc, false);
38
-		}
39
-		this.graphDrawing.drawPoint(path.getLastNode().getPoint(), 4, color);
40
-	}
41
-	
42
-	/**
43
-	 * Draw the given path with default color.
44
-	 * 
45
-	 * @param path
46
-	 */
47
-	public void drawPath(Path path) {
48
-		drawPath(path, DEFAULT_PATH_COLOR);
49
-		drawing.repaint();
50
-	}
51
-
52
-}

+ 2
- 0
src/main/org/insa/graph/io/BinaryGraphReader.java View File

@@ -61,6 +61,8 @@ public class BinaryGraphReader extends BinaryReader implements AbstractGraphRead
61 61
 	@Override
62 62
 	public Graph read() throws IOException {
63 63
 		
64
+		System.out.println(getClass());
65
+		
64 66
 		// Read and check magic number and file version.
65 67
 		checkMagicNumberOrThrow(dis.readInt());
66 68
 		checkVersionOrThrow(dis.readInt());

+ 170
- 0
src/main/org/insa/graph/io/BinaryGraphReaderV2.java View File

@@ -0,0 +1,170 @@
1
+package org.insa.graph.io;
2
+
3
+import java.io.DataInputStream;
4
+import java.io.IOException;
5
+import java.util.ArrayList;
6
+import java.util.Collections;
7
+
8
+import org.insa.graph.Arc;
9
+import org.insa.graph.Graph;
10
+import org.insa.graph.Node;
11
+import org.insa.graph.Point;
12
+import org.insa.graph.RoadInformation;
13
+import org.insa.graph.RoadInformation.RoadType;
14
+
15
+public class BinaryGraphReaderV2 extends BinaryReader implements AbstractGraphReader {
16
+	
17
+	// Map version and magic number targeted for this reader.
18
+	private static final int VERSION = 5;
19
+	private static final int MAGIC_NUMBER = 0x208BC3B3;
20
+
21
+	/**
22
+	 * Convert a character to its corresponding road type.
23
+	 * 
24
+	 * @param ch Character to convert.
25
+	 * 
26
+	 * @return Road type corresponding to ch.
27
+	 * 
28
+	 * @see http://wiki.openstreetmap.org/wiki/Highway_tag_usage.
29
+	 */
30
+	public static RoadType toRoadType(char ch) {
31
+		switch (ch) {
32
+			case 'a': return RoadType.MOTORWAY;
33
+			case 'b': return RoadType.TRUNK;
34
+			case 'c': return RoadType.PRIMARY;
35
+			case 'd': return RoadType.SECONDARY;
36
+			case 'e': return RoadType.MOTORWAY_LINK;
37
+			case 'f': return RoadType.TRUNK_LINK;
38
+			case 'g': return RoadType.PRIMARY_LINK;
39
+			case 'h': return RoadType.SECONDARY_LINK;
40
+			case 'i': return RoadType.TERTIARY;
41
+			case 'j': return RoadType.RESIDENTIAL;
42
+			case 'k': return RoadType.UNCLASSIFIED;
43
+			case 'l': return RoadType.ROAD;
44
+			case 'm': return RoadType.LIVING_STREET;
45
+			case 'n': return RoadType.SERVICE;
46
+			case 'o': return RoadType.ROUNDABOUT;
47
+			case 'z': return RoadType.COASTLINE;
48
+		}
49
+		return RoadType.UNCLASSIFIED;
50
+	}
51
+
52
+	/**
53
+	 * Create a new BinaryGraphReader using the given DataInputStream.
54
+	 * 
55
+	 * @param dis
56
+	 */
57
+	public BinaryGraphReaderV2(DataInputStream dis) {
58
+		super(MAGIC_NUMBER, VERSION, dis);
59
+	}
60
+
61
+	@Override
62
+	public Graph read() throws IOException {
63
+				
64
+		// Read and check magic number and file version.
65
+		checkMagicNumberOrThrow(dis.readInt());
66
+		checkVersionOrThrow(dis.readInt());
67
+		
68
+		// Read map id.
69
+		int mapId = dis.readInt();
70
+
71
+		// Number of descriptors and nodes.
72
+		int nbDesc = dis.readInt();
73
+		int nbNodes = dis.readInt();
74
+		
75
+		// Number of successors for each nodes.
76
+		int[] nbSuccessors = new int[nbNodes];
77
+
78
+		// Construct an array list with initial capacity of nbNodes.
79
+		ArrayList<Node> nodes = new ArrayList<Node>(nbNodes);
80
+		
81
+		// Read nodes.
82
+		for (int node = 0; node < nbNodes; ++node) {
83
+			float longitude = ((float)dis.readInt ()) / 1E6f;
84
+			float latitude = ((float)dis.readInt ()) / 1E6f;
85
+			nbSuccessors[node] = dis.readUnsignedByte();
86
+			nodes.add(new Node(node, new Point(longitude, latitude)));
87
+		}
88
+		
89
+		// Check format.
90
+		checkByteOrThrow(255);
91
+		
92
+		// Read descriptors.
93
+		RoadInformation[] descs = new RoadInformation[nbDesc];
94
+		
95
+		// Read
96
+		for (int descr = 0; descr < nbDesc; ++descr) {
97
+			descs[descr] = readRoadInformation();
98
+		}
99
+		
100
+		// Check format.
101
+		checkByteOrThrow(254);
102
+				
103
+		// Read successors and convert to arcs.
104
+		for (int node = 0; node < nbNodes; ++node) {
105
+			for (int succ = 0; succ < nbSuccessors[node]; ++succ) {
106
+				
107
+				// Read target node number.
108
+				int destNode = this.read24bits();
109
+				
110
+				// Read information number.
111
+				int descrNum = this.read24bits();
112
+				
113
+				// Length of the arc.
114
+				int length = dis.readUnsignedShort();
115
+				
116
+				// Number of segments.
117
+				int nbSegments = dis.readUnsignedShort();
118
+
119
+				// Chain of points corresponding to the segments.
120
+				ArrayList<Point> points = new ArrayList<Point>(nbSegments + 2);
121
+				points.add(nodes.get(node).getPoint());
122
+				
123
+				for (int seg = 0; seg < nbSegments; ++seg) {
124
+					Point lastPoint = points.get(points.size() - 1);
125
+							
126
+					float dlon = (dis.readShort()) / 2.0e5f;
127
+					float dlat = (dis.readShort()) / 2.0e5f;
128
+					
129
+					points.add(new Point(lastPoint.getLongitude() + dlon,
130
+										lastPoint.getLatitude() + dlat));
131
+				}
132
+				
133
+				points.add(nodes.get(destNode).getPoint());
134
+									
135
+				RoadInformation info = descs[descrNum];
136
+				Node orig = nodes.get(node);
137
+				Node dest = nodes.get(destNode);
138
+
139
+				// Add successor to initial arc.
140
+				new Arc(orig, dest, length, info, points);
141
+				
142
+				// And reverse arc if its a two-way road.
143
+				if (!info.isOneWay()) {
144
+					// Add without segments.
145
+					ArrayList<Point> rPoints = new ArrayList<Point>(points);
146
+					Collections.reverse(rPoints);
147
+					new Arc(dest, orig, length, info, rPoints);
148
+				}
149
+					
150
+			}
151
+		}
152
+		
153
+		// Check format.
154
+		checkByteOrThrow(253);
155
+		
156
+		return new Graph(mapId, nodes);
157
+	}
158
+		
159
+	/**
160
+	 * Read the next road information from the stream.
161
+	 * 
162
+	 * @throws IOException
163
+	 */
164
+	private RoadInformation readRoadInformation() throws IOException {
165
+		char type = (char)dis.readUnsignedByte();
166
+		int x = dis.readUnsignedByte() ;
167
+		return new RoadInformation(toRoadType(type), (x & 0x80) > 0, (x & 0x7F) * 5, dis.readUTF());
168
+	}
169
+	
170
+}

Loading…
Cancel
Save