Search in sources :

Example 21 with StopTime

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

the class StopTimesMapper method mapToStopTimes.

/**
 * @return a map of stop-times indexed by the TimetabledPassingTime id.
 */
MappedStopTimes mapToStopTimes(JourneyPattern journeyPattern, Trip trip, List<TimetabledPassingTime> passingTimes) {
    MappedStopTimes result = new MappedStopTimes();
    for (int i = 0; i < passingTimes.size(); i++) {
        TimetabledPassingTime currentPassingTime = passingTimes.get(i);
        String pointInJourneyPattern = currentPassingTime.getPointInJourneyPatternRef().getValue().getRef();
        StopPointInJourneyPattern stopPoint = findStopPoint(pointInJourneyPattern, journeyPattern);
        Stop stop = lookupStop(stopPoint, quayIdByStopPointRef, stopsById);
        if (stop == null) {
            LOG.warn("Stop with id {} not found for StopPoint {} in JourneyPattern {}. " + "Trip {} will not be mapped.", stopPoint != null && stopPoint.getScheduledStopPointRef() != null ? stopPoint.getScheduledStopPointRef().getValue().getRef() : "null", stopPoint != null ? stopPoint.getId() : "null", journeyPattern.getId(), trip.getId());
            return null;
        }
        StopTime stopTime = mapToStopTime(trip, stopPoint, stop, currentPassingTime, i);
        result.add(currentPassingTime.getId(), stopTime);
    }
    return result;
}
Also used : Stop(org.opentripplanner.model.Stop) StopPointInJourneyPattern(org.rutebanken.netex.model.StopPointInJourneyPattern) TimetabledPassingTime(org.rutebanken.netex.model.TimetabledPassingTime) StopTime(org.opentripplanner.model.StopTime)

Example 22 with StopTime

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

the class TimetableSnapshotSource method addTripToGraphAndBuffer.

/**
 * Add a (new) trip to the graph and the buffer
 *
 * @param graph graph
 * @param trip trip
 * @param tripUpdate trip update containing stop time updates
 * @param stops list of stops corresponding to stop time updates
 * @param serviceDate service date of trip
 * @param realTimeState real-time state of new trip
 * @return true iff successful
 */
private boolean addTripToGraphAndBuffer(final String feedId, final Graph graph, final Trip trip, final TripUpdate tripUpdate, final List<Stop> stops, final ServiceDate serviceDate, final RealTimeState realTimeState) {
    // Preconditions
    Preconditions.checkNotNull(stops);
    Preconditions.checkArgument(tripUpdate.getStopTimeUpdateCount() == stops.size(), "number of stop should match the number of stop time updates");
    // Calculate seconds since epoch on GTFS midnight (noon minus 12h) of service date
    final Calendar serviceCalendar = serviceDate.getAsCalendar(timeZone);
    final long midnightSecondsSinceEpoch = serviceCalendar.getTimeInMillis() / MILLIS_PER_SECOND;
    // Create StopTimes
    final List<StopTime> stopTimes = new ArrayList<>(tripUpdate.getStopTimeUpdateCount());
    for (int index = 0; index < tripUpdate.getStopTimeUpdateCount(); ++index) {
        final StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(index);
        final Stop stop = stops.get(index);
        // Determine whether stop is skipped
        final boolean skippedStop = isStopSkipped(stopTimeUpdate);
        // Only create stop time for non-skipped stops
        if (!skippedStop) {
            // Create stop time
            final StopTime stopTime = new StopTime();
            stopTime.setTrip(trip);
            stopTime.setStop(stop);
            // Set arrival time
            if (stopTimeUpdate.hasArrival() && stopTimeUpdate.getArrival().hasTime()) {
                final long arrivalTime = stopTimeUpdate.getArrival().getTime() - midnightSecondsSinceEpoch;
                if (arrivalTime < 0 || arrivalTime > MAX_ARRIVAL_DEPARTURE_TIME) {
                    LOG.warn("ADDED trip has invalid arrival time (compared to start date in " + "TripDescriptor), skipping.");
                    return false;
                }
                stopTime.setArrivalTime((int) arrivalTime);
            }
            // Set departure time
            if (stopTimeUpdate.hasDeparture() && stopTimeUpdate.getDeparture().hasTime()) {
                final long departureTime = stopTimeUpdate.getDeparture().getTime() - midnightSecondsSinceEpoch;
                if (departureTime < 0 || departureTime > MAX_ARRIVAL_DEPARTURE_TIME) {
                    LOG.warn("ADDED trip has invalid departure time (compared to start date in " + "TripDescriptor), skipping.");
                    return false;
                }
                stopTime.setDepartureTime((int) departureTime);
            }
            // Exact time
            stopTime.setTimepoint(1);
            if (stopTimeUpdate.hasStopSequence()) {
                stopTime.setStopSequence(stopTimeUpdate.getStopSequence());
            }
            // Set different pickup type for last stop
            if (index == tripUpdate.getStopTimeUpdateCount() - 1) {
                // No pickup available
                stopTime.setPickupType(1);
            } else {
                // Regularly scheduled pickup
                stopTime.setPickupType(0);
            }
            // Set different drop off type for first stop
            if (index == 0) {
                // No drop off available
                stopTime.setDropOffType(1);
            } else {
                // Regularly scheduled drop off
                stopTime.setDropOffType(0);
            }
            // Add stop time to list
            stopTimes.add(stopTime);
        }
    }
    // TODO: filter/interpolate stop times like in PatternHopFactory?
    // Create StopPattern
    final StopPattern stopPattern = new StopPattern(stopTimes);
    // Get cached trip pattern or create one if it doesn't exist yet
    final TripPattern pattern = tripPatternCache.getOrCreateTripPattern(stopPattern, trip, graph);
    // Add service code to bitset of pattern if needed (using copy on write)
    final int serviceCode = graph.getServiceCodes().get(trip.getServiceId());
    if (!pattern.getServices().get(serviceCode)) {
        final BitSet services = (BitSet) pattern.getServices().clone();
        services.set(serviceCode);
        pattern.setServices(services);
    }
    // Create new trip times
    final TripTimes newTripTimes = new TripTimes(trip, stopTimes, graph.deduplicator);
    // TODO: should we incorporate the delay field if present?
    for (int stopIndex = 0; stopIndex < newTripTimes.getNumStops(); stopIndex++) {
        newTripTimes.updateArrivalTime(stopIndex, newTripTimes.getScheduledArrivalTime(stopIndex));
        newTripTimes.updateDepartureTime(stopIndex, newTripTimes.getScheduledDepartureTime(stopIndex));
    }
    // Set service code of new trip times
    newTripTimes.serviceCode = serviceCode;
    // Make sure that updated trip times have the correct real time state
    newTripTimes.setRealTimeState(realTimeState);
    // Add new trip times to the buffer
    final boolean success = buffer.update(pattern, newTripTimes, serviceDate);
    return success;
}
Also used : StopPattern(org.opentripplanner.model.StopPattern) Stop(org.opentripplanner.model.Stop) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) TripPattern(org.opentripplanner.model.TripPattern) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) StopTime(org.opentripplanner.model.StopTime)

Example 23 with StopTime

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

the class GeometryAndBlockProcessor method getStopLocations.

/**
 * Find a consistent, increasing list of LinearLocations along a shape for a set of stops.
 * Handles loops routes.
 */
private List<LinearLocation> getStopLocations(List<List<IndexedLineSegment>> possibleSegmentsForStop, List<StopTime> stopTimes, int index, int prevSegmentIndex) {
    if (index == stopTimes.size()) {
        return new LinkedList<>();
    }
    StopTime st = stopTimes.get(index);
    StopLocation stop = st.getStop();
    Coordinate stopCoord = stop.getCoordinate().asJtsCoordinate();
    for (IndexedLineSegment segment : possibleSegmentsForStop.get(index)) {
        if (segment.index < prevSegmentIndex) {
            // can't go backwards along line
            continue;
        }
        List<LinearLocation> locations = getStopLocations(possibleSegmentsForStop, stopTimes, index + 1, segment.index);
        if (locations != null) {
            LinearLocation location = new LinearLocation(0, segment.index, segment.fraction(stopCoord));
            locations.add(0, location);
            // we found one!
            return locations;
        }
    }
    return null;
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) LinearLocation(org.locationtech.jts.linearref.LinearLocation) StopLocation(org.opentripplanner.model.StopLocation) LinkedList(java.util.LinkedList) StopTime(org.opentripplanner.model.StopTime)

Example 24 with StopTime

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

the class GeometryAndBlockProcessor method getHopGeometriesViaShapeDistTravelled.

private LineString[] getHopGeometriesViaShapeDistTravelled(List<StopTime> stopTimes, FeedScopedId shapeId) {
    LineString[] geoms = new LineString[stopTimes.size() - 1];
    StopTime st0;
    for (int i = 0; i < stopTimes.size() - 1; ++i) {
        st0 = stopTimes.get(i);
        StopTime st1 = stopTimes.get(i + 1);
        geoms[i] = getHopGeometryViaShapeDistTraveled(shapeId, st0, st1);
    }
    return geoms;
}
Also used : LineString(org.locationtech.jts.geom.LineString) ShapePoint(org.opentripplanner.model.ShapePoint) StopTime(org.opentripplanner.model.StopTime)

Example 25 with StopTime

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

the class RepairStopTimesForEachTripOperation method removeRepeatedStops.

/**
 * Filter out any series of stop times that refer to the same stop. This is very inefficient in
 * an array-backed list, but we are assuming that this is a rare occurrence. The alternative is
 * to copy every list of stop times during filtering.
 *
 * TODO: OBA GFTS makes the stoptime lists unmodifiable, so this will not work.
 * We need to copy any modified list.
 *
 * @return whether any repeated stops were filtered out.
 */
private TIntList removeRepeatedStops(List<StopTime> stopTimes) {
    StopTime prev = null;
    Iterator<StopTime> it = stopTimes.iterator();
    TIntList stopSequencesRemoved = new TIntArrayList();
    while (it.hasNext()) {
        StopTime st = it.next();
        if (prev != null) {
            if (prev.getStop().equals(st.getStop())) {
                // This is particularly important at the last stop in a route (see issue #2220)
                if (prev.getArrivalTime() == StopTime.MISSING_VALUE) {
                    prev.setArrivalTime(st.getArrivalTime());
                }
                // prefer to replace with the departure time of this stop time, unless this stop time has no departure time
                if (st.getDepartureTime() != StopTime.MISSING_VALUE) {
                    prev.setDepartureTime(st.getDepartureTime());
                }
                it.remove();
                stopSequencesRemoved.add(st.getStopSequence());
            }
        }
        prev = st;
    }
    return stopSequencesRemoved;
}
Also used : TIntList(gnu.trove.list.TIntList) TIntArrayList(gnu.trove.list.array.TIntArrayList) StopTime(org.opentripplanner.model.StopTime)

Aggregations

StopTime (org.opentripplanner.model.StopTime)41 Trip (org.opentripplanner.model.Trip)15 ArrayList (java.util.ArrayList)11 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)10 FeedScopedId (org.opentripplanner.model.FeedScopedId)8 Test (org.junit.Test)7 TripPattern (org.opentripplanner.model.TripPattern)7 StopLocation (org.opentripplanner.model.StopLocation)6 StopPattern (org.opentripplanner.model.StopPattern)6 Stop (org.opentripplanner.model.Stop)5 Route (org.opentripplanner.model.Route)4 MultilingualString (org.rutebanken.netex.model.MultilingualString)4 StopPointInJourneyPattern (org.rutebanken.netex.model.StopPointInJourneyPattern)4 TimetabledPassingTime (org.rutebanken.netex.model.TimetabledPassingTime)4 ZonedDateTime (java.time.ZonedDateTime)3 HashSet (java.util.HashSet)3 List (java.util.List)3 DataImportIssueStore (org.opentripplanner.graph_builder.DataImportIssueStore)3 VersionOfObjectRefStructure (org.rutebanken.netex.model.VersionOfObjectRefStructure)3 EstimatedCall (uk.org.siri.siri20.EstimatedCall)3