use of org.opentripplanner.model.TripTimeShort in project OpenTripPlanner by opentripplanner.
the class TripTimeShortHelper method getTripTimeShortForFromPlace.
/**
* Find trip time short for the from place in transit leg, or null.
*/
public TripTimeShort getTripTimeShortForFromPlace(Leg leg, RoutingService routingService) {
if (!leg.isTransitLeg()) {
return null;
}
ServiceDate serviceDate = leg.serviceDate;
List<TripTimeShort> tripTimes = routingService.getTripTimesShort(leg.getTrip(), serviceDate);
long startTimeSeconds = (leg.startTime.toInstant().toEpochMilli() - serviceDate.getAsDate().getTime()) / 1000;
if (leg.realTime) {
return tripTimes.stream().filter(tripTime -> tripTime.realtimeDeparture == startTimeSeconds && matchesQuayOrSiblingQuay(leg.from.stopId, tripTime.stopId, routingService)).findFirst().orElse(null);
}
return tripTimes.stream().filter(tripTime -> tripTime.scheduledDeparture == startTimeSeconds && matchesQuayOrSiblingQuay(leg.from.stopId, tripTime.stopId, routingService)).findFirst().orElse(null);
}
use of org.opentripplanner.model.TripTimeShort in project OpenTripPlanner by opentripplanner.
the class StopPlaceType method getTripTimesForStop.
public static Stream<TripTimeShort> getTripTimesForStop(Stop stop, Long startTimeSeconds, int timeRage, boolean omitNonBoarding, int numberOfDepartures, Integer departuresPerLineAndDestinationDisplay, Collection<FeedScopedId> authorityIdsWhiteListed, Collection<FeedScopedId> lineIdsWhiteListed, Collection<TransitMode> transitModes, DataFetchingEnvironment environment) {
RoutingService routingService = GqlUtil.getRoutingService(environment);
boolean limitOnDestinationDisplay = departuresPerLineAndDestinationDisplay != null && departuresPerLineAndDestinationDisplay > 0 && departuresPerLineAndDestinationDisplay < numberOfDepartures;
int departuresPerTripPattern = limitOnDestinationDisplay ? departuresPerLineAndDestinationDisplay : numberOfDepartures;
List<StopTimesInPattern> stopTimesInPatterns = routingService.stopTimesForStop(stop, startTimeSeconds, timeRage, departuresPerTripPattern, omitNonBoarding);
// TODO OTP2 - Applying filters here is not correct - the `departuresPerTripPattern` is used
// - to limit the result, and using filters after that point may result in
// - loosing valid results.
Stream<StopTimesInPattern> stopTimesStream = stopTimesInPatterns.stream();
if (transitModes != null && !transitModes.isEmpty()) {
stopTimesStream = stopTimesStream.filter(it -> transitModes.contains(it.pattern.getMode()));
}
Stream<TripTimeShort> tripTimesStream = stopTimesStream.flatMap(p -> p.times.stream());
tripTimesStream = JourneyWhiteListed.whiteListAuthoritiesAndOrLines(tripTimesStream, authorityIdsWhiteListed, lineIdsWhiteListed, routingService);
if (!limitOnDestinationDisplay) {
return tripTimesStream;
}
// Group by line and destination display, limit departures per group and merge
return tripTimesStream.collect(Collectors.groupingBy(t -> destinationDisplayPerLine(((TripTimeShort) t), routingService))).values().stream().flatMap(tripTimes -> tripTimes.stream().sorted(TripTimeShort.compareByDeparture()).distinct().limit(departuresPerLineAndDestinationDisplay));
}
use of org.opentripplanner.model.TripTimeShort in project OpenTripPlanner by opentripplanner.
the class StopTimesHelper method stopTimesForPatternAtStop.
/**
* Fetch upcoming vehicle departures from a stop for a single pattern, passing the stop for the previous, current and
* next service date. It uses a priority queue to keep track of the next departures. The queue is shared between all
* dates, as services from the previous service date can visit the stop later than the current service date's
* services.
*
* TODO: Add frequency based trips
* @param stop Stop object to perform the search for
* @param pattern Pattern object to perform the search for
* @param startTime Start time for the search. Seconds from UNIX epoch
* @param timeRange Searches forward for timeRange seconds from startTime
* @param numberOfDepartures Number of departures to fetch per pattern
* @param omitNonPickups If true, do not include vehicles that will not pick up passengers.
*/
public static List<TripTimeShort> stopTimesForPatternAtStop(RoutingService routingService, TimetableSnapshot timetableSnapshot, Stop stop, TripPattern pattern, long startTime, int timeRange, int numberOfDepartures, boolean omitNonPickups) {
if (startTime == 0) {
startTime = System.currentTimeMillis() / 1000;
}
Date date = new Date(startTime * 1000);
ServiceDate[] serviceDates = { new ServiceDate(date).previous(), new ServiceDate(date), new ServiceDate(date).next() };
Queue<TripTimeShort> pq = listTripTimeShortsForPatternAtStop(routingService, timetableSnapshot, stop, pattern, startTime, timeRange, numberOfDepartures, omitNonPickups, serviceDates);
return new ArrayList<>(pq);
}
use of org.opentripplanner.model.TripTimeShort in project OpenTripPlanner by opentripplanner.
the class StopTimesHelper method listTripTimeShortsForPatternAtStop.
private static Queue<TripTimeShort> listTripTimeShortsForPatternAtStop(RoutingService routingService, TimetableSnapshot timetableSnapshot, Stop stop, TripPattern pattern, long startTime, int timeRange, int numberOfDepartures, boolean omitNonPickups, ServiceDate[] serviceDates) {
// The bounded priority Q is used to keep a sorted short list of trip times. We can not
// relay on the trip times to be in order because of real-time updates. This code can
// probably be optimized, and the trip search in the Raptor search does almost the same
// thing. This is no part of a routing request, but is a used frequently in some
// operation like Entur for "departure boards" (apps, widgets, screens on platforms, and
// hotel lobbies). Setting the numberOfDepartures and timeRange to a big number for a
// transit hub could result in a DOS attack, but there are probably other more effective
// ways to do it.
//
// The {@link MinMaxPriorityQueue} is marked beta, but we do not have a god alternative.
MinMaxPriorityQueue<TripTimeShort> pq = MinMaxPriorityQueue.orderedBy(Comparator.comparing((TripTimeShort tts) -> tts.serviceDay + tts.realtimeDeparture)).maximumSize(numberOfDepartures).create();
// Loop through all possible days
for (ServiceDate serviceDate : serviceDates) {
ServiceDay sd = new ServiceDay(routingService.getServiceCodes(), serviceDate, routingService.getCalendarService(), pattern.route.getAgency().getId());
Timetable tt;
if (timetableSnapshot != null) {
tt = timetableSnapshot.resolve(pattern, serviceDate);
} else {
tt = pattern.scheduledTimetable;
}
if (!tt.temporallyViable(sd, startTime, timeRange, true))
continue;
int secondsSinceMidnight = sd.secondsSinceMidnight(startTime);
int sidx = 0;
for (Stop currStop : pattern.stopPattern.stops) {
if (currStop == stop) {
if (omitNonPickups && pattern.stopPattern.pickups[sidx] == StopPattern.PICKDROP_NONE)
continue;
for (TripTimes t : tt.tripTimes) {
if (!sd.serviceRunning(t.serviceCode))
continue;
if (t.getDepartureTime(sidx) != -1 && t.getDepartureTime(sidx) >= secondsSinceMidnight) {
pq.add(new TripTimeShort(t, sidx, stop, sd));
}
}
// TODO: This needs to be adapted after #1647 is merged
for (FrequencyEntry freq : tt.frequencyEntries) {
if (!sd.serviceRunning(freq.tripTimes.serviceCode))
continue;
int departureTime = freq.nextDepartureTime(sidx, secondsSinceMidnight);
if (departureTime == -1)
continue;
int lastDeparture = freq.endTime + freq.tripTimes.getArrivalTime(sidx) - freq.tripTimes.getDepartureTime(0);
int i = 0;
while (departureTime <= lastDeparture && i < numberOfDepartures) {
pq.add(new TripTimeShort(freq.materialize(sidx, departureTime, true), sidx, stop, sd));
departureTime += freq.headway;
i++;
}
}
}
sidx++;
}
}
return pq;
}
use of org.opentripplanner.model.TripTimeShort in project OpenTripPlanner by opentripplanner.
the class StopTimesHelper method stopTimesForStop.
/**
* Get a list of all trips that pass through a stop during a single ServiceDate. Useful when creating complete stop
* timetables for a single day.
*
* @param stop Stop object to perform the search for
* @param serviceDate Return all departures for the specified date
*/
public static List<StopTimesInPattern> stopTimesForStop(RoutingService routingService, Stop stop, ServiceDate serviceDate, boolean omitNonPickups) {
List<StopTimesInPattern> ret = new ArrayList<>();
Collection<TripPattern> patternsForStop = routingService.getPatternsForStop(stop, true);
for (TripPattern pattern : patternsForStop) {
StopTimesInPattern stopTimes = new StopTimesInPattern(pattern);
Timetable tt;
TimetableSnapshot timetableSnapshot = routingService.getTimetableSnapshot();
if (timetableSnapshot != null) {
tt = timetableSnapshot.resolve(pattern, serviceDate);
} else {
tt = pattern.scheduledTimetable;
}
ServiceDay sd = new ServiceDay(routingService.getServiceCodes(), serviceDate, routingService.getCalendarService(), pattern.route.getAgency().getId());
int sidx = 0;
for (Stop currStop : pattern.stopPattern.stops) {
if (currStop == stop) {
if (omitNonPickups && pattern.stopPattern.pickups[sidx] == StopPattern.PICKDROP_NONE)
continue;
for (TripTimes t : tt.tripTimes) {
if (!sd.serviceRunning(t.serviceCode))
continue;
stopTimes.times.add(new TripTimeShort(t, sidx, stop, sd));
}
}
sidx++;
}
ret.add(stopTimes);
}
return ret;
}
Aggregations