Merge remote-tracking branch 'origin/RoutePreparationMenu' into RoutePreparationMenu

This commit is contained in:
Chumva 2018-12-14 11:16:41 +02:00
commit 7a8e779046
13 changed files with 269 additions and 24 deletions

View file

@ -578,7 +578,21 @@ public class RouteDataObject {
public boolean loop(){
return pointsX[0] == pointsX[pointsX.length - 1] && pointsY[0] == pointsY[pointsY.length - 1] ;
}
public boolean platform(){
int sz = types.length;
for(int i=0; i<sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if(r.getTag().equals("railway") && r.getValue().equals("platform")) {
return true;
}
if(r.getTag().equals("public_transport") && r.getValue().equals("platform")) {
return true;
}
}
return false;
}
public boolean roundabout(){
int sz = types.length;
for(int i=0; i<sz; i++) {

View file

@ -55,6 +55,10 @@ public class RoutePlannerFrontEnd {
}
public RouteSegmentPoint findRouteSegment(double lat, double lon, RoutingContext ctx, List<RouteSegmentPoint> list) throws IOException {
return findRouteSegment(lat, lon, ctx, list, false);
}
public RouteSegmentPoint findRouteSegment(double lat, double lon, RoutingContext ctx, List<RouteSegmentPoint> list, boolean transportStop) throws IOException {
int px = MapUtils.get31TileNumberX(lon);
int py = MapUtils.get31TileNumberY(lat);
ArrayList<RouteDataObject> dataObjects = new ArrayList<RouteDataObject>();
@ -92,7 +96,26 @@ public class RoutePlannerFrontEnd {
}
});
if (list.size() > 0) {
RouteSegmentPoint ps = list.get(0);
RouteSegmentPoint ps = null;
if (ctx.publicTransport) {
for (RouteSegmentPoint p : list) {
if (transportStop && p.distSquare > 100) {
break;
}
boolean platform = p.road.platform();
if (transportStop && platform) {
ps = p;
break;
}
if (!transportStop && !platform) {
ps = p;
break;
}
}
}
if (ps == null) {
ps = list.get(0);
}
ps.others = list;
return ps;
}
@ -185,17 +208,17 @@ public class RoutePlannerFrontEnd {
}
int indexNotFound = 0;
List<RouteSegmentPoint> points = new ArrayList<RouteSegmentPoint>();
if (!addSegment(start, ctx, indexNotFound++, points)) {
if (!addSegment(start, ctx, indexNotFound++, points, ctx.startTransportStop)) {
return null;
}
if (intermediates != null) {
for (LatLon l : intermediates) {
if (!addSegment(l, ctx, indexNotFound++, points)) {
if (!addSegment(l, ctx, indexNotFound++, points, false)) {
return null;
}
}
}
if (!addSegment(end, ctx, indexNotFound++, points)) {
if (!addSegment(end, ctx, indexNotFound++, points, ctx.targetTransportStop)) {
return null;
}
ctx.calculationProgress.nextIteration();
@ -315,8 +338,8 @@ public class RoutePlannerFrontEnd {
}
private boolean addSegment(LatLon s, RoutingContext ctx, int indexNotFound, List<RouteSegmentPoint> res) throws IOException {
RouteSegmentPoint f = findRouteSegment(s.getLatitude(), s.getLongitude(), ctx, null);
private boolean addSegment(LatLon s, RoutingContext ctx, int indexNotFound, List<RouteSegmentPoint> res, boolean transportStop) throws IOException {
RouteSegmentPoint f = findRouteSegment(s.getLatitude(), s.getLongitude(), ctx, null, transportStop);
if (f == null) {
ctx.calculationProgress.segmentNotFound = indexNotFound;
return false;

View file

@ -41,7 +41,6 @@ public class RouteResultPreparation {
*/
List<RouteSegmentResult> prepareResult(RoutingContext ctx, FinalRouteSegment finalSegment) throws IOException {
List<RouteSegmentResult> result = convertFinalSegmentToResults(ctx, finalSegment);
combineWayPointsForAreaRouting(ctx, result);
prepareResult(ctx, result);
return result;
}
@ -158,6 +157,7 @@ public class RouteResultPreparation {
}
List<RouteSegmentResult> prepareResult(RoutingContext ctx, List<RouteSegmentResult> result) throws IOException {
combineWayPointsForAreaRouting(ctx, result);
validateAllPointsConnected(result);
splitRoadsAndAttachRoadSegments(ctx, result);
calculateTimeSpeed(ctx, result);

View file

@ -57,8 +57,11 @@ public class RoutingContext {
// 1. Initial variables
public int startX;
public int startY;
public boolean startTransportStop;
public int targetX;
public int targetY;
public boolean targetTransportStop;
public boolean publicTransport;
// deprecated
public long firstRoadId;
public int firstRoadDirection;

View file

@ -327,7 +327,7 @@ public class TransportRoutePlanner {
public List<TransportRouteResultSegment> getSegments() {
return segments;
}
public double getWalkDist() {
double d = finishWalkDist;
for (TransportRouteResultSegment s : segments) {

View file

@ -222,7 +222,7 @@ public class SearchCoreUITest {
});
if (files != null) {
for (File f : files) {
testSearchImpl(f);
//testSearchImpl(f);
}
}
}

View file

@ -10,6 +10,7 @@ import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.routing.RouteCalculationParams;
import net.osmand.plus.routing.RouteCalculationResult;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.router.RouteCalculationProgress;
import net.osmand.util.MapUtils;
@ -246,7 +247,8 @@ public class MarkersPlanRouteContext {
};
params.resultListener = new RouteCalculationParams.RouteCalculationResultListener() {
@Override
public void onRouteCalculated(List<Location> locations) {
public void onRouteCalculated(RouteCalculationResult route) {
List<Location> locations = route.getRouteLocations();
ArrayList<WptPt> pts = new ArrayList<>(locations.size());
for (Location loc : locations) {
WptPt pt = new WptPt();

View file

@ -10,6 +10,7 @@ import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.measurementtool.command.MeasurementCommandManager;
import net.osmand.plus.routing.RouteCalculationParams;
import net.osmand.plus.routing.RouteCalculationResult;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.router.RouteCalculationProgress;
@ -312,7 +313,8 @@ public class MeasurementEditingContext {
};
params.resultListener = new RouteCalculationParams.RouteCalculationResultListener() {
@Override
public void onRouteCalculated(List<Location> locations) {
public void onRouteCalculated(RouteCalculationResult route) {
List<Location> locations = route.getRouteLocations();
ArrayList<WptPt> pts = new ArrayList<>(locations.size());
for (Location loc : locations) {
if(!loc.hasAltitude()){

View file

@ -29,11 +29,14 @@ public class RouteCalculationParams {
public boolean fast;
public boolean leftSide;
public boolean inSnapToRoadMode;
public boolean inPublicTransportMode;
public boolean startTransportStop;
public boolean targetTransportStop;
public RouteCalculationProgress calculationProgress;
public RouteCalculationProgressCallback calculationProgressCallback;
public RouteCalculationResultListener resultListener;
public interface RouteCalculationResultListener {
void onRouteCalculated(List<Location> locations);
void onRouteCalculated(RouteCalculationResult route);
}
}

View file

@ -648,6 +648,9 @@ public class RouteProvider {
&& precalculated == null;
ctx.leftSideNavigation = params.leftSide;
ctx.calculationProgress = params.calculationProgress;
ctx.publicTransport = params.inPublicTransportMode;
ctx.startTransportStop = params.startTransportStop;
ctx.targetTransportStop = params.targetTransportStop;
if(params.previousToRecalculate != null && params.onlyStartPointChanged) {
int currentRoute = params.previousToRecalculate.getCurrentRoute();
List<RouteSegmentResult> originalRoute = params.previousToRecalculate.getOriginalRoute();

View file

@ -91,6 +91,7 @@ public class RoutingHelper {
voiceRouter = new VoiceRouter(this, settings);
provider = new RouteProvider();
transportRoutingHelper = context.getTransportRoutingHelper();
transportRoutingHelper.setRoutingHelper(this);
setAppMode(settings.APPLICATION_MODE.get());
}
@ -894,11 +895,11 @@ public class RoutingHelper {
RouteCalculationResult prev = route;
synchronized (RoutingHelper.this) {
if (res.isCalculated()) {
if (!params.inSnapToRoadMode) {
if (!params.inSnapToRoadMode && !params.inPublicTransportMode) {
route = res;
}
if (params.resultListener != null) {
params.resultListener.onRouteCalculated(res.getRouteLocations());
params.resultListener.onRouteCalculated(res);
}
} else {
evalWaitInterval = Math.max(3000, evalWaitInterval * 3 / 2); // for Issue #3899
@ -907,7 +908,7 @@ public class RoutingHelper {
currentRunningJob = null;
}
if(res.isCalculated()){
if (!params.inSnapToRoadMode) {
if (!params.inSnapToRoadMode && !params.inPublicTransportMode) {
setNewRoute(prev, res, params.start);
}
} else if (onlineSourceWithoutInternet) {

View file

@ -1,27 +1,39 @@
package net.osmand.plus.routing;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.ValueHolder;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.LatLon;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.routing.RouteCalculationParams.RouteCalculationResultListener;
import net.osmand.plus.routing.RouteProvider.RouteService;
import net.osmand.plus.routing.RoutingHelper.RouteCalculationProgressCallback;
import net.osmand.router.RouteCalculationProgress;
import net.osmand.router.RoutingConfiguration;
import net.osmand.router.TransportRoutePlanner;
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.util.MapUtils;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import static net.osmand.plus.notifications.OsmandNotification.NotificationType.NAVIGATION;
@ -32,8 +44,10 @@ public class TransportRoutingHelper {
private List<WeakReference<IRouteInformationListener>> listeners = new LinkedList<>();
private OsmandApplication app;
private RoutingHelper routingHelper;
private List<TransportRouteResult> routes;
private Map<Pair<TransportRouteResultSegment, TransportRouteResultSegment>, RouteCalculationResult> walkingRouteSegments;
private int currentRoute;
private LatLon startLocation;
@ -51,6 +65,10 @@ public class TransportRoutingHelper {
this.app = app;
}
public void setRoutingHelper(RoutingHelper routingHelper) {
this.routingHelper = routingHelper;
}
public LatLon getStartLocation() {
return startLocation;
}
@ -75,6 +93,11 @@ public class TransportRoutingHelper {
return routes;
}
@Nullable
public RouteCalculationResult getWalkingRouteSegment(TransportRouteResultSegment s1, TransportRouteResultSegment s2) {
return walkingRouteSegments.get(new Pair<>(s1, s2));
}
public void setCurrentRoute(int currentRoute) {
this.currentRoute = currentRoute;
}
@ -113,6 +136,9 @@ public class TransportRoutingHelper {
params.ctx = app;
params.calculationProgress = new RouteCalculationProgress();
float rd = (float) MapUtils.getDistance(start, end);
params.calculationProgress.totalEstimatedDistance = rd * 1.5f;
startRouteCalculationThread(params);
}
@ -203,6 +229,7 @@ public class TransportRoutingHelper {
public synchronized void clearCurrentRoute(LatLon newFinalLocation) {
routes = null;
walkingRouteSegments = null;
app.getWaypointHelper().setNewRoute(new RouteCalculationResult(""));
app.runInUIThread(new Runnable() {
@Override
@ -267,11 +294,48 @@ public class TransportRoutingHelper {
}
}
private class WalkingRouteSegment {
TransportRouteResultSegment s1;
TransportRouteResultSegment s2;
LatLon start;
boolean startTransportStop;
LatLon end;
boolean endTransportStop;
WalkingRouteSegment(TransportRouteResultSegment s1, TransportRouteResultSegment s2) {
this.s1 = s1;
this.s2 = s2;
start = s1.getEnd().getLocation();
end = s2.getStart().getLocation();
startTransportStop = true;
endTransportStop = true;
}
WalkingRouteSegment(LatLon start, TransportRouteResultSegment s) {
this.start = start;
this.s2 = s;
end = s2.getStart().getLocation();
endTransportStop = true;
}
WalkingRouteSegment(TransportRouteResultSegment s, LatLon end) {
this.s1 = s;
this.end = end;
start = s1.getEnd().getLocation();
startTransportStop = true;
}
}
private class RouteRecalculationThread extends Thread {
private final TransportRouteCalculationParams params;
private Thread prevRunningJob;
private final Queue<WalkingRouteSegment> walkingSegmentsToCalculate = new ConcurrentLinkedQueue<>();
private Map<Pair<TransportRouteResultSegment, TransportRouteResultSegment>, RouteCalculationResult> walkingRouteSegments = new HashMap<>();
private boolean walkingSegmentsCalculated;
public RouteRecalculationThread(String name, TransportRouteCalculationParams params) {
super(name);
this.params = params;
@ -295,6 +359,118 @@ public class TransportRoutingHelper {
return planner.buildRoute(ctx, params.start, params.end);
}
@Nullable
private RouteCalculationParams getWalkingRouteParams() {
ApplicationMode walkingMode = ApplicationMode.PEDESTRIAN;
final WalkingRouteSegment walkingRouteSegment = walkingSegmentsToCalculate.poll();
if (walkingRouteSegment == null) {
return null;
}
Location start = new Location("");
start.setLatitude(walkingRouteSegment.start.getLatitude());
start.setLongitude(walkingRouteSegment.start.getLongitude());
LatLon end = new LatLon(walkingRouteSegment.end.getLatitude(), walkingRouteSegment.end.getLongitude());
final float currentDistanceFromBegin =
RouteRecalculationThread.this.params.calculationProgress.distanceFromBegin +
(walkingRouteSegment.s1 != null ? (float) walkingRouteSegment.s1.getTravelDist() : 0);
final RouteCalculationParams params = new RouteCalculationParams();
params.inPublicTransportMode = true;
params.start = start;
params.end = end;
params.startTransportStop = walkingRouteSegment.startTransportStop;
params.targetTransportStop = walkingRouteSegment.endTransportStop;
RoutingHelper.applyApplicationSettings(params, app.getSettings(), walkingMode);
params.mode = walkingMode;
params.ctx = app;
params.calculationProgress = new RouteCalculationProgress();
params.calculationProgressCallback = new RouteCalculationProgressCallback() {
@Override
public void start() {
}
@Override
public void updateProgress(int progress) {
float p = Math.max(params.calculationProgress.distanceFromBegin,
params.calculationProgress.distanceFromEnd);
RouteRecalculationThread.this.params.calculationProgress.distanceFromBegin =
Math.max(RouteRecalculationThread.this.params.calculationProgress.distanceFromBegin, currentDistanceFromBegin + p);
}
@Override
public void requestPrivateAccessRouting() {
}
@Override
public void finish() {
if (walkingSegmentsToCalculate.isEmpty()) {
walkingSegmentsCalculated = true;
} else {
updateProgress(0);
}
}
};
params.resultListener = new RouteCalculationResultListener() {
@Override
public void onRouteCalculated(RouteCalculationResult route) {
RouteRecalculationThread.this.walkingRouteSegments.put(new Pair<>(walkingRouteSegment.s1, walkingRouteSegment.s2), route);
if (!walkingSegmentsToCalculate.isEmpty()) {
RouteCalculationParams walkingRouteParams = getWalkingRouteParams();
if (walkingRouteParams != null) {
routingHelper.startRouteCalculationThread(walkingRouteParams, true, true);
}
}
}
};
return params;
}
private void calculateWalkingRoutes(List<TransportRouteResult> routes) {
walkingSegmentsCalculated = false;
walkingSegmentsToCalculate.clear();
walkingRouteSegments.clear();
if (routes != null && routes.size() > 0) {
for (TransportRouteResult r : routes) {
TransportRouteResultSegment prev = null;
for (TransportRouteResultSegment s : r.getSegments()) {
LatLon start = prev != null ? prev.getEnd().getLocation() : params.start;
LatLon end = s.getStart().getLocation();
if (start != null && end != null) {
if (prev == null || MapUtils.getDistance(start, end) > 50) {
walkingSegmentsToCalculate.add(prev == null ?
new WalkingRouteSegment(start, s) : new WalkingRouteSegment(prev, s));
}
}
prev = s;
}
if (prev != null) {
walkingSegmentsToCalculate.add(new WalkingRouteSegment(prev, params.end));
}
}
RouteCalculationParams walkingRouteParams = getWalkingRouteParams();
if (walkingRouteParams != null) {
routingHelper.startRouteCalculationThread(walkingRouteParams, true, true);
// wait until all segments calculated
while (!walkingSegmentsCalculated) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// ignore
}
}
}
}
}
@Override
public void run() {
synchronized (TransportRoutingHelper.this) {
@ -317,6 +493,9 @@ public class TransportRoutingHelper {
String error = null;
try {
res = calculateRouteImpl(params);
if (res != null) {
calculateWalkingRoutes(res);
}
} catch (IOException e) {
error = e.getMessage();
log.error(e);
@ -329,6 +508,7 @@ public class TransportRoutingHelper {
}
synchronized (TransportRoutingHelper.this) {
routes = res;
TransportRoutingHelper.this.walkingRouteSegments = walkingRouteSegments;
if (res != null) {
if (params.resultListener != null) {
params.resultListener.onRouteCalculated(res);

View file

@ -41,6 +41,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TreeMap;
import gnu.trove.list.array.TByteArrayList;
@ -804,25 +805,38 @@ public class RouteLayer extends OsmandMapLayer {
private void calculateTransportResult(LatLon start, LatLon end, TransportRouteResult r, List<Way> res, List<Integer> colors) {
if (r != null) {
LatLon p = start;
TransportRouteResultSegment prev = null;
for (TransportRouteResultSegment s : r.getSegments()) {
LatLon floc = s.getStart().getLocation();
addRouteWalk(p, floc, res, colors);
addRouteWalk(prev, s, p, floc, res, colors);
List<Way> geometry = s.getGeometry();
res.addAll(geometry);
addColors(s.route, geometry.size(), colors);
p = s.getEnd().getLocation();
prev = s;
}
addRouteWalk(p, end, res, colors);
addRouteWalk(prev, null, p, end, res, colors);
}
}
private void addRouteWalk(LatLon s, LatLon e, List<Way> res, List<Integer> colors) {
double dist = MapUtils.getDistance(s, e);
if (dist > 50) {
private void addRouteWalk(TransportRouteResultSegment s1, TransportRouteResultSegment s2,
LatLon start, LatLon end, List<Way> res, List<Integer> colors) {
final RouteCalculationResult walkingRouteSegment = transportHelper.getWalkingRouteSegment(s1, s2);
if (walkingRouteSegment != null && walkingRouteSegment.getRouteLocations().size() > 0) {
final List<Location> routeLocations = walkingRouteSegment.getRouteLocations();
Way way = new Way(-1);
way.putTag(OSMSettings.OSMTagKey.NAME.getValue(), String.format("Walk %.1f m", dist));
way.addNode(new Node(s.getLatitude(), s.getLongitude(), -1));
way.addNode(new Node(e.getLatitude(), e.getLongitude(), -1));
way.putTag(OSMSettings.OSMTagKey.NAME.getValue(), String.format(Locale.US, "Walk %d m", walkingRouteSegment.getWholeDistance()));
for (Location l : routeLocations) {
way.addNode(new Node(l.getLatitude(), l.getLongitude(), -1));
}
res.add(way);
addColors(null, 1, colors);
} else {
double dist = MapUtils.getDistance(start, end);
Way way = new Way(-1);
way.putTag(OSMSettings.OSMTagKey.NAME.getValue(), String.format(Locale.US, "Walk %.1f m", dist));
way.addNode(new Node(start.getLatitude(), start.getLongitude(), -1));
way.addNode(new Node(end.getLatitude(), end.getLongitude(), -1));
res.add(way);
addColors(null, 1, colors);
}