Add routing animation to see

This commit is contained in:
Victor Shcherb 2011-05-09 21:50:03 +02:00
parent 5bd4ab79ef
commit 22ee910089
2 changed files with 98 additions and 27 deletions

View file

@ -183,12 +183,18 @@ public class BinaryRouteDataReader {
}
public interface RouteSegmentVisitor {
public void visitSegment(RouteSegment segment);
}
public static class RoutingContext {
TLongObjectMap<BinaryMapDataObject> idObjects = new TLongObjectHashMap<BinaryMapDataObject>();
TLongObjectMap<RouteSegment> routes = new TLongObjectHashMap<RouteSegment>();
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

View file

@ -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<Way> ways = route(startRoute, endRoute);
DataTileManager<Way> points = new DataTileManager<Way>();
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<Way> ways = route(startRoute, endRoute);
DataTileManager<Way> points = new DataTileManager<Way>();
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<Way> ways = alternateRoute(startRoute, endRoute);
DataTileManager<Way> points = new DataTileManager<Way>();
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<Way> ways = alternateRoute(startRoute, endRoute);
DataTileManager<Way> points = new DataTileManager<Way>();
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<Way> ways = selfRoute(startRoute, endRoute);
if (ways != null) {
DataTileManager<Way> points = new DataTileManager<Way>();
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<Way> ways = selfRoute(startRoute, endRoute);
if (ways != null) {
DataTileManager<Way> points = new DataTileManager<Way>();
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<Way> points = new DataTileManager<Way>();
points.setZoom(11);
map.setPoints(points);
ctx.visitor = new RouteSegmentVisitor() {
private final static int F_SIZE = 10;
private List<RouteSegment> cache = new ArrayList<RouteSegment>();
@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<RouteSegmentResult> 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;