use of org.opentripplanner.routing.edgetype.TripPattern in project OpenTripPlanner by opentripplanner.
the class IndexAPI method getSemanticHashForTrip.
@GET
@Path("/trips/{tripId}/semanticHash")
public Response getSemanticHashForTrip(@PathParam("tripId") String tripIdString) {
AgencyAndId tripId = GtfsLibrary.convertIdFromString(tripIdString);
Trip trip = index.tripForId.get(tripId);
if (trip != null) {
TripPattern pattern = index.patternForTrip.get(trip);
String hashString = pattern.semanticHashString(trip);
return Response.status(Status.OK).entity(hashString).build();
} else {
return Response.status(Status.NOT_FOUND).entity(MSG_404).build();
}
}
use of org.opentripplanner.routing.edgetype.TripPattern in project OpenTripPlanner by opentripplanner.
the class IndexAPI method getRoutes.
/**
* Return a list of all routes in the graph.
*/
// with repeated hasStop parameters, replaces old routesBetweenStops
@GET
@Path("/routes")
public Response getRoutes(@QueryParam("hasStop") List<String> stopIds) {
Collection<Route> routes = index.routeForId.values();
// Filter routes to include only those that pass through all given stops
if (stopIds != null) {
// Protective copy, we are going to calculate the intersection destructively
routes = Lists.newArrayList(routes);
for (String stopId : stopIds) {
Stop stop = index.stopForId.get(GtfsLibrary.convertIdFromString(stopId));
if (stop == null)
return Response.status(Status.NOT_FOUND).entity(MSG_404).build();
Set<Route> routesHere = Sets.newHashSet();
for (TripPattern pattern : index.patternsForStop.get(stop)) {
routesHere.add(pattern.route);
}
routes.retainAll(routesHere);
}
}
return Response.status(Status.OK).entity(RouteShort.list(routes)).build();
}
use of org.opentripplanner.routing.edgetype.TripPattern 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();
}
}
use of org.opentripplanner.routing.edgetype.TripPattern 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 GTFSPatternHopFactory?
// 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.getRoute(), graph);
// Add service code to bitset of pattern if needed (using copy on write)
final int serviceCode = graph.serviceCodes.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(feedId, pattern, newTripTimes, serviceDate);
return success;
}
use of org.opentripplanner.routing.edgetype.TripPattern 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;
}
Aggregations