From 07b83955e3327a9d4ea3e5d0a84c29dbdef99855 Mon Sep 17 00:00:00 2001 From: Jaime Ventura Date: Thu, 16 May 2019 00:03:58 +0100 Subject: [PATCH] Make 'stoptimes' call return expected data, when using a GTFS file with frequencies. (Fixes #299) --- .../index/IndexGraphQLSchema.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java index 56101850b44..15f4a983260 100644 --- a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java +++ b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java @@ -1,5 +1,8 @@ package org.opentripplanner.index; +import java.util.List; +import com.beust.jcommander.internal.Lists; + import com.google.transit.realtime.GtfsRealtime; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Envelope; @@ -37,6 +40,7 @@ import org.opentripplanner.routing.graph.GraphIndex.PlaceAndDistance; import org.opentripplanner.routing.trippattern.RealTimeState; import org.opentripplanner.routing.trippattern.TripTimes; +import org.opentripplanner.routing.trippattern.FrequencyEntry; import org.opentripplanner.routing.vertextype.TransitVertex; import org.opentripplanner.updater.GtfsRealtimeFuzzyTripMatcher; import org.opentripplanner.updater.stoptime.TimetableSnapshotSource; @@ -1819,9 +1823,30 @@ public IndexGraphQLSchema(GraphIndex index) { .name("stoptimes") .description("List of times when this trip arrives to or departs from a stop") .type(new GraphQLList(stoptimeType)) - .dataFetcher(environment -> TripTimeShort.fromTripTimes( - index.patternForTrip.get((Trip) environment.getSource()).scheduledTimetable, - environment.getSource())) + .dataFetcher(environment ->{ + Timetable timetable = index.patternForTrip.get((Trip) environment.getSource()).scheduledTimetable; + + // If the Trip is frequency-based, there are no scheduled tripTimes (they must com from .tripTimes) + if (timetable.tripTimes.isEmpty()) { + + // This should probably be encapsulated into a function named TripTimeShort.fromFrequencyTripTimes, + // since it does the same as existing function TripTimeShort.fromTripTimes, but for Frequency. + // Or, it could also be moved into TripTimeShort.fromTripTimes. + + List out = Lists.newArrayList(); + + for (FrequencyEntry freq : timetable.frequencyEntries) { + TripTimes times = freq.tripTimes; + // one per stop, not one per hop, thus the <= operator + for (int i = 0; i < times.getNumStops(); ++i) { + out.add(new TripTimeShort(times, i, timetable.pattern.getStop(i), null)); + } + } + return out; + } else { + return TripTimeShort.fromTripTimes(timetable, environment.getSource()); + } + }) .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("departureStoptime")