Search in sources :

Example 81 with Trip

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();
    }
}
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 82 with Trip

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;
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Stop(org.onebusaway.gtfs.model.Stop)

Example 83 with Trip

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;
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Route(org.onebusaway.gtfs.model.Route)

Example 84 with Trip

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();
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ParseException(java.text.ParseException) Route(org.onebusaway.gtfs.model.Route)

Example 85 with Trip

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()));
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) Agency(org.onebusaway.gtfs.model.Agency) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Route(org.onebusaway.gtfs.model.Route) Test(org.junit.Test)

Aggregations

Trip (org.onebusaway.gtfs.model.Trip)166 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)89 Test (org.junit.Test)56 StopTime (org.onebusaway.gtfs.model.StopTime)52 Route (org.onebusaway.gtfs.model.Route)51 Stop (org.onebusaway.gtfs.model.Stop)40 ArrayList (java.util.ArrayList)34 List (java.util.List)23 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)21 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)20 Agency (org.onebusaway.gtfs.model.Agency)19 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)19 HashMap (java.util.HashMap)14 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)13 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)13 HashSet (java.util.HashSet)12 Vertex (org.opentripplanner.routing.graph.Vertex)12 FactoryMap (org.onebusaway.collections.FactoryMap)10 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)10 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)10