From 03b77cd5c986398bf95a7ca57017c8a50fa393e3 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Tue, 12 Jun 2012 01:09:59 +0200 Subject: [PATCH] Fix basic issues --- .../binary/BinaryMapRouteReaderAdapter.java | 2 + .../net/osmand/router/BinaryRoutePlanner.java | 10 +- .../src/net/osmand/router/RoutingContext.java | 2 +- .../osmand/plus/routing/RouteProvider.java | 144 ++++++++++-------- 4 files changed, 93 insertions(+), 65 deletions(-) diff --git a/DataExtractionOSM/src/net/osmand/binary/BinaryMapRouteReaderAdapter.java b/DataExtractionOSM/src/net/osmand/binary/BinaryMapRouteReaderAdapter.java index 194a0127a2..c2d1837fe6 100644 --- a/DataExtractionOSM/src/net/osmand/binary/BinaryMapRouteReaderAdapter.java +++ b/DataExtractionOSM/src/net/osmand/binary/BinaryMapRouteReaderAdapter.java @@ -598,6 +598,8 @@ public class BinaryMapRouteReaderAdapter { req.publish(ro); } } + // free objects + rs.dataObjects = null; } } diff --git a/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java b/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java index 57a149fd53..126796f8b2 100644 --- a/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java +++ b/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java @@ -147,7 +147,7 @@ public class BinaryRoutePlanner { * return list of segments */ public List searchRoute(final RoutingContext ctx, RouteSegment start, RouteSegment end) throws IOException { - boolean relaxingStrategy = false; + boolean relaxingStrategy = true; // measure time ctx.timeToLoad = 0; ctx.visitedSegments = 0; @@ -308,7 +308,7 @@ public class BinaryRoutePlanner { mine = s.distanceToEnd; } } - double d = mine * 2.5; + double d = mine * 3; iterator = graphSegments.iterator(); while (iterator.hasNext()) { RouteSegment s = iterator.next(); @@ -771,6 +771,7 @@ public class BinaryRoutePlanner { } Collections.reverse(result); // calculate time + float completeTime = 0; for (int i = 0; i < result.size(); i++) { RouteSegmentResult rr = result.get(i); RouteDataObject road = rr.getObject(); @@ -790,6 +791,7 @@ public class BinaryRoutePlanner { // last point turn time can be added // if(i + 1 < result.size()) { distOnRoadToPass += ctx.getRouter().calculateTurnTime(); } rr.setSegmentTime((float) distOnRoadToPass); + completeTime += distOnRoadToPass; } if (PRINT_TO_CONSOLE_ROUTE_INFORMATION_TO_TEST) { @@ -799,8 +801,8 @@ public class BinaryRoutePlanner { double endLat = MapUtils.get31LatitudeY(ctx.targetEndY); double endLon = MapUtils.get31LongitudeX(ctx.targetEndX); System.out.println(MessageFormat.format("", - startLat+"", startLon+"", endLat+"", endLon+"")); + " start_lat=\"{0}\" start_lon=\"{1}\" target_lat=\"{2}\" target_lon=\"{3}\" complete_time=\"{4}\">", + startLat+"", startLon+"", endLat+"", endLon+"", completeTime+"")); for (RouteSegmentResult res : result) { String name = "Unknown";//res.object.getName(); String ref = "";//res.object.getNameByType(res.object.getMapIndex().refEncodingType); diff --git a/DataExtractionOSM/src/net/osmand/router/RoutingContext.java b/DataExtractionOSM/src/net/osmand/router/RoutingContext.java index 05cc7545b2..5366cc8303 100644 --- a/DataExtractionOSM/src/net/osmand/router/RoutingContext.java +++ b/DataExtractionOSM/src/net/osmand/router/RoutingContext.java @@ -35,6 +35,7 @@ public class RoutingContext { TIntSet loadedTiles = new TIntHashSet(); // TODO delete this object ? TLongObjectHashMap idObjects = new TLongObjectHashMap(); + int relaxedIteration = 0; // 4. Warm object caches TLongSet nonRestrictedIds = new TLongHashSet(); @@ -58,7 +59,6 @@ public class RoutingContext { long timeToCalculate = 0; int visitedSegments = 0; int relaxedSegments = 0; - int relaxedIteration = 0; // callback of processing segments RouteSegmentVisitor visitor = null; diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java index 15d4c85c07..f3375993af 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java @@ -27,7 +27,6 @@ import net.osmand.LogUtil; import net.osmand.OsmAndFormatter; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.osm.LatLon; -import net.osmand.osm.MapUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; @@ -159,6 +158,35 @@ public class RouteProvider { } } + public RouteCalculationResult(List list, Location start, LatLon end) { + // TODO put directions information + this.directions = null; + this.errorMessage = null; + this.locations = new ArrayList(); + + for (RouteSegmentResult s : list) { + boolean plus = s.getStartPointIndex() < s.getEndPointIndex(); + int i = s.getStartPointIndex(); + while (true) { + Location n = new Location(""); //$NON-NLS-1$ + LatLon point = s.getPoint(i); + n.setLatitude(point.getLatitude()); + n.setLongitude(point.getLongitude()); + locations.add(n); + if (i == s.getEndPointIndex()) { + break; + } + if (plus) { + i++; + } else { + i--; + } + } + } + introduceFirstPoint(start); + updateListDistanceTime(); + } + public List getLocations() { return locations; } @@ -172,55 +200,70 @@ public class RouteProvider { } private void prepareResult(Location start, LatLon end) { - if (locations != null && !locations.isEmpty()) { // if there is no closest points to start - add it - if (locations.get(0).distanceTo(start) > 200) { - // add start point - locations.add(0, start); - if (directions != null) { - for (RouteDirectionInfo i : directions) { - i.routePointOffset++; - } - RouteDirectionInfo info = new RouteDirectionInfo(); - info.turnType = TurnType.valueOf(TurnType.C, false); - info.routePointOffset = 0; - info.descriptionRoute = "" ;//getString(ctx, R.string.route_head); //$NON-NLS-1$ - directions.add(0, info); - } - } + introduceFirstPoint(start); - // check points for duplicates (it is very bad for routing) - cloudmade could return it - for (int i = 0; i < locations.size() - 1; ) { - if(locations.get(i).distanceTo(locations.get(i+1)) == 0){ - locations.remove(i); - if (directions != null) { - for (RouteDirectionInfo info : directions) { - if(info.routePointOffset > i){ - info.routePointOffset--; - } - } - } + checkForDuplicatePoints(); + // Remove unnecessary go straight from CloudMade + // Remove also last direction because it will be added after + removeUnnecessaryGoAhead(); + } + + updateListDistanceTime(); + } + + // Remove unnecessary go straight from CloudMade + // Remove also last direction because it will be added after + private void removeUnnecessaryGoAhead() { + if(directions != null && directions.size() > 1){ + for (int i = 1; i < directions.size(); ) { + RouteDirectionInfo r = directions.get(i); + if(r.turnType.getValue().equals(TurnType.C)){ + RouteDirectionInfo prev = directions.get(i-1); + prev.expectedTime += r.expectedTime; + directions.remove(i); } else { i++; } } - // Remove unnecessary go straight from CloudMade - // Remove also last direction because it will be added after - if(directions != null && directions.size() > 1){ - for (int i = 1; i < directions.size(); ) { - RouteDirectionInfo r = directions.get(i); - if(r.turnType.getValue().equals(TurnType.C)){ - RouteDirectionInfo prev = directions.get(i-1); - prev.expectedTime += r.expectedTime; - directions.remove(i); - } else { - i++; + } + } + + private void checkForDuplicatePoints() { + // check points for duplicates (it is very bad for routing) - cloudmade could return it + for (int i = 0; i < locations.size() - 1; ) { + if(locations.get(i).distanceTo(locations.get(i+1)) == 0){ + locations.remove(i); + if (directions != null) { + for (RouteDirectionInfo info : directions) { + if(info.routePointOffset > i){ + info.routePointOffset--; + } } } + } else { + i++; } } - + } + private void introduceFirstPoint(Location start) { + if (locations.get(0).distanceTo(start) > 200) { + // add start point + locations.add(0, start); + if (directions != null) { + for (RouteDirectionInfo i : directions) { + i.routePointOffset++; + } + RouteDirectionInfo info = new RouteDirectionInfo(); + info.turnType = TurnType.valueOf(TurnType.C, false); + info.routePointOffset = 0; + info.descriptionRoute = "" ;//getString(ctx, R.string.route_head); //$NON-NLS-1$ + directions.add(0, info); + } + } + } + private void updateListDistanceTime() { listDistance = new int[locations.size()]; if (!locations.isEmpty()) { listDistance[locations.size() - 1] = 0; @@ -633,29 +676,10 @@ public class RouteProvider { if (en == null) { return new RouteCalculationResult("End point is far from allowed road."); } - List res = new ArrayList(); try { List result = router.searchRoute(ctx, st, en); - for (RouteSegmentResult s : result) { - boolean plus = s.getStartPointIndex() < s.getEndPointIndex(); - int i = s.getStartPointIndex(); - while (true) { - Location n = new Location(""); //$NON-NLS-1$ - LatLon point = s.getPoint(i); - n.setLatitude(point.getLatitude()); - n.setLongitude(point.getLongitude()); - res.add(n); - if (i == s.getEndPointIndex()) { - break; - } - if (plus) { - i++; - } else { - i--; - } - } - } - return new RouteCalculationResult(res, null, start, end, null); + + return new RouteCalculationResult(result, start, end); } catch (OutOfMemoryError e) { return new RouteCalculationResult("Not enough process memory"); }