use of org.onebusaway.gtfs.model.Trip 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.onebusaway.gtfs.model.Trip in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSource method validateAndHandleAddedTrip.
/**
* Validate and handle GTFS-RT TripUpdate message containing an ADDED trip.
*
* @param graph graph to update
* @param tripUpdate GTFS-RT TripUpdate message
* @param feedId
* @param serviceDate
* @return true iff successful
*/
private boolean validateAndHandleAddedTrip(final Graph graph, final TripUpdate tripUpdate, final String feedId, final ServiceDate serviceDate) {
// Preconditions
Preconditions.checkNotNull(graph);
Preconditions.checkNotNull(tripUpdate);
Preconditions.checkNotNull(serviceDate);
//
// Validate added trip
//
// Check whether trip id of ADDED trip is available
final TripDescriptor tripDescriptor = tripUpdate.getTrip();
if (!tripDescriptor.hasTripId()) {
LOG.warn("No trip id found for ADDED trip, skipping.");
return false;
}
// Check whether trip id already exists in graph
final String tripId = tripDescriptor.getTripId();
final Trip trip = getTripForTripId(feedId, tripId);
if (trip != null) {
// TODO: should we support this and add a new instantiation of this trip (making it
// frequency based)?
LOG.warn("Graph already contains trip id of ADDED trip, skipping.");
return false;
}
// Check whether a start date exists
if (!tripDescriptor.hasStartDate()) {
// TODO: should we support this and apply update to all days?
LOG.warn("ADDED trip doesn't have a start date in TripDescriptor, skipping.");
return false;
}
// Check whether at least two stop updates exist
if (tripUpdate.getStopTimeUpdateCount() < 2) {
LOG.warn("ADDED trip has less then two stops, skipping.");
return false;
}
// Check whether all stop times are available and all stops exist
final List<Stop> stops = checkNewStopTimeUpdatesAndFindStops(feedId, tripUpdate);
if (stops == null) {
return false;
}
//
// Handle added trip
//
final boolean success = handleAddedTrip(graph, tripUpdate, stops, feedId, serviceDate);
return success;
}
use of org.onebusaway.gtfs.model.Trip in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSource method handleAddedTrip.
/**
* Handle GTFS-RT TripUpdate message containing an ADDED trip.
*
* @param graph graph to update
* @param tripUpdate GTFS-RT TripUpdate message
* @param stops the stops of each StopTimeUpdate in the TripUpdate message
* @param feedId
* @param serviceDate service date for added trip
* @return true iff successful
*/
private boolean handleAddedTrip(final Graph graph, final TripUpdate tripUpdate, final List<Stop> stops, final String feedId, final ServiceDate serviceDate) {
// Preconditions
Preconditions.checkNotNull(stops);
Preconditions.checkArgument(tripUpdate.getStopTimeUpdateCount() == stops.size(), "number of stop should match the number of stop time updates");
// Check whether trip id has been used for previously ADDED trip message and cancel
// previously created trip
final String tripId = tripUpdate.getTrip().getTripId();
cancelPreviouslyAddedTrip(feedId, tripId, serviceDate);
//
// Create added trip
//
Route route = null;
if (tripUpdate.getTrip().hasRouteId()) {
// Try to find route
route = getRouteForRouteId(feedId, tripUpdate.getTrip().getRouteId());
}
if (route == null) {
// Create new Route
route = new Route();
// Use route id of trip descriptor if available
if (tripUpdate.getTrip().hasRouteId()) {
route.setId(new AgencyAndId(feedId, tripUpdate.getTrip().getRouteId()));
} else {
route.setId(new AgencyAndId(feedId, tripId));
}
route.setAgency(dummyAgency);
// Guess the route type as it doesn't exist yet in the specifications
// Bus. Used for short- and long-distance bus routes.
route.setType(3);
// Create route name
route.setLongName(tripId);
}
// Create new Trip
final Trip trip = new Trip();
// TODO: which Agency ID to use? Currently use feed id.
trip.setId(new AgencyAndId(feedId, tripUpdate.getTrip().getTripId()));
trip.setRoute(route);
// Find service ID running on this service date
final Set<AgencyAndId> serviceIds = graph.getCalendarService().getServiceIdsOnDate(serviceDate);
if (serviceIds.isEmpty()) {
// No service id exists: return error for now
LOG.warn("ADDED trip has service date for which no service id is available, skipping.");
return false;
} else {
// Just use first service id of set
trip.setServiceId(serviceIds.iterator().next());
}
final boolean success = addTripToGraphAndBuffer(feedId, graph, trip, tripUpdate, stops, serviceDate, RealTimeState.ADDED);
return success;
}
use of org.onebusaway.gtfs.model.Trip in project OpenTripPlanner by opentripplanner.
the class GtfsRealtimeFuzzyTripMatcher method match.
public TripDescriptor match(String feedId, TripDescriptor trip) {
if (trip.hasTripId()) {
// trip_id already exists
return trip;
}
if (!trip.hasRouteId() || !trip.hasDirectionId() || !trip.hasStartTime() || !trip.hasStartDate()) {
// Could not determine trip_id, returning original TripDescriptor
return trip;
}
AgencyAndId routeId = new AgencyAndId(feedId, trip.getRouteId());
int time = StopTimeFieldMappingFactory.getStringAsSeconds(trip.getStartTime());
ServiceDate date;
try {
date = ServiceDate.parseString(trip.getStartDate());
} catch (ParseException e) {
return trip;
}
Route route = index.routeForId.get(routeId);
if (route == null) {
return trip;
}
int direction = trip.getDirectionId();
Trip matchedTrip = getTrip(route, direction, time, date);
if (matchedTrip == null) {
// Check if the trip is carried over from previous day
date = date.previous();
time += 24 * 60 * 60;
matchedTrip = getTrip(route, direction, time, date);
}
if (matchedTrip == null) {
return trip;
}
// If everything succeeds, build a new TripDescriptor with the matched trip_id
return trip.toBuilder().setTripId(matchedTrip.getId().getId()).build();
}
use of org.onebusaway.gtfs.model.Trip in project OpenTripPlanner by opentripplanner.
the class RoutingRequestTest method testPreferencesPenaltyForRoute.
@Test
public void testPreferencesPenaltyForRoute() {
AgencyAndId agencyAndId = new AgencyAndId();
Agency agency = new Agency();
Route route = new Route();
Trip trip = new Trip();
RoutingRequest routingRequest = new RoutingRequest();
trip.setRoute(route);
route.setId(agencyAndId);
route.setAgency(agency);
assertEquals(0, routingRequest.preferencesPenaltyForRoute(trip.getRoute()));
}
Aggregations