Update MapViewer.

This commit is contained in:
Mikael Capelle 2018-02-23 20:39:56 +01:00
parent 5899dfa276
commit 24eeab7971
3 changed files with 936 additions and 904 deletions

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,20 @@
*/ */
package org.insa.base; package org.insa.base;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.prefs.Preferences;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import org.insa.graph.Arc; import org.insa.graph.Arc;
import org.insa.graph.Graph; import org.insa.graph.Graph;
import org.insa.graph.Path; import org.insa.graph.Path;
@ -34,7 +48,6 @@ import org.mapsforge.core.model.MapPosition;
import org.mapsforge.core.model.Point; import org.mapsforge.core.model.Point;
import org.mapsforge.core.util.LatLongUtils; import org.mapsforge.core.util.LatLongUtils;
import org.mapsforge.core.util.Parameters; import org.mapsforge.core.util.Parameters;
import org.mapsforge.core.util.Utils;
import org.mapsforge.map.awt.graphics.AwtGraphicFactory; import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
import org.mapsforge.map.awt.util.AwtUtil; import org.mapsforge.map.awt.util.AwtUtil;
import org.mapsforge.map.awt.util.JavaPreferences; import org.mapsforge.map.awt.util.JavaPreferences;
@ -59,227 +72,231 @@ import org.mapsforge.map.model.common.PreferencesFacade;
import org.mapsforge.map.reader.MapFile; import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme; import org.mapsforge.map.rendertheme.InternalRenderTheme;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.prefs.Preferences;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
public final class Samples { public final class Samples {
private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE; private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
private static final boolean SHOW_DEBUG_LAYERS = false; private static final boolean SHOW_DEBUG_LAYERS = false;
private static final boolean SHOW_RASTER_MAP = false; private static final boolean SHOW_RASTER_MAP = false;
private static final String MESSAGE = "Are you sure you want to exit the application?"; private static final String MESSAGE = "Are you sure you want to exit the application?";
private static final String TITLE = "Confirm close"; private static final String TITLE = "Confirm close";
/** /**
* Starts the {@code Samples}. * Starts the {@code Samples}.
* *
* @param args command line args: expects the map files as multiple parameters * @param args
* with possible SRTM hgt folder as 1st argument. * command line args: expects the map files as multiple parameters
* @throws Exception * with possible SRTM hgt folder as 1st argument.
*/ * @throws Exception
public static void main(String[] args) throws Exception { */
public static void main(String[] args) throws Exception {
// Multithreaded map rendering // Multithreaded map rendering
Parameters.NUMBER_OF_THREADS = 2; Parameters.NUMBER_OF_THREADS = 2;
// Square frame buffer // Square frame buffer
Parameters.SQUARE_FRAME_BUFFER = false; Parameters.SQUARE_FRAME_BUFFER = false;
HillsRenderConfig hillsCfg = null; HillsRenderConfig hillsCfg = null;
File demFolder = getDemFolder(args); File demFolder = getDemFolder(args);
if (demFolder != null) { if (demFolder != null) {
MemoryCachingHgtReaderTileSource tileSource = new MemoryCachingHgtReaderTileSource(demFolder, new DiffuseLightShadingAlgorithm(), AwtGraphicFactory.INSTANCE); MemoryCachingHgtReaderTileSource tileSource = new MemoryCachingHgtReaderTileSource(
tileSource.setEnableInterpolationOverlap(true); demFolder, new DiffuseLightShadingAlgorithm(), AwtGraphicFactory.INSTANCE);
hillsCfg = new HillsRenderConfig(tileSource); tileSource.setEnableInterpolationOverlap(true);
hillsCfg.indexOnThread(); hillsCfg = new HillsRenderConfig(tileSource);
args = Arrays.copyOfRange(args, 1, args.length); hillsCfg.indexOnThread();
} args = Arrays.copyOfRange(args, 1, args.length);
}
List<File> mapFiles = getMapFiles(args); List<File> mapFiles = getMapFiles(args);
final MapView mapView = createMapView(); final MapView mapView = createMapView();
final BoundingBox boundingBox = addLayers(mapView, mapFiles, hillsCfg); final BoundingBox boundingBox = addLayers(mapView, mapFiles, hillsCfg);
// addAPath(mapView); // addAPath(mapView);
final PreferencesFacade preferencesFacade = new JavaPreferences(Preferences.userNodeForPackage(Samples.class)); final PreferencesFacade preferencesFacade = new JavaPreferences(
Preferences.userNodeForPackage(Samples.class));
final JFrame frame = new JFrame(); final JFrame frame = new JFrame();
frame.setTitle("Mapsforge Samples"); frame.setTitle("Mapsforge Samples");
frame.add(mapView); frame.add(mapView);
frame.pack(); frame.pack();
frame.setSize(new Dimension(800, 600)); frame.setSize(new Dimension(800, 600));
frame.setLocationRelativeTo(null); frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() { frame.addWindowListener(new WindowAdapter() {
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(frame, MESSAGE, TITLE, JOptionPane.YES_NO_OPTION); int result = JOptionPane.showConfirmDialog(frame, MESSAGE, TITLE,
if (result == JOptionPane.YES_OPTION) { JOptionPane.YES_NO_OPTION);
mapView.getModel().save(preferencesFacade); if (result == JOptionPane.YES_OPTION) {
mapView.destroyAll(); mapView.getModel().save(preferencesFacade);
AwtGraphicFactory.clearResourceMemoryCache(); mapView.destroyAll();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); AwtGraphicFactory.clearResourceMemoryCache();
} frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} }
}
@Override @Override
public void windowOpened(WindowEvent e) { public void windowOpened(WindowEvent e) {
final Model model = mapView.getModel(); final Model model = mapView.getModel();
model.init(preferencesFacade); model.init(preferencesFacade);
if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) { if (model.mapViewPosition.getZoomLevel() == 0
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, model.displayModel.getTileSize()); || !boundingBox.contains(model.mapViewPosition.getCenter())) {
model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel)); byte zoomLevel = LatLongUtils.zoomForBounds(
} model.mapViewDimension.getDimension(), boundingBox,
} model.displayModel.getTileSize());
}); model.mapViewPosition.setMapPosition(
frame.setVisible(true); new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
} }
}
});
frame.setVisible(true);
}
private static void addAPath(MapView mapView) throws Exception { private static void addAPath(MapView mapView) throws Exception {
Graph gr = (new BinaryGraphReader(Openfile.open("Maps/midip.map"))).read(); Graph gr = (new BinaryGraphReader(Openfile.open("Maps/midip.map"))).read();
Path path = (new BinaryPathReader(Openfile.open("Paths/chemin_0x400_119963_96676.path"))).readPath(gr); Path path = (new BinaryPathReader(Openfile.open("Paths/chemin_0x400_119963_96676.path")))
.readPath(gr);
Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint(); Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
paintStroke.setColor(Color.GREEN); paintStroke.setColor(Color.GREEN);
paintStroke.setStrokeWidth(3); paintStroke.setStrokeWidth(3);
paintStroke.setStyle(Style.STROKE); paintStroke.setStyle(Style.STROKE);
Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE); Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
for (Arc arc: path.getArcs()) { for (Arc arc : path.getArcs()) {
ArrayList<org.insa.graph.Point> points = arc.getPoints(); ArrayList<org.insa.graph.Point> points = arc.getPoints();
for (int i = 0; i < points.size(); ++i) { for (int i = 0; i < points.size(); ++i) {
line.getLatLongs().add(new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude())); line.getLatLongs().add(
} new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
} }
}
mapView.getLayerManager().getLayers().add(line);
}
mapView.getLayerManager().getLayers().add(line); private static BoundingBox addLayers(MapView mapView, List<File> mapFiles,
} HillsRenderConfig hillsRenderConfig) {
Layers layers = mapView.getLayerManager().getLayers();
private static BoundingBox addLayers(MapView mapView, List<File> mapFiles, HillsRenderConfig hillsRenderConfig) { int tileSize = SHOW_RASTER_MAP ? 256 : 512;
Layers layers = mapView.getLayerManager().getLayers();
int tileSize = SHOW_RASTER_MAP ? 256 : 512; // Tile cache
TileCache tileCache = AwtUtil.createTileCache(tileSize,
mapView.getModel().frameBufferModel.getOverdrawFactor(), 1024,
new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
// Tile cache final BoundingBox boundingBox;
TileCache tileCache = AwtUtil.createTileCache( if (SHOW_RASTER_MAP) {
tileSize, // Raster
mapView.getModel().frameBufferModel.getOverdrawFactor(), mapView.getModel().displayModel.setFixedTileSize(tileSize);
1024, TileSource tileSource = OpenStreetMapMapnik.INSTANCE;
new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString())); TileDownloadLayer tileDownloadLayer = createTileDownloadLayer(tileCache,
mapView.getModel().mapViewPosition, tileSource);
layers.add(tileDownloadLayer);
tileDownloadLayer.start();
mapView.setZoomLevelMin(tileSource.getZoomLevelMin());
mapView.setZoomLevelMax(tileSource.getZoomLevelMax());
boundingBox = new BoundingBox(LatLongUtils.LATITUDE_MIN, LatLongUtils.LONGITUDE_MIN,
LatLongUtils.LATITUDE_MAX, LatLongUtils.LONGITUDE_MAX);
}
else {
// Vector
mapView.getModel().displayModel.setFixedTileSize(tileSize);
MultiMapDataStore mapDataStore = new MultiMapDataStore(
MultiMapDataStore.DataPolicy.RETURN_ALL);
for (File file : mapFiles) {
mapDataStore.addMapDataStore(new MapFile(file), false, false);
}
TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore,
mapView.getModel().mapViewPosition, hillsRenderConfig);
layers.add(tileRendererLayer);
boundingBox = mapDataStore.boundingBox();
}
final BoundingBox boundingBox; // Debug
if (SHOW_RASTER_MAP) { if (SHOW_DEBUG_LAYERS) {
// Raster layers.add(new TileGridLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
mapView.getModel().displayModel.setFixedTileSize(tileSize); layers.add(new TileCoordinatesLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
TileSource tileSource = OpenStreetMapMapnik.INSTANCE; }
TileDownloadLayer tileDownloadLayer = createTileDownloadLayer(tileCache, mapView.getModel().mapViewPosition, tileSource);
layers.add(tileDownloadLayer);
tileDownloadLayer.start();
mapView.setZoomLevelMin(tileSource.getZoomLevelMin());
mapView.setZoomLevelMax(tileSource.getZoomLevelMax());
boundingBox = new BoundingBox(LatLongUtils.LATITUDE_MIN, LatLongUtils.LONGITUDE_MIN, LatLongUtils.LATITUDE_MAX, LatLongUtils.LONGITUDE_MAX);
} else {
// Vector
mapView.getModel().displayModel.setFixedTileSize(tileSize);
MultiMapDataStore mapDataStore = new MultiMapDataStore(MultiMapDataStore.DataPolicy.RETURN_ALL);
for (File file : mapFiles) {
mapDataStore.addMapDataStore(new MapFile(file), false, false);
}
TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore, mapView.getModel().mapViewPosition, hillsRenderConfig);
layers.add(tileRendererLayer);
boundingBox = mapDataStore.boundingBox();
}
// Debug return boundingBox;
if (SHOW_DEBUG_LAYERS) { }
layers.add(new TileGridLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
layers.add(new TileCoordinatesLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
}
return boundingBox; private static MapView createMapView() {
} MapView mapView = new MapView();
mapView.getMapScaleBar().setVisible(true);
if (SHOW_DEBUG_LAYERS) {
mapView.getFpsCounter().setVisible(true);
}
private static MapView createMapView() { return mapView;
MapView mapView = new MapView(); }
mapView.getMapScaleBar().setVisible(true);
if (SHOW_DEBUG_LAYERS) {
mapView.getFpsCounter().setVisible(true);
}
return mapView; @SuppressWarnings("unused")
} private static TileDownloadLayer createTileDownloadLayer(TileCache tileCache,
MapViewPosition mapViewPosition, TileSource tileSource) {
return new TileDownloadLayer(tileCache, mapViewPosition, tileSource, GRAPHIC_FACTORY) {
@Override
public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
System.out.println("Tap on: " + tapLatLong);
return true;
}
};
}
@SuppressWarnings("unused") private static TileRendererLayer createTileRendererLayer(TileCache tileCache,
private static TileDownloadLayer createTileDownloadLayer(TileCache tileCache, MapViewPosition mapViewPosition, TileSource tileSource) { MapDataStore mapDataStore, MapViewPosition mapViewPosition,
return new TileDownloadLayer(tileCache, mapViewPosition, tileSource, GRAPHIC_FACTORY) { HillsRenderConfig hillsRenderConfig) {
@Override TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) { mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
System.out.println("Tap on: " + tapLatLong); @Override
return true; public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
} System.out.println("Tap on: " + tapLatLong);
}; return true;
} }
};
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT);
return tileRendererLayer;
}
private static TileRendererLayer createTileRendererLayer(TileCache tileCache, MapDataStore mapDataStore, MapViewPosition mapViewPosition, HillsRenderConfig hillsRenderConfig) { private static File getDemFolder(String[] args) {
TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) { if (args.length == 0) {
@Override throw new IllegalArgumentException("missing argument: <mapFile>");
public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) { }
System.out.println("Tap on: " + tapLatLong);
return true;
}
};
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT);
return tileRendererLayer;
}
private static File getDemFolder(String[] args) { File demFolder = new File(args[0]);
if (args.length == 0) { if (demFolder.exists() && demFolder.isDirectory() && demFolder.canRead()) {
throw new IllegalArgumentException("missing argument: <mapFile>"); return demFolder;
} }
return null;
}
File demFolder = new File(args[0]); private static List<File> getMapFiles(String[] args) {
if (demFolder.exists() && demFolder.isDirectory() && demFolder.canRead()) { if (args.length == 0) {
return demFolder; throw new IllegalArgumentException("missing argument: <mapFile>");
} }
return null;
}
private static List<File> getMapFiles(String[] args) { List<File> result = new ArrayList<>();
if (args.length == 0) { for (String arg : args) {
throw new IllegalArgumentException("missing argument: <mapFile>"); File mapFile = new File(arg);
} if (!mapFile.exists()) {
throw new IllegalArgumentException("file does not exist: " + mapFile);
}
else if (!mapFile.isFile()) {
throw new IllegalArgumentException("not a file: " + mapFile);
}
else if (!mapFile.canRead()) {
throw new IllegalArgumentException("cannot read file: " + mapFile);
}
result.add(mapFile);
}
return result;
}
List<File> result = new ArrayList<>(); private Samples() {
for (String arg : args) { throw new IllegalStateException();
File mapFile = new File(arg); }
if (!mapFile.exists()) {
throw new IllegalArgumentException("file does not exist: " + mapFile);
} else if (!mapFile.isFile()) {
throw new IllegalArgumentException("not a file: " + mapFile);
} else if (!mapFile.canRead()) {
throw new IllegalArgumentException("cannot read file: " + mapFile);
}
result.add(mapFile);
}
return result;
}
private Samples() {
throw new IllegalStateException();
}
} }

View File

@ -4,12 +4,13 @@ import java.awt.Color;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelEvent;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.insa.graph.Arc; import org.insa.graph.Arc;
import org.insa.graph.Graph; import org.insa.graph.Graph;
@ -24,10 +25,8 @@ import org.mapsforge.core.model.MapPosition;
import org.mapsforge.core.util.LatLongUtils; import org.mapsforge.core.util.LatLongUtils;
import org.mapsforge.map.awt.graphics.AwtGraphicFactory; import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
import org.mapsforge.map.awt.util.AwtUtil; import org.mapsforge.map.awt.util.AwtUtil;
import org.mapsforge.map.awt.util.JavaPreferences;
import org.mapsforge.map.awt.view.MapView; import org.mapsforge.map.awt.view.MapView;
import org.mapsforge.map.datastore.MapDataStore; import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.layer.Layer;
import org.mapsforge.map.layer.Layers; import org.mapsforge.map.layer.Layers;
import org.mapsforge.map.layer.cache.TileCache; import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.hills.HillsRenderConfig; import org.mapsforge.map.layer.hills.HillsRenderConfig;
@ -37,42 +36,37 @@ import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.model.DisplayModel; import org.mapsforge.map.model.DisplayModel;
import org.mapsforge.map.model.MapViewPosition; import org.mapsforge.map.model.MapViewPosition;
import org.mapsforge.map.model.Model; import org.mapsforge.map.model.Model;
import org.mapsforge.map.model.common.Observer;
import org.mapsforge.map.model.common.PreferencesFacade;
import org.mapsforge.map.reader.MapFile; import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme; import org.mapsforge.map.rendertheme.InternalRenderTheme;
import org.mapsforge.map.rendertheme.XmlRenderTheme; import org.mapsforge.map.rendertheme.XmlRenderTheme;
import com.google.common.util.concurrent.SettableFuture;
public class MapViewDrawing extends MapView implements Drawing { public class MapViewDrawing extends MapView implements Drawing {
/** /**
* *
*/ */
private static final long serialVersionUID = 8606967833704938092L; private static final long serialVersionUID = 8606967833704938092L;
// Default path color. // Default path color.
public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244); public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244);
// Graphic factory. // Graphic factory.
private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE; private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
// Default tile size. // Default tile size.
private static final int DEFAULT_TILE_SIZE = 512; private static final int DEFAULT_TILE_SIZE = 512;
// Tile size. // Tile size.
int tileSize; int tileSize;
ArrayList<Paint> extraLayers; ArrayList<Paint> extraLayers;
public MapViewDrawing() { public MapViewDrawing() {
setBackground(Color.WHITE); getMapScaleBar().setVisible(true);
getMapScaleBar().setVisible(true); this.tileSize = DEFAULT_TILE_SIZE;
this.tileSize = DEFAULT_TILE_SIZE; DisplayModel model = getModel().displayModel;
DisplayModel model = getModel().displayModel;
model.setFixedTileSize(tileSize); model.setFixedTileSize(tileSize);
model.setBackgroundColor(convertColor(Color.WHITE)); // model.setBackgroundColor(convertColor(Color.WHITE));
extraLayers = new ArrayList<Paint>(); extraLayers = new ArrayList<Paint>();
addMouseWheelListener(new MouseAdapter() { addMouseWheelListener(new MouseAdapter() {
@ -80,66 +74,81 @@ public class MapViewDrawing extends MapView implements Drawing {
public void mouseWheelMoved(MouseWheelEvent e) { public void mouseWheelMoved(MouseWheelEvent e) {
byte zoomLevelDiff = (byte) -e.getWheelRotation(); byte zoomLevelDiff = (byte) -e.getWheelRotation();
for (Paint p: extraLayers) { for (Paint p: extraLayers) {
p.setStrokeWidth(p.getStrokeWidth() + zoomLevelDiff); p.setStrokeWidth(p.getStrokeWidth() + zoomLevelDiff);
} }
} }
}); });
} }
protected int convertColor(Color color) { protected int convertColor(Color color) {
return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), color.getGreen(),
color.getGreen(), color.getBlue()); color.getBlue());
} }
private int getStrokeWidth(int width) { private int getStrokeWidth(int width) {
byte zoomLevel = getModel().mapViewPosition.getZoomLevel(); byte zoomLevel = getModel().mapViewPosition.getZoomLevel();
return width * (2 - (8 - zoomLevel)); return width * (2 - (8 - zoomLevel));
} }
private Paint createPaintStroke(int width, Color color) { private Paint createPaintStroke(int width, Color color) {
Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint(); Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
paintStroke.setStyle(Style.STROKE); paintStroke.setStyle(Style.STROKE);
if (width != 0) { if (width != 0) {
paintStroke.setStrokeWidth(getStrokeWidth(width)); paintStroke.setStrokeWidth(getStrokeWidth(width));
} }
if (color != null) { if (color != null) {
paintStroke.setColor(convertColor(color)); paintStroke.setColor(convertColor(color));
} }
return paintStroke; return paintStroke;
} }
/** /**
* *
* @param color * @param color
* @return * @return
*/ */
private static File getMapsforgeFileFromGraph(Graph graph) { private File getMapsforgeFileFromGraph(Graph graph) {
// TODO: Find a way to change this... // TODO: Find a way to change this...
Map<Integer, String> idToNames = new HashMap<Integer, String>(); Map<Integer, String> idToNames = new HashMap<Integer, String>();
idToNames.put(0x100, "insa"); idToNames.put(0x100, "insa");
idToNames.put(0x101, "insa"); idToNames.put(0x110, "paris");
idToNames.put(0x110, "paris"); idToNames.put(0x200, "mayotte");
idToNames.put(0x200, "mayotte"); idToNames.put(0x250, "newzealand");
idToNames.put(0x250, "newzealand"); idToNames.put(0x300, "reunion");
idToNames.put(0x300, "reunion"); idToNames.put(0x400, "midip");
idToNames.put(0x400, "midip"); idToNames.put(0x410, "morbihan");
idToNames.put(0x410, "morbihan");
File file = null; File file = null;
if (idToNames.containsKey(graph.getMapId())) { if (idToNames.containsKey(graph.getMapId())) {
file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg"); file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg");
} if (!file.exists()) {
return file; file = new File("Maps/new/" + idToNames.get(graph.getMapId()) + ".mapfg");
} }
}
protected LatLong convertPoint(Point point) { if (file == null || !file.exists()) {
return new LatLong(point.getLatitude(), point.getLongitude()); JFileChooser fileChooser = new JFileChooser("Maps/");
} fileChooser.setFileFilter(new FileNameExtensionFilter("mapsforge files", "" + "mapfg"));
if (fileChooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
}
}
private static TileRendererLayer createTileRendererLayer(TileCache tileCache, MapDataStore mapDataStore, MapViewPosition mapViewPosition, HillsRenderConfig hillsRenderConfig) { return file;
TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) { }
protected LatLong convertPoint(Point point) {
return new LatLong(point.getLatitude(), point.getLongitude());
}
private static TileRendererLayer createTileRendererLayer(TileCache tileCache,
MapDataStore mapDataStore, MapViewPosition mapViewPosition,
HillsRenderConfig hillsRenderConfig) {
TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
@Override @Override
public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY, org.mapsforge.core.model.Point tapXY) { public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY,
org.mapsforge.core.model.Point tapXY) {
System.out.println("Tap on: " + tapLatLong); System.out.println("Tap on: " + tapLatLong);
return true; return true;
} }
@ -149,111 +158,115 @@ public class MapViewDrawing extends MapView implements Drawing {
return tileRendererLayer; return tileRendererLayer;
} }
@Override @Override
public void clear() { public void clear() {
getLayerManager().getLayers().clear(); getLayerManager().getLayers().clear();
extraLayers.clear(); extraLayers.clear();
repaint(); repaint();
} }
@Override @Override
public void drawLine(Point from, Point to) { public void drawLine(Point from, Point to) {
drawLine(from, to, 0, null); drawLine(from, to, 0, null);
} }
@Override @Override
public void drawLine(Point from, Point to, int width) { public void drawLine(Point from, Point to, int width) {
drawLine(from, to, width, null); drawLine(from, to, width, null);
} }
@Override @Override
public void drawLine(Point from, Point to, int width, Color color) { public void drawLine(Point from, Point to, int width, Color color) {
Paint paintStroke = createPaintStroke(width, color); Paint paintStroke = createPaintStroke(width, color);
Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE); Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
line.getLatLongs().add(convertPoint(from)); line.getLatLongs().add(convertPoint(from));
line.getLatLongs().add(convertPoint(to)); line.getLatLongs().add(convertPoint(to));
getLayerManager().getLayers().add(line); getLayerManager().getLayers().add(line);
} }
@Override @Override
public void drawMarker(Point point) { public void drawMarker(Point point) {
drawMarker(point, null); drawMarker(point, null);
} }
@Override @Override
public void drawMarker(Point point, Color color) { public void drawMarker(Point point, Color color) {
Marker marker = new Marker(convertPoint(point), GRAPHIC_FACTORY.createBitmap(10, 20), 1, 2); Marker marker = new Marker(convertPoint(point), GRAPHIC_FACTORY.createBitmap(10, 20), 1, 2);
getLayerManager().getLayers().add(marker); getLayerManager().getLayers().add(marker);
} }
@Override @Override
public void drawPoint(Point point, int width, Color color) { public void drawPoint(Point point, int width, Color color) {
// TODO: Maybe do something? // TODO: Maybe do something?
} }
@Override @Override
public void drawGraph(Graph graph, GraphPalette palette) { public void drawGraph(Graph graph, GraphPalette palette) {
File graphFile = getMapsforgeFileFromGraph(graph); File graphFile = getMapsforgeFileFromGraph(graph);
// Tile cache // Tile cache
TileCache tileCache = AwtUtil.createTileCache( TileCache tileCache = AwtUtil.createTileCache(tileSize,
tileSize, getModel().frameBufferModel.getOverdrawFactor(), getModel().frameBufferModel.getOverdrawFactor(), 1024,
1024, new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString())); new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
// Layers // Layers
Layers layers = getLayerManager().getLayers(); Layers layers = getLayerManager().getLayers();
MapDataStore mapDataStore = new MapFile(graphFile); MapDataStore mapDataStore = new MapFile(graphFile);
TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore, TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore,
getModel().mapViewPosition, null); getModel().mapViewPosition, null);
layers.add(tileRendererLayer); layers.add(tileRendererLayer);
BoundingBox boundingBox = mapDataStore.boundingBox(); BoundingBox boundingBox = mapDataStore.boundingBox();
final Model model = getModel(); final Model model = getModel();
if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) { if (model.mapViewPosition.getZoomLevel() == 0
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, model.displayModel.getTileSize()); || !boundingBox.contains(model.mapViewPosition.getCenter())) {
model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel)); byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(),
boundingBox, model.displayModel.getTileSize());
model.mapViewPosition
.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
model.mapViewPosition.setZoomLevelMin(zoomLevel); model.mapViewPosition.setZoomLevelMin(zoomLevel);
} }
} }
@Override @Override
public void drawGraph(Graph graph) { public void drawGraph(Graph graph) {
drawGraph(graph, null); drawGraph(graph, null);
} }
@Override @Override
public void drawPath(Path path, Color color, boolean markers) { public void drawPath(Path path, Color color, boolean markers) {
Paint paintStroke = createPaintStroke(1, DEFAULT_PATH_COLOR); Paint paintStroke = createPaintStroke(1, DEFAULT_PATH_COLOR);
Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE); Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
for (Arc arc: path.getArcs()) { for (Arc arc: path.getArcs()) {
ArrayList<Point> points = arc.getPoints(); ArrayList<Point> points = arc.getPoints();
for (int i = 0; i < points.size(); ++i) { for (int i = 0; i < points.size(); ++i) {
line.getLatLongs().add(new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude())); line.getLatLongs().add(
} new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
}
} }
getLayerManager().getLayers().add(line); getLayerManager().getLayers().add(line);
extraLayers.add(paintStroke); extraLayers.add(paintStroke);
if (markers) { if (markers) {
drawMarker(path.getOrigin().getPoint()); drawMarker(path.getOrigin().getPoint());
drawMarker(path.getDestination().getPoint()); drawMarker(path.getDestination().getPoint());
} }
} }
@Override @Override
public void drawPath(Path path, Color color) { public void drawPath(Path path, Color color) {
drawPath(path, color, true); drawPath(path, color, true);
} }
@Override @Override
public void drawPath(Path path) { public void drawPath(Path path) {
drawPath(path, DEFAULT_PATH_COLOR, true); drawPath(path, DEFAULT_PATH_COLOR, true);
} }
@Override @Override
public void drawPath(Path path, boolean markers) { public void drawPath(Path path, boolean markers) {
drawPath(path, DEFAULT_PATH_COLOR, markers); drawPath(path, DEFAULT_PATH_COLOR, markers);
} }
} }