Search in sources :

Example 31 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate 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 32 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.

the class CalendarServiceDataFactoryImpl method createData.

private CalendarServiceData createData() {
    CalendarServiceData data = new CalendarServiceData();
    setTimeZonesForAgencies(data);
    int index = 0;
    for (FeedScopedId serviceId : serviceIds) {
        index++;
        LOG.debug("serviceId=" + serviceId + " (" + index + "/" + serviceIds.size() + ")");
        TimeZone serviceIdTimeZone = data.getTimeZoneForAgencyId(data.getAgencyIds().stream().filter(agency -> agency.getFeedId().equals(serviceId.getFeedId())).findAny().orElse(null));
        if (serviceIdTimeZone == null) {
            serviceIdTimeZone = TimeZone.getDefault();
        }
        Set<ServiceDate> activeDates = getServiceDatesForServiceId(serviceId, serviceIdTimeZone);
        List<ServiceDate> serviceDates = new ArrayList<>(activeDates);
        Collections.sort(serviceDates);
        data.putServiceDatesForServiceId(serviceId, serviceDates);
    }
    return data;
}
Also used : CalendarServiceData(org.opentripplanner.model.calendar.CalendarServiceData) FeedScopedId(org.opentripplanner.model.FeedScopedId) Logger(org.slf4j.Logger) Date(java.util.Date) TimeZone(java.util.TimeZone) Collection(java.util.Collection) ServiceCalendar(org.opentripplanner.model.calendar.ServiceCalendar) LoggerFactory(org.slf4j.LoggerFactory) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Set(java.util.Set) Agency(org.opentripplanner.model.Agency) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) CalendarServiceData(org.opentripplanner.model.calendar.CalendarServiceData) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Calendar(java.util.Calendar) Map(java.util.Map) ServiceCalendarDate(org.opentripplanner.model.calendar.ServiceCalendarDate) Collections(java.util.Collections) TimeZone(java.util.TimeZone) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) ArrayList(java.util.ArrayList) FeedScopedId(org.opentripplanner.model.FeedScopedId)

Example 33 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.

the class CalendarServiceDataFactoryImpl method addDatesFromCalendar.

private void addDatesFromCalendar(ServiceCalendar calendar, TimeZone timeZone, Set<ServiceDate> activeDates) {
    // We calculate service dates relative to noon so as to avoid any weirdness
    // relative to DST.
    Date startDate = getServiceDateAsNoon(calendar.getPeriod().getStart(), timeZone);
    Date endDate = getServiceDateAsNoon(calendar.getPeriod().getEnd(), timeZone);
    java.util.Calendar c = java.util.Calendar.getInstance(timeZone);
    c.setTime(startDate);
    while (true) {
        Date date = c.getTime();
        if (date.after(endDate))
            break;
        int day = c.get(java.util.Calendar.DAY_OF_WEEK);
        boolean active = false;
        switch(day) {
            case java.util.Calendar.MONDAY:
                active = calendar.getMonday() == 1;
                break;
            case java.util.Calendar.TUESDAY:
                active = calendar.getTuesday() == 1;
                break;
            case java.util.Calendar.WEDNESDAY:
                active = calendar.getWednesday() == 1;
                break;
            case java.util.Calendar.THURSDAY:
                active = calendar.getThursday() == 1;
                break;
            case java.util.Calendar.FRIDAY:
                active = calendar.getFriday() == 1;
                break;
            case java.util.Calendar.SATURDAY:
                active = calendar.getSaturday() == 1;
                break;
            case java.util.Calendar.SUNDAY:
                active = calendar.getSunday() == 1;
                break;
        }
        if (active) {
            addServiceDate(activeDates, new ServiceDate(c));
        }
        c.add(java.util.Calendar.DAY_OF_YEAR, 1);
    }
}
Also used : ServiceDate(org.opentripplanner.model.calendar.ServiceDate) Calendar(java.util.Calendar) Date(java.util.Date) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) ServiceCalendarDate(org.opentripplanner.model.calendar.ServiceCalendarDate)

Example 34 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate in project OpenTripPlanner by opentripplanner.

the class RaptorPathToItineraryMapper method mapTransitLeg.

private Leg mapTransitLeg(RoutingRequest request, TransitPathLeg<TripSchedule> pathLeg, boolean firstLeg) {
    Stop boardStop = transitLayer.getStopByIndex(pathLeg.fromStop());
    Stop alightStop = transitLayer.getStopByIndex(pathLeg.toStop());
    TripSchedule tripSchedule = pathLeg.trip();
    TripTimes tripTimes = tripSchedule.getOriginalTripTimes();
    Leg leg = new Leg(tripTimes.trip);
    // Find stop positions in pattern where this leg boards and alights.
    // We cannot assume every stop appears only once in a pattern, so we match times instead of stops.
    int boardStopIndexInPattern = tripSchedule.findStopPosInPattern(pathLeg.fromStop(), pathLeg.fromTime(), true);
    int alightStopIndexInPattern = tripSchedule.findStopPosInPattern(pathLeg.toStop(), pathLeg.toTime(), false);
    // Include real-time information in the Leg.
    if (!tripTimes.isScheduled()) {
        leg.realTime = true;
        leg.departureDelay = tripTimes.getDepartureDelay(boardStopIndexInPattern);
        leg.arrivalDelay = tripTimes.getArrivalDelay(alightStopIndexInPattern);
    }
    leg.serviceDate = new ServiceDate(tripSchedule.getServiceDate());
    leg.intermediateStops = new ArrayList<>();
    leg.startTime = createCalendar(pathLeg.fromTime());
    leg.endTime = createCalendar(pathLeg.toTime());
    leg.from = mapStopToPlace(boardStop, boardStopIndexInPattern);
    leg.to = mapStopToPlace(alightStop, alightStopIndexInPattern);
    List<Coordinate> transitLegCoordinates = extractTransitLegCoordinates(pathLeg);
    leg.legGeometry = PolylineEncoder.createEncodings(transitLegCoordinates);
    leg.distanceMeters = getDistanceFromCoordinates(transitLegCoordinates);
    if (request.showIntermediateStops) {
        leg.intermediateStops = extractIntermediateStops(pathLeg);
    }
    leg.headsign = tripTimes.getHeadsign(boardStopIndexInPattern);
    leg.walkSteps = new ArrayList<>();
    // TODO OTP2 - alightRule and boardRule needs mapping
    // Under Raptor, for transit trips, ItineraryMapper converts Path<TripSchedule> directly to Itinerary
    // (the old API response element, within TripPlan). Non-transit trips still use GraphPathToTripPlanConverter
    // to turn A* results (sequences of States and Edges called GraphPaths) into TripPlans which also contain
    // Itineraries.
    // So transit results do not go through GraphPathToTripPlanConverter. It contains logic to find board/alight
    // rules from StopPatterns within TripPatterns, and attach them as machine-readable codes on Legs, but only
    // where you are really boarding or alighting (considering interlining / in-seat transfers).
    // That needs to be re-implemented for the Raptor transit case.
    // - See e2118e0a -> GraphPathToTripPlanConverter#fixupLegs(List<Leg>, State[][]))
    // leg.alightRule = <Assign here>;
    // leg.boardRule =  <Assign here>;
    AlertToLegMapper.addAlertPatchesToLeg(request.getRoutingContext().graph, leg, firstLeg, request.locale);
    return leg;
}
Also used : ServiceDate(org.opentripplanner.model.calendar.ServiceDate) Stop(org.opentripplanner.model.Stop) Coordinate(org.locationtech.jts.geom.Coordinate) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripSchedule(org.opentripplanner.routing.algorithm.raptor.transit.TripSchedule) Leg(org.opentripplanner.model.plan.Leg) TransitPathLeg(org.opentripplanner.transit.raptor.api.path.TransitPathLeg) TransferPathLeg(org.opentripplanner.transit.raptor.api.path.TransferPathLeg) AccessPathLeg(org.opentripplanner.transit.raptor.api.path.AccessPathLeg) PathLeg(org.opentripplanner.transit.raptor.api.path.PathLeg) EgressPathLeg(org.opentripplanner.transit.raptor.api.path.EgressPathLeg)

Example 35 with ServiceDate

use of org.opentripplanner.model.calendar.ServiceDate 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)

Aggregations

ServiceDate (org.opentripplanner.model.calendar.ServiceDate)40 FeedScopedId (org.opentripplanner.model.FeedScopedId)15 ArrayList (java.util.ArrayList)11 TripPattern (org.opentripplanner.model.TripPattern)10 Test (org.junit.Test)9 Stop (org.opentripplanner.model.Stop)9 Trip (org.opentripplanner.model.Trip)9 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)8 Date (java.util.Date)8 HashSet (java.util.HashSet)7 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)6 ZonedDateTime (java.time.ZonedDateTime)6 TripTimeShort (org.opentripplanner.model.TripTimeShort)6 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)6 Calendar (java.util.Calendar)5 Timetable (org.opentripplanner.model.Timetable)4 ServiceCalendarDate (org.opentripplanner.model.calendar.ServiceCalendarDate)4 EstimatedCall (uk.org.siri.siri20.EstimatedCall)4 TimetableHelper.createUpdatedTripTimes (org.opentripplanner.ext.siri.TimetableHelper.createUpdatedTripTimes)3 Route (org.opentripplanner.model.Route)3