From 22ee91008963179ab6bfccf80aa86a079aa3ea87 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 9 May 2011 21:50:03 +0200 Subject: [PATCH] Add routing animation to see --- .../osmand/binary/BinaryRouteDataReader.java | 11 ++ .../src/net/osmand/swing/MapRouterLayer.java | 114 +++++++++++++----- 2 files changed, 98 insertions(+), 27 deletions(-) diff --git a/DataExtractionOSM/src/net/osmand/binary/BinaryRouteDataReader.java b/DataExtractionOSM/src/net/osmand/binary/BinaryRouteDataReader.java index ad5b27319c..daad89cb1d 100644 --- a/DataExtractionOSM/src/net/osmand/binary/BinaryRouteDataReader.java +++ b/DataExtractionOSM/src/net/osmand/binary/BinaryRouteDataReader.java @@ -183,12 +183,18 @@ public class BinaryRouteDataReader { } + public interface RouteSegmentVisitor { + public void visitSegment(RouteSegment segment); + } + public static class RoutingContext { TLongObjectMap idObjects = new TLongObjectHashMap(); TLongObjectMap routes = new TLongObjectHashMap(); CarRouter router = new CarRouter(); TIntSet loadedTiles = new TIntHashSet(); + // set collection to not null to monitor visited ways + public RouteSegmentVisitor visitor = null; long timeToLoad = 0; long timeToCalculate = 0; @@ -399,6 +405,11 @@ public class BinaryRouteDataReader { RouteSegment segment = graphSegments.poll(); BinaryMapDataObject road = segment.road; + // for debug purposes + if (ctx.visitor != null) { + ctx.visitor.visitSegment(segment); + } + // Always start from segmentStart (!), not from segmentEnd // It makes difference only for the first start segment // Middle point will always be skipped from observation considering already visited diff --git a/DataExtractionOSM/src/net/osmand/swing/MapRouterLayer.java b/DataExtractionOSM/src/net/osmand/swing/MapRouterLayer.java index 5188f4e767..62cd9c0b7e 100644 --- a/DataExtractionOSM/src/net/osmand/swing/MapRouterLayer.java +++ b/DataExtractionOSM/src/net/osmand/swing/MapRouterLayer.java @@ -10,6 +10,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.io.StringReader; +import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; @@ -19,6 +20,7 @@ import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; +import javax.swing.SwingUtilities; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -29,6 +31,7 @@ import net.osmand.binary.BinaryRouteDataReader; import net.osmand.binary.BinaryMapIndexReader.TagValuePair; import net.osmand.binary.BinaryRouteDataReader.RouteSegment; import net.osmand.binary.BinaryRouteDataReader.RouteSegmentResult; +import net.osmand.binary.BinaryRouteDataReader.RouteSegmentVisitor; import net.osmand.binary.BinaryRouteDataReader.RoutingContext; import net.osmand.data.DataTileManager; import net.osmand.osm.LatLon; @@ -92,14 +95,19 @@ public class MapRouterLayer implements MapPanelLayer { private static final long serialVersionUID = 507156107455281238L; public void actionPerformed(ActionEvent e) { - List ways = route(startRoute, endRoute); - DataTileManager points = new DataTileManager(); - points.setZoom(11); - for(Way w : ways){ - LatLon n = w.getLatLon(); - points.registerObject(n.getLatitude(), n.getLongitude(), w); - } - map.setPoints(points); + new Thread(){ + @Override + public void run() { + List ways = route(startRoute, endRoute); + DataTileManager points = new DataTileManager(); + points.setZoom(11); + for(Way w : ways){ + LatLon n = w.getLatLon(); + points.registerObject(n.getLatitude(), n.getLongitude(), w); + } + map.setPoints(points); + } + }.start(); } }; menu.add(route); @@ -107,14 +115,19 @@ public class MapRouterLayer implements MapPanelLayer { private static final long serialVersionUID = 507156107455281238L; public void actionPerformed(ActionEvent e) { - List ways = alternateRoute(startRoute, endRoute); - DataTileManager points = new DataTileManager(); - points.setZoom(11); - for(Way w : ways){ - LatLon n = w.getLatLon(); - points.registerObject(n.getLatitude(), n.getLongitude(), w); - } - map.setPoints(points); + new Thread() { + @Override + public void run() { + List ways = alternateRoute(startRoute, endRoute); + DataTileManager points = new DataTileManager(); + points.setZoom(11); + for (Way w : ways) { + LatLon n = w.getLatLon(); + points.registerObject(n.getLatitude(), n.getLongitude(), w); + } + map.setPoints(points); + } + }.start(); } }; menu.add(altroute); @@ -122,16 +135,21 @@ public class MapRouterLayer implements MapPanelLayer { private static final long serialVersionUID = 507156107455281238L; public void actionPerformed(ActionEvent e) { - List ways = selfRoute(startRoute, endRoute); - if (ways != null) { - DataTileManager points = new DataTileManager(); - points.setZoom(11); - for (Way w : ways) { - LatLon n = w.getLatLon(); - points.registerObject(n.getLatitude(), n.getLongitude(), w); + new Thread() { + @Override + public void run() { + List ways = selfRoute(startRoute, endRoute); + if (ways != null) { + DataTileManager points = new DataTileManager(); + points.setZoom(11); + for (Way w : ways) { + LatLon n = w.getLatLon(); + points.registerObject(n.getLatitude(), n.getLongitude(), w); + } + map.setPoints(points); + } } - map.setPoints(points); - } + }.start(); } }; @@ -340,9 +358,51 @@ public class MapRouterLayer implements MapPanelLayer { + (road.getId() >> 3)); } - + final DataTileManager points = new DataTileManager(); + points.setZoom(11); + map.setPoints(points); + ctx.visitor = new RouteSegmentVisitor() { + private final static int F_SIZE = 10; + private List cache = new ArrayList(); + + @Override + public void visitSegment(RouteSegment s) { + cache.add(s); + if(cache.size() < F_SIZE){ + return; + } + for (RouteSegment segment : cache) { + Way way = new Way(-1); + for (int i = 0; i < segment.getRoad().getPointsLength(); i++) { + net.osmand.osm.Node n = new net.osmand.osm.Node(MapUtils.get31LatitudeY(segment.getRoad() + .getPoint31YTile(i)), MapUtils.get31LongitudeX(segment.getRoad().getPoint31XTile(i)), -1); + way.addNode(n); + } + LatLon n = way.getLatLon(); + points.registerObject(n.getLatitude(), n.getLongitude(), way); + } + cache.clear(); + try { + SwingUtilities.invokeAndWait(new Runnable() { - for(RouteSegmentResult s : router.searchRoute(ctx, st, e)){ + @Override + public void run() { + map.prepareImage(); + } + }); + } catch (InterruptedException e1) { + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + }; + List searchRoute = router.searchRoute(ctx, st, e); + try { + Thread.sleep(4000); + } catch (InterruptedException e1) { + } + + for(RouteSegmentResult s : searchRoute){ // double dist = MapUtils.getDistance(s.startPoint, s.endPoint); Way way = new Way(-1); boolean plus = s.startPointIndex < s.endPointIndex;