Search in sources :

Example 1 with Trip

use of org.opentripplanner.model.Trip in project OpenTripPlanner by opentripplanner.

the class GeometryAndBlockProcessor method run.

/**
 * Generate the edges. Assumes that there are already vertices in the graph for the stops.
 */
@SuppressWarnings("Convert2MethodRef")
public void run(Graph graph, DataImportIssueStore issueStore) {
    this.issueStore = issueStore;
    fareServiceFactory.processGtfs(transitService);
    /* Assign 0-based numeric codes to all GTFS service IDs. */
    for (FeedScopedId serviceId : transitService.getAllServiceIds()) {
        // TODO: FIX Service code collision for multiple feeds.
        graph.getServiceCodes().put(serviceId, graph.getServiceCodes().size());
    }
    LOG.info("Processing geometries and blocks on graph...");
    // Wwe have to build the hop geometries before we throw away the modified stopTimes, saving
    // only the tripTimes (which don't have enough information to build a geometry). So we keep
    // them here. In the current design, a trip pattern does not have a single geometry, but
    // one per hop, so we store them in an array.
    Map<TripPattern, LineString[]> geometriesByTripPattern = Maps.newHashMap();
    Collection<TripPattern> tripPatterns = transitService.getTripPatterns();
    /* Generate unique short IDs for all the TableTripPatterns. */
    if (!TripPattern.idsAreUniqueAndNotNull(tripPatterns)) {
        TripPattern.generateUniqueIds(tripPatterns);
    }
    /* Generate unique human-readable names for all the TableTripPatterns. */
    TripPattern.generateUniqueNames(tripPatterns, issueStore);
    /* Loop over all new TripPatterns, creating edges, setting the service codes and geometries, etc. */
    ProgressTracker progress = ProgressTracker.track("Generate TripPattern geometries", 100, tripPatterns.size());
    LOG.info(progress.startMessage());
    for (TripPattern tripPattern : tripPatterns) {
        for (Trip trip : tripPattern.getTrips()) {
            // there would be a trip pattern with no geometry yet because it failed some of these tests
            if (!geometriesByTripPattern.containsKey(tripPattern) && trip.getShapeId() != null && trip.getShapeId().getId() != null && !trip.getShapeId().getId().equals("")) {
                // save the geometry to later be applied to the hops
                geometriesByTripPattern.put(tripPattern, createGeometry(trip.getShapeId(), transitService.getStopTimesForTrip(trip)));
            }
        }
        // Keep lambda! A method-ref would causes incorrect class and line number to be logged
        progress.step(m -> LOG.info(m));
    }
    LOG.info(progress.completeMessage());
    /* Loop over all new TripPatterns setting the service codes and geometries, etc. */
    for (TripPattern tripPattern : tripPatterns) {
        LineString[] hopGeometries = geometriesByTripPattern.get(tripPattern);
        if (hopGeometries != null) {
            // Make a single unified geometry, and also store the per-hop split geometries.
            tripPattern.setHopGeometries(hopGeometries);
        }
        // TODO this could be more elegant
        tripPattern.setServiceCodes(graph.getServiceCodes());
        // Store the tripPattern in the Graph so it will be serialized and usable in routing.
        graph.tripPatternForId.put(tripPattern.getId(), tripPattern);
    }
    /* Identify interlined trips and create the necessary edges. */
    interline(tripPatterns, graph);
    // it is already done at deserialization, but standalone mode allows using graphs without serializing them.
    for (TripPattern tableTripPattern : tripPatterns) {
        tableTripPattern.scheduledTimetable.finish();
    }
    graph.putService(FareService.class, fareServiceFactory.makeFareService());
}
Also used : Trip(org.opentripplanner.model.Trip) ProgressTracker(org.opentripplanner.util.ProgressTracker) LineString(org.locationtech.jts.geom.LineString) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripPattern(org.opentripplanner.model.TripPattern)

Example 2 with Trip

use of org.opentripplanner.model.Trip in project OpenTripPlanner by opentripplanner.

the class SiriTimetableSnapshotSource method getTripForJourney.

/**
 * Finds the correct trip based on OTP-ServiceDate and SIRI-DepartureTime
 * @param trips
 * @param journey
 * @return
 */
private Set<Trip> getTripForJourney(Set<Trip> trips, EstimatedVehicleJourney journey) {
    List<RecordedCall> recordedCalls = (journey.getRecordedCalls() != null ? journey.getRecordedCalls().getRecordedCalls() : new ArrayList<>());
    List<EstimatedCall> estimatedCalls = journey.getEstimatedCalls().getEstimatedCalls();
    ZonedDateTime date;
    int stopNumber = 1;
    String firstStopId;
    if (recordedCalls != null && !recordedCalls.isEmpty()) {
        RecordedCall recordedCall = recordedCalls.get(0);
        date = recordedCall.getAimedDepartureTime();
        firstStopId = recordedCall.getStopPointRef().getValue();
    } else if (estimatedCalls != null && !estimatedCalls.isEmpty()) {
        EstimatedCall estimatedCall = estimatedCalls.get(0);
        if (estimatedCall.getOrder() != null) {
            stopNumber = estimatedCall.getOrder().intValue();
        } else if (estimatedCall.getVisitNumber() != null) {
            stopNumber = estimatedCall.getVisitNumber().intValue();
        }
        firstStopId = estimatedCall.getStopPointRef().getValue();
        date = estimatedCall.getAimedDepartureTime();
    } else {
        return null;
    }
    if (date == null) {
        // If no date is set - assume Realtime-data is reported for 'today'.
        date = ZonedDateTime.now();
    }
    ServiceDate serviceDate = new ServiceDate(date.getYear(), date.getMonthValue(), date.getDayOfMonth());
    int departureInSecondsSinceMidnight = calculateSecondsSinceMidnight(date);
    Set<Trip> result = new HashSet<>();
    for (Iterator<Trip> iterator = trips.iterator(); iterator.hasNext(); ) {
        Trip trip = iterator.next();
        Set<ServiceDate> serviceDatesForServiceId = routingService.getCalendarService().getServiceDatesForServiceId(trip.getServiceId());
        if (serviceDatesForServiceId.contains(serviceDate)) {
            TripPattern pattern = routingService.getPatternForTrip().get(trip);
            if (stopNumber < pattern.stopPattern.stops.length) {
                boolean firstReportedStopIsFound = false;
                Stop stop = pattern.stopPattern.stops[stopNumber - 1];
                if (firstStopId.equals(stop.getId().getId())) {
                    firstReportedStopIsFound = true;
                } else {
                    String agencyId = stop.getId().getFeedId();
                    if (stop.isPartOfStation()) {
                        Stop alternativeStop = routingService.getStopForId(new FeedScopedId(agencyId, firstStopId));
                        if (stop.isPartOfSameStationAs(alternativeStop)) {
                            firstReportedStopIsFound = true;
                        }
                    }
                }
                if (firstReportedStopIsFound) {
                    for (TripTimes times : getCurrentTimetable(pattern, serviceDate).tripTimes) {
                        if (times.getScheduledDepartureTime(stopNumber - 1) == departureInSecondsSinceMidnight) {
                            if (routingService.getCalendarService().getServiceDatesForServiceId(times.trip.getServiceId()).contains(serviceDate)) {
                                result.add(times.trip);
                            }
                        }
                    }
                }
            }
        }
    }
    if (result.size() >= 1) {
        return result;
    } else {
        return null;
    }
}
Also used : Trip(org.opentripplanner.model.Trip) Stop(org.opentripplanner.model.Stop) ArrayList(java.util.ArrayList) RecordedCall(uk.org.siri.siri20.RecordedCall) TripPattern(org.opentripplanner.model.TripPattern) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) ZonedDateTime(java.time.ZonedDateTime) FeedScopedId(org.opentripplanner.model.FeedScopedId) TimetableHelper.createUpdatedTripTimes(org.opentripplanner.ext.siri.TimetableHelper.createUpdatedTripTimes) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) EstimatedCall(uk.org.siri.siri20.EstimatedCall) HashSet(java.util.HashSet)

Example 3 with Trip

use of org.opentripplanner.model.Trip in project OpenTripPlanner by opentripplanner.

the class SiriTimetableSnapshotSource method getTripForJourney.

/**
 * Finds the correct trip based on OTP-ServiceDate and SIRI-DepartureTime
 * @param trips
 * @param monitoredVehicleJourney
 * @return
 */
private Trip getTripForJourney(Set<Trip> trips, VehicleActivityStructure.MonitoredVehicleJourney monitoredVehicleJourney) {
    ZonedDateTime date = monitoredVehicleJourney.getOriginAimedDepartureTime();
    if (date == null) {
        // If no date is set - assume Realtime-data is reported for 'today'.
        date = ZonedDateTime.now();
    }
    ServiceDate serviceDate = new ServiceDate(date.getYear(), date.getMonthValue(), date.getDayOfMonth());
    List<Trip> results = new ArrayList<>();
    for (Iterator<Trip> iterator = trips.iterator(); iterator.hasNext(); ) {
        Trip trip = iterator.next();
        Set<ServiceDate> serviceDatesForServiceId = routingService.getCalendarService().getServiceDatesForServiceId(trip.getServiceId());
        for (Iterator<ServiceDate> serviceDateIterator = serviceDatesForServiceId.iterator(); serviceDateIterator.hasNext(); ) {
            ServiceDate next = serviceDateIterator.next();
            if (next.equals(serviceDate)) {
                results.add(trip);
            }
        }
    }
    if (results.size() == 1) {
        return results.get(0);
    } else if (results.size() > 1) {
        // Multiple possible matches - check if lineRef/routeId matches
        if (monitoredVehicleJourney.getLineRef() != null && monitoredVehicleJourney.getLineRef().getValue() != null) {
            String lineRef = monitoredVehicleJourney.getLineRef().getValue();
            for (Trip trip : results) {
                if (lineRef.equals(trip.getRoute().getId().getId())) {
                    // Return first trip where the lineRef matches routeId
                    return trip;
                }
            }
        }
        // Line does not match any routeId - return first result.
        return results.get(0);
    }
    return null;
}
Also used : ServiceDate(org.opentripplanner.model.calendar.ServiceDate) Trip(org.opentripplanner.model.Trip) ZonedDateTime(java.time.ZonedDateTime) ArrayList(java.util.ArrayList)

Example 4 with Trip

use of org.opentripplanner.model.Trip in project OpenTripPlanner by opentripplanner.

the class SiriTimetableSnapshotSource method handleAddedTrip.

private boolean handleAddedTrip(Graph graph, String feedId, EstimatedVehicleJourney estimatedVehicleJourney) {
    // Verifying values required in SIRI Profile
    // Added ServiceJourneyId
    String newServiceJourneyRef = estimatedVehicleJourney.getEstimatedVehicleJourneyCode();
    Preconditions.checkNotNull(newServiceJourneyRef, "EstimatedVehicleJourneyCode is required");
    // Replaced/duplicated ServiceJourneyId
    // VehicleJourneyRef existingServiceJourneyRef = estimatedVehicleJourney.getVehicleJourneyRef();
    // Preconditions.checkNotNull(existingServiceJourneyRef, "VehicleJourneyRef is required");
    // LineRef of added trip
    Preconditions.checkNotNull(estimatedVehicleJourney.getLineRef(), "LineRef is required");
    String lineRef = estimatedVehicleJourney.getLineRef().getValue();
    // OperatorRef of added trip
    Preconditions.checkNotNull(estimatedVehicleJourney.getOperatorRef(), "OperatorRef is required");
    String operatorRef = estimatedVehicleJourney.getOperatorRef().getValue();
    // Required in SIRI, but currently not in use by OTP
    // Preconditions.checkNotNull(estimatedVehicleJourney.getRouteRef(), "RouteRef is required");
    // String routeRef = estimatedVehicleJourney.getRouteRef().getValue();
    // Preconditions.checkNotNull(estimatedVehicleJourney.getGroupOfLinesRef(), "GroupOfLinesRef is required");
    // String groupOfLines = estimatedVehicleJourney.getGroupOfLinesRef().getValue();
    // Preconditions.checkNotNull(estimatedVehicleJourney.getExternalLineRef(), "ExternalLineRef is required");
    String externalLineRef = estimatedVehicleJourney.getExternalLineRef().getValue();
    // TODO - SIRI: Where is the Operator?
    // Operator operator = graphIndex.operatorForId.get(new FeedScopedId(feedId, operatorRef));
    // Preconditions.checkNotNull(operator, "Operator " + operatorRef + " is unknown");
    FeedScopedId tripId = new FeedScopedId(feedId, newServiceJourneyRef);
    FeedScopedId serviceId = new FeedScopedId(feedId, newServiceJourneyRef);
    Route replacedRoute = null;
    if (externalLineRef != null) {
        replacedRoute = graph.index.getRouteForId(new FeedScopedId(feedId, externalLineRef));
    }
    FeedScopedId routeId = new FeedScopedId(feedId, lineRef);
    Route route = graph.index.getRouteForId(routeId);
    if (route == null) {
        // Route is unknown - create new
        route = new Route();
        route.setId(routeId);
        route.setType(getRouteType(estimatedVehicleJourney.getVehicleModes()));
        // route.setOperator(operator);
        // TODO - SIRI: Is there a better way to find authority/Agency?
        // Finding first Route with same Operator, and using same Authority
        Agency agency = graph.index.getAllRoutes().stream().findFirst().get().getAgency();
        route.setAgency(agency);
        if (estimatedVehicleJourney.getPublishedLineNames() != null && !estimatedVehicleJourney.getPublishedLineNames().isEmpty()) {
            route.setShortName("" + estimatedVehicleJourney.getPublishedLineNames().get(0).getValue());
        }
        LOG.info("Adding route {} to graph.", routeId);
        graph.index.addRoutes(route);
    }
    Trip trip = new Trip();
    trip.setId(tripId);
    trip.setRoute(route);
    // TODO - SIRI: Set transport-submode based on replaced- and replacement-route
    if (replacedRoute != null) {
        if (replacedRoute.getType() >= 100 && replacedRoute.getType() < 200) {
            // Replaced-route is RAIL
            if (route.getType() == 100) {
            // Replacement-route is also RAIL
            // trip.setTransportSubmode(TransmodelTransportSubmode.REPLACEMENT_RAIL_SERVICE);
            } else if (route.getType() == 700) {
            // Replacement-route is BUS
            // trip.setTransportSubmode(TransmodelTransportSubmode.RAIL_REPLACEMENT_BUS);
            }
        }
    }
    trip.setServiceId(serviceId);
    // TODO - SIRI: PublishedLineName not defined in SIRI-profile
    if (estimatedVehicleJourney.getPublishedLineNames() != null && !estimatedVehicleJourney.getPublishedLineNames().isEmpty()) {
        trip.setRouteShortName("" + estimatedVehicleJourney.getPublishedLineNames().get(0).getValue());
    }
    // trip.setTripOperator(operator);
    // TODO - SIRI: Populate these?
    // Replacement-trip has different shape
    trip.setShapeId(null);
    // trip.setTripPrivateCode(null);
    // trip.setTripPublicCode(null);
    trip.setBlockId(null);
    trip.setTripShortName(null);
    trip.setTripHeadsign(null);
    // trip.setKeyValues(null);
    List<Stop> addedStops = new ArrayList<>();
    List<StopTime> aimedStopTimes = new ArrayList<>();
    List<EstimatedCall> estimatedCalls = estimatedVehicleJourney.getEstimatedCalls().getEstimatedCalls();
    for (int i = 0; i < estimatedCalls.size(); i++) {
        EstimatedCall estimatedCall = estimatedCalls.get(i);
        Stop stop = getStopForStopId(feedId, estimatedCall.getStopPointRef().getValue());
        StopTime stopTime = new StopTime();
        stopTime.setStop(stop);
        stopTime.setStopSequence(i);
        stopTime.setTrip(trip);
        ZonedDateTime aimedArrivalTime = estimatedCall.getAimedArrivalTime();
        ZonedDateTime aimedDepartureTime = estimatedCall.getAimedDepartureTime();
        if (aimedArrivalTime != null) {
            stopTime.setArrivalTime(calculateSecondsSinceMidnight(aimedArrivalTime));
        }
        if (aimedDepartureTime != null) {
            stopTime.setDepartureTime(calculateSecondsSinceMidnight(aimedDepartureTime));
        }
        if (estimatedCall.getArrivalBoardingActivity() == ArrivalBoardingActivityEnumeration.ALIGHTING) {
            stopTime.setDropOffType(PICKDROP_SCHEDULED);
        } else {
            stopTime.setDropOffType(PICKDROP_NONE);
        }
        if (estimatedCall.getDepartureBoardingActivity() == DepartureBoardingActivityEnumeration.BOARDING) {
            stopTime.setPickupType(PICKDROP_SCHEDULED);
        } else {
            stopTime.setPickupType(PICKDROP_NONE);
        }
        if (estimatedCall.getDestinationDisplaies() != null && !estimatedCall.getDestinationDisplaies().isEmpty()) {
            NaturalLanguageStringStructure destinationDisplay = estimatedCall.getDestinationDisplaies().get(0);
            stopTime.setStopHeadsign(destinationDisplay.getValue());
        }
        if (i == 0) {
            // Fake arrival on first stop
            stopTime.setArrivalTime(stopTime.getDepartureTime());
        } else if (i == (estimatedCalls.size() - 1)) {
            // Fake departure from last stop
            stopTime.setDepartureTime(stopTime.getArrivalTime());
        }
        addedStops.add(stop);
        aimedStopTimes.add(stopTime);
    }
    StopPattern stopPattern = new StopPattern(aimedStopTimes);
    TripPattern pattern = new TripPattern(trip.getRoute(), stopPattern);
    TripTimes tripTimes = new TripTimes(trip, aimedStopTimes, graph.deduplicator);
    boolean isJourneyPredictionInaccurate = (estimatedVehicleJourney.isPredictionInaccurate() != null && estimatedVehicleJourney.isPredictionInaccurate());
    // If added trip is updated with realtime - loop through and add delays
    for (int i = 0; i < estimatedCalls.size(); i++) {
        EstimatedCall estimatedCall = estimatedCalls.get(i);
        ZonedDateTime expectedArrival = estimatedCall.getExpectedArrivalTime();
        ZonedDateTime expectedDeparture = estimatedCall.getExpectedDepartureTime();
        int aimedArrivalTime = aimedStopTimes.get(i).getArrivalTime();
        int aimedDepartureTime = aimedStopTimes.get(i).getDepartureTime();
        if (expectedArrival != null) {
            int expectedArrivalTime = calculateSecondsSinceMidnight(expectedArrival);
            tripTimes.updateArrivalDelay(i, expectedArrivalTime - aimedArrivalTime);
        }
        if (expectedDeparture != null) {
            int expectedDepartureTime = calculateSecondsSinceMidnight(expectedDeparture);
            tripTimes.updateDepartureDelay(i, expectedDepartureTime - aimedDepartureTime);
        }
        if (estimatedCall.isCancellation() != null) {
            tripTimes.setCancelledStop(i, estimatedCall.isCancellation());
        }
        boolean isCallPredictionInaccurate = estimatedCall.isPredictionInaccurate() != null && estimatedCall.isPredictionInaccurate();
        tripTimes.setPredictionInaccurate(i, (isJourneyPredictionInaccurate | isCallPredictionInaccurate));
        if (i == 0) {
            // Fake arrival on first stop
            tripTimes.updateArrivalTime(i, tripTimes.getDepartureTime(i));
        } else if (i == (estimatedCalls.size() - 1)) {
            // Fake departure from last stop
            tripTimes.updateDepartureTime(i, tripTimes.getArrivalTime(i));
        }
    }
    // Adding trip to index necessary to include values in graphql-queries
    // TODO - SIRI: should more data be added to index?
    graph.index.getTripForId().put(tripId, trip);
    graph.index.getPatternForTrip().put(trip, pattern);
    if (estimatedVehicleJourney.isCancellation() != null && estimatedVehicleJourney.isCancellation()) {
        tripTimes.cancel();
    } else {
        tripTimes.setRealTimeState(RealTimeState.ADDED);
    }
    if (!graph.getServiceCodes().containsKey(serviceId)) {
        graph.getServiceCodes().put(serviceId, graph.getServiceCodes().size());
    }
    tripTimes.serviceCode = graph.getServiceCodes().get(serviceId);
    pattern.add(tripTimes);
    Preconditions.checkState(tripTimes.timesIncreasing(), "Non-increasing triptimes for added trip");
    ServiceDate serviceDate = getServiceDateForEstimatedVehicleJourney(estimatedVehicleJourney);
    if (graph.getCalendarService().getServiceDatesForServiceId(serviceId) == null || graph.getCalendarService().getServiceDatesForServiceId(serviceId).isEmpty()) {
        LOG.info("Adding serviceId {} to CalendarService", serviceId);
    // TODO - SIRI: Need to add the ExtraJourney as a Trip - alerts may be attached to it
    // graph.getCalendarService().addServiceIdAndServiceDates(serviceId, Arrays.asList(serviceDate));
    }
    return addTripToGraphAndBuffer(feedId, graph, trip, aimedStopTimes, addedStops, tripTimes, serviceDate);
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri20.NaturalLanguageStringStructure) StopPattern(org.opentripplanner.model.StopPattern) Trip(org.opentripplanner.model.Trip) Agency(org.opentripplanner.model.Agency) Stop(org.opentripplanner.model.Stop) ArrayList(java.util.ArrayList) TripPattern(org.opentripplanner.model.TripPattern) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) ZonedDateTime(java.time.ZonedDateTime) FeedScopedId(org.opentripplanner.model.FeedScopedId) TimetableHelper.createUpdatedTripTimes(org.opentripplanner.ext.siri.TimetableHelper.createUpdatedTripTimes) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) EstimatedCall(uk.org.siri.siri20.EstimatedCall) Route(org.opentripplanner.model.Route) StopTime(org.opentripplanner.model.StopTime)

Example 5 with Trip

use of org.opentripplanner.model.Trip in project OpenTripPlanner by opentripplanner.

the class TimetableHelper method createUpdatedTripTimes.

/**
 * Apply the TripUpdate to the appropriate TripTimes from this Timetable. The existing TripTimes
 * must not be modified directly because they may be shared with the underlying
 * scheduledTimetable, or other updated Timetables. The {@link TimetableSnapshot} performs the
 * protective copying of this Timetable. It is not done in this update method to avoid
 * repeatedly cloning the same Timetable when several updates are applied to it at once. We
 * assume here that all trips in a timetable are from the same feed, which should always be the
 * case.
 *
 * @param journey  SIRI-ET EstimatedVehicleJourney
 * @param timeZone time zone of trip update
 * @param tripId
 * @return new copy of updated TripTimes after TripUpdate has been applied on TripTimes of trip
 * with the id specified in the trip descriptor of the TripUpdate; null if something
 * went wrong
 */
public static TripTimes createUpdatedTripTimes(final Graph graph, Timetable timetable, EstimatedVehicleJourney journey, TimeZone timeZone, FeedScopedId tripId) {
    if (journey == null) {
        return null;
    }
    int tripIndex = timetable.getTripIndex(tripId);
    if (tripIndex == -1) {
        LOG.debug("tripId {} not found in pattern.", tripId);
        return null;
    }
    final TripTimes existingTripTimes = timetable.getTripTimes(tripIndex);
    TripTimes oldTimes = new TripTimes(existingTripTimes);
    if (journey.isCancellation() != null && journey.isCancellation()) {
        oldTimes.cancel();
        return oldTimes;
    }
    EstimatedVehicleJourney.EstimatedCalls journeyEstimatedCalls = journey.getEstimatedCalls();
    EstimatedVehicleJourney.RecordedCalls journeyRecordedCalls = journey.getRecordedCalls();
    if (journeyEstimatedCalls == null) {
        return null;
    }
    List<EstimatedCall> estimatedCalls = journeyEstimatedCalls.getEstimatedCalls();
    List<RecordedCall> recordedCalls;
    if (journeyRecordedCalls != null) {
        recordedCalls = journeyRecordedCalls.getRecordedCalls();
    } else {
        recordedCalls = new ArrayList<>();
    }
    boolean stopPatternChanged = false;
    Stop[] modifiedStops = timetable.pattern.stopPattern.stops;
    Trip trip = getTrip(tripId, timetable);
    List<StopTime> modifiedStopTimes = createModifiedStopTimes(timetable, oldTimes, journey, trip, new RoutingService(graph));
    if (modifiedStopTimes == null) {
        return null;
    }
    TripTimes newTimes = new TripTimes(trip, modifiedStopTimes, graph.deduplicator);
    // Populate missing data from existing TripTimes
    newTimes.serviceCode = oldTimes.serviceCode;
    int callCounter = 0;
    ZonedDateTime departureDate = null;
    Set<Object> alreadyVisited = new HashSet<>();
    boolean isJourneyPredictionInaccurate = (journey.isPredictionInaccurate() != null && journey.isPredictionInaccurate());
    int departureFromPreviousStop = 0;
    int lastArrivalDelay = 0;
    int lastDepartureDelay = 0;
    for (Stop stop : modifiedStops) {
        boolean foundMatch = false;
        for (RecordedCall recordedCall : recordedCalls) {
            if (alreadyVisited.contains(recordedCall)) {
                continue;
            }
            // Current stop is being updated
            foundMatch = stop.getId().getId().equals(recordedCall.getStopPointRef().getValue());
            if (!foundMatch && stop.isPartOfStation()) {
                Stop alternativeStop = graph.index.getStopForId(new FeedScopedId(stop.getId().getFeedId(), recordedCall.getStopPointRef().getValue()));
                if (alternativeStop != null && stop.isPartOfSameStationAs(alternativeStop)) {
                    foundMatch = true;
                    stopPatternChanged = true;
                }
            }
            if (foundMatch) {
                if (departureDate == null) {
                    departureDate = recordedCall.getAimedDepartureTime();
                    if (departureDate == null) {
                        departureDate = recordedCall.getAimedArrivalTime();
                    }
                    if (oldTimes.getDepartureTime(0) > 86400) {
                        // The "departure-date" for this trip is set to "yesterday" (or before) even though it actually departs "today"
                        // calculate number of offset-days
                        int dayOffsetCount = oldTimes.getDepartureTime(0) / 86400;
                        departureDate = departureDate.minusDays(dayOffsetCount);
                    }
                }
                // Flag as recorded
                newTimes.setRecorded(callCounter, true);
                if (recordedCall.isCancellation() != null) {
                    newTimes.setCancelledStop(callCounter, recordedCall.isCancellation());
                }
                newTimes.setDropoffType(callCounter, timetable.pattern.stopPattern.dropoffs[callCounter]);
                newTimes.setPickupType(callCounter, timetable.pattern.stopPattern.pickups[callCounter]);
                int arrivalTime = newTimes.getArrivalTime(callCounter);
                int realtimeArrivalTime = arrivalTime;
                if (recordedCall.getActualArrivalTime() != null) {
                    realtimeArrivalTime = calculateSecondsSinceMidnight(departureDate, recordedCall.getActualArrivalTime());
                } else if (recordedCall.getExpectedArrivalTime() != null) {
                    realtimeArrivalTime = calculateSecondsSinceMidnight(departureDate, recordedCall.getExpectedArrivalTime());
                } else if (recordedCall.getAimedArrivalTime() != null) {
                    realtimeArrivalTime = calculateSecondsSinceMidnight(departureDate, recordedCall.getAimedArrivalTime());
                }
                int arrivalDelay = realtimeArrivalTime - arrivalTime;
                newTimes.updateArrivalDelay(callCounter, arrivalDelay);
                lastArrivalDelay = arrivalDelay;
                int departureTime = newTimes.getDepartureTime(callCounter);
                int realtimeDepartureTime = departureTime;
                if (recordedCall.getActualDepartureTime() != null) {
                    realtimeDepartureTime = calculateSecondsSinceMidnight(departureDate, recordedCall.getActualDepartureTime());
                } else if (recordedCall.getExpectedDepartureTime() != null) {
                    realtimeDepartureTime = calculateSecondsSinceMidnight(departureDate, recordedCall.getExpectedDepartureTime());
                } else if (recordedCall.getAimedDepartureTime() != null) {
                    realtimeDepartureTime = calculateSecondsSinceMidnight(departureDate, recordedCall.getAimedDepartureTime());
                }
                if (realtimeDepartureTime < realtimeArrivalTime) {
                    realtimeDepartureTime = realtimeArrivalTime;
                }
                int departureDelay = realtimeDepartureTime - departureTime;
                newTimes.updateDepartureDelay(callCounter, departureDelay);
                lastDepartureDelay = departureDelay;
                departureFromPreviousStop = newTimes.getDepartureTime(callCounter);
                alreadyVisited.add(recordedCall);
                break;
            }
        }
        if (!foundMatch) {
            for (EstimatedCall estimatedCall : estimatedCalls) {
                if (alreadyVisited.contains(estimatedCall)) {
                    continue;
                }
                // Current stop is being updated
                foundMatch = stop.getId().getId().equals(estimatedCall.getStopPointRef().getValue());
                if (!foundMatch && stop.isPartOfStation()) {
                    Stop alternativeStop = graph.index.getStopForId(new FeedScopedId(stop.getId().getFeedId(), estimatedCall.getStopPointRef().getValue()));
                    if (alternativeStop != null && stop.isPartOfSameStationAs(alternativeStop)) {
                        foundMatch = true;
                        stopPatternChanged = true;
                    }
                }
                if (foundMatch) {
                    if (departureDate == null) {
                        departureDate = estimatedCall.getAimedDepartureTime();
                        if (departureDate == null) {
                            departureDate = estimatedCall.getAimedArrivalTime();
                        }
                    }
                    if (estimatedCall.isCancellation() != null) {
                        newTimes.setCancelledStop(callCounter, estimatedCall.isCancellation());
                    }
                    boolean isCallPredictionInaccurate = estimatedCall.isPredictionInaccurate() != null && estimatedCall.isPredictionInaccurate();
                    // Set flag for inaccurate prediction if either call OR journey has inaccurate-flag set.
                    newTimes.setPredictionInaccurate(callCounter, (isJourneyPredictionInaccurate | isCallPredictionInaccurate));
                    // Update dropoff-/pickuptype only if status is cancelled
                    CallStatusEnumeration arrivalStatus = estimatedCall.getArrivalStatus();
                    if (arrivalStatus == CallStatusEnumeration.CANCELLED) {
                        newTimes.setDropoffType(callCounter, PICKDROP_NONE);
                    }
                    CallStatusEnumeration departureStatus = estimatedCall.getDepartureStatus();
                    if (departureStatus == CallStatusEnumeration.CANCELLED) {
                        newTimes.setPickupType(callCounter, PICKDROP_NONE);
                    }
                    int arrivalTime = newTimes.getArrivalTime(callCounter);
                    int realtimeArrivalTime = -1;
                    if (estimatedCall.getExpectedArrivalTime() != null) {
                        realtimeArrivalTime = calculateSecondsSinceMidnight(departureDate, estimatedCall.getExpectedArrivalTime());
                    } else if (estimatedCall.getAimedArrivalTime() != null) {
                        realtimeArrivalTime = calculateSecondsSinceMidnight(departureDate, estimatedCall.getAimedArrivalTime());
                    }
                    int departureTime = newTimes.getDepartureTime(callCounter);
                    int realtimeDepartureTime = departureTime;
                    if (estimatedCall.getExpectedDepartureTime() != null) {
                        realtimeDepartureTime = calculateSecondsSinceMidnight(departureDate, estimatedCall.getExpectedDepartureTime());
                    } else if (estimatedCall.getAimedDepartureTime() != null) {
                        realtimeDepartureTime = calculateSecondsSinceMidnight(departureDate, estimatedCall.getAimedDepartureTime());
                    }
                    if (realtimeArrivalTime == -1) {
                        realtimeArrivalTime = realtimeDepartureTime;
                    }
                    if (realtimeDepartureTime < realtimeArrivalTime) {
                        realtimeDepartureTime = realtimeArrivalTime;
                    }
                    int arrivalDelay = realtimeArrivalTime - arrivalTime;
                    newTimes.updateArrivalDelay(callCounter, arrivalDelay);
                    lastArrivalDelay = arrivalDelay;
                    int departureDelay = realtimeDepartureTime - departureTime;
                    newTimes.updateDepartureDelay(callCounter, departureDelay);
                    lastDepartureDelay = departureDelay;
                    departureFromPreviousStop = newTimes.getDepartureTime(callCounter);
                    alreadyVisited.add(estimatedCall);
                    break;
                }
            }
        }
        if (!foundMatch) {
            if (timetable.pattern.stopPattern.pickups[callCounter] == PICKDROP_NONE && timetable.pattern.stopPattern.dropoffs[callCounter] == PICKDROP_NONE) {
                // When newTimes contains stops without pickup/dropoff - set both arrival/departure to previous stop's departure
                // This necessary to accommodate the case when delay is reduced/eliminated between to stops with pickup/dropoff, and
                // multiple non-pickup/dropoff stops are in between.
                newTimes.updateArrivalTime(callCounter, departureFromPreviousStop);
                newTimes.updateDepartureTime(callCounter, departureFromPreviousStop);
            } else {
                int arrivalDelay = lastArrivalDelay;
                int departureDelay = lastDepartureDelay;
                if (lastArrivalDelay == 0 && lastDepartureDelay == 0) {
                    // No match has been found yet (i.e. still in RecordedCalls) - keep existing delays
                    arrivalDelay = existingTripTimes.getArrivalDelay(callCounter);
                    departureDelay = existingTripTimes.getDepartureDelay(callCounter);
                }
                newTimes.updateArrivalDelay(callCounter, arrivalDelay);
                newTimes.updateDepartureDelay(callCounter, departureDelay);
            }
            departureFromPreviousStop = newTimes.getDepartureTime(callCounter);
        }
        callCounter++;
    }
    if (stopPatternChanged) {
        // This update modified stopPattern
        newTimes.setRealTimeState(RealTimeState.MODIFIED);
    } else {
        // This is the first update, and StopPattern has not been changed
        newTimes.setRealTimeState(RealTimeState.UPDATED);
    }
    if (journey.isCancellation() != null && journey.isCancellation()) {
        LOG.debug("Trip is cancelled");
        newTimes.cancel();
    }
    if (!newTimes.timesIncreasing()) {
        LOG.info("TripTimes are non-increasing after applying SIRI delay propagation - LineRef {}, TripId {}.", journey.getLineRef().getValue(), tripId);
        return null;
    }
    if (newTimes.getNumStops() != timetable.pattern.stopPattern.stops.length) {
        return null;
    }
    LOG.debug("A valid TripUpdate object was applied using the Timetable class update method.");
    return newTimes;
}
Also used : Trip(org.opentripplanner.model.Trip) EstimatedVehicleJourney(uk.org.siri.siri20.EstimatedVehicleJourney) Stop(org.opentripplanner.model.Stop) RoutingService(org.opentripplanner.routing.RoutingService) RecordedCall(uk.org.siri.siri20.RecordedCall) ZonedDateTime(java.time.ZonedDateTime) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) FeedScopedId(org.opentripplanner.model.FeedScopedId) EstimatedCall(uk.org.siri.siri20.EstimatedCall) CallStatusEnumeration(uk.org.siri.siri20.CallStatusEnumeration) StopTime(org.opentripplanner.model.StopTime) HashSet(java.util.HashSet)

Aggregations

Trip (org.opentripplanner.model.Trip)58 FeedScopedId (org.opentripplanner.model.FeedScopedId)29 TripPattern (org.opentripplanner.model.TripPattern)28 Stop (org.opentripplanner.model.Stop)20 Test (org.junit.Test)19 Route (org.opentripplanner.model.Route)15 ArrayList (java.util.ArrayList)13 ServiceDate (org.opentripplanner.model.calendar.ServiceDate)13 StopTime (org.opentripplanner.model.StopTime)10 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)9 ZonedDateTime (java.time.ZonedDateTime)7 HashSet (java.util.HashSet)7 List (java.util.List)6 Agency (org.opentripplanner.model.Agency)6 Timetable (org.opentripplanner.model.Timetable)6 RoutingService (org.opentripplanner.routing.RoutingService)5 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 Station (org.opentripplanner.model.Station)4 TimetableSnapshot (org.opentripplanner.model.TimetableSnapshot)4