Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix public transport routing #21538

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading