Skip to content

Commit e61c3dd

Browse files
committed
gtfs: fix a bug where we would get an inconsistent realtime graph for some feeds
1 parent 1527a03 commit e61c3dd

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

reader-gtfs/src/main/java/com/graphhopper/gtfs/RealtimeFeed.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,19 @@ private static int[] findLeaveEdgesForTrip(GtfsStorage staticGtfs, String feedKe
171171
Trip trip = feed.trips.get(tripUpdate.getTrip().getTripId());
172172
StopTime next = feed.getOrderedStopTimesForTrip(trip.trip_id).iterator().next();
173173
int station = staticGtfs.getStationNodes().get(new GtfsStorage.FeedIdWithStopId(feedKey, next.stop_id));
174-
Optional<PtGraph.PtEdge> firstBoarding = StreamSupport.stream(staticGtfs.getPtGraph().backEdgesAround(station).spliterator(), false)
174+
Optional<PtGraph.PtEdge> firstAlighting = StreamSupport.stream(staticGtfs.getPtGraph().backEdgesAround(station).spliterator(), false)
175175
.flatMap(e -> StreamSupport.stream(staticGtfs.getPtGraph().backEdgesAround(e.getAdjNode()).spliterator(), false))
176176
.flatMap(e -> StreamSupport.stream(staticGtfs.getPtGraph().backEdgesAround(e.getAdjNode()).spliterator(), false))
177177
.filter(e -> e.getType() == GtfsStorage.EdgeType.ALIGHT)
178178
.filter(e -> normalize(e.getAttrs().tripDescriptor).equals(tripUpdate.getTrip()))
179-
.findAny();
180-
int n = firstBoarding.get().getAdjNode();
181-
Stream<PtGraph.PtEdge> boardEdges = evenIndexed(nodes(hopDwellChain(staticGtfs, n)))
179+
.min(Comparator.comparingInt(e -> e.getAttrs().stop_sequence));
180+
if (firstAlighting.isEmpty()) {
181+
return null;
182+
}
183+
int n = firstAlighting.get().getAdjNode();
184+
Stream<PtGraph.PtEdge> leaveEdges = evenIndexed(nodes(hopDwellChain(staticGtfs, n)))
182185
.mapToObj(e -> alightForBaseNode(staticGtfs, e));
183-
return collectWithPadding(boardEdges);
186+
return collectWithPadding(leaveEdges);
184187
}
185188

186189
private static int[] findBoardEdgesForTrip(GtfsStorage staticGtfs, String feedKey, GTFSFeed feed, GtfsRealtime.TripUpdate tripUpdate) {
@@ -192,7 +195,10 @@ private static int[] findBoardEdgesForTrip(GtfsStorage staticGtfs, String feedKe
192195
.flatMap(e -> StreamSupport.stream(staticGtfs.getPtGraph().edgesAround(e.getAdjNode()).spliterator(), false))
193196
.filter(e -> e.getType() == GtfsStorage.EdgeType.BOARD)
194197
.filter(e -> normalize(e.getAttrs().tripDescriptor).equals(tripUpdate.getTrip()))
195-
.findAny();
198+
.min(Comparator.comparingInt(e -> e.getAttrs().stop_sequence));
199+
if (firstBoarding.isEmpty()) {
200+
return null;
201+
}
196202
int n = firstBoarding.get().getAdjNode();
197203
Stream<PtGraph.PtEdge> boardEdges = evenIndexed(nodes(hopDwellChain(staticGtfs, n)))
198204
.mapToObj(e -> boardForAdjNode(staticGtfs, e));

0 commit comments

Comments
 (0)