Search in sources :

Example 6 with TripTimeShort

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);
}
Also used : TripTimeShort(org.opentripplanner.model.TripTimeShort) ServiceDate(org.opentripplanner.model.calendar.ServiceDate)

Example 7 with TripTimeShort

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));
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) RoutingService(org.opentripplanner.routing.RoutingService) Trip(org.opentripplanner.model.Trip) MultiModalStation(org.opentripplanner.model.MultiModalStation) StopTimesInPattern(org.opentripplanner.model.StopTimesInPattern) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) TRANSPORT_MODE(org.opentripplanner.ext.transmodelapi.model.EnumTypes.TRANSPORT_MODE) HashSet(java.util.HashSet) Scalars(graphql.Scalars) TripTimeShort(org.opentripplanner.model.TripTimeShort) TransitMode(org.opentripplanner.model.TransitMode) GraphQLObjectType(graphql.schema.GraphQLObjectType) FeedScopedId(org.opentripplanner.model.FeedScopedId) Station(org.opentripplanner.model.Station) TRANSPORT_SUBMODE(org.opentripplanner.ext.transmodelapi.model.EnumTypes.TRANSPORT_SUBMODE) EnumTypes(org.opentripplanner.ext.transmodelapi.model.EnumTypes) GqlUtil(org.opentripplanner.ext.transmodelapi.support.GqlUtil) GraphQLNonNull(graphql.schema.GraphQLNonNull) Stop(org.opentripplanner.model.Stop) StopCollection(org.opentripplanner.model.StopCollection) Collection(java.util.Collection) Set(java.util.Set) GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLArgument(graphql.schema.GraphQLArgument) TransmodelTransportSubmode(org.opentripplanner.ext.transmodelapi.model.TransmodelTransportSubmode) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) GraphQLList(graphql.schema.GraphQLList) Stream(java.util.stream.Stream) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) JourneyWhiteListed(org.opentripplanner.ext.transmodelapi.model.route.JourneyWhiteListed) TRUE(java.lang.Boolean.TRUE) TripTimeShort(org.opentripplanner.model.TripTimeShort) RoutingService(org.opentripplanner.routing.RoutingService) StopTimesInPattern(org.opentripplanner.model.StopTimesInPattern)

Example 8 with TripTimeShort

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);
}
Also used : TripTimeShort(org.opentripplanner.model.TripTimeShort) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) ArrayList(java.util.ArrayList) Date(java.util.Date) ServiceDate(org.opentripplanner.model.calendar.ServiceDate)

Example 9 with TripTimeShort

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;
}
Also used : TripTimeShort(org.opentripplanner.model.TripTimeShort) Timetable(org.opentripplanner.model.Timetable) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) ServiceDay(org.opentripplanner.routing.core.ServiceDay) Stop(org.opentripplanner.model.Stop) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) FrequencyEntry(org.opentripplanner.routing.trippattern.FrequencyEntry)

Example 10 with TripTimeShort

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;
}
Also used : Timetable(org.opentripplanner.model.Timetable) TripTimeShort(org.opentripplanner.model.TripTimeShort) ServiceDay(org.opentripplanner.routing.core.ServiceDay) Stop(org.opentripplanner.model.Stop) ArrayList(java.util.ArrayList) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TimetableSnapshot(org.opentripplanner.model.TimetableSnapshot) TripPattern(org.opentripplanner.model.TripPattern) StopTimesInPattern(org.opentripplanner.model.StopTimesInPattern)

Aggregations

TripTimeShort (org.opentripplanner.model.TripTimeShort)10 ServiceDate (org.opentripplanner.model.calendar.ServiceDate)7 ArrayList (java.util.ArrayList)5 Stop (org.opentripplanner.model.Stop)5 StopTimesInPattern (org.opentripplanner.model.StopTimesInPattern)4 TripPattern (org.opentripplanner.model.TripPattern)4 Station (org.opentripplanner.model.Station)3 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)2 Collection (java.util.Collection)2 Date (java.util.Date)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 FeedScopedId (org.opentripplanner.model.FeedScopedId)2 Timetable (org.opentripplanner.model.Timetable)2 RoutingService (org.opentripplanner.routing.RoutingService)2 ServiceDay (org.opentripplanner.routing.core.ServiceDay)2 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)2 Scalars (graphql.Scalars)1 Relay (graphql.relay.Relay)1