From 91967e9ead6d3ac64f93ea0cacd4ec9618b2b7e6 Mon Sep 17 00:00:00 2001 From: MadWasp79 Date: Fri, 3 Apr 2020 10:39:35 +0300 Subject: [PATCH] interface to native --- .../main/java/net/osmand/NativeLibrary.java | 2 +- .../router/NativeTransportRoutingResult.java | 78 ---------- .../osmand/router/TransportRoutePlanner.java | 133 ++++++++++++++++ .../router/ptresult/NativeTransportRoute.java | 33 ++++ .../NativeTransportRouteResultSegment.java | 12 ++ .../NativeTransportRoutingResult.java | 10 ++ .../router/ptresult/NativeTransportStop.java | 29 ++++ .../plus/routing/TransportRoutingHelper.java | 147 +----------------- 8 files changed, 223 insertions(+), 221 deletions(-) delete mode 100644 OsmAnd-java/src/main/java/net/osmand/router/NativeTransportRoutingResult.java create mode 100644 OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRoute.java create mode 100644 OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRouteResultSegment.java create mode 100644 OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRoutingResult.java create mode 100644 OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportStop.java diff --git a/OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java b/OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java index 7131dd7389..d06886bd17 100644 --- a/OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java +++ b/OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java @@ -25,7 +25,7 @@ import net.osmand.data.MapObject; import net.osmand.data.QuadRect; import net.osmand.render.RenderingRuleSearchRequest; import net.osmand.render.RenderingRulesStorage; -import net.osmand.router.NativeTransportRoutingResult; +import net.osmand.router.ptresult.NativeTransportRoutingResult; import net.osmand.router.PrecalculatedRouteDirection; import net.osmand.router.RouteCalculationProgress; import net.osmand.router.RouteSegmentResult; diff --git a/OsmAnd-java/src/main/java/net/osmand/router/NativeTransportRoutingResult.java b/OsmAnd-java/src/main/java/net/osmand/router/NativeTransportRoutingResult.java deleted file mode 100644 index 024ee7658f..0000000000 --- a/OsmAnd-java/src/main/java/net/osmand/router/NativeTransportRoutingResult.java +++ /dev/null @@ -1,78 +0,0 @@ -package net.osmand.router; - -public class NativeTransportRoutingResult { - - public NativeTransportRouteResultSegment[] segments; - public double finishWalkDist; - public double routeTime; - - public static class NativeTransportRouteResultSegment { - public NativeTransportRoute route; - public double walkTime; - public double travelDistApproximate; - public double travelTime; - public int start; - public int end; - public double walkDist ; - public int depTime; - } - - public static class NativeTransportRoute { - //MapObject part: - public long id; - public double routeLat; - public double routeLon; - public String name; - public String enName; - //to HashMap names - public String[] namesLng; - public String[] namesNames; - public int fileOffset; - //----- - public NativeTransportStop[] forwardStops; - public String ref; - public String routeOperator; - public String type; - public int dist; - public String color; - - // Convert into TransportSchedule: - public int[] intervals; - public int[] avgStopIntervals; - public int[] avgWaitIntervals; - - // Convert into ways (and nodes): - public long[] waysIds; - public long[][] waysNodesIds; - public double[][] waysNodesLats; - public double[][] waysNodesLons; - } - - public static class NativeTransportStop { - //MapObject part: - public long id; - public double stopLat; - public double stopLon; - public String name; - public String enName; - public String[] namesLng; - public String[] namesNames; - public int fileOffset; - //Leave next 3 field as arrays: - public int[] referencesToRoutes; - public long[] deletedRoutesIds; - public long[] routesIds; - public int distance; - public int x31; - public int y31; - - public NativeTransportRoute[] routes; - // Convert into List exits: - public int[] pTStopExit_x31s; - public int[] pTStopExit_y31s; - public String[] pTStopExit_refs; - // Convert into LinkedHashMap - public String[] referenceToRoutesKeys; - public int[][] referenceToRoutesVals; - } -} 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 54e7ff8a26..b527170d8b 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java @@ -24,8 +24,13 @@ import net.osmand.data.QuadRect; import net.osmand.data.TransportRoute; import net.osmand.data.TransportSchedule; import net.osmand.data.TransportStop; +import net.osmand.data.TransportStopExit; import net.osmand.osm.edit.Node; import net.osmand.osm.edit.Way; +import net.osmand.router.ptresult.NativeTransportRoute; +import net.osmand.router.ptresult.NativeTransportRouteResultSegment; +import net.osmand.router.ptresult.NativeTransportRoutingResult; +import net.osmand.router.ptresult.NativeTransportStop; import net.osmand.util.MapUtils; public class TransportRoutePlanner { @@ -962,9 +967,137 @@ public class TransportRoutePlanner { } } } + } + //cache for converted TransportRoutes: + private static TLongObjectHashMap convertedRoutesCache; + private static TLongObjectHashMap convertedStopsCache; + public static List convertToTransportRoutingResult(NativeTransportRoutingResult[] res, + TransportRoutingConfiguration cfg) { + List convertedRes = new ArrayList(); + for (net.osmand.router.ptresult.NativeTransportRoutingResult ntrr : res) { + TransportRouteResult trr = new TransportRouteResult(cfg); + trr.setFinishWalkDist(ntrr.finishWalkDist); + trr.setRouteTime(ntrr.routeTime); + + for (NativeTransportRouteResultSegment ntrs : ntrr.segments) { + TransportRouteResultSegment trs = new TransportRouteResultSegment(); + trs.route = convertTransportRoute(ntrs.route); + trs.walkTime = ntrs.walkTime; + trs.travelDistApproximate = ntrs.travelDistApproximate; + trs.travelTime = ntrs.travelTime; + trs.start = ntrs.start; + trs.end = ntrs.end; + trs.walkDist = ntrs.walkDist; + trs.depTime = ntrs.depTime; + + trr.addSegment(trs); + } + convertedRes.add(trr); + } + convertedStopsCache.clear(); + convertedRoutesCache.clear(); + return convertedRes; + } + + private static TransportRoute convertTransportRoute(NativeTransportRoute nr) { + TransportRoute r = new TransportRoute(); + r.setId(nr.id); + r.setLocation(nr.routeLat, nr.routeLon); + r.setName(nr.name); + r.setEnName(nr.enName); + if (nr.namesLng.length > 0 && nr.namesLng.length == nr.namesNames.length) { + for (int i = 0; i < nr.namesLng.length; i++) { + r.setName(nr.namesLng[i], nr.namesNames[i]); + } + } + r.setFileOffset(nr.fileOffset); + r.setForwardStops(convertTransportStops(nr.forwardStops)); + r.setRef(nr.ref); + r.setOperator(nr.routeOperator); + r.setType(nr.type); + r.setDist(nr.dist); + r.setColor(nr.color); + + if (nr.intervals.length > 0 || nr.avgStopIntervals.length > 0 || 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++) { + List wnodes = new ArrayList<>(); + for (int j = 0; j < nr.waysNodesIds[i].length; j++) { + wnodes.add(new Node(nr.waysNodesLats[i][j], nr.waysNodesLons[i][j], nr.waysNodesIds[i][j])); + } + 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) { + List stops = new ArrayList<>(); + for (NativeTransportStop ns : nstops) { + if (convertedStopsCache != null && convertedStopsCache.get(ns.id) != null) { + stops.add(convertedStopsCache.get(ns.id)); + continue; + } + TransportStop s = new TransportStop(); + s.setId(ns.id); + s.setLocation(ns.stopLat, ns.stopLon); + s.setName(ns.name); + s.setEnName(ns.enName); + if (ns.namesLng.length > 0 && ns.namesLng.length == ns.namesNames.length) { + for (int i = 0; i < ns.namesLng.length; i++) { + s.setName(ns.namesLng[i], ns.namesNames[i]); + } + } + s.setFileOffset(ns.fileOffset); + s.setReferencesToRoutes(ns.referencesToRoutes); + s.setDeletedRoutesIds(ns.deletedRoutesIds); + s.setRoutesIds(ns.routesIds); + 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.length > 0) { + for (int i = 0; i < ns.pTStopExit_refs.length; i++) { + s.addExit(new TransportStopExit(ns.pTStopExit_x31s[i], + ns.pTStopExit_y31s[i], ns.pTStopExit_refs[i])); + } + } + + if (ns.referenceToRoutesKeys.length > 0) { + for (int i = 0; i < ns.referenceToRoutesKeys.length; i++) { + s.putReferencesToRoutes(ns.referenceToRoutesKeys[i], ns.referenceToRoutesVals[i]); + } + } + if (convertedStopsCache == null) { + convertedStopsCache = new TLongObjectHashMap<>(); + } + if (convertedStopsCache.get(s.getId()) == null) { + convertedStopsCache.put(s.getId(), s); + } + stops.add(s); + } + return stops; } } diff --git a/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRoute.java b/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRoute.java new file mode 100644 index 0000000000..438acc2314 --- /dev/null +++ b/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRoute.java @@ -0,0 +1,33 @@ +package net.osmand.router.ptresult; + +public class NativeTransportRoute { + //MapObject part: + public long id = -1l; + public double routeLat = -1; + public double routeLon = -1; + public String name = ""; + public String enName = ""; + //to HashMap names + public String[] namesLng; + public String[] namesNames; + public int fileOffset; + //----- + public NativeTransportStop[] forwardStops; + public String ref = ""; + public String routeOperator = ""; + public String type = ""; + public int dist = -1; + public String color = ""; + + // Convert into TransportSchedule: + public int[] intervals; + public int[] avgStopIntervals; + public int[] avgWaitIntervals; + + // Convert into ways (and nodes): + public long[] waysIds; + public long[][] waysNodesIds; + public double[][] waysNodesLats; + public double[][] waysNodesLons; + +} diff --git a/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRouteResultSegment.java b/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRouteResultSegment.java new file mode 100644 index 0000000000..5430b5e5b3 --- /dev/null +++ b/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRouteResultSegment.java @@ -0,0 +1,12 @@ +package net.osmand.router.ptresult; + +public class NativeTransportRouteResultSegment { + public NativeTransportRoute route; + public double walkTime; + public double travelDistApproximate; + public double travelTime; + public int start; + public int end; + public double walkDist ; + public int depTime; +} diff --git a/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRoutingResult.java b/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRoutingResult.java new file mode 100644 index 0000000000..c6bba20647 --- /dev/null +++ b/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportRoutingResult.java @@ -0,0 +1,10 @@ +package net.osmand.router.ptresult; + +public class NativeTransportRoutingResult { + + public NativeTransportRouteResultSegment[] segments; + public double finishWalkDist; + public double routeTime; + + +} diff --git a/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportStop.java b/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportStop.java new file mode 100644 index 0000000000..acd329668a --- /dev/null +++ b/OsmAnd-java/src/main/java/net/osmand/router/ptresult/NativeTransportStop.java @@ -0,0 +1,29 @@ +package net.osmand.router.ptresult; + +public class NativeTransportStop { + //MapObject part: + public long id; + public double stopLat; + public double stopLon; + public String name; + public String enName; + public String[] namesLng; + public String[] namesNames; + public int fileOffset; + //Leave next 3 field as arrays: + public int[] referencesToRoutes; + public long[] deletedRoutesIds; + public long[] routesIds; + public int distance; + public int x31; + public int y31; + + public NativeTransportRoute[] routes; + // Convert into List exits: + public int[] pTStopExit_x31s; + public int[] pTStopExit_y31s; + public String[] pTStopExit_refs; + // Convert into LinkedHashMap + public String[] referenceToRoutesKeys; + public int[][] referenceToRoutesVals; +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/routing/TransportRoutingHelper.java b/OsmAnd/src/net/osmand/plus/routing/TransportRoutingHelper.java index f00759701b..ee62fadfdf 100644 --- a/OsmAnd/src/net/osmand/plus/routing/TransportRoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/routing/TransportRoutingHelper.java @@ -12,12 +12,7 @@ import net.osmand.ValueHolder; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.data.LatLon; import net.osmand.data.QuadRect; -import net.osmand.data.TransportRoute; -import net.osmand.data.TransportSchedule; -import net.osmand.data.TransportStop; -import net.osmand.data.TransportStopExit; import net.osmand.osm.edit.Node; -import net.osmand.osm.edit.Way; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; @@ -28,7 +23,7 @@ import net.osmand.plus.routing.RouteCalculationParams.RouteCalculationResultList import net.osmand.plus.routing.RouteProvider.RouteService; import net.osmand.plus.routing.RoutingHelper.RouteCalculationProgressCallback; import net.osmand.router.GeneralRouter; -import net.osmand.router.NativeTransportRoutingResult; + import net.osmand.router.RouteCalculationProgress; import net.osmand.router.RoutingConfiguration; import net.osmand.router.TransportRoutePlanner; @@ -36,11 +31,11 @@ import net.osmand.router.TransportRoutePlanner.TransportRouteResult; import net.osmand.router.TransportRoutePlanner.TransportRouteResultSegment; import net.osmand.router.TransportRoutePlanner.TransportRoutingContext; import net.osmand.router.TransportRoutingConfiguration; +import net.osmand.router.ptresult.NativeTransportRoutingResult; import net.osmand.util.MapUtils; import java.io.IOException; import java.lang.ref.WeakReference; -import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -50,9 +45,6 @@ import java.util.Queue; import java.util.TreeMap; import java.util.concurrent.ConcurrentLinkedQueue; -import gnu.trove.list.array.TIntArrayList; -import gnu.trove.map.hash.TLongObjectHashMap; - import static net.osmand.plus.notifications.OsmandNotification.NotificationType.NAVIGATION; public class TransportRoutingHelper { @@ -475,7 +467,8 @@ public class TransportRoutingHelper { * @throws IOException * @throws InterruptedException */ - private List calculateRouteImpl(TransportRouteCalculationParams params, NativeLibrary library) throws IOException, InterruptedException { + private List calculateRouteImpl(TransportRouteCalculationParams params, NativeLibrary library) + throws IOException, InterruptedException { RoutingConfiguration.Builder config = params.ctx.getRoutingConfigForMode(params.mode); BinaryMapIndexReader[] files = params.ctx.getResourceManager().getTransportRoutingMapFiles(); params.params.clear(); @@ -508,7 +501,7 @@ public class TransportRoutingHelper { MapUtils.get31TileNumberY(params.end.getLatitude()), cfg, ctx.calculationProgress); - return convertToTransportRoutingResult(nativeRes, cfg); + return TransportRoutePlanner.convertToTransportRoutingResult(nativeRes, cfg); } else { return planner.buildRoute(ctx, params.start, params.end); } @@ -692,134 +685,4 @@ public class TransportRoutingHelper { this.prevRunningJob = prevRunningJob; } } - - //cache for converted TransportRoutes: - private TLongObjectHashMap convertedRoutesCache; - private TLongObjectHashMap convertedStopsCache; - - private List convertToTransportRoutingResult(NativeTransportRoutingResult[] res, - TransportRoutingConfiguration cfg) { - List convertedRes = new ArrayList(); - for (NativeTransportRoutingResult ntrr : res) { - TransportRouteResult trr = new TransportRouteResult(cfg); - trr.setFinishWalkDist(ntrr.finishWalkDist); - trr.setRouteTime(ntrr.routeTime); - - for (NativeTransportRoutingResult.NativeTransportRouteResultSegment ntrs : ntrr.segments) { - TransportRouteResultSegment trs = new TransportRouteResultSegment(); - trs.route = convertTransportRoute(ntrs.route); - trs.walkTime = ntrs.walkTime; - trs.travelDistApproximate = ntrs.travelDistApproximate; - trs.travelTime = ntrs.travelTime; - trs.start = ntrs.start; - trs.end = ntrs.end; - trs.walkDist = ntrs.walkDist; - trs.depTime = ntrs.depTime; - - trr.addSegment(trs); - } - convertedRes.add(trr); - } - convertedStopsCache.clear(); - convertedRoutesCache.clear(); - return convertedRes; - } - - private TransportRoute convertTransportRoute(NativeTransportRoutingResult.NativeTransportRoute nr) { - TransportRoute r = new TransportRoute(); - r.setId(nr.id); - r.setLocation(nr.routeLat, nr.routeLon); - r.setName(nr.name); - r.setEnName(nr.enName); - if (nr.namesLng.length > 0 && nr.namesLng.length == nr.namesNames.length) { - for (int i = 0; i < nr.namesLng.length; i++) { - r.setName(nr.namesLng[i], nr.namesNames[i]); - } - } - r.setFileOffset(nr.fileOffset); - r.setForwardStops(convertTransportStops(nr.forwardStops)); - r.setRef(nr.ref); - r.setOperator(nr.routeOperator); - r.setType(nr.type); - r.setDist(nr.dist); - r.setColor(nr.color); - - if (nr.intervals.length > 0 || nr.avgStopIntervals.length > 0 || 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++) { - List wnodes = new ArrayList<>(); - for (int j = 0; j < nr.waysNodesIds[i].length; j++) { - wnodes.add(new Node(nr.waysNodesLats[i][j], nr.waysNodesLons[i][j], nr.waysNodesIds[i][j])); - } - 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 List convertTransportStops(NativeTransportRoutingResult.NativeTransportStop[] nstops) { - List stops = new ArrayList<>(); - for (NativeTransportRoutingResult.NativeTransportStop ns : nstops) { - if (convertedStopsCache != null && convertedStopsCache.get(ns.id) != null) { - stops.add(convertedStopsCache.get(ns.id)); - continue; - } - TransportStop s = new TransportStop(); - s.setId(ns.id); - s.setLocation(ns.stopLat, ns.stopLon); - s.setName(ns.name); - s.setEnName(ns.enName); - if (ns.namesLng.length > 0 && ns.namesLng.length == ns.namesNames.length) { - for (int i = 0; i < ns.namesLng.length; i++) { - s.setName(ns.namesLng[i], ns.namesNames[i]); - } - } - s.setFileOffset(ns.fileOffset); - s.setReferencesToRoutes(ns.referencesToRoutes); - s.setDeletedRoutesIds(ns.deletedRoutesIds); - s.setRoutesIds(ns.routesIds); - 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.length > 0) { - for (int i = 0; i < ns.pTStopExit_refs.length; i++) { - s.addExit(new TransportStopExit(ns.pTStopExit_x31s[i], - ns.pTStopExit_y31s[i], ns.pTStopExit_refs[i])); - } - } - - if (ns.referenceToRoutesKeys.length > 0) { - for (int i = 0; i < ns.referenceToRoutesKeys.length; i++) { - s.putReferencesToRoutes(ns.referenceToRoutesKeys[i], ns.referenceToRoutesVals[i]); - } - } - if (convertedStopsCache == null) { - convertedStopsCache = new TLongObjectHashMap<>(); - } - if (convertedStopsCache.get(s.getId()) == null) { - convertedStopsCache.put(s.getId(), s); - } - stops.add(s); - } - return stops; - } }