diff --git a/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java b/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java index d55a7e8087..7b0b5b9f67 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java @@ -965,13 +965,12 @@ public class TransportRoutePlanner { } } - - //cache for converted TransportRoutes: - private static TLongObjectHashMap convertedRoutesCache; - private static TLongObjectHashMap convertedStopsCache; - public static List convertToTransportRoutingResult(NativeTransportRoutingResult[] res, - TransportRoutingConfiguration cfg) { + TransportRoutingConfiguration cfg) { + //cache for converted TransportRoutes: + TLongObjectHashMap convertedRoutesCache = new TLongObjectHashMap<>(); + TLongObjectHashMap convertedStopsCache = new TLongObjectHashMap<>(); + if (res.length == 0) { return new ArrayList(); } @@ -983,7 +982,7 @@ public class TransportRoutePlanner { for (NativeTransportRouteResultSegment ntrs : ntrr.segments) { TransportRouteResultSegment trs = new TransportRouteResultSegment(); - trs.route = convertTransportRoute(ntrs.route); + trs.route = convertTransportRoute(ntrs.route, convertedRoutesCache, convertedStopsCache); trs.walkTime = ntrs.walkTime; trs.travelDistApproximate = ntrs.travelDistApproximate; trs.travelTime = ntrs.travelTime; @@ -1001,7 +1000,9 @@ public class TransportRoutePlanner { return convertedRes; } - private static TransportRoute convertTransportRoute(NativeTransportRoute nr) { + private static TransportRoute convertTransportRoute(NativeTransportRoute nr, + TLongObjectHashMap convertedRoutesCache, + TLongObjectHashMap convertedStopsCache) { TransportRoute r = new TransportRoute(); r.setId(nr.id); r.setLocation(nr.routeLat, nr.routeLon); @@ -1013,15 +1014,17 @@ public class TransportRoutePlanner { } } r.setFileOffset(nr.fileOffset); - r.setForwardStops(convertTransportStops(nr.forwardStops)); + r.setForwardStops(convertTransportStops(nr.forwardStops, convertedStopsCache)); r.setRef(nr.ref); r.setOperator(nr.routeOperator); r.setType(nr.type); r.setDist(nr.dist); r.setColor(nr.color); - if (nr.intervals != null && nr.intervals.length > 0 && nr.avgStopIntervals !=null && nr.avgStopIntervals.length > 0 && nr.avgWaitIntervals != null && nr.avgWaitIntervals.length > 0) { - r.setSchedule(new TransportSchedule(new TIntArrayList(nr.intervals), new TIntArrayList(nr.avgStopIntervals), new TIntArrayList(nr.avgWaitIntervals))); + if (nr.intervals != null && nr.intervals.length > 0 && nr.avgStopIntervals !=null + && nr.avgStopIntervals.length > 0 && nr.avgWaitIntervals != null && nr.avgWaitIntervals.length > 0) { + r.setSchedule(new TransportSchedule(new TIntArrayList(nr.intervals), + new TIntArrayList(nr.avgStopIntervals), new TIntArrayList(nr.avgWaitIntervals))); } for (int i = 0; i < nr.waysIds.length; i++) { @@ -1032,16 +1035,14 @@ public class TransportRoutePlanner { r.addWay(new Way(nr.waysIds[i], wnodes)); } - if (convertedRoutesCache == null) { - convertedRoutesCache = new TLongObjectHashMap<>(); - } if (convertedRoutesCache.get(r.getId()) == null) { convertedRoutesCache.put(r.getId(), r); } return r; } - private static List convertTransportStops(NativeTransportStop[] nstops) { + private static List convertTransportStops(NativeTransportStop[] nstops, + TLongObjectHashMap convertedStopsCache) { List stops = new ArrayList<>(); for (NativeTransportStop ns : nstops) { if (convertedStopsCache != null && convertedStopsCache.get(ns.id) != null) { @@ -1065,16 +1066,6 @@ public class TransportRoutePlanner { s.distance = ns.distance; s.x31 = ns.x31; s.y31 = ns.y31; -// List routes1 = new ArrayList<>(); - //cache routes to avoid circular conversion and just search them by id -// for (int i = 0; i < ns.routes.length; i++) { -// if (s.getRoutesIds().length == ns.routes.length && convertedRoutesCache != null -// && convertedRoutesCache.get(ns.routesIds[i]) != null) { -// s.addRoute(convertedRoutesCache.get(ns.routesIds[i])); -// } else { -// s.addRoute(convertTransportRoute(ns.routes[i])); -// } -// } if (ns.pTStopExit_refs != null && ns.pTStopExit_refs.length > 0) { for (int i = 0; i < ns.pTStopExit_refs.length; i++) { diff --git a/OsmAnd/src/net/osmand/plus/routing/TransportRoutingHelper.java b/OsmAnd/src/net/osmand/plus/routing/TransportRoutingHelper.java index 822f1dac85..47084fb1f6 100644 --- a/OsmAnd/src/net/osmand/plus/routing/TransportRoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/routing/TransportRoutingHelper.java @@ -491,7 +491,7 @@ public class TransportRoutingHelper { } GeneralRouter prouter = config.getRouter(params.mode.getRoutingProfile()); TransportRoutingConfiguration cfg = new TransportRoutingConfiguration(prouter, params.params); - TransportRoutePlanner planner = new TransportRoutePlanner(); + TransportRoutingContext ctx = new TransportRoutingContext(cfg, library, files); ctx.calculationProgress = params.calculationProgress; if (ctx.library != null && !settings.PT_SAFE_MODE.get()) { @@ -501,9 +501,9 @@ public class TransportRoutingHelper { MapUtils.get31TileNumberX(params.end.getLongitude()), MapUtils.get31TileNumberY(params.end.getLatitude()), cfg, ctx.calculationProgress); - List res = TransportRoutePlanner.convertToTransportRoutingResult(nativeRes, cfg); - return res; + return TransportRoutePlanner.convertToTransportRoutingResult(nativeRes, cfg); } else { + TransportRoutePlanner planner = new TransportRoutePlanner(); return planner.buildRoute(ctx, params.start, params.end); } }