Search in sources :

Example 1 with EdgesForRoute

use of org.opentripplanner.extra_graph.EdgesForRoute 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)

Aggregations

Coordinate (com.vividsolutions.jts.geom.Coordinate)1 LineString (com.vividsolutions.jts.geom.LineString)1 ArrayList (java.util.ArrayList)1 Route (org.onebusaway.gtfs.model.Route)1 EdgesForRoute (org.opentripplanner.extra_graph.EdgesForRoute)1 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)1 Edge (org.opentripplanner.routing.graph.Edge)1 DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)1