Search in sources :

Example 1 with TraverseMode

use of org.opentripplanner.routing.core.TraverseMode in project OpenTripPlanner by opentripplanner.

the class SimpleStreetSplitter method getClosestVertex.

/**
 * Used to link origin and destination points to graph non destructively.
 *
 * Split edges don't replace existing ones and only temporary edges and vertices are created.
 *
 * Will throw ThrivialPathException if origin and destination Location are on the same edge
 *
 * @param location
 * @param options
 * @param endVertex true if this is destination vertex
 * @return
 */
public Vertex getClosestVertex(GenericLocation location, RoutingRequest options, boolean endVertex) {
    if (destructiveSplitting) {
        throw new RuntimeException("Origin and destination search is used with destructive splitting. Something is wrong!");
    }
    if (endVertex) {
        LOG.debug("Finding end vertex for {}", location);
    } else {
        LOG.debug("Finding start vertex for {}", location);
    }
    Coordinate coord = location.getCoordinate();
    // TODO: add nice name
    String name;
    if (location.name == null || location.name.isEmpty()) {
        if (endVertex) {
            name = "Destination";
        } else {
            name = "Origin";
        }
    } else {
        name = location.name;
    }
    TemporaryStreetLocation closest = new TemporaryStreetLocation(UUID.randomUUID().toString(), coord, new NonLocalizedString(name), endVertex);
    TraverseMode nonTransitMode = TraverseMode.WALK;
    // It can be null in tests
    if (options != null) {
        TraverseModeSet modes = options.modes;
        if (modes.getCar())
            // for park and ride we will start in car mode and walk to the end vertex
            if (endVertex && (options.parkAndRide || options.kissAndRide)) {
                nonTransitMode = TraverseMode.WALK;
            } else {
                nonTransitMode = TraverseMode.CAR;
            }
        else if (modes.getWalk())
            nonTransitMode = TraverseMode.WALK;
        else if (modes.getBicycle())
            nonTransitMode = TraverseMode.BICYCLE;
    }
    if (!link(closest, nonTransitMode, options)) {
        LOG.warn("Couldn't link {}", location);
    }
    return closest;
}
Also used : TemporaryStreetLocation(org.opentripplanner.routing.location.TemporaryStreetLocation) Coordinate(com.vividsolutions.jts.geom.Coordinate) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) TraverseMode(org.opentripplanner.routing.core.TraverseMode) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) LineString(com.vividsolutions.jts.geom.LineString) NonLocalizedString(org.opentripplanner.util.NonLocalizedString)

Example 2 with TraverseMode

use of org.opentripplanner.routing.core.TraverseMode in project OpenTripPlanner by opentripplanner.

the class SimpleStreetSplitter method link.

/**
 * Link this vertex into the graph
 */
public boolean link(Vertex vertex, TraverseMode traverseMode, RoutingRequest options) {
    // find nearby street edges
    // TODO: we used to use an expanding-envelope search, which is more efficient in
    // dense areas. but first let's see how inefficient this is. I suspect it's not too
    // bad and the gains in simplicity are considerable.
    final double radiusDeg = SphericalDistanceLibrary.metersToDegrees(MAX_SEARCH_RADIUS_METERS);
    Envelope env = new Envelope(vertex.getCoordinate());
    // Perform a simple local equirectangular projection, so distances are expressed in degrees latitude.
    final double xscale = Math.cos(vertex.getLat() * Math.PI / 180);
    // Expand more in the longitude direction than the latitude direction to account for converging meridians.
    env.expandBy(radiusDeg / xscale, radiusDeg);
    final double DUPLICATE_WAY_EPSILON_DEGREES = SphericalDistanceLibrary.metersToDegrees(DUPLICATE_WAY_EPSILON_METERS);
    final TraverseModeSet traverseModeSet;
    if (traverseMode == TraverseMode.BICYCLE) {
        traverseModeSet = new TraverseModeSet(traverseMode, TraverseMode.WALK);
    } else {
        traverseModeSet = new TraverseModeSet(traverseMode);
    }
    // We sort the list of candidate edges by distance to the stop
    // This should remove any issues with things coming out of the spatial index in different orders
    // Then we link to everything that is within DUPLICATE_WAY_EPSILON_METERS of of the best distance
    // so that we capture back edges and duplicate ways.
    List<StreetEdge> candidateEdges = idx.query(env).stream().filter(streetEdge -> streetEdge instanceof StreetEdge).map(edge -> (StreetEdge) edge).filter(edge -> edge.canTraverse(traverseModeSet) && // only link to edges still in the graph.
    edge.getToVertex().getIncoming().contains(edge)).collect(Collectors.toList());
    // Make a map of distances to all edges.
    final TIntDoubleMap distances = new TIntDoubleHashMap();
    for (StreetEdge e : candidateEdges) {
        distances.put(e.getId(), distance(vertex, e, xscale));
    }
    // Sort the list.
    Collections.sort(candidateEdges, (o1, o2) -> {
        double diff = distances.get(o1.getId()) - distances.get(o2.getId());
        // A Comparator must return an integer but our distances are doubles.
        if (diff < 0)
            return -1;
        if (diff > 0)
            return 1;
        return 0;
    });
    // find the closest candidate edges
    if (candidateEdges.isEmpty() || distances.get(candidateEdges.get(0).getId()) > radiusDeg) {
        // We only link to stops if we are searching for origin/destination and for that we need transitStopIndex.
        if (destructiveSplitting || transitStopIndex == null) {
            return false;
        }
        LOG.debug("No street edge was found for {}", vertex);
        // We search for closest stops (since this is only used in origin/destination linking if no edges were found)
        // in the same way the closest edges are found.
        List<TransitStop> candidateStops = new ArrayList<>();
        transitStopIndex.query(env).forEach(candidateStop -> candidateStops.add((TransitStop) candidateStop));
        final TIntDoubleMap stopDistances = new TIntDoubleHashMap();
        for (TransitStop t : candidateStops) {
            stopDistances.put(t.getIndex(), distance(vertex, t, xscale));
        }
        Collections.sort(candidateStops, (o1, o2) -> {
            double diff = stopDistances.get(o1.getIndex()) - stopDistances.get(o2.getIndex());
            if (diff < 0) {
                return -1;
            }
            if (diff > 0) {
                return 1;
            }
            return 0;
        });
        if (candidateStops.isEmpty() || stopDistances.get(candidateStops.get(0).getIndex()) > radiusDeg) {
            LOG.debug("Stops aren't close either!");
            return false;
        } else {
            List<TransitStop> bestStops = Lists.newArrayList();
            // Add stops until there is a break of epsilon meters.
            // we do this to enforce determinism. if there are a lot of stops that are all extremely close to each other,
            // we want to be sure that we deterministically link to the same ones every time. Any hard cutoff means things can
            // fall just inside or beyond the cutoff depending on floating-point operations.
            int i = 0;
            do {
                bestStops.add(candidateStops.get(i++));
            } while (i < candidateStops.size() && stopDistances.get(candidateStops.get(i).getIndex()) - stopDistances.get(candidateStops.get(i - 1).getIndex()) < DUPLICATE_WAY_EPSILON_DEGREES);
            for (TransitStop stop : bestStops) {
                LOG.debug("Linking vertex to stop: {}", stop.getName());
                makeTemporaryEdges((TemporaryStreetLocation) vertex, stop);
            }
            return true;
        }
    } else {
        // find the best edges
        List<StreetEdge> bestEdges = Lists.newArrayList();
        // add edges until there is a break of epsilon meters.
        // we do this to enforce determinism. if there are a lot of edges that are all extremely close to each other,
        // we want to be sure that we deterministically link to the same ones every time. Any hard cutoff means things can
        // fall just inside or beyond the cutoff depending on floating-point operations.
        int i = 0;
        do {
            bestEdges.add(candidateEdges.get(i++));
        } while (i < candidateEdges.size() && distances.get(candidateEdges.get(i).getId()) - distances.get(candidateEdges.get(i - 1).getId()) < DUPLICATE_WAY_EPSILON_DEGREES);
        for (StreetEdge edge : bestEdges) {
            link(vertex, edge, xscale, options);
        }
        // Warn if a linkage was made, but the linkage was suspiciously long.
        if (vertex instanceof TransitStop) {
            double distanceDegreesLatitude = distances.get(candidateEdges.get(0).getId());
            int distanceMeters = (int) SphericalDistanceLibrary.degreesLatitudeToMeters(distanceDegreesLatitude);
            if (distanceMeters > WARNING_DISTANCE_METERS) {
                // Registering an annotation but not logging because tests produce thousands of these warnings.
                graph.addBuilderAnnotation(new StopLinkedTooFar((TransitStop) vertex, distanceMeters));
            }
        }
        return true;
    }
}
Also used : P2(org.opentripplanner.common.model.P2) Iterables(com.google.common.collect.Iterables) TemporarySplitterVertex(org.opentripplanner.routing.vertextype.TemporarySplitterVertex) TemporaryFreeEdge(org.opentripplanner.routing.edgetype.TemporaryFreeEdge) TemporaryVertex(org.opentripplanner.routing.vertextype.TemporaryVertex) LocationIndexedLine(com.vividsolutions.jts.linearref.LocationIndexedLine) LoggerFactory(org.slf4j.LoggerFactory) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) SphericalDistanceLibrary(org.opentripplanner.common.geometry.SphericalDistanceLibrary) TemporaryStreetLocation(org.opentripplanner.routing.location.TemporaryStreetLocation) LineString(com.vividsolutions.jts.geom.LineString) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) StopLinkedTooFar(org.opentripplanner.graph_builder.annotation.StopLinkedTooFar) SplitterVertex(org.opentripplanner.routing.vertextype.SplitterVertex) ArrayList(java.util.ArrayList) StopUnlinked(org.opentripplanner.graph_builder.annotation.StopUnlinked) StreetTransitLink(org.opentripplanner.routing.edgetype.StreetTransitLink) Graph(org.opentripplanner.routing.graph.Graph) BikeRentalStationVertex(org.opentripplanner.routing.vertextype.BikeRentalStationVertex) GenericLocation(org.opentripplanner.common.model.GenericLocation) SpatialIndex(com.vividsolutions.jts.index.SpatialIndex) TraverseMode(org.opentripplanner.routing.core.TraverseMode) Lists(jersey.repackaged.com.google.common.collect.Lists) HashGridSpatialIndex(org.opentripplanner.common.geometry.HashGridSpatialIndex) Coordinate(com.vividsolutions.jts.geom.Coordinate) Logger(org.slf4j.Logger) Envelope(com.vividsolutions.jts.geom.Envelope) Vertex(org.opentripplanner.routing.graph.Vertex) StreetBikeRentalLink(org.opentripplanner.routing.edgetype.StreetBikeRentalLink) TIntDoubleHashMap(gnu.trove.map.hash.TIntDoubleHashMap) BikeParkUnlinked(org.opentripplanner.graph_builder.annotation.BikeParkUnlinked) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) StreetBikeParkLink(org.opentripplanner.routing.edgetype.StreetBikeParkLink) StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) GeometryUtils(org.opentripplanner.common.geometry.GeometryUtils) List(java.util.List) TIntDoubleMap(gnu.trove.map.TIntDoubleMap) BikeRentalStationUnlinked(org.opentripplanner.graph_builder.annotation.BikeRentalStationUnlinked) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) LinearLocation(com.vividsolutions.jts.linearref.LinearLocation) BikeParkVertex(org.opentripplanner.routing.vertextype.BikeParkVertex) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) Collections(java.util.Collections) Edge(org.opentripplanner.routing.graph.Edge) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) TIntDoubleMap(gnu.trove.map.TIntDoubleMap) ArrayList(java.util.ArrayList) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) StopLinkedTooFar(org.opentripplanner.graph_builder.annotation.StopLinkedTooFar) Envelope(com.vividsolutions.jts.geom.Envelope) TIntDoubleHashMap(gnu.trove.map.hash.TIntDoubleHashMap)

Example 3 with TraverseMode

use of org.opentripplanner.routing.core.TraverseMode in project OpenTripPlanner by opentripplanner.

the class SimpleConcreteEdge method traverse.

@Override
public State traverse(State s0) {
    double d = getDistance();
    TraverseMode mode = s0.getNonTransitMode();
    int t = (int) (d / s0.getOptions().getSpeed(mode));
    StateEditor s1 = s0.edit(this);
    s1.incrementTimeInSeconds(t);
    s1.incrementWeight(d);
    return s1.makeState();
}
Also used : StateEditor(org.opentripplanner.routing.core.StateEditor) TraverseMode(org.opentripplanner.routing.core.TraverseMode)

Example 4 with TraverseMode

use of org.opentripplanner.routing.core.TraverseMode 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 5 with TraverseMode

use of org.opentripplanner.routing.core.TraverseMode in project OpenTripPlanner by opentripplanner.

the class ElevatorHopEdge method traverse.

@Override
public State traverse(State s0) {
    RoutingRequest options = s0.getOptions();
    if (options.wheelchairAccessible && !wheelchairAccessible) {
        return null;
    }
    TraverseMode mode = s0.getNonTransitMode();
    if (mode == TraverseMode.WALK && !permission.allows(StreetTraversalPermission.PEDESTRIAN)) {
        return null;
    }
    if (mode == TraverseMode.BICYCLE && !permission.allows(StreetTraversalPermission.BICYCLE)) {
        return null;
    }
    // there are elevators which allow cars
    if (mode == TraverseMode.CAR && !permission.allows(StreetTraversalPermission.CAR)) {
        return null;
    }
    StateEditor s1 = s0.edit(this);
    s1.setBackMode(TraverseMode.WALK);
    s1.incrementWeight(options.elevatorHopCost);
    s1.incrementTimeInSeconds(options.elevatorHopTime);
    return s1.makeState();
}
Also used : StateEditor(org.opentripplanner.routing.core.StateEditor) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) TraverseMode(org.opentripplanner.routing.core.TraverseMode)

Aggregations

TraverseMode (org.opentripplanner.routing.core.TraverseMode)13 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)5 Edge (org.opentripplanner.routing.graph.Edge)5 LineString (com.vividsolutions.jts.geom.LineString)4 StateEditor (org.opentripplanner.routing.core.StateEditor)4 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)4 Coordinate (com.vividsolutions.jts.geom.Coordinate)3 ArrayList (java.util.ArrayList)3 State (org.opentripplanner.routing.core.State)3 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)3 Vertex (org.opentripplanner.routing.graph.Vertex)3 HashSet (java.util.HashSet)2 List (java.util.List)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 GenericLocation (org.opentripplanner.common.model.GenericLocation)2 TemporaryStreetLocation (org.opentripplanner.routing.location.TemporaryStreetLocation)2 NonLocalizedString (org.opentripplanner.util.NonLocalizedString)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Iterables (com.google.common.collect.Iterables)1 Envelope (com.vividsolutions.jts.geom.Envelope)1