Search in sources :

Example 46 with Route

use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.

the class DefaultFareServiceFactory method fillFareRules.

protected void fillFareRules(String agencyId, Collection<FareAttribute> fareAttributes, Collection<FareRule> fareRules, Map<AgencyAndId, FareRuleSet> fareRuleSet) {
    /*
         * Create an empty FareRuleSet for each FareAttribute, as some FareAttribute may have no
         * rules attached to them.
         */
    for (FareAttribute fare : fareAttributes) {
        AgencyAndId id = fare.getId();
        FareRuleSet fareRule = fareRuleSet.get(id);
        if (fareRule == null) {
            fareRule = new FareRuleSet(fare);
            fareRuleSet.put(id, fareRule);
            if (agencyId != null) {
                // TODO With the new GTFS lib, use fareAttribute.agency_id directly
                fareRule.setAgency(agencyId);
            }
        }
    }
    /*
         * For each fare rule, add it to the FareRuleSet of the fare.
         */
    for (FareRule rule : fareRules) {
        FareAttribute fare = rule.getFare();
        AgencyAndId id = fare.getId();
        FareRuleSet fareRule = fareRuleSet.get(id);
        if (fareRule == null) {
            // Should never happen by design
            LOG.error("Inexistant fare ID in fare rule: " + id);
            continue;
        }
        String contains = rule.getContainsId();
        if (contains != null) {
            fareRule.addContains(contains);
        }
        String origin = rule.getOriginId();
        String destination = rule.getDestinationId();
        if (origin != null || destination != null) {
            fareRule.addOriginDestination(origin, destination);
        }
        Route route = rule.getRoute();
        if (route != null) {
            AgencyAndId routeId = route.getId();
            fareRule.addRoute(routeId);
        }
    }
}
Also used : FareAttribute(org.onebusaway.gtfs.model.FareAttribute) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) FareRule(org.onebusaway.gtfs.model.FareRule) FareRuleSet(org.opentripplanner.routing.core.FareRuleSet) Route(org.onebusaway.gtfs.model.Route)

Example 47 with Route

use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.

the class BusRouteStreetMatcher method buildGraph.

/*
       The "extra" parameter is a mechanism for passing arbitrary things between graph builder modules.
       Whether or not this is a good idea is open to debate, but that's what it is.
       An EdgesForRoute instance is generated by MapBuilder and StreetMatcher, then retrieved later by the
       NetworkLinkerLibrary later (actually in LinkRequests).
     */
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
    // Mapbuilder needs transit index
    graph.index(new DefaultStreetVertexIndexFactory());
    StreetMatcher matcher = new StreetMatcher(graph);
    EdgesForRoute edgesForRoute = new EdgesForRoute();
    extra.put(EdgesForRoute.class, edgesForRoute);
    log.info("Finding corresponding street edges for trip patterns...");
    // Why do we need to iterate over the routes? Why not just patterns?
    for (Route route : graph.index.routeForId.values()) {
        for (TripPattern pattern : graph.index.patternsForRoute.get(route)) {
            if (pattern.mode == TraverseMode.BUS) {
                /* we can only match geometry to streets on bus routes */
                log.debug("Matching {}", pattern);
                // that is why pattern.geometry is null in that case
                if (pattern.geometry == null) {
                    continue;
                }
                List<Edge> edges = matcher.match(pattern.geometry);
                if (edges == null || edges.isEmpty()) {
                    log.warn("Could not match to street network: {}", pattern);
                    continue;
                }
                List<Coordinate> coordinates = new ArrayList<Coordinate>();
                for (Edge e : edges) {
                    coordinates.addAll(Arrays.asList(e.getGeometry().getCoordinates()));
                    edgesForRoute.edgesForRoute.put(route, e);
                }
                Coordinate[] coordinateArray = new Coordinate[coordinates.size()];
                LineString ls = GeometryUtils.getGeometryFactory().createLineString(coordinates.toArray(coordinateArray));
                // Replace the pattern's geometry from GTFS with that of the equivalent OSM edges.
                pattern.geometry = ls;
            }
        }
    }
}
Also used : EdgesForRoute(org.opentripplanner.extra_graph.EdgesForRoute) Coordinate(com.vividsolutions.jts.geom.Coordinate) LineString(com.vividsolutions.jts.geom.LineString) ArrayList(java.util.ArrayList) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) Edge(org.opentripplanner.routing.graph.Edge) EdgesForRoute(org.opentripplanner.extra_graph.EdgesForRoute) Route(org.onebusaway.gtfs.model.Route) TripPattern(org.opentripplanner.routing.edgetype.TripPattern)

Example 48 with Route

use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.

the class IndexAPI method getPatternsForRoute.

/**
 * Return all stop patterns used by trips on the given route.
 */
@GET
@Path("/routes/{routeId}/patterns")
public Response getPatternsForRoute(@PathParam("routeId") String routeIdString) {
    AgencyAndId routeId = GtfsLibrary.convertIdFromString(routeIdString);
    Route route = index.routeForId.get(routeId);
    if (route != null) {
        Collection<TripPattern> patterns = index.patternsForRoute.get(route);
        return Response.status(Status.OK).entity(PatternShort.list(patterns)).build();
    } else {
        return Response.status(Status.NOT_FOUND).entity(MSG_404).build();
    }
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Route(org.onebusaway.gtfs.model.Route) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 49 with Route

use of org.onebusaway.gtfs.model.Route 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();
}
Also used : TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) Route(org.onebusaway.gtfs.model.Route) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 50 with Route

use of org.onebusaway.gtfs.model.Route 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)

Aggregations

Route (org.onebusaway.gtfs.model.Route)90 Trip (org.onebusaway.gtfs.model.Trip)52 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)46 Test (org.junit.Test)33 Stop (org.onebusaway.gtfs.model.Stop)28 Agency (org.onebusaway.gtfs.model.Agency)21 ArrayList (java.util.ArrayList)16 StopTime (org.onebusaway.gtfs.model.StopTime)14 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)14 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)12 HashMap (java.util.HashMap)10 Edge (org.opentripplanner.routing.graph.Edge)8 GET (javax.ws.rs.GET)7 Path (javax.ws.rs.Path)7 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)7 TransitGraphImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TransitGraphImpl)7 List (java.util.List)6 RouteEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.RouteEntryImpl)6 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)6 Coordinate (com.vividsolutions.jts.geom.Coordinate)5