Skip to content

Commit 1ea783b

Browse files
Merge pull request #6805 from leonardehrenfried/fix-multimodal
Restore lookup of multi-modal stations and group of stations
2 parents 33115bb + fb3832a commit 1ea783b

File tree

26 files changed

+405
-125
lines changed

26 files changed

+405
-125
lines changed

application/src/ext-test/java/org/opentripplanner/ext/flex/template/ClosestTripTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ClosestTripTest {
4444
private static final FlexAccessEgressCallbackAdapter ADAPTER =
4545
new FlexAccessEgressCallbackAdapter() {
4646
@Override
47-
public TransitStopVertex getStopVertexForStopId(FeedScopedId id) {
47+
public TransitStopVertex getStopVertex(FeedScopedId id) {
4848
return null;
4949
}
5050

@@ -76,7 +76,7 @@ void doNotFilter() {
7676

7777
var trips = closestTrips(matcher);
7878
assertThat(trips).hasSize(1);
79-
assertEquals(List.copyOf(trips).getFirst().flexTrip(), FLEX_TRIP);
79+
assertEquals(FLEX_TRIP, List.copyOf(trips).getFirst().flexTrip());
8080
}
8181

8282
@Test

application/src/ext/java/org/opentripplanner/ext/flex/FlexRouter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ private List<FlexServiceDate> createFlexServiceDates(
197197
private class CallbackAdapter implements FlexAccessEgressCallbackAdapter {
198198

199199
@Override
200-
public TransitStopVertex getStopVertexForStopId(FeedScopedId stopId) {
201-
return graph.getStopVertexForStopId(stopId);
200+
public TransitStopVertex getStopVertex(FeedScopedId stopId) {
201+
return graph.getStopVertex(stopId);
202202
}
203203

204204
@Override

application/src/ext/java/org/opentripplanner/ext/flex/template/AbstractFlexTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ StopLocation getAccessEgressStop() {
9999
*/
100100
Stream<FlexAccessEgress> createFlexAccessEgressStream(FlexAccessEgressCallbackAdapter callback) {
101101
if (transferStop instanceof RegularStop stop) {
102-
var flexVertex = callback.getStopVertexForStopId(stop.getId());
102+
var flexVertex = callback.getStopVertex(stop.getId());
103103
return Stream.of(createFlexAccessEgress(new ArrayList<>(), flexVertex, stop)).filter(
104104
Objects::nonNull
105105
);

application/src/ext/java/org/opentripplanner/ext/flex/template/FlexAccessEgressCallbackAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
public interface FlexAccessEgressCallbackAdapter {
2020
/** Adapter, look at implementing service for documentation. */
21-
TransitStopVertex getStopVertexForStopId(FeedScopedId id);
21+
TransitStopVertex getStopVertex(FeedScopedId id);
2222

2323
/** Adapter, look at implementing service for documentation. */
2424
Collection<PathTransfer> getTransfersFromStop(StopLocation stop);

application/src/main/java/org/opentripplanner/routing/algorithm/raptoradapter/router/TransitRouter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ private TemporaryVerticesContainer createTemporaryVerticesContainer(
382382
return new TemporaryVerticesContainer(
383383
serverContext.graph(),
384384
serverContext.vertexLinker(),
385+
serverContext.transitService()::findStopOrChildIds,
385386
request.from(),
386387
request.to(),
387388
request.journey().access().mode(),

application/src/main/java/org/opentripplanner/routing/algorithm/raptoradapter/router/street/DirectFlexRouter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static List<Itinerary> route(
3030
var temporaryVertices = new TemporaryVerticesContainer(
3131
serverContext.graph(),
3232
serverContext.vertexLinker(),
33+
serverContext.transitService()::findStopOrChildIds,
3334
request.from(),
3435
request.to(),
3536
request.journey().direct().mode(),

application/src/main/java/org/opentripplanner/routing/algorithm/raptoradapter/router/street/DirectStreetRouter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static List<Itinerary> route(OtpServerRequestContext serverContext, Route
3636
var temporaryVertices = new TemporaryVerticesContainer(
3737
serverContext.graph(),
3838
serverContext.vertexLinker(),
39+
serverContext.transitService()::findStopOrChildIds,
3940
request.from(),
4041
request.to(),
4142
request.journey().direct().mode(),

application/src/main/java/org/opentripplanner/routing/graph/Graph.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.HashSet;
88
import java.util.List;
99
import java.util.Map;
10+
import java.util.Optional;
1011
import java.util.Set;
1112
import java.util.concurrent.ConcurrentHashMap;
1213
import java.util.stream.Collectors;
@@ -21,6 +22,7 @@
2122
import org.opentripplanner.routing.services.notes.StreetNotesService;
2223
import org.opentripplanner.street.model.edge.Edge;
2324
import org.opentripplanner.street.model.edge.StreetEdge;
25+
import org.opentripplanner.street.model.vertex.StationCentroidVertex;
2426
import org.opentripplanner.street.model.vertex.TransitStopVertex;
2527
import org.opentripplanner.street.model.vertex.Vertex;
2628
import org.opentripplanner.street.model.vertex.VertexLabel;
@@ -207,19 +209,27 @@ public <T extends Vertex> List<T> getVerticesOfType(Class<T> cls) {
207209
* Return the vertex corresponding to the stop id, or null.
208210
*/
209211
@Nullable
210-
public TransitStopVertex getStopVertexForStopId(FeedScopedId id) {
212+
public TransitStopVertex getStopVertex(FeedScopedId id) {
211213
requireIndex();
212-
return streetIndex.findTransitStopVertex(id);
214+
return streetIndex.findStopVertex(id).orElse(null);
213215
}
214216

215217
/**
216-
* If the {@code id} is a stop id return a set with a single element.
217-
* If it is a station id return a set containing all child stop vertices, or an empty
218-
* set otherwise.
218+
* If the {@code id} is a stop id return the corresponding vertex, otherwise return an empty
219+
* optional.
219220
*/
220-
public Set<TransitStopVertex> findStopOrChildStopsVertices(FeedScopedId stopId) {
221+
public Optional<TransitStopVertex> findStopVertex(FeedScopedId stopId) {
221222
requireIndex();
222-
return streetIndex.getStopOrChildStopsVertices(stopId);
223+
return streetIndex.findStopVertex(stopId);
224+
}
225+
226+
/**
227+
* If the {@code stopId} is a station id and it is configured to route to its center,
228+
* return the corresponding vertex, otherwise return an empty optional.
229+
*/
230+
public Optional<StationCentroidVertex> findStationCentroidVertex(FeedScopedId stopId) {
231+
requireIndex();
232+
return streetIndex.findStationCentroidVertex(stopId);
223233
}
224234

225235
/**
@@ -319,18 +329,7 @@ public OpeningHoursCalendarService getOpeningHoursCalendarService() {
319329
*/
320330
public Collection<Vertex> findVertices(Envelope env) {
321331
requireIndex();
322-
return streetIndex.getVerticesForEnvelope(env);
323-
}
324-
325-
/**
326-
* Get the street vertices for an id. If the id corresponds to a regular stop we will return the
327-
* coordinate for the stop.
328-
* If the id corresponds to a station we will either return the coordinates of the child stops or
329-
* the station centroid if the station is configured to route to centroid.
330-
*/
331-
public Set<Vertex> findStopVertices(FeedScopedId stopId) {
332-
requireIndex();
333-
return streetIndex.findStopVertices(stopId);
332+
return streetIndex.findVertices(env);
334333
}
335334

336335
/**

application/src/main/java/org/opentripplanner/routing/graph/StreetIndex.java

Lines changed: 22 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package org.opentripplanner.routing.graph;
22

3-
import com.google.common.collect.ArrayListMultimap;
4-
import com.google.common.collect.ImmutableSetMultimap;
5-
import com.google.common.collect.Multimap;
63
import java.util.Collection;
7-
import java.util.Collections;
84
import java.util.HashMap;
95
import java.util.List;
106
import java.util.Map;
11-
import java.util.Set;
7+
import java.util.Optional;
128
import java.util.stream.Collectors;
13-
import javax.annotation.Nullable;
149
import org.locationtech.jts.geom.Coordinate;
1510
import org.locationtech.jts.geom.Envelope;
1611
import org.locationtech.jts.geom.LineString;
@@ -37,8 +32,7 @@ class StreetIndex {
3732

3833
private static final Logger LOG = LoggerFactory.getLogger(StreetIndex.class);
3934

40-
private final Map<FeedScopedId, TransitStopVertex> stopVerticesById;
41-
private final ImmutableSetMultimap<FeedScopedId, TransitStopVertex> stopVerticesByParentId;
35+
private final Map<FeedScopedId, TransitStopVertex> stopVertices;
4236

4337
/**
4438
* This list contains transitStationVertices for the stations that are configured to route to centroid
@@ -54,31 +48,39 @@ class StreetIndex {
5448
StreetIndex(Graph graph) {
5549
this.edgeIndex = new EdgeSpatialIndex();
5650
this.vertexIndex = new HashGridSpatialIndex<>();
57-
var stopVertices = graph.getVerticesOfType(TransitStopVertex.class);
58-
this.stopVerticesById = indexStopIds(stopVertices);
59-
this.stopVerticesByParentId = indexStationIds(stopVertices);
51+
this.stopVertices = indexStopIds(graph);
6052

6153
this.stationCentroidVertices = indexStationCentroids(graph);
6254
postSetup(graph.getVertices());
6355
}
6456

65-
@Nullable
66-
TransitStopVertex findTransitStopVertex(FeedScopedId stopId) {
67-
return stopVerticesById.get(stopId);
57+
/**
58+
* @see Graph#findStopVertex(FeedScopedId) (FeedScopedId)
59+
*/
60+
Optional<TransitStopVertex> findStopVertex(FeedScopedId id) {
61+
return Optional.ofNullable(stopVertices.get(id));
62+
}
63+
64+
/**
65+
* @see Graph#findStationCentroidVertex(FeedScopedId)
66+
*/
67+
Optional<StationCentroidVertex> findStationCentroidVertex(FeedScopedId id) {
68+
return Optional.ofNullable(stationCentroidVertices.get(id));
6869
}
6970

7071
/**
7172
* Returns the vertices intersecting with the specified envelope.
7273
*/
73-
List<Vertex> getVerticesForEnvelope(Envelope envelope) {
74+
List<Vertex> findVertices(Envelope envelope) {
7475
List<Vertex> vertices = vertexIndex.query(envelope);
7576
// Here we assume vertices list modifiable
7677
vertices.removeIf(v -> !envelope.contains(new Coordinate(v.getLon(), v.getLat())));
7778
return vertices;
7879
}
7980

8081
/**
81-
* Return the edges whose geometry intersect with the specified envelope. Warning: edges disconnected from the graph
82+
* Return the edges whose geometry intersect with the specified envelope.
83+
* Warning: edges disconnected from the graph
8284
* will not be indexed.
8385
*/
8486
Collection<Edge> findEdges(Envelope envelope) {
@@ -92,42 +94,6 @@ Collection<Edge> findEdges(Envelope envelope) {
9294
.toList();
9395
}
9496

95-
@Override
96-
public String toString() {
97-
return (
98-
getClass().getName() +
99-
" -- edgeTree: " +
100-
edgeIndex.toString() +
101-
" -- verticesTree: " +
102-
vertexIndex.toString()
103-
);
104-
}
105-
106-
/**
107-
* @param id Id of Stop, Station, MultiModalStation or GroupOfStations
108-
* @return The associated TransitStopVertex or all underlying TransitStopVertices
109-
*/
110-
Set<TransitStopVertex> getStopOrChildStopsVertices(FeedScopedId id) {
111-
if (stopVerticesById.containsKey(id)) {
112-
return Set.of(stopVerticesById.get(id));
113-
} else if (stopVerticesByParentId.containsKey(id)) {
114-
return stopVerticesByParentId.get(id);
115-
} else {
116-
return Set.of();
117-
}
118-
}
119-
120-
/**
121-
* @see Graph#findStopVertices(FeedScopedId)
122-
*/
123-
Set<Vertex> findStopVertices(FeedScopedId id) {
124-
var stationVertex = stationCentroidVertices.get(id);
125-
if (stationVertex != null) {
126-
return Set.of(stationVertex);
127-
}
128-
return Collections.unmodifiableSet(getStopOrChildStopsVertices(id));
129-
}
130-
13197
Collection<Edge> findEdges(Envelope env, Scope scope) {
13298
return edgeIndex.query(env, scope).toList();
13399
}
@@ -147,6 +113,8 @@ void remove(Vertex vertex) {
147113
vertexIndex.remove(new Envelope(vertex.getCoordinate()), vertex);
148114
}
149115

116+
// private methods
117+
150118
private static LineString edgeGeometryOrStraightLine(Edge e) {
151119
LineString geometry = e.getGeometry();
152120
if (geometry == null) {
@@ -181,29 +149,15 @@ private void postSetup(Collection<Vertex> vertices) {
181149
LOG.info(progress.completeMessage());
182150
}
183151

184-
private static Map<FeedScopedId, TransitStopVertex> indexStopIds(
185-
Collection<TransitStopVertex> vertices
186-
) {
152+
private static Map<FeedScopedId, TransitStopVertex> indexStopIds(Graph graph) {
153+
var vertices = graph.getVerticesOfType(TransitStopVertex.class);
187154
var map = new HashMap<FeedScopedId, TransitStopVertex>();
188155
for (TransitStopVertex it : vertices) {
189156
map.put(it.getStop().getId(), it);
190157
}
191158
return Map.copyOf(map);
192159
}
193160

194-
private static ImmutableSetMultimap<FeedScopedId, TransitStopVertex> indexStationIds(
195-
Collection<TransitStopVertex> vertices
196-
) {
197-
Multimap<FeedScopedId, TransitStopVertex> map = ArrayListMultimap.create();
198-
vertices
199-
.stream()
200-
.filter(v -> v.getStop().isPartOfStation())
201-
.forEach(v -> {
202-
map.put(v.getStop().getParentStation().getId(), v);
203-
});
204-
return ImmutableSetMultimap.copyOf(map);
205-
}
206-
207161
private static Map<FeedScopedId, StationCentroidVertex> indexStationCentroids(Graph graph) {
208162
return graph
209163
.getVerticesOfType(StationCentroidVertex.class)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Comparator;
66
import java.util.List;
7+
import java.util.Set;
78
import org.locationtech.jts.geom.Coordinate;
89
import org.opentripplanner.astar.spi.SkipEdgeStrategy;
910
import org.opentripplanner.astar.spi.TraverseVisitor;
@@ -101,6 +102,7 @@ private void findClosestUsingStreets(
101102
var temporaryVertices = new TemporaryVerticesContainer(
102103
graph,
103104
linker,
105+
id -> Set.of(),
104106
GenericLocation.fromCoordinate(lat, lon),
105107
GenericLocation.UNKNOWN,
106108
StreetMode.WALK,

0 commit comments

Comments
 (0)