Skip to content

Commit 705b80b

Browse files
Merge pull request opentripplanner#7482 from leonardehrenfried/traversal-distance
Convert A* walk distance to traversal distance
2 parents 3811ea4 + ce2508f commit 705b80b

12 files changed

Lines changed: 78 additions & 32 deletions

File tree

application/src/main/java/org/opentripplanner/routing/graphfinder/PlaceFinderTraverseVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void visitEdge(Edge edge) {}
104104
@Override
105105
public void visitVertex(State state) {
106106
Vertex vertex = state.getVertex();
107-
double distance = state.getWalkDistance();
107+
double distance = state.getTraversalDistanceMeters();
108108
if (vertex instanceof TransitStopVertex transitVertex) {
109109
var stop = Objects.requireNonNull(transitService.getRegularStop(transitVertex.getId()));
110110
handleStop(stop, distance);
@@ -142,7 +142,7 @@ public SkipEdgeStrategy<State, Edge> getSkipEdgeStrategy() {
142142
}
143143
}
144144

145-
return current.getWalkDistance() > furthestDistance;
145+
return current.getTraversalDistanceMeters() > furthestDistance;
146146
};
147147
}
148148

application/src/main/java/org/opentripplanner/routing/graphfinder/StopFinderTraverseVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ public List<NearbyStop> stopsFound() {
5454
* reached.
5555
*/
5656
public SkipEdgeStrategy<State, Edge> getSkipEdgeStrategy() {
57-
return (current, edge) -> current.getWalkDistance() > radiusMeters;
57+
return (current, edge) -> current.getTraversalDistanceMeters() > radiusMeters;
5858
}
5959
}

application/src/main/java/org/opentripplanner/routing/graphfinder/StreetGraphFinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private void findClosestUsingStreets(
107107
.withPreStartHook(OTPRequestTimeoutException::checkForTimeout)
108108
.withSkipEdgeStrategy(skipEdgeStrategy)
109109
.withTraverseVisitor(visitor)
110-
.withDominanceFunction(new DominanceFunctions.LeastWalk())
110+
.withDominanceFunction(new DominanceFunctions.ShortestDistance())
111111
.withRequest(request)
112112
.withFrom(linkerContext.findVertices(from))
113113
.getShortestPathTree();

application/src/main/java/org/opentripplanner/visualizer/GraphVisualizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ public void valueChanged(ListSelectionEvent e) {
14441444
stateListModel.addElement("weightdelta:" + st.getWeightDelta());
14451445
stateListModel.addElement("rentingVehicle:" + st.isRentingVehicle());
14461446
stateListModel.addElement("vehicleParked:" + st.isVehicleParked());
1447-
stateListModel.addElement("walkDistance:" + st.getWalkDistance());
1447+
stateListModel.addElement("traversalDistance:" + st.getTraversalDistanceMeters());
14481448
stateListModel.addElement("elapsedTime:" + st.getElapsedTimeSeconds());
14491449
outputList.setModel(stateListModel);
14501450

street/src/main/java/org/opentripplanner/street/model/edge/EscalatorEdge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public State[] traverse(State s0) {
3737
}
3838
s1.incrementWeight(s0.getRequest().walk().escalator().reluctance() * time);
3939
s1.incrementTimeInSeconds((long) time);
40-
s1.incrementWalkDistance(getDistanceMeters());
40+
s1.incrementTraversalDistanceMeters(getDistanceMeters());
4141
return s1.makeStateArray();
4242
} else {
4343
return State.empty();

street/src/main/java/org/opentripplanner/street/model/edge/StreetEdge.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,9 +1068,7 @@ private StateEditor doTraverse(State s0, TraverseMode traverseMode, boolean walk
10681068
weight += modeReluctance * request.turnReluctance() * turnDuration;
10691069
}
10701070

1071-
if (!traverseMode.isInCar()) {
1072-
s1.incrementWalkDistance(getDistanceWithElevation());
1073-
}
1071+
s1.incrementTraversalDistanceMeters(getDistanceWithElevation());
10741072

10751073
if (costExtension != null) {
10761074
weight += costExtension.calculateExtraCost(s0, length_mm, traverseMode);

street/src/main/java/org/opentripplanner/street/search/state/State.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ public class State implements AStarState<State, Edge, Vertex>, Cloneable {
4545
/* StateData contains data which is unlikely to change as often */
4646
public StateData stateData;
4747

48-
// how far have we walked
49-
// TODO(flamholz): this is a very confusing name as it actually applies to all non-transit modes.
50-
// we should DEFINITELY rename this variable and the associated methods.
51-
public double walkDistance;
48+
// how far have we traversed through the graph
49+
public double traversalDistance_m;
5250

5351
/* CONSTRUCTORS */
5452

@@ -75,7 +73,7 @@ public State(Vertex vertex, Instant startTime, StateData stateData, StreetSearch
7573
.rentalRestrictions()
7674
.noDropOffNetworks();
7775
}
78-
this.walkDistance = 0;
76+
this.traversalDistance_m = 0;
7977
this.time_ms = startTime.toEpochMilli();
8078
}
8179

@@ -269,8 +267,11 @@ public PropulsionType rentalVehiclePropulsionType() {
269267
return stateData.rentalVehiclePropulsionType;
270268
}
271269

272-
public double getWalkDistance() {
273-
return walkDistance;
270+
/**
271+
* Return how far this state has traversed through the graph, in meters.
272+
*/
273+
public double getTraversalDistanceMeters() {
274+
return traversalDistance_m;
274275
}
275276

276277
public Vertex getVertex() {
@@ -370,7 +371,7 @@ public State reverse() {
370371

371372
editor.incrementTimeInMilliseconds(orig.getAbsTimeDeltaMilliseconds());
372373
editor.incrementWeight(orig.getWeightDelta());
373-
editor.incrementWalkDistance(orig.getWalkDistanceDelta());
374+
editor.incrementTraversalDistanceMeters(orig.getTraversalDistanceDeltaMeters());
374375

375376
// propagate the modes through to the reversed edge
376377
editor.setBackMode(orig.getBackMode());
@@ -516,9 +517,9 @@ private int getAbsTimeDeltaMilliseconds() {
516517
return Math.abs(getTimeDeltaMilliseconds());
517518
}
518519

519-
private double getWalkDistanceDelta() {
520+
private double getTraversalDistanceDeltaMeters() {
520521
if (backState != null) {
521-
return Math.abs(this.walkDistance - backState.walkDistance);
522+
return Math.abs(this.traversalDistance_m - backState.traversalDistance_m);
522523
} else {
523524
return 0.0;
524525
}

street/src/main/java/org/opentripplanner/street/search/state/StateEditor.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,14 @@ public void incrementTimeInSeconds(long seconds) {
195195
incrementTimeInMilliseconds(1000L * seconds);
196196
}
197197

198-
public void incrementWalkDistance(double length) {
198+
/**
199+
* Increment the distance traversed through the graph in meters.
200+
*/
201+
public void incrementTraversalDistanceMeters(double length) {
199202
if (length < 0) {
200-
LOG.warn("A state's walk distance is being incremented by a negative amount.");
201-
defectiveTraversal = true;
202-
return;
203+
throw new IllegalArgumentException("Traversal distance cannot be negative");
203204
}
204-
child.walkDistance += length;
205+
child.traversalDistance_m += length;
205206
}
206207

207208
/* Basic Setters */

street/src/main/java/org/opentripplanner/street/search/strategy/DominanceFunctions.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,13 @@ public boolean betterOrEqual(State a, State b) {
143143
}
144144

145145
/**
146-
* A dominance function that prefers the least walking. This should only be used with walk-only
147-
* searches because it does not include any functions of time, and once transit is boarded walk
148-
* distance is constant.
149-
* <p>
150-
* It is used when building stop tree caches for egress from transit stops.
146+
* A dominance function that prefers the shortest distance.
151147
*/
152-
public static class LeastWalk extends DominanceFunctions {
148+
public static class ShortestDistance extends DominanceFunctions {
153149

154150
@Override
155151
protected boolean betterOrEqual(State a, State b) {
156-
return a.getWalkDistance() <= b.getWalkDistance();
152+
return a.getTraversalDistanceMeters() <= b.getTraversalDistanceMeters();
157153
}
158154
}
159155

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.opentripplanner.street.geometry;
2+
3+
import org.locationtech.jts.geom.Coordinate;
4+
5+
public class TestCoordinates {
6+
7+
public static final Coordinate BERLIN_TV_TOWER = of(52.5212, 13.4105);
8+
public static final Coordinate BERLIN_BRANDENBURG_GATE = of(52.51627, 13.37770);
9+
10+
/**
11+
* Because it is a frequent mistake to swap x/y and longitude/latitude when
12+
* constructing JTS Coordinates, this static factory method makes is clear
13+
* which is which.
14+
*/
15+
public static Coordinate of(double latitude, double longitude) {
16+
return new Coordinate(longitude, latitude);
17+
}
18+
}

0 commit comments

Comments
 (0)