Search in sources :

Example 76 with Edge

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

the class TestUnconnectedAreas method testGeometricGraphWithClasspathFile.

/**
 * We've written several OSM files that exhibit different situations but should show the same results. Test with this code.
 */
public List<String> testGeometricGraphWithClasspathFile(String fn, int prCount, int prlCount) {
    Graph g = new Graph();
    OpenStreetMapModule loader = new OpenStreetMapModule();
    loader.setDefaultWayPropertySetSource(new DefaultWayPropertySetSource());
    FileBasedOpenStreetMapProviderImpl provider = new FileBasedOpenStreetMapProviderImpl();
    File file = new File(getClass().getResource(fn).getFile());
    provider.setPath(file);
    loader.setProvider(provider);
    loader.buildGraph(g, new HashMap<Class<?>, Object>());
    Vertex pr = null;
    int nParkAndRide = 0;
    int nParkAndRideLink = 0;
    for (Vertex v : g.getVertices()) {
        if (v instanceof ParkAndRideVertex) {
            nParkAndRide++;
            pr = v;
        }
    }
    for (Edge e : g.getEdges()) {
        if (e instanceof ParkAndRideLinkEdge) {
            nParkAndRideLink++;
        }
    }
    assertEquals(prCount, nParkAndRide);
    assertEquals(prlCount, nParkAndRideLink);
    assertNotNull(pr);
    // make sure it is connected
    List<String> connections = new ArrayList<String>();
    for (Edge e : pr.getOutgoing()) {
        if (e instanceof ParkAndRideEdge)
            continue;
        assertTrue(e instanceof ParkAndRideLinkEdge);
        connections.add(e.getToVertex().getLabel());
    }
    // symmetry
    for (Edge e : pr.getIncoming()) {
        if (e instanceof ParkAndRideEdge)
            continue;
        assertTrue(e instanceof ParkAndRideLinkEdge);
        assertTrue(connections.contains(e.getFromVertex().getLabel()));
    }
    return connections;
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) ParkAndRideVertex(org.opentripplanner.routing.vertextype.ParkAndRideVertex) FileBasedOpenStreetMapProviderImpl(org.opentripplanner.openstreetmap.impl.FileBasedOpenStreetMapProviderImpl) ParkAndRideLinkEdge(org.opentripplanner.routing.edgetype.ParkAndRideLinkEdge) ArrayList(java.util.ArrayList) ParkAndRideEdge(org.opentripplanner.routing.edgetype.ParkAndRideEdge) Graph(org.opentripplanner.routing.graph.Graph) ParkAndRideVertex(org.opentripplanner.routing.vertextype.ParkAndRideVertex) File(java.io.File) ParkAndRideLinkEdge(org.opentripplanner.routing.edgetype.ParkAndRideLinkEdge) ParkAndRideEdge(org.opentripplanner.routing.edgetype.ParkAndRideEdge) Edge(org.opentripplanner.routing.graph.Edge)

Example 77 with Edge

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

the class TestUnconnectedAreas method testCoincidentNodeUnconnectedParkAndRide.

/**
 * This test ensures that if a Park and Ride has a node that is exactly atop a node on a way, the graph
 * builder will not loop forever trying to split it. The hackett-pr.osm.gz file contains a park-and-ride lot in
 * Hackettstown, NJ, which demonstrates this behavior. See discussion in ticket 1605.
 */
@Test
public void testCoincidentNodeUnconnectedParkAndRide() throws Exception {
    Graph g = new Graph();
    OpenStreetMapModule loader = new OpenStreetMapModule();
    loader.setDefaultWayPropertySetSource(new DefaultWayPropertySetSource());
    FileBasedOpenStreetMapProviderImpl provider = new FileBasedOpenStreetMapProviderImpl();
    File file = new File(getClass().getResource("hackett_pr.osm.gz").getFile());
    provider.setPath(file);
    loader.setProvider(provider);
    loader.buildGraph(g, new HashMap<Class<?>, Object>());
    Vertex washTwp = null;
    int nParkAndRide = 0;
    int nParkAndRideLink = 0;
    for (Vertex v : g.getVertices()) {
        if (v instanceof ParkAndRideVertex) {
            nParkAndRide++;
            washTwp = v;
        }
    }
    for (Edge e : g.getEdges()) {
        if (e instanceof ParkAndRideLinkEdge) {
            nParkAndRideLink++;
        }
    }
    assertEquals(1, nParkAndRide);
    // the P+R should get four connections, since the entrance road is duplicated as well, and crosses twice
    // since there are in and out edges, that brings the total to 8 per P+R.
    // Even though the park and rides get merged, duplicate edges remain from when they were separate.
    // FIXME: we shouldn't have duplicate edges.
    assertEquals(16, nParkAndRideLink);
    assertNotNull(washTwp);
    List<String> connections = new ArrayList<String>();
    for (Edge e : washTwp.getOutgoing()) {
        if (e instanceof ParkAndRideEdge)
            continue;
        assertTrue(e instanceof ParkAndRideLinkEdge);
        connections.add(e.getToVertex().getLabel());
    }
    // symmetry
    for (Edge e : washTwp.getIncoming()) {
        if (e instanceof ParkAndRideEdge)
            continue;
        assertTrue(e instanceof ParkAndRideLinkEdge);
        assertTrue(connections.contains(e.getFromVertex().getLabel()));
    }
    assertTrue(connections.contains("osm:node:3096570222"));
    assertTrue(connections.contains("osm:node:3094264704"));
    assertTrue(connections.contains("osm:node:3094264709"));
    assertTrue(connections.contains("osm:node:3096570227"));
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) ParkAndRideVertex(org.opentripplanner.routing.vertextype.ParkAndRideVertex) FileBasedOpenStreetMapProviderImpl(org.opentripplanner.openstreetmap.impl.FileBasedOpenStreetMapProviderImpl) ParkAndRideLinkEdge(org.opentripplanner.routing.edgetype.ParkAndRideLinkEdge) ArrayList(java.util.ArrayList) ParkAndRideEdge(org.opentripplanner.routing.edgetype.ParkAndRideEdge) Graph(org.opentripplanner.routing.graph.Graph) ParkAndRideVertex(org.opentripplanner.routing.vertextype.ParkAndRideVertex) File(java.io.File) ParkAndRideLinkEdge(org.opentripplanner.routing.edgetype.ParkAndRideLinkEdge) ParkAndRideEdge(org.opentripplanner.routing.edgetype.ParkAndRideEdge) Edge(org.opentripplanner.routing.graph.Edge) Test(org.junit.Test)

Example 78 with Edge

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

the class AlertPatchTest method testTimeRanges.

public void testTimeRanges() {
    AlertPatch snp1 = new AlertPatch();
    snp1.setFeedId(feedId);
    LinkedList<TimePeriod> timePeriods = new LinkedList<TimePeriod>();
    long breakTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 0, 0, 0);
    // until the beginning of the day
    timePeriods.add(new TimePeriod(0, breakTime));
    long secondPeriodStartTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 7, 0, 0);
    long secondPeriodEndTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 8, 0, 0, 0);
    timePeriods.add(new TimePeriod(secondPeriodStartTime, secondPeriodEndTime));
    snp1.setTimePeriods(timePeriods);
    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 path;
    options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 0, 0, 0);
    options.setRoutingContext(graph, stop_a, stop_e);
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(stop_e, true);
    assertNotNull(path);
    // expect no notes because we are during the break
    State noAlertPatchesState = path.states.get(1);
    Edge noAlertPatchesEdge = noAlertPatchesState.getBackEdge();
    HashSet<Alert> noAlertPatchesAlerts = new HashSet<Alert>();
    for (AlertPatch alertPatch : graph.getAlertPatches(noAlertPatchesEdge)) {
        if (alertPatch.displayDuring(noAlertPatchesState)) {
            noAlertPatchesAlerts.add(alertPatch.getAlert());
        }
    }
    assertEquals(new HashSet<Alert>(), noAlertPatchesAlerts);
    // now a trip during the second period
    options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 8, 0, 0);
    options.setRoutingContext(graph, stop_a, stop_e);
    spt = aStar.getShortestPathTree(options);
    // do not optimize because we want the first trip
    path = spt.getPath(stop_e, false);
    assertNotNull(path);
    HashSet<Alert> expectedNotes = new HashSet<Alert>();
    expectedNotes.add(note1);
    State oneAlertPatchState = path.states.get(1);
    Edge oneAlertPatchEdge = oneAlertPatchState.getBackEdge();
    HashSet<Alert> oneAlertPatchAlerts = new HashSet<Alert>();
    for (AlertPatch alertPatch : graph.getAlertPatches(oneAlertPatchEdge)) {
        if (alertPatch.displayDuring(oneAlertPatchState)) {
            oneAlertPatchAlerts.add(alertPatch.getAlert());
        }
    }
    assertEquals(expectedNotes, oneAlertPatchAlerts);
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) GraphPath(org.opentripplanner.routing.spt.GraphPath) LinkedList(java.util.LinkedList) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) State(org.opentripplanner.routing.core.State) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 79 with Edge

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

the class AlertPatchTest method testRouteNotePatch.

public void testRouteNotePatch() {
    AlertPatch rnp1 = new AlertPatch();
    rnp1.setFeedId(feedId);
    rnp1.setTimePeriods(Collections.singletonList(new TimePeriod(0, // until ~1/1/2011
    1000L * 60 * 60 * 24 * 365 * 40)));
    Alert note1 = Alert.createSimpleAlerts("The route note");
    rnp1.setAlert(note1);
    rnp1.setId("id1");
    // Routes isn't patched in tests through GtfsBundle, which is why we have have a reference to agency id here.
    rnp1.setRoute(new AgencyAndId("agency", "1"));
    rnp1.apply(graph);
    Vertex stop_a = graph.getVertex(feedId + ":A");
    Vertex stop_e = graph.getVertex(feedId + ":E_arrive");
    ShortestPathTree spt;
    GraphPath path;
    options.setRoutingContext(graph, stop_a, stop_e);
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(stop_e, false);
    assertNotNull(path);
    HashSet<Alert> expectedAlerts = new HashSet<Alert>();
    expectedAlerts.add(note1);
    Edge actualEdge = path.states.get(2).getBackEdge();
    HashSet<Alert> actualAlerts = new HashSet<Alert>();
    for (AlertPatch alertPatch : graph.getAlertPatches(actualEdge)) {
        actualAlerts.add(alertPatch.getAlert());
    }
    assertEquals(expectedAlerts, actualAlerts);
}
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 80 with Edge

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

the class GraphPathToTripPlanConverter method sliceStates.

/**
 * Slice a {@link State} array at the leg boundaries. Leg switches occur when:
 * 1. A LEG_SWITCH mode (which itself isn't part of any leg) is seen
 * 2. The mode changes otherwise, for instance from BICYCLE to WALK
 * 3. A PatternInterlineDwell edge (i.e. interlining) is seen
 *
 * @param states The one-dimensional array of input states
 * @return An array of arrays of states belonging to a single leg (i.e. a two-dimensional array)
 */
private static State[][] sliceStates(State[] states) {
    boolean trivial = true;
    for (State state : states) {
        TraverseMode traverseMode = state.getBackMode();
        if (traverseMode != null && traverseMode != TraverseMode.LEG_SWITCH) {
            trivial = false;
            break;
        }
    }
    if (trivial) {
        throw new TrivialPathException();
    }
    int[] legIndexPairs = { 0, states.length - 1 };
    List<int[]> legsIndexes = new ArrayList<int[]>();
    for (int i = 1; i < states.length - 1; i++) {
        TraverseMode backMode = states[i].getBackMode();
        TraverseMode forwardMode = states[i + 1].getBackMode();
        if (backMode == null || forwardMode == null)
            continue;
        Edge edge = states[i + 1].getBackEdge();
        if (backMode == TraverseMode.LEG_SWITCH || forwardMode == TraverseMode.LEG_SWITCH) {
            if (backMode != TraverseMode.LEG_SWITCH) {
                // Start of leg switch
                legIndexPairs[1] = i;
            } else if (forwardMode != TraverseMode.LEG_SWITCH) {
                // End of leg switch
                if (legIndexPairs[1] != states.length - 1) {
                    legsIndexes.add(legIndexPairs);
                }
                legIndexPairs = new int[] { i, states.length - 1 };
            }
        } else if (backMode != forwardMode) {
            // Mode change => leg switch
            legIndexPairs[1] = i;
            legsIndexes.add(legIndexPairs);
            legIndexPairs = new int[] { i, states.length - 1 };
        } else if (edge instanceof PatternInterlineDwell) {
            // Interlining => leg switch
            legIndexPairs[1] = i;
            legsIndexes.add(legIndexPairs);
            legIndexPairs = new int[] { i + 1, states.length - 1 };
        }
    }
    // Final leg
    legsIndexes.add(legIndexPairs);
    State[][] legsStates = new State[legsIndexes.size()][];
    // Fill the two-dimensional array with states
    for (int i = 0; i < legsStates.length; i++) {
        legIndexPairs = legsIndexes.get(i);
        legsStates[i] = new State[legIndexPairs[1] - legIndexPairs[0] + 1];
        for (int j = 0; j <= legIndexPairs[1] - legIndexPairs[0]; j++) {
            legsStates[i][j] = states[legIndexPairs[0] + j];
        }
    }
    return legsStates;
}
Also used : Edge(org.opentripplanner.routing.graph.Edge) TrivialPathException(org.opentripplanner.routing.error.TrivialPathException)

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