From 01d7a74f2c4dc85ea94adfc93eb0e65eba5599ea Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Tue, 5 Jul 2011 10:22:33 +0200 Subject: [PATCH] Implement increasing road priority algorithm (so in the middle routing does not attempt to work with low priorities roads --- .../src/net/osmand/router/BicycleRouter.java | 1 + .../net/osmand/router/BinaryRoutePlanner.java | 19 ++++++++++++++--- .../src/net/osmand/router/CarRouter.java | 21 ++++++++++++++++++- .../src/net/osmand/router/VehicleRouter.java | 10 +++++++++ .../src/net/osmand/swing/MapRouterLayer.java | 2 +- 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/DataExtractionOSM/src/net/osmand/router/BicycleRouter.java b/DataExtractionOSM/src/net/osmand/router/BicycleRouter.java index c501adb6c8..92a4884e7b 100644 --- a/DataExtractionOSM/src/net/osmand/router/BicycleRouter.java +++ b/DataExtractionOSM/src/net/osmand/router/BicycleRouter.java @@ -84,6 +84,7 @@ public class BicycleRouter extends VehicleRouter { } return false; } + public boolean isOneWay(int highwayAttributes) { return MapRenderingTypes.isOneWayWay(highwayAttributes) || MapRenderingTypes.isRoundabout(highwayAttributes); diff --git a/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java b/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java index 61f0d36183..54e7c477a6 100644 --- a/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java +++ b/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java @@ -203,11 +203,13 @@ public class BinaryRoutePlanner { // Extract & analyze segment with min(f(x)) from queue while final segment is not found boolean inverse = false; + double roadDirectPriority = 0; + double roadOppositePriority = 0; + PriorityQueue graphSegments = inverse ? graphReverseSegments : graphDirectSegments; while(!graphSegments.isEmpty()){ RouteSegment segment = graphSegments.poll(); - ctx.visitedSegments ++; // for debug purposes if (ctx.visitor != null) { @@ -234,7 +236,8 @@ public class BinaryRoutePlanner { break; } inverse = nonHeuristicSegmentsComparator.compare(graphDirectSegments.peek(), graphReverseSegments.peek()) > 0; -// inverse = !inverse; + // different strategy : use onedirectional graph +// inverse = true; graphSegments = inverse ? graphReverseSegments : graphDirectSegments; } @@ -388,9 +391,18 @@ public class BinaryRoutePlanner { oppSegment.segmentEnd = next.segmentStart; return oppSegment; } + boolean processRoad = true; + if (ctx.useStrategyOfIncreasingRoadPriorities) { + double roadPriority = ctx.router.getRoadPriority(segment.road); + double nextRoadPriority = ctx.router.getRoadPriority(segment.road); + if (nextRoadPriority < roadPriority) { + processRoad = false; + } + } + /* next.road.getId() >> 3 (1) != road.getId() >> 3 (1) - used that line for debug with osm map */ // road.id could be equal on roundabout, but we should accept them - if (!visitedSegments.contains(nts)) { + if (!visitedSegments.contains(nts) && processRoad) { int type = -1; for (int i = 0; i < road.getRestrictionCount(); i++) { if (road.getRestriction(i) == next.road.getId()) { @@ -621,6 +633,7 @@ public class BinaryRoutePlanner { public static class RoutingContext { // parameters of routing public int heuristicCoefficient = DEFAULT_HEURISTIC_COEFFICIENT; + public boolean useStrategyOfIncreasingRoadPriorities = true; public VehicleRouter router = new CarRouter(); // diff --git a/DataExtractionOSM/src/net/osmand/router/CarRouter.java b/DataExtractionOSM/src/net/osmand/router/CarRouter.java index cd206f1899..095ecb555a 100644 --- a/DataExtractionOSM/src/net/osmand/router/CarRouter.java +++ b/DataExtractionOSM/src/net/osmand/router/CarRouter.java @@ -95,6 +95,25 @@ public class CarRouter extends VehicleRouter { } return 0; } + + @Override + public double getRoadPriority(BinaryMapDataObject road) { + TagValuePair pair = road.getTagValue(0); + boolean highway = "highway".equals(pair.tag); + double priority = highway && autoPriorityValues.containsKey(pair.value) ? autoPriorityValues.get(pair.value) : 0.5d; + if("motorway_link".equals(pair.value) || "trunk".equals(pair.value) || + "trunk_link".equals(pair.value) || "motorway".equals(pair.value)) { + return 1.3d; + } else if(priority >= 1){ + return 1; + } else if(priority >= 0.7){ + return 0.7; + } else if(priority >= 0.5){ + return 0.5; + } else { + return 0.3; + } + } /** * return speed in m/s @@ -103,7 +122,7 @@ public class CarRouter extends VehicleRouter { TagValuePair pair = road.getTagValue(0); double speed = MapRenderingTypes.getMaxSpeedIfDefined(road.getHighwayAttributes()) / 3.6d; boolean highway = "highway".equals(pair.tag); - double priority = highway && autoPriorityValues.containsKey(pair.value) ? autoPriorityValues.get(pair.value) : 1d; + double priority = highway && autoPriorityValues.containsKey(pair.value) ? autoPriorityValues.get(pair.value) : 0.5d; if (speed == 0 && highway) { Double value = autoNotDefinedValues.get(pair.value); if (value == null) { diff --git a/DataExtractionOSM/src/net/osmand/router/VehicleRouter.java b/DataExtractionOSM/src/net/osmand/router/VehicleRouter.java index 5eb40c1f24..de16511472 100644 --- a/DataExtractionOSM/src/net/osmand/router/VehicleRouter.java +++ b/DataExtractionOSM/src/net/osmand/router/VehicleRouter.java @@ -28,6 +28,16 @@ public abstract class VehicleRouter { int attributes = road.getHighwayAttributes(); return MapRenderingTypes.isOneWayWay(attributes) || MapRenderingTypes.isRoundabout(attributes); } + + /** + * Used for algorithm of increasing road priorities (actually make sense only for car routing) + * other routers can increase/decrease road priorities in the middle of route + * @param road + * @return + */ + public double getRoadPriority(BinaryMapDataObject road) { + return 1; + } /** * return delay in seconds diff --git a/DataExtractionOSM/src/net/osmand/swing/MapRouterLayer.java b/DataExtractionOSM/src/net/osmand/swing/MapRouterLayer.java index 21d1eb398d..3e7e03f37e 100644 --- a/DataExtractionOSM/src/net/osmand/swing/MapRouterLayer.java +++ b/DataExtractionOSM/src/net/osmand/swing/MapRouterLayer.java @@ -49,7 +49,7 @@ import org.xml.sax.SAXException; public class MapRouterLayer implements MapPanelLayer { private /*final */ static boolean ANIMATE_CALCULATING_ROUTE = false; - private /*final */ static int SIZE_OF_ROUTES_TO_ANIMATE = 50; + private /*final */ static int SIZE_OF_ROUTES_TO_ANIMATE = 250; private MapPanel map;