Update MapViewer.
This commit is contained in:
parent
5899dfa276
commit
24eeab7971
3 changed files with 936 additions and 904 deletions
File diff suppressed because it is too large
Load diff
|
@ -18,6 +18,20 @@
|
|||
*/
|
||||
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.Graph;
|
||||
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.util.LatLongUtils;
|
||||
import org.mapsforge.core.util.Parameters;
|
||||
import org.mapsforge.core.util.Utils;
|
||||
import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
|
||||
import org.mapsforge.map.awt.util.AwtUtil;
|
||||
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.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 {
|
||||
private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
|
||||
private static final boolean SHOW_DEBUG_LAYERS = false;
|
||||
private static final boolean SHOW_RASTER_MAP = false;
|
||||
private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
|
||||
private static final boolean SHOW_DEBUG_LAYERS = 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 TITLE = "Confirm close";
|
||||
private static final String MESSAGE = "Are you sure you want to exit the application?";
|
||||
private static final String TITLE = "Confirm close";
|
||||
|
||||
/**
|
||||
* Starts the {@code Samples}.
|
||||
*
|
||||
* @param args command line args: expects the map files as multiple parameters
|
||||
* with possible SRTM hgt folder as 1st argument.
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// Multithreaded map rendering
|
||||
Parameters.NUMBER_OF_THREADS = 2;
|
||||
/**
|
||||
* Starts the {@code Samples}.
|
||||
*
|
||||
* @param args
|
||||
* command line args: expects the map files as multiple parameters
|
||||
* with possible SRTM hgt folder as 1st argument.
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// Square frame buffer
|
||||
Parameters.SQUARE_FRAME_BUFFER = false;
|
||||
// Multithreaded map rendering
|
||||
Parameters.NUMBER_OF_THREADS = 2;
|
||||
|
||||
HillsRenderConfig hillsCfg = null;
|
||||
File demFolder = getDemFolder(args);
|
||||
if (demFolder != null) {
|
||||
MemoryCachingHgtReaderTileSource tileSource = new MemoryCachingHgtReaderTileSource(demFolder, new DiffuseLightShadingAlgorithm(), AwtGraphicFactory.INSTANCE);
|
||||
tileSource.setEnableInterpolationOverlap(true);
|
||||
hillsCfg = new HillsRenderConfig(tileSource);
|
||||
hillsCfg.indexOnThread();
|
||||
args = Arrays.copyOfRange(args, 1, args.length);
|
||||
}
|
||||
// Square frame buffer
|
||||
Parameters.SQUARE_FRAME_BUFFER = false;
|
||||
|
||||
List<File> mapFiles = getMapFiles(args);
|
||||
final MapView mapView = createMapView();
|
||||
final BoundingBox boundingBox = addLayers(mapView, mapFiles, hillsCfg);
|
||||
|
||||
// addAPath(mapView);
|
||||
HillsRenderConfig hillsCfg = null;
|
||||
File demFolder = getDemFolder(args);
|
||||
if (demFolder != null) {
|
||||
MemoryCachingHgtReaderTileSource tileSource = new MemoryCachingHgtReaderTileSource(
|
||||
demFolder, new DiffuseLightShadingAlgorithm(), AwtGraphicFactory.INSTANCE);
|
||||
tileSource.setEnableInterpolationOverlap(true);
|
||||
hillsCfg = new HillsRenderConfig(tileSource);
|
||||
hillsCfg.indexOnThread();
|
||||
args = Arrays.copyOfRange(args, 1, args.length);
|
||||
}
|
||||
|
||||
final PreferencesFacade preferencesFacade = new JavaPreferences(Preferences.userNodeForPackage(Samples.class));
|
||||
List<File> mapFiles = getMapFiles(args);
|
||||
final MapView mapView = createMapView();
|
||||
final BoundingBox boundingBox = addLayers(mapView, mapFiles, hillsCfg);
|
||||
|
||||
final JFrame frame = new JFrame();
|
||||
frame.setTitle("Mapsforge Samples");
|
||||
frame.add(mapView);
|
||||
frame.pack();
|
||||
frame.setSize(new Dimension(800, 600));
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
int result = JOptionPane.showConfirmDialog(frame, MESSAGE, TITLE, JOptionPane.YES_NO_OPTION);
|
||||
if (result == JOptionPane.YES_OPTION) {
|
||||
mapView.getModel().save(preferencesFacade);
|
||||
mapView.destroyAll();
|
||||
AwtGraphicFactory.clearResourceMemoryCache();
|
||||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
}
|
||||
}
|
||||
// addAPath(mapView);
|
||||
|
||||
@Override
|
||||
public void windowOpened(WindowEvent e) {
|
||||
final Model model = mapView.getModel();
|
||||
model.init(preferencesFacade);
|
||||
if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) {
|
||||
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, model.displayModel.getTileSize());
|
||||
model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
|
||||
}
|
||||
}
|
||||
});
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private static void addAPath(MapView mapView) throws Exception {
|
||||
final PreferencesFacade preferencesFacade = new JavaPreferences(
|
||||
Preferences.userNodeForPackage(Samples.class));
|
||||
|
||||
Graph gr = (new BinaryGraphReader(Openfile.open("Maps/midip.map"))).read();
|
||||
Path path = (new BinaryPathReader(Openfile.open("Paths/chemin_0x400_119963_96676.path"))).readPath(gr);
|
||||
|
||||
Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
|
||||
paintStroke.setColor(Color.GREEN);
|
||||
paintStroke.setStrokeWidth(3);
|
||||
paintStroke.setStyle(Style.STROKE);
|
||||
final JFrame frame = new JFrame();
|
||||
frame.setTitle("Mapsforge Samples");
|
||||
frame.add(mapView);
|
||||
frame.pack();
|
||||
frame.setSize(new Dimension(800, 600));
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
int result = JOptionPane.showConfirmDialog(frame, MESSAGE, TITLE,
|
||||
JOptionPane.YES_NO_OPTION);
|
||||
if (result == JOptionPane.YES_OPTION) {
|
||||
mapView.getModel().save(preferencesFacade);
|
||||
mapView.destroyAll();
|
||||
AwtGraphicFactory.clearResourceMemoryCache();
|
||||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
}
|
||||
}
|
||||
|
||||
Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
|
||||
@Override
|
||||
public void windowOpened(WindowEvent e) {
|
||||
final Model model = mapView.getModel();
|
||||
model.init(preferencesFacade);
|
||||
if (model.mapViewPosition.getZoomLevel() == 0
|
||||
|| !boundingBox.contains(model.mapViewPosition.getCenter())) {
|
||||
byte zoomLevel = LatLongUtils.zoomForBounds(
|
||||
model.mapViewDimension.getDimension(), boundingBox,
|
||||
model.displayModel.getTileSize());
|
||||
model.mapViewPosition.setMapPosition(
|
||||
new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
|
||||
}
|
||||
}
|
||||
});
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
for (Arc arc: path.getArcs()) {
|
||||
ArrayList<org.insa.graph.Point> points = arc.getPoints();
|
||||
for (int i = 0; i < points.size(); ++i) {
|
||||
line.getLatLongs().add(new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
mapView.getLayerManager().getLayers().add(line);
|
||||
}
|
||||
private static void addAPath(MapView mapView) throws Exception {
|
||||
|
||||
private static BoundingBox addLayers(MapView mapView, List<File> mapFiles, HillsRenderConfig hillsRenderConfig) {
|
||||
Layers layers = mapView.getLayerManager().getLayers();
|
||||
Graph gr = (new BinaryGraphReader(Openfile.open("Maps/midip.map"))).read();
|
||||
Path path = (new BinaryPathReader(Openfile.open("Paths/chemin_0x400_119963_96676.path")))
|
||||
.readPath(gr);
|
||||
|
||||
int tileSize = SHOW_RASTER_MAP ? 256 : 512;
|
||||
Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
|
||||
paintStroke.setColor(Color.GREEN);
|
||||
paintStroke.setStrokeWidth(3);
|
||||
paintStroke.setStyle(Style.STROKE);
|
||||
|
||||
// Tile cache
|
||||
TileCache tileCache = AwtUtil.createTileCache(
|
||||
tileSize,
|
||||
mapView.getModel().frameBufferModel.getOverdrawFactor(),
|
||||
1024,
|
||||
new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
|
||||
Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
|
||||
|
||||
final BoundingBox boundingBox;
|
||||
if (SHOW_RASTER_MAP) {
|
||||
// Raster
|
||||
mapView.getModel().displayModel.setFixedTileSize(tileSize);
|
||||
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();
|
||||
}
|
||||
for (Arc arc : path.getArcs()) {
|
||||
ArrayList<org.insa.graph.Point> points = arc.getPoints();
|
||||
for (int i = 0; i < points.size(); ++i) {
|
||||
line.getLatLongs().add(
|
||||
new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
|
||||
}
|
||||
}
|
||||
|
||||
// Debug
|
||||
if (SHOW_DEBUG_LAYERS) {
|
||||
layers.add(new TileGridLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
|
||||
layers.add(new TileCoordinatesLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
|
||||
}
|
||||
mapView.getLayerManager().getLayers().add(line);
|
||||
}
|
||||
|
||||
return boundingBox;
|
||||
}
|
||||
private static BoundingBox addLayers(MapView mapView, List<File> mapFiles,
|
||||
HillsRenderConfig hillsRenderConfig) {
|
||||
Layers layers = mapView.getLayerManager().getLayers();
|
||||
|
||||
private static MapView createMapView() {
|
||||
MapView mapView = new MapView();
|
||||
mapView.getMapScaleBar().setVisible(true);
|
||||
if (SHOW_DEBUG_LAYERS) {
|
||||
mapView.getFpsCounter().setVisible(true);
|
||||
}
|
||||
int tileSize = SHOW_RASTER_MAP ? 256 : 512;
|
||||
|
||||
return mapView;
|
||||
}
|
||||
// Tile cache
|
||||
TileCache tileCache = AwtUtil.createTileCache(tileSize,
|
||||
mapView.getModel().frameBufferModel.getOverdrawFactor(), 1024,
|
||||
new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
|
||||
|
||||
@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;
|
||||
}
|
||||
};
|
||||
}
|
||||
final BoundingBox boundingBox;
|
||||
if (SHOW_RASTER_MAP) {
|
||||
// Raster
|
||||
mapView.getModel().displayModel.setFixedTileSize(tileSize);
|
||||
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();
|
||||
}
|
||||
|
||||
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
|
||||
public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
|
||||
System.out.println("Tap on: " + tapLatLong);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT);
|
||||
return tileRendererLayer;
|
||||
}
|
||||
// Debug
|
||||
if (SHOW_DEBUG_LAYERS) {
|
||||
layers.add(new TileGridLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
|
||||
layers.add(new TileCoordinatesLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
|
||||
}
|
||||
|
||||
private static File getDemFolder(String[] args) {
|
||||
if (args.length == 0) {
|
||||
throw new IllegalArgumentException("missing argument: <mapFile>");
|
||||
}
|
||||
return boundingBox;
|
||||
}
|
||||
|
||||
File demFolder = new File(args[0]);
|
||||
if (demFolder.exists() && demFolder.isDirectory() && demFolder.canRead()) {
|
||||
return demFolder;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private static MapView createMapView() {
|
||||
MapView mapView = new MapView();
|
||||
mapView.getMapScaleBar().setVisible(true);
|
||||
if (SHOW_DEBUG_LAYERS) {
|
||||
mapView.getFpsCounter().setVisible(true);
|
||||
}
|
||||
|
||||
private static List<File> getMapFiles(String[] args) {
|
||||
if (args.length == 0) {
|
||||
throw new IllegalArgumentException("missing argument: <mapFile>");
|
||||
}
|
||||
return mapView;
|
||||
}
|
||||
|
||||
List<File> result = new ArrayList<>();
|
||||
for (String arg : args) {
|
||||
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;
|
||||
}
|
||||
@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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Samples() {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
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
|
||||
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) {
|
||||
if (args.length == 0) {
|
||||
throw new IllegalArgumentException("missing argument: <mapFile>");
|
||||
}
|
||||
|
||||
File demFolder = new File(args[0]);
|
||||
if (demFolder.exists() && demFolder.isDirectory() && demFolder.canRead()) {
|
||||
return demFolder;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<File> getMapFiles(String[] args) {
|
||||
if (args.length == 0) {
|
||||
throw new IllegalArgumentException("missing argument: <mapFile>");
|
||||
}
|
||||
|
||||
List<File> result = new ArrayList<>();
|
||||
for (String arg : args) {
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -4,12 +4,13 @@ import java.awt.Color;
|
|||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseWheelEvent;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
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.Graph;
|
||||
|
@ -24,10 +25,8 @@ import org.mapsforge.core.model.MapPosition;
|
|||
import org.mapsforge.core.util.LatLongUtils;
|
||||
import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
|
||||
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.datastore.MapDataStore;
|
||||
import org.mapsforge.map.layer.Layer;
|
||||
import org.mapsforge.map.layer.Layers;
|
||||
import org.mapsforge.map.layer.cache.TileCache;
|
||||
import org.mapsforge.map.layer.hills.HillsRenderConfig;
|
||||
|
@ -37,109 +36,119 @@ import org.mapsforge.map.layer.renderer.TileRendererLayer;
|
|||
import org.mapsforge.map.model.DisplayModel;
|
||||
import org.mapsforge.map.model.MapViewPosition;
|
||||
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.rendertheme.InternalRenderTheme;
|
||||
import org.mapsforge.map.rendertheme.XmlRenderTheme;
|
||||
|
||||
import com.google.common.util.concurrent.SettableFuture;
|
||||
|
||||
public class MapViewDrawing extends MapView implements Drawing {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 8606967833704938092L;
|
||||
|
||||
// Default path color.
|
||||
public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244);
|
||||
|
||||
// Graphic factory.
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 8606967833704938092L;
|
||||
|
||||
// Default path color.
|
||||
public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244);
|
||||
|
||||
// Graphic factory.
|
||||
private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
|
||||
|
||||
|
||||
// Default tile size.
|
||||
private static final int DEFAULT_TILE_SIZE = 512;
|
||||
|
||||
// Tile size.
|
||||
int tileSize;
|
||||
|
||||
ArrayList<Paint> extraLayers;
|
||||
|
||||
public MapViewDrawing() {
|
||||
setBackground(Color.WHITE);
|
||||
getMapScaleBar().setVisible(true);
|
||||
this.tileSize = DEFAULT_TILE_SIZE;
|
||||
DisplayModel model = getModel().displayModel;
|
||||
// Tile size.
|
||||
int tileSize;
|
||||
|
||||
ArrayList<Paint> extraLayers;
|
||||
|
||||
public MapViewDrawing() {
|
||||
getMapScaleBar().setVisible(true);
|
||||
this.tileSize = DEFAULT_TILE_SIZE;
|
||||
DisplayModel model = getModel().displayModel;
|
||||
model.setFixedTileSize(tileSize);
|
||||
model.setBackgroundColor(convertColor(Color.WHITE));
|
||||
|
||||
// model.setBackgroundColor(convertColor(Color.WHITE));
|
||||
|
||||
extraLayers = new ArrayList<Paint>();
|
||||
addMouseWheelListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||
byte zoomLevelDiff = (byte) -e.getWheelRotation();
|
||||
for (Paint p: extraLayers) {
|
||||
p.setStrokeWidth(p.getStrokeWidth() + zoomLevelDiff);
|
||||
p.setStrokeWidth(p.getStrokeWidth() + zoomLevelDiff);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected int convertColor(Color color) {
|
||||
return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(),
|
||||
color.getGreen(), color.getBlue());
|
||||
}
|
||||
|
||||
private int getStrokeWidth(int width) {
|
||||
byte zoomLevel = getModel().mapViewPosition.getZoomLevel();
|
||||
return width * (2 - (8 - zoomLevel));
|
||||
}
|
||||
|
||||
private Paint createPaintStroke(int width, Color color) {
|
||||
Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
|
||||
paintStroke.setStyle(Style.STROKE);
|
||||
if (width != 0) {
|
||||
paintStroke.setStrokeWidth(getStrokeWidth(width));
|
||||
}
|
||||
if (color != null) {
|
||||
paintStroke.setColor(convertColor(color));
|
||||
}
|
||||
return paintStroke;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param color
|
||||
* @return
|
||||
*/
|
||||
private static File getMapsforgeFileFromGraph(Graph graph) {
|
||||
// TODO: Find a way to change this...
|
||||
Map<Integer, String> idToNames = new HashMap<Integer, String>();
|
||||
idToNames.put(0x100, "insa");
|
||||
idToNames.put(0x101, "insa");
|
||||
idToNames.put(0x110, "paris");
|
||||
idToNames.put(0x200, "mayotte");
|
||||
idToNames.put(0x250, "newzealand");
|
||||
idToNames.put(0x300, "reunion");
|
||||
idToNames.put(0x400, "midip");
|
||||
idToNames.put(0x410, "morbihan");
|
||||
|
||||
File file = null;
|
||||
if (idToNames.containsKey(graph.getMapId())) {
|
||||
file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg");
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
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) {
|
||||
});
|
||||
}
|
||||
|
||||
protected int convertColor(Color color) {
|
||||
return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), color.getGreen(),
|
||||
color.getBlue());
|
||||
}
|
||||
|
||||
private int getStrokeWidth(int width) {
|
||||
byte zoomLevel = getModel().mapViewPosition.getZoomLevel();
|
||||
return width * (2 - (8 - zoomLevel));
|
||||
}
|
||||
|
||||
private Paint createPaintStroke(int width, Color color) {
|
||||
Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
|
||||
paintStroke.setStyle(Style.STROKE);
|
||||
if (width != 0) {
|
||||
paintStroke.setStrokeWidth(getStrokeWidth(width));
|
||||
}
|
||||
if (color != null) {
|
||||
paintStroke.setColor(convertColor(color));
|
||||
}
|
||||
return paintStroke;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param color
|
||||
* @return
|
||||
*/
|
||||
private File getMapsforgeFileFromGraph(Graph graph) {
|
||||
// TODO: Find a way to change this...
|
||||
Map<Integer, String> idToNames = new HashMap<Integer, String>();
|
||||
idToNames.put(0x100, "insa");
|
||||
idToNames.put(0x110, "paris");
|
||||
idToNames.put(0x200, "mayotte");
|
||||
idToNames.put(0x250, "newzealand");
|
||||
idToNames.put(0x300, "reunion");
|
||||
idToNames.put(0x400, "midip");
|
||||
idToNames.put(0x410, "morbihan");
|
||||
|
||||
File file = null;
|
||||
if (idToNames.containsKey(graph.getMapId())) {
|
||||
file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg");
|
||||
if (!file.exists()) {
|
||||
file = new File("Maps/new/" + idToNames.get(graph.getMapId()) + ".mapfg");
|
||||
}
|
||||
}
|
||||
|
||||
if (file == null || !file.exists()) {
|
||||
JFileChooser fileChooser = new JFileChooser("Maps/");
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("mapsforge files", "" + "mapfg"));
|
||||
if (fileChooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) {
|
||||
file = fileChooser.getSelectedFile();
|
||||
}
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
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
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
@ -148,112 +157,116 @@ public class MapViewDrawing extends MapView implements Drawing {
|
|||
tileRendererLayer.setXmlRenderTheme(renderTheme);
|
||||
return tileRendererLayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
getLayerManager().getLayers().clear();
|
||||
extraLayers.clear();
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawLine(Point from, Point to) {
|
||||
drawLine(from, to, 0, null);
|
||||
}
|
||||
@Override
|
||||
public void clear() {
|
||||
getLayerManager().getLayers().clear();
|
||||
extraLayers.clear();
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawLine(Point from, Point to, int width) {
|
||||
drawLine(from, to, width, null);
|
||||
}
|
||||
@Override
|
||||
public void drawLine(Point from, Point to) {
|
||||
drawLine(from, to, 0, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawLine(Point from, Point to, int width, Color color) {
|
||||
Paint paintStroke = createPaintStroke(width, color);
|
||||
@Override
|
||||
public void drawLine(Point from, Point to, int width) {
|
||||
drawLine(from, to, width, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawLine(Point from, Point to, int width, Color color) {
|
||||
Paint paintStroke = createPaintStroke(width, color);
|
||||
Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
|
||||
line.getLatLongs().add(convertPoint(from));
|
||||
line.getLatLongs().add(convertPoint(to));
|
||||
getLayerManager().getLayers().add(line);
|
||||
getLayerManager().getLayers().add(line);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawMarker(Point point) {
|
||||
drawMarker(point, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawMarker(Point point, Color color) {
|
||||
Marker marker = new Marker(convertPoint(point), GRAPHIC_FACTORY.createBitmap(10, 20), 1, 2);
|
||||
getLayerManager().getLayers().add(marker);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPoint(Point point, int width, Color color) {
|
||||
// TODO: Maybe do something?
|
||||
}
|
||||
@Override
|
||||
public void drawMarker(Point point) {
|
||||
drawMarker(point, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawGraph(Graph graph, GraphPalette palette) {
|
||||
|
||||
File graphFile = getMapsforgeFileFromGraph(graph);
|
||||
@Override
|
||||
public void drawMarker(Point point, Color color) {
|
||||
Marker marker = new Marker(convertPoint(point), GRAPHIC_FACTORY.createBitmap(10, 20), 1, 2);
|
||||
getLayerManager().getLayers().add(marker);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPoint(Point point, int width, Color color) {
|
||||
// TODO: Maybe do something?
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawGraph(Graph graph, GraphPalette palette) {
|
||||
|
||||
File graphFile = getMapsforgeFileFromGraph(graph);
|
||||
|
||||
// Tile cache
|
||||
TileCache tileCache = AwtUtil.createTileCache(
|
||||
tileSize, getModel().frameBufferModel.getOverdrawFactor(),
|
||||
1024, new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
|
||||
|
||||
TileCache tileCache = AwtUtil.createTileCache(tileSize,
|
||||
getModel().frameBufferModel.getOverdrawFactor(), 1024,
|
||||
new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
|
||||
|
||||
// Layers
|
||||
Layers layers = getLayerManager().getLayers();
|
||||
|
||||
MapDataStore mapDataStore = new MapFile(graphFile);
|
||||
TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore,
|
||||
getModel().mapViewPosition, null);
|
||||
TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore,
|
||||
getModel().mapViewPosition, null);
|
||||
layers.add(tileRendererLayer);
|
||||
BoundingBox boundingBox = mapDataStore.boundingBox();
|
||||
|
||||
|
||||
final Model model = getModel();
|
||||
if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) {
|
||||
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, model.displayModel.getTileSize());
|
||||
model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
|
||||
if (model.mapViewPosition.getZoomLevel() == 0
|
||||
|| !boundingBox.contains(model.mapViewPosition.getCenter())) {
|
||||
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(),
|
||||
boundingBox, model.displayModel.getTileSize());
|
||||
model.mapViewPosition
|
||||
.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
|
||||
model.mapViewPosition.setZoomLevelMin(zoomLevel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawGraph(Graph graph) {
|
||||
drawGraph(graph, null);
|
||||
}
|
||||
@Override
|
||||
public void drawGraph(Graph graph) {
|
||||
drawGraph(graph, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPath(Path path, Color color, boolean markers) {
|
||||
Paint paintStroke = createPaintStroke(1, DEFAULT_PATH_COLOR);
|
||||
Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
|
||||
@Override
|
||||
public void drawPath(Path path, Color color, boolean markers) {
|
||||
Paint paintStroke = createPaintStroke(1, DEFAULT_PATH_COLOR);
|
||||
Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
|
||||
for (Arc arc: path.getArcs()) {
|
||||
ArrayList<Point> points = arc.getPoints();
|
||||
for (int i = 0; i < points.size(); ++i) {
|
||||
line.getLatLongs().add(new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
|
||||
}
|
||||
ArrayList<Point> points = arc.getPoints();
|
||||
for (int i = 0; i < points.size(); ++i) {
|
||||
line.getLatLongs().add(
|
||||
new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
|
||||
}
|
||||
}
|
||||
getLayerManager().getLayers().add(line);
|
||||
extraLayers.add(paintStroke);
|
||||
if (markers) {
|
||||
drawMarker(path.getOrigin().getPoint());
|
||||
drawMarker(path.getDestination().getPoint());
|
||||
drawMarker(path.getOrigin().getPoint());
|
||||
drawMarker(path.getDestination().getPoint());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPath(Path path, Color color) {
|
||||
drawPath(path, color, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPath(Path path) {
|
||||
drawPath(path, DEFAULT_PATH_COLOR, true);
|
||||
}
|
||||
@Override
|
||||
public void drawPath(Path path, Color color) {
|
||||
drawPath(path, color, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPath(Path path) {
|
||||
drawPath(path, DEFAULT_PATH_COLOR, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPath(Path path, boolean markers) {
|
||||
drawPath(path, DEFAULT_PATH_COLOR, markers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPath(Path path, boolean markers) {
|
||||
drawPath(path, DEFAULT_PATH_COLOR, markers);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue