Skip to content

Commit

Permalink
Fix public transport routing (#21538)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanPyrohivskyi authored Dec 9, 2024
1 parent 6bb3982 commit 915a3ba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import gnu.trove.list.array.TIntArrayList;
import gnu.trove.map.hash.TLongObjectHashMap;

import net.osmand.PlatformUtil;
import net.osmand.data.LatLon;
import net.osmand.data.QuadRect;
import net.osmand.data.TransportRoute;
Expand All @@ -22,13 +23,16 @@
import net.osmand.osm.edit.Way;
import net.osmand.util.MapUtils;

import org.apache.commons.logging.Log;

public class TransportRoutePlanner {

private static final boolean MEASURE_TIME = false;

private static final int MIN_DIST_STOP_TO_GEOMETRY = 150;
public static final long GEOMETRY_WAY_ID = -1;
public static final long STOPS_WAY_ID = -2;
private final static Log LOG = PlatformUtil.getLog(TransportRoutePlanner.class);

public List<TransportRouteResult> buildRoute(TransportRoutingContext ctx, LatLon start, LatLon end) throws IOException, InterruptedException {
ctx.startCalcTime = System.currentTimeMillis();
Expand All @@ -41,6 +45,7 @@ public List<TransportRouteResult> buildRoute(TransportRoutingContext ctx, LatLon
endSegments.put(s.getId(), s);
}
if(startStops.size() == 0) {
LOG.info("Public transport. Start stop is empty");
return Collections.emptyList();
}
PriorityQueue<TransportRouteSegment> queue = new PriorityQueue<TransportRouteSegment>(startStops.size(), new SegmentsComparator(ctx));
Expand Down Expand Up @@ -581,6 +586,7 @@ public static List<TransportRouteResult> convertToTransportRoutingResult(NativeT
TLongObjectHashMap<TransportStop> convertedStopsCache = new TLongObjectHashMap<>();

if (res.length == 0) {
LOG.info("Public transport. No route found");
return new ArrayList<TransportRouteResult>();
}
List<TransportRouteResult> convertedRes = new ArrayList<TransportRouteResult>();
Expand Down
26 changes: 23 additions & 3 deletions OsmAnd/src/net/osmand/plus/routing/TransportRoutingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.osmand.data.LatLon;
import net.osmand.data.QuadRect;
import net.osmand.data.ValueHolder;
import net.osmand.map.WorldRegion;
import net.osmand.osm.edit.Node;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
Expand Down Expand Up @@ -225,8 +224,12 @@ private void recalculateRouteInBackground(LatLon start, LatLon end) {
private void startRouteCalculationThread(TransportRouteCalculationParams params) {
synchronized (this) {
app.getSettings().LAST_ROUTE_APPLICATION_MODE.set(routingHelper.getAppMode());
RouteRecalculationTask newTask = new RouteRecalculationTask(this, params,
app.getSettings().SAFE_MODE.get() ? null : NativeOsmandLibrary.getLoadedLibrary());
NativeOsmandLibrary library = null;
if (!app.getSettings().SAFE_MODE.get()) {
library = NativeOsmandLibrary.getLoadedLibrary();
initNativeRouteFiles(params);
}
RouteRecalculationTask newTask = new RouteRecalculationTask(this, params, library);
lastTask = newTask;
startProgress(params);
updateProgress(params);
Expand All @@ -235,6 +238,21 @@ private void startRouteCalculationThread(TransportRouteCalculationParams params)
}
}

private void initNativeRouteFiles(TransportRouteCalculationParams params) {
NativeOsmandLibrary library = NativeOsmandLibrary.getLoadedLibrary();
if (library == null) {
return;
}
QuadRect bbox = new QuadRect();
MapUtils.insetLatLonRect(bbox, params.start.getLatitude(), params.start.getLongitude());
MapUtils.insetLatLonRect(bbox, params.end.getLatitude(), params.end.getLongitude());
int leftX = MapUtils.get31TileNumberX(bbox.left);
int rightX = MapUtils.get31TileNumberX(bbox.right);
int topY = MapUtils.get31TileNumberY(bbox.top);
int bottomY = MapUtils.get31TileNumberY(bbox.bottom);
params.ctx.getResourceManager().getRenderer().checkInitialized(15, library, leftX, rightX, bottomY, topY);
}

public void setProgressBar(TransportRouteCalculationProgressCallback progressRoute) {
this.progressRoute = progressRoute;
}
Expand Down Expand Up @@ -512,6 +530,7 @@ private List<TransportRouteResult> calculateRouteImpl(TransportRouteCalculationP
TransportRoutingContext ctx = new TransportRoutingContext(cfg, library, files);
ctx.calculationProgress = params.calculationProgress;
if (ctx.library != null && !settings.PT_SAFE_MODE.get()) {
log.info("Public transport. Use native library");
NativeTransportRoutingResult[] nativeRes = library.runNativePTRouting(
MapUtils.get31TileNumberX(params.start.getLongitude()),
MapUtils.get31TileNumberY(params.start.getLatitude()),
Expand All @@ -520,6 +539,7 @@ private List<TransportRouteResult> calculateRouteImpl(TransportRouteCalculationP
cfg, ctx.calculationProgress);
return TransportRoutePlanner.convertToTransportRoutingResult(nativeRes, cfg);
} else {
log.info("Public transport. No native library present");
TransportRoutePlanner planner = new TransportRoutePlanner();
return planner.buildRoute(ctx, params.start, params.end);
}
Expand Down

0 comments on commit 915a3ba

Please sign in to comment.