Search in sources :

Example 51 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath in project OpenTripPlanner by opentripplanner.

the class GtfsTest method plan.

public Leg[] plan(long dateTime, String fromVertex, String toVertex, String onTripId, boolean wheelchairAccessible, boolean preferLeastTransfers, TraverseMode preferredMode, String excludedRoute, String excludedStop, int legCount) {
    final TraverseMode mode = preferredMode != null ? preferredMode : TraverseMode.TRANSIT;
    RoutingRequest routingRequest = new RoutingRequest();
    routingRequest.setNumItineraries(1);
    routingRequest.setArriveBy(dateTime < 0);
    routingRequest.dateTime = Math.abs(dateTime);
    if (fromVertex != null && !fromVertex.isEmpty()) {
        routingRequest.from = (new GenericLocation(null, feedId.getId() + ":" + fromVertex));
    }
    if (toVertex != null && !toVertex.isEmpty()) {
        routingRequest.to = new GenericLocation(null, feedId.getId() + ":" + toVertex);
    }
    if (onTripId != null && !onTripId.isEmpty()) {
        routingRequest.startingTransitTripId = (new AgencyAndId(feedId.getId(), onTripId));
    }
    routingRequest.setRoutingContext(graph);
    routingRequest.setWheelchairAccessible(wheelchairAccessible);
    routingRequest.transferPenalty = (preferLeastTransfers ? 300 : 0);
    routingRequest.setModes(new TraverseModeSet(TraverseMode.WALK, mode));
    // TODO route matcher still using underscores because it's quite nonstandard and should be eliminated from the 1.0 release rather than reworked
    if (excludedRoute != null && !excludedRoute.isEmpty()) {
        routingRequest.setBannedRoutes(feedId.getId() + "__" + excludedRoute);
    }
    if (excludedStop != null && !excludedStop.isEmpty()) {
        routingRequest.setBannedStopsHard(feedId.getId() + ":" + excludedStop);
    }
    routingRequest.setOtherThanPreferredRoutesPenalty(0);
    // The walk board cost is set low because it interferes with test 2c1.
    // As long as boarding has a very low cost, waiting should not be "better" than riding
    // since this makes interlining _worse_ than alighting and re-boarding the same line.
    // TODO rethink whether it makes sense to weight waiting to board _less_ than 1.
    routingRequest.setWaitReluctance(1);
    routingRequest.setWalkBoardCost(30);
    List<GraphPath> paths = new GraphPathFinder(router).getPaths(routingRequest);
    TripPlan tripPlan = GraphPathToTripPlanConverter.generatePlan(paths, routingRequest);
    // Stored in instance field for use in individual tests
    itinerary = tripPlan.itinerary.get(0);
    assertEquals(legCount, itinerary.legs.size());
    return itinerary.legs.toArray(new Leg[legCount]);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) GraphPath(org.opentripplanner.routing.spt.GraphPath) TripPlan(org.opentripplanner.api.model.TripPlan) GenericLocation(org.opentripplanner.common.model.GenericLocation) TraverseMode(org.opentripplanner.routing.core.TraverseMode) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) GraphPathFinder(org.opentripplanner.routing.impl.GraphPathFinder)

Example 52 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath 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 53 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath 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 54 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath in project OpenTripPlanner by opentripplanner.

the class TestAStar method testBasic.

public void testBasic() throws Exception {
    GtfsContext context = GtfsLibrary.readGtfs(new File(ConstantsForTests.CALTRAIN_GTFS));
    Graph gg = new Graph();
    GTFSPatternHopFactory factory = new GTFSPatternHopFactory(context);
    factory.run(gg);
    gg.putService(CalendarServiceData.class, GtfsLibrary.createCalendarServiceData(context.getDao()));
    RoutingRequest options = new RoutingRequest();
    ShortestPathTree spt;
    GraphPath path = null;
    String feedId = gg.getFeedIds().iterator().next();
    options.dateTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 8, 7, 12, 0, 0);
    options.setRoutingContext(gg, feedId + ":Millbrae Caltrain", feedId + ":Mountain View Caltrain");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":Mountain View Caltrain"), true);
    long endTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 8, 7, 13, 29, 0);
    assertEquals(path.getEndTime(), endTime);
    /* test backwards traversal */
    options.setArriveBy(true);
    options.dateTime = endTime;
    options.setRoutingContext(gg, feedId + ":Millbrae Caltrain", feedId + ":Mountain View Caltrain");
    spt = aStar.getShortestPathTree(options);
    path = spt.getPath(gg.getVertex(feedId + ":Millbrae Caltrain"), true);
    long expectedStartTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 8, 7, 12, 39, 0);
    assertTrue(path.getStartTime() - expectedStartTime <= 1);
}
Also used : Graph(org.opentripplanner.routing.graph.Graph) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) GtfsContext(org.opentripplanner.gtfs.GtfsContext) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) GTFSPatternHopFactory(org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory) File(java.io.File)

Example 55 with GraphPath

use of org.opentripplanner.routing.spt.GraphPath in project OpenTripPlanner by opentripplanner.

the class NearbyStopFinder method stopAtDistanceForState.

/**
 * Given a State at a TransitStop, bundle the TransitStop together with information about how far away it is
 * and the geometry of the path leading up to the given State.
 *
 * TODO this should probably be merged with similar classes in Profile routing.
 */
public static StopAtDistance stopAtDistanceForState(State state) {
    double distance = 0.0;
    GraphPath graphPath = new GraphPath(state, false);
    CoordinateArrayListSequence coordinates = new CoordinateArrayListSequence();
    List<Edge> edges = new ArrayList<>();
    for (Edge edge : graphPath.edges) {
        if (edge instanceof StreetEdge) {
            LineString geometry = edge.getGeometry();
            if (geometry != null) {
                if (coordinates.size() == 0) {
                    coordinates.extend(geometry.getCoordinates());
                } else {
                    coordinates.extend(geometry.getCoordinates(), 1);
                }
            }
            distance += edge.getDistance();
        }
        edges.add(edge);
    }
    if (coordinates.size() < 2) {
        // Otherwise the walk step generator breaks.
        ArrayList<Coordinate> coordinateList = new ArrayList<Coordinate>(2);
        coordinateList.add(graphPath.states.get(1).getVertex().getCoordinate());
        State lastState = graphPath.states.getLast().getBackState();
        coordinateList.add(lastState.getVertex().getCoordinate());
        coordinates = new CoordinateArrayListSequence(coordinateList);
    }
    StopAtDistance sd = new StopAtDistance((TransitStop) state.getVertex(), distance);
    sd.geom = geometryFactory.createLineString(new PackedCoordinateSequence.Double(coordinates.toCoordinateArray()));
    sd.edges = edges;
    return sd;
}
Also used : GraphPath(org.opentripplanner.routing.spt.GraphPath) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) CoordinateArrayListSequence(org.opentripplanner.api.resource.CoordinateArrayListSequence) LineString(com.vividsolutions.jts.geom.LineString) Coordinate(com.vividsolutions.jts.geom.Coordinate) State(org.opentripplanner.routing.core.State) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge)

Aggregations

GraphPath (org.opentripplanner.routing.spt.GraphPath)76 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)56 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)47 Vertex (org.opentripplanner.routing.graph.Vertex)39 State (org.opentripplanner.routing.core.State)25 Test (org.junit.Test)18 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)18 IntersectionVertex (org.opentripplanner.routing.vertextype.IntersectionVertex)17 Edge (org.opentripplanner.routing.graph.Edge)15 Graph (org.opentripplanner.routing.graph.Graph)13 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)11 Stop (org.onebusaway.gtfs.model.Stop)10 HashSet (java.util.HashSet)9 Trip (org.onebusaway.gtfs.model.Trip)9 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)9 NonLocalizedString (org.opentripplanner.util.NonLocalizedString)8 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)6 File (java.io.File)5 TransitBoardAlight (org.opentripplanner.routing.edgetype.TransitBoardAlight)5 TemporaryStreetLocation (org.opentripplanner.routing.location.TemporaryStreetLocation)5