Search in sources :

Example 41 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class TestShapefileStreetGraphBuilderImpl method testBasic.

@Test
public void testBasic() throws Exception {
    Graph gg = new Graph();
    URL resource = getClass().getResource("nyc_streets/streets.shp");
    File file = null;
    if (resource != null) {
        file = new File(resource.getFile());
    }
    if (file == null || !file.exists()) {
        System.out.println("No New York City basemap; skipping; see comment here for details");
        /*
             * This test requires the New York City base map. Place it among the source 
             * resources and Eclipse should automatically copy it over to the target directory.
             * Once you have prepared these files, you may need to 'refresh' in Eclipse's package 
             * explorer to force Eclipse to notice the new resources.
             * 
             * Recent versions of this map are available only in Arcview Geodatabase format.
             * For conversion to a Shapefile, you will need the archived MapInfo version at:
             * http://www.nyc.gov/html/dcp/html/bytes/bytesarchive.shtml#lion
             * Download the MapInfo file of Lion version 10B. 
             * 
             * This must then be converted to a ShapeFile as follows:
             * cd opentripplanner-graph-builder/src/test/resources/org/opentripplanner/graph_builder/module/shapefile
             * mkdir nyc_streets       (this is where we will store the shapefile)
             * unzip nyc_lion10ami.zip (this should place zipfile contents in a ./lion directory)
             * ogr2ogr -f 'ESRI Shapefile' nyc_streets/streets.shp lion/MNLION1.tab 
             * ogr2ogr -update -append -f 'ESRI Shapefile' nyc_streets lion/SILION1.tab -nln streets 
             * ogr2ogr -update -append -f 'ESRI Shapefile' nyc_streets lion/QNLION1.tab -nln streets 
             * ogr2ogr -update -append -f 'ESRI Shapefile' nyc_streets lion/BKLION1.tab -nln streets 
             * ogr2ogr -update -append -f 'ESRI Shapefile' nyc_streets lion/BXLION1.tab -nln streets
             * 
             * Testing also requires NYC Subway data in GTFS in the same location: 
             * wget http://data.topplabs.org/data/mta_nyct_subway/subway.zip
             */
        return;
    }
    ShapefileFeatureSourceFactoryImpl factory = new ShapefileFeatureSourceFactoryImpl(file);
    ShapefileStreetSchema schema = new ShapefileStreetSchema();
    schema.setIdAttribute("SegmentID");
    schema.setNameAttribute("Street");
    /* only featuretyp=0 are streets */
    CaseBasedBooleanConverter selector = new CaseBasedBooleanConverter("FeatureTyp", false);
    HashMap<String, Boolean> streets = new HashMap<String, Boolean>();
    streets.put("0", true);
    selector.setValues(streets);
    schema.setFeatureSelector(selector);
    /* street directions */
    CaseBasedTraversalPermissionConverter perms = new CaseBasedTraversalPermissionConverter("TrafDir", StreetTraversalPermission.PEDESTRIAN_AND_BICYCLE);
    perms.addPermission("W", StreetTraversalPermission.ALL, StreetTraversalPermission.PEDESTRIAN);
    perms.addPermission("A", StreetTraversalPermission.PEDESTRIAN, StreetTraversalPermission.ALL);
    perms.addPermission("T", StreetTraversalPermission.ALL, StreetTraversalPermission.ALL);
    schema.setPermissionConverter(perms);
    ShapefileStreetModule loader = new ShapefileStreetModule();
    loader.setFeatureSourceFactory(factory);
    loader.setSchema(schema);
    loader.buildGraph(gg, new HashMap<Class<?>, Object>());
    // find start and end vertices
    Vertex start = null;
    Vertex end = null;
    Vertex carlton = null;
    Coordinate vanderbiltAtPark = new Coordinate(-73.969178, 40.676785);
    Coordinate grandAtLafayette = new Coordinate(-73.999095, 40.720005);
    Coordinate carltonAtPark = new Coordinate(-73.972347, 40.677447);
    for (Vertex v : gg.getVertices()) {
        if (v.getCoordinate().distance(vanderbiltAtPark) < 0.00005) {
            /* we need the correct vanderbilt at park.  In this case,
                 * that's the one facing west on vanderbilt.
                 */
            int numParks = 0;
            int numCarltons = 0;
            for (Edge e : v.getOutgoing()) {
                if (e.getToVertex().getName().contains("PARK")) {
                    numParks++;
                }
                if (e.getToVertex().getName().contains("CARLTON")) {
                    numCarltons++;
                }
            }
            if (numCarltons != 2 || numParks != 1) {
                continue;
            }
            start = v;
        } else if (v.getCoordinate().distance(grandAtLafayette) < 0.0001) {
            end = v;
        } else if (v.getCoordinate().distance(carltonAtPark) < 0.00005) {
            /* we need the correct carlton at park.  In this case,
                 * that's the one facing west.
                 */
            int numFlatbushes = 0;
            int numParks = 0;
            for (Edge e : v.getOutgoing()) {
                if (e.getToVertex().getName().contains("FLATBUSH")) {
                    numFlatbushes++;
                }
                if (e.getToVertex().getName().contains("PARK")) {
                    numParks++;
                }
            }
            if (numFlatbushes != 2 || numParks != 1) {
                continue;
            }
            carlton = v;
        }
    }
    assertNotNull(start);
    assertNotNull(end);
    assertNotNull(carlton);
    assertEquals(3, start.getDegreeOut());
    assertEquals(3, start.getDegreeIn());
    AStar aStar = new AStar();
    RoutingRequest opt = new RoutingRequest();
    opt.setRoutingContext(gg, start, end);
    ShortestPathTree spt = aStar.getShortestPathTree(opt);
    assertNotNull(spt);
    // test that the option to walk bikes on the first or last segment works
    opt = new RoutingRequest(new TraverseModeSet(TraverseMode.BICYCLE));
    // Real live cyclists tell me that they would prefer to ride around the long way than to
    // walk their bikes the short way.  If we slow down the default biking speed, that will
    // force a change in preferences.
    opt.bikeSpeed = 2;
    opt.setRoutingContext(gg, start, carlton);
    spt = aStar.getShortestPathTree(opt);
    assertNotNull(spt);
/* commented out as bike walking is not supported */
/*
        GraphPath path = spt.getPath(carlton.vertex);
        assertNotNull(path);
        assertTrue(path.edges.size() <= 3);

        wo.setArriveBy(true);
        spt = AStar.getShortestPathTreeBack(gg, start.vertex, carlton.vertex, new State(0), wo);
        assertNotNull(spt);
        
        path = spt.getPath(carlton.vertex);
        assertTrue(path.edges.size() <= 3);
         */
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) HashMap(java.util.HashMap) AStar(org.opentripplanner.routing.algorithm.AStar) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) URL(java.net.URL) Graph(org.opentripplanner.routing.graph.Graph) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) Coordinate(com.vividsolutions.jts.geom.Coordinate) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) File(java.io.File) Edge(org.opentripplanner.routing.graph.Edge) Test(org.junit.Test)

Example 42 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class AlertPatchTest method testStopAlertPatch.

public void testStopAlertPatch() {
    AlertPatch snp1 = new AlertPatch();
    snp1.setFeedId(feedId);
    snp1.setTimePeriods(Collections.singletonList(new TimePeriod(0, // until ~1/1/2011
    1000L * 60 * 60 * 24 * 365 * 40)));
    Alert note1 = Alert.createSimpleAlerts("The first note");
    snp1.setAlert(note1);
    snp1.setId("id1");
    snp1.setStop(new AgencyAndId(feedId, "A"));
    snp1.apply(graph);
    Vertex stop_a = graph.getVertex(feedId + ":A");
    Vertex stop_e = graph.getVertex(feedId + ":E_arrive");
    ShortestPathTree spt;
    GraphPath optimizedPath, unoptimizedPath;
    options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 0, 0, 0);
    options.setRoutingContext(graph, stop_a, stop_e);
    spt = aStar.getShortestPathTree(options);
    optimizedPath = spt.getPath(stop_e, true);
    unoptimizedPath = spt.getPath(stop_e, false);
    assertNotNull(optimizedPath);
    HashSet<Alert> expectedAlerts = new HashSet<Alert>();
    expectedAlerts.add(note1);
    Edge optimizedEdge = optimizedPath.states.get(1).getBackEdge();
    HashSet<Alert> optimizedAlerts = new HashSet<Alert>();
    for (AlertPatch alertPatch : graph.getAlertPatches(optimizedEdge)) {
        optimizedAlerts.add(alertPatch.getAlert());
    }
    assertEquals(expectedAlerts, optimizedAlerts);
    Edge unoptimizedEdge = unoptimizedPath.states.get(1).getBackEdge();
    HashSet<Alert> unoptimizedAlerts = new HashSet<Alert>();
    for (AlertPatch alertPatch : graph.getAlertPatches(unoptimizedEdge)) {
        unoptimizedAlerts.add(alertPatch.getAlert());
    }
    assertEquals(expectedAlerts, unoptimizedAlerts);
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) GraphPath(org.opentripplanner.routing.spt.GraphPath) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 43 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class TestBikeRental method testBasic.

public void testBasic() throws Exception {
    // generate a very simple graph
    Graph graph = new Graph();
    StreetVertex v1 = new IntersectionVertex(graph, "v1", -77.0492, 38.856, "v1");
    StreetVertex v2 = new IntersectionVertex(graph, "v2", -77.0492, 38.857, "v2");
    StreetVertex v3 = new IntersectionVertex(graph, "v3", -77.0492, 38.858, "v3");
    @SuppressWarnings("unused") Edge walk = new StreetEdge(v1, v2, GeometryUtils.makeLineString(-77.0492, 38.856, -77.0492, 38.857), "S. Crystal Dr", 87, StreetTraversalPermission.PEDESTRIAN, false);
    @SuppressWarnings("unused") Edge mustBike = new StreetEdge(v2, v3, GeometryUtils.makeLineString(-77.0492, 38.857, -77.0492, 38.858), "S. Crystal Dr", 87, StreetTraversalPermission.BICYCLE, false);
    AStar aStar = new AStar();
    // it is impossible to get from v1 to v3 by walking
    RoutingRequest options = new RoutingRequest(new TraverseModeSet("WALK,TRANSIT"));
    options.setRoutingContext(graph, v1, v3);
    ShortestPathTree tree = aStar.getShortestPathTree(options);
    GraphPath path = tree.getPath(v3, false);
    assertNull(path);
    // or biking + walking (assuming walking bikes is disallowed)
    options = new RoutingRequest(new TraverseModeSet("WALK,BICYCLE,TRANSIT"));
    options.freezeTraverseMode();
    options.setRoutingContext(graph, v1, v3);
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(v3, false);
    assertNull(path);
    // so we add a bike share
    BikeRentalStation station = new BikeRentalStation();
    station.id = "id";
    station.name = new NonLocalizedString("station");
    station.x = -77.049;
    station.y = 36.856;
    station.bikesAvailable = 5;
    station.spacesAvailable = 5;
    BikeRentalStationVertex stationVertex = new BikeRentalStationVertex(graph, station);
    new StreetBikeRentalLink(stationVertex, v2);
    new StreetBikeRentalLink(v2, stationVertex);
    Set<String> networks = new HashSet<String>(Arrays.asList("default"));
    new RentABikeOnEdge(stationVertex, stationVertex, networks);
    new RentABikeOffEdge(stationVertex, stationVertex, networks);
    // but we can't get off the bike at v3, so we still fail
    options = new RoutingRequest(new TraverseModeSet("WALK,BICYCLE,TRANSIT"));
    options.freezeTraverseMode();
    options.setRoutingContext(graph, v1, v3);
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(v3, false);
    // null is returned because the only state at the target is not final
    assertNull(path);
    BikeRentalStation station2 = new BikeRentalStation();
    station2.id = "id2";
    station2.name = new NonLocalizedString("station2");
    station2.x = -77.049;
    station2.y = 36.857;
    station2.bikesAvailable = 5;
    station2.spacesAvailable = 5;
    BikeRentalStationVertex stationVertex2 = new BikeRentalStationVertex(graph, station2);
    new StreetBikeRentalLink(stationVertex2, v3);
    new StreetBikeRentalLink(v3, stationVertex2);
    new RentABikeOnEdge(stationVertex2, stationVertex2, networks);
    new RentABikeOffEdge(stationVertex2, stationVertex2, networks);
    // now we succeed!
    options = new RoutingRequest();
    new QualifiedModeSet("BICYCLE_RENT,TRANSIT").applyToRoutingRequest(options);
    options.setRoutingContext(graph, v1, v3);
    tree = aStar.getShortestPathTree(options);
    path = tree.getPath(v3, false);
    assertNotNull(path);
}
Also used : GraphPath(org.opentripplanner.routing.spt.GraphPath) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) QualifiedModeSet(org.opentripplanner.api.parameter.QualifiedModeSet) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) BikeRentalStation(org.opentripplanner.routing.bike_rental.BikeRentalStation) Graph(org.opentripplanner.routing.graph.Graph) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) BikeRentalStationVertex(org.opentripplanner.routing.vertextype.BikeRentalStationVertex) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 44 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class WalkableAreaBuilder method pruneAreaEdges.

/**
 * Do an all-pairs shortest path search from a list of vertices over a specified set of edges,
 * and retain only those edges which are actually used in some shortest path.
 *
 * @param startingVertices
 * @param edges
 */
private void pruneAreaEdges(Collection<Vertex> startingVertices, Set<Edge> edges) {
    if (edges.size() == 0)
        return;
    TraverseMode mode;
    StreetEdge firstEdge = (StreetEdge) edges.iterator().next();
    if (firstEdge.getPermission().allows(StreetTraversalPermission.PEDESTRIAN)) {
        mode = TraverseMode.WALK;
    } else if (firstEdge.getPermission().allows(StreetTraversalPermission.BICYCLE)) {
        mode = TraverseMode.BICYCLE;
    } else {
        mode = TraverseMode.CAR;
    }
    RoutingRequest options = new RoutingRequest(mode);
    options.setDummyRoutingContext(graph);
    options.dominanceFunction = new DominanceFunction.EarliestArrival();
    GenericDijkstra search = new GenericDijkstra(options);
    search.setSkipEdgeStrategy(new ListedEdgesOnly(edges));
    Set<Edge> usedEdges = new HashSet<Edge>();
    for (Vertex vertex : startingVertices) {
        State state = new State(vertex, options);
        ShortestPathTree spt = search.getShortestPathTree(state);
        for (Vertex endVertex : startingVertices) {
            GraphPath path = spt.getPath(endVertex, false);
            if (path != null) {
                for (Edge edge : path.edges) {
                    usedEdges.add(edge);
                }
            }
        }
    }
    for (Edge edge : edges) {
        if (!usedEdges.contains(edge)) {
            graph.removeEdge(edge);
        }
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) GenericDijkstra(org.opentripplanner.routing.algorithm.GenericDijkstra) GraphPath(org.opentripplanner.routing.spt.GraphPath) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) TraverseMode(org.opentripplanner.routing.core.TraverseMode) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) EarliestArrival(org.opentripplanner.routing.spt.DominanceFunction.EarliestArrival) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) AreaEdge(org.opentripplanner.routing.edgetype.AreaEdge) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 45 with Edge

use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.

the class EarliestArrivalSearch method getShortestPathTree.

public ShortestPathTree getShortestPathTree(RoutingRequest options, double relTimeout, SearchTerminationStrategy terminationStrategy) {
    // clone options before modifying, otherwise disabling resource limiting will cause
    // SPT cache misses for subsequent requests.
    options = options.clone();
    // disable any resource limiting, which is algorithmically invalid here
    options.maxTransfers = Integer.MAX_VALUE;
    options.setMaxWalkDistance(Double.MAX_VALUE);
    if (options.clampInitialWait < 0)
        options.clampInitialWait = (60 * 30);
    // impose search cutoff
    final long maxt = maxDuration + options.clampInitialWait;
    options.worstTime = options.dateTime + (options.arriveBy ? -maxt : maxt);
    // SPT cache does not look at routing request in SPT to perform lookup,
    // so it's OK to construct with the local cloned one
    ShortestPathTree spt = new DominanceFunction.EarliestArrival().getNewShortestPathTree(options);
    State initialState = new State(options);
    spt.add(initialState);
    BinHeap<State> pq = new BinHeap<State>();
    pq.insert(initialState, 0);
    while (!pq.empty()) {
        State u = pq.extract_min();
        Vertex u_vertex = u.getVertex();
        if (!spt.visit(u))
            continue;
        Collection<Edge> edges = options.arriveBy ? u_vertex.getIncoming() : u_vertex.getOutgoing();
        for (Edge edge : edges) {
            for (State v = edge.traverse(u); v != null; v = v.getNextResult()) {
                if (isWorstTimeExceeded(v, options)) {
                    continue;
                }
                if (spt.add(v)) {
                    // activeTime?
                    pq.insert(v, v.getActiveTime());
                }
            }
        }
    }
    return spt;
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) BinHeap(org.opentripplanner.common.pqueue.BinHeap) DominanceFunction(org.opentripplanner.routing.spt.DominanceFunction) Edge(org.opentripplanner.routing.graph.Edge)

Aggregations

Edge (org.opentripplanner.routing.graph.Edge)113 Vertex (org.opentripplanner.routing.graph.Vertex)61 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)53 IntersectionVertex (org.opentripplanner.routing.vertextype.IntersectionVertex)26 HashSet (java.util.HashSet)23 State (org.opentripplanner.routing.core.State)22 Coordinate (com.vividsolutions.jts.geom.Coordinate)19 Graph (org.opentripplanner.routing.graph.Graph)19 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)18 Test (org.junit.Test)17 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)17 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)17 ArrayList (java.util.ArrayList)16 LineString (com.vividsolutions.jts.geom.LineString)15 GraphPath (org.opentripplanner.routing.spt.GraphPath)15 StreetVertex (org.opentripplanner.routing.vertextype.StreetVertex)12 PathwayEdge (org.opentripplanner.routing.edgetype.PathwayEdge)11 Geometry (com.vividsolutions.jts.geom.Geometry)9 Stop (org.onebusaway.gtfs.model.Stop)9 TripPattern (org.opentripplanner.routing.edgetype.TripPattern)9