Search in sources :

Example 6 with Timetable

use of org.opentripplanner.routing.edgetype.Timetable in project OpenTripPlanner by opentripplanner.

the class GraphIndex method getStopTimesForStop.

/**
 * 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
 * @return
 */
public List<StopTimesInPattern> getStopTimesForStop(Stop stop, ServiceDate serviceDate, boolean omitNonPickups) {
    List<StopTimesInPattern> ret = new ArrayList<>();
    TimetableSnapshot snapshot = null;
    if (graph.timetableSnapshotSource != null) {
        snapshot = graph.timetableSnapshotSource.getTimetableSnapshot();
    }
    Collection<TripPattern> patterns = patternsForStop.get(stop);
    for (TripPattern pattern : patterns) {
        StopTimesInPattern stopTimes = new StopTimesInPattern(pattern);
        Timetable tt;
        if (snapshot != null) {
            tt = snapshot.resolve(pattern, serviceDate);
        } else {
            tt = pattern.scheduledTimetable;
        }
        ServiceDay sd = new ServiceDay(graph, serviceDate, calendarService, pattern.route.getAgency().getId());
        int sidx = 0;
        for (Stop currStop : pattern.stopPattern.stops) {
            if (currStop == stop) {
                if (omitNonPickups && pattern.stopPattern.pickups[sidx] == pattern.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.routing.edgetype.Timetable) TripTimeShort(org.opentripplanner.index.model.TripTimeShort) ServiceDay(org.opentripplanner.routing.core.ServiceDay) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) ArrayList(java.util.ArrayList) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TimetableSnapshot(org.opentripplanner.routing.edgetype.TimetableSnapshot) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) StopTimesInPattern(org.opentripplanner.index.model.StopTimesInPattern)

Example 7 with Timetable

use of org.opentripplanner.routing.edgetype.Timetable in project OpenTripPlanner by opentripplanner.

the class GraphIndex method stopTimesForStop.

/**
 * Fetch upcoming vehicle departures from a stop.
 * It goes though all patterns 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. This happens
 * eg. with sleeper trains.
 *
 * TODO: Add frequency based trips
 * @param stop Stop 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.
 * @return
 */
public List<StopTimesInPattern> stopTimesForStop(Stop stop, long startTime, int timeRange, int numberOfDepartures, boolean omitNonPickups) {
    if (startTime == 0) {
        startTime = System.currentTimeMillis() / 1000;
    }
    List<StopTimesInPattern> ret = new ArrayList<>();
    TimetableSnapshot snapshot = null;
    if (graph.timetableSnapshotSource != null) {
        snapshot = graph.timetableSnapshotSource.getTimetableSnapshot();
    }
    Date date = new Date(startTime * 1000);
    ServiceDate[] serviceDates = { new ServiceDate(date).previous(), new ServiceDate(date), new ServiceDate(date).next() };
    for (TripPattern pattern : patternsForStop.get(stop)) {
        // Use the Lucene PriorityQueue, which has a fixed size
        PriorityQueue<TripTimeShort> pq = new PriorityQueue<TripTimeShort>(numberOfDepartures) {

            @Override
            protected boolean lessThan(TripTimeShort tripTimeShort, TripTimeShort t1) {
                // Calculate exact timestamp
                return (tripTimeShort.serviceDay + tripTimeShort.realtimeDeparture) > (t1.serviceDay + t1.realtimeDeparture);
            }
        };
        // Loop through all possible days
        for (ServiceDate serviceDate : serviceDates) {
            ServiceDay sd = new ServiceDay(graph, serviceDate, calendarService, pattern.route.getAgency().getId());
            Timetable tt;
            if (snapshot != null) {
                tt = snapshot.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] == pattern.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.insertWithOverflow(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.insertWithOverflow(new TripTimeShort(freq.materialize(sidx, departureTime, true), sidx, stop, sd));
                            departureTime += freq.headway;
                            i++;
                        }
                    }
                }
                sidx++;
            }
        }
        if (pq.size() != 0) {
            StopTimesInPattern stopTimes = new StopTimesInPattern(pattern);
            while (pq.size() != 0) {
                stopTimes.times.add(0, pq.pop());
            }
            ret.add(stopTimes);
        }
    }
    return ret;
}
Also used : Timetable(org.opentripplanner.routing.edgetype.Timetable) ServiceDay(org.opentripplanner.routing.core.ServiceDay) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) ArrayList(java.util.ArrayList) FrequencyEntry(org.opentripplanner.routing.trippattern.FrequencyEntry) TimetableSnapshot(org.opentripplanner.routing.edgetype.TimetableSnapshot) PriorityQueue(org.apache.lucene.util.PriorityQueue) Date(java.util.Date) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) LocalDate(org.joda.time.LocalDate) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) TripTimeShort(org.opentripplanner.index.model.TripTimeShort) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) StopTimesInPattern(org.opentripplanner.index.model.StopTimesInPattern)

Example 8 with Timetable

use of org.opentripplanner.routing.edgetype.Timetable in project OpenTripPlanner by opentripplanner.

the class IndexAPI method getStoptimesForTrip.

@GET
@Path("/trips/{tripId}/stoptimes")
public Response getStoptimesForTrip(@PathParam("tripId") String tripIdString) {
    AgencyAndId tripId = GtfsLibrary.convertIdFromString(tripIdString);
    Trip trip = index.tripForId.get(tripId);
    if (trip != null) {
        TripPattern pattern = index.patternForTrip.get(trip);
        // Note, we need the updated timetable not the scheduled one (which contains no real-time updates).
        Timetable table = index.currentUpdatedTimetableForTripPattern(pattern);
        return Response.status(Status.OK).entity(TripTimeShort.fromTripTimes(table, trip)).build();
    } else {
        return Response.status(Status.NOT_FOUND).entity(MSG_404).build();
    }
}
Also used : Timetable(org.opentripplanner.routing.edgetype.Timetable) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 9 with Timetable

use of org.opentripplanner.routing.edgetype.Timetable in project OpenTripPlanner by opentripplanner.

the class TimetableSnapshotSource method cancelScheduledTrip.

/**
 * Cancel scheduled trip in buffer given trip id (without agency id) on service date
 *
 * @param tripId trip id without agency id
 * @param serviceDate service date
 * @return true if scheduled trip was cancelled
 */
private boolean cancelScheduledTrip(String feedId, String tripId, final ServiceDate serviceDate) {
    boolean success = false;
    final TripPattern pattern = getPatternForTripId(feedId, tripId);
    if (pattern != null) {
        // Cancel scheduled trip times for this trip in this pattern
        final Timetable timetable = pattern.scheduledTimetable;
        final int tripIndex = timetable.getTripIndex(tripId);
        if (tripIndex == -1) {
            LOG.warn("Could not cancel scheduled trip {}", tripId);
        } else {
            final TripTimes newTripTimes = new TripTimes(timetable.getTripTimes(tripIndex));
            newTripTimes.cancel();
            buffer.update(feedId, pattern, newTripTimes, serviceDate);
            success = true;
        }
    }
    return success;
}
Also used : Timetable(org.opentripplanner.routing.edgetype.Timetable) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripPattern(org.opentripplanner.routing.edgetype.TripPattern)

Example 10 with Timetable

use of org.opentripplanner.routing.edgetype.Timetable in project OpenTripPlanner by opentripplanner.

the class TimetableSnapshotSource method cancelPreviouslyAddedTrip.

/**
 * Cancel previously added trip from buffer if there is a previously added trip with given trip
 * id (without agency id) on service date
 *
 * @param feedId feed id the trip id belongs to
 * @param tripId trip id without agency id
 * @param serviceDate service date
 * @return true if a previously added trip was cancelled
 */
private boolean cancelPreviouslyAddedTrip(final String feedId, final String tripId, final ServiceDate serviceDate) {
    boolean success = false;
    final TripPattern pattern = buffer.getLastAddedTripPattern(feedId, tripId, serviceDate);
    if (pattern != null) {
        // Cancel trip times for this trip in this pattern
        final Timetable timetable = buffer.resolve(pattern, serviceDate);
        final int tripIndex = timetable.getTripIndex(tripId);
        if (tripIndex == -1) {
            LOG.warn("Could not cancel previously added trip {}", tripId);
        } else {
            final TripTimes newTripTimes = new TripTimes(timetable.getTripTimes(tripIndex));
            newTripTimes.cancel();
            buffer.update(feedId, pattern, newTripTimes, serviceDate);
            success = true;
        }
    }
    return success;
}
Also used : Timetable(org.opentripplanner.routing.edgetype.Timetable) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripPattern(org.opentripplanner.routing.edgetype.TripPattern)

Aggregations

Timetable (org.opentripplanner.routing.edgetype.Timetable)12 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)11 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)9 TimetableSnapshot (org.opentripplanner.routing.edgetype.TimetableSnapshot)7 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)4 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 Stop (org.onebusaway.gtfs.model.Stop)3 Trip (org.onebusaway.gtfs.model.Trip)3 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)3 ServiceDay (org.opentripplanner.routing.core.ServiceDay)3 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)3 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)2 StopTimeEvent (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeEvent)2 Calendar (java.util.Calendar)2 List (java.util.List)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 StopTimesInPattern (org.opentripplanner.index.model.StopTimesInPattern)2 TripTimeShort (org.opentripplanner.index.model.TripTimeShort)2