interface to native

This commit is contained in:
MadWasp79 2020-04-03 10:39:35 +03:00
parent 0ca08b7581
commit 91967e9ead
8 changed files with 223 additions and 221 deletions

View file

@ -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;

View file

@ -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 <string, string> 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<TransportStopExit> exits:
public int[] pTStopExit_x31s;
public int[] pTStopExit_y31s;
public String[] pTStopExit_refs;
// Convert into LinkedHashMap<String, int[]>
public String[] referenceToRoutesKeys;
public int[][] referenceToRoutesVals;
}
}

View file

@ -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<TransportRoute> convertedRoutesCache;
private static TLongObjectHashMap<TransportStop> convertedStopsCache;
public static List<TransportRouteResult> convertToTransportRoutingResult(NativeTransportRoutingResult[] res,
TransportRoutingConfiguration cfg) {
List<TransportRouteResult> convertedRes = new ArrayList<TransportRouteResult>();
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<Node> 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<TransportStop> convertTransportStops(NativeTransportStop[] nstops) {
List<TransportStop> 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<TransportRoute> 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;
}
}

View file

@ -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 <string, string> 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;
}

View file

@ -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;
}

View file

@ -0,0 +1,10 @@
package net.osmand.router.ptresult;
public class NativeTransportRoutingResult {
public NativeTransportRouteResultSegment[] segments;
public double finishWalkDist;
public double routeTime;
}

View file

@ -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<TransportStopExit> exits:
public int[] pTStopExit_x31s;
public int[] pTStopExit_y31s;
public String[] pTStopExit_refs;
// Convert into LinkedHashMap<String, int[]>
public String[] referenceToRoutesKeys;
public int[][] referenceToRoutesVals;
}

View file

@ -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<TransportRouteResult> calculateRouteImpl(TransportRouteCalculationParams params, NativeLibrary library) throws IOException, InterruptedException {
private List<TransportRouteResult> 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<TransportRoute> convertedRoutesCache;
private TLongObjectHashMap<TransportStop> convertedStopsCache;
private List<TransportRouteResult> convertToTransportRoutingResult(NativeTransportRoutingResult[] res,
TransportRoutingConfiguration cfg) {
List<TransportRouteResult> convertedRes = new ArrayList<TransportRouteResult>();
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<Node> 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<TransportStop> convertTransportStops(NativeTransportRoutingResult.NativeTransportStop[] nstops) {
List<TransportStop> 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<TransportRoute> 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;
}
}