Search in sources :

Example 61 with Edge

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

the class StreetMatcher method toEdgeList.

private List<Edge> toEdgeList(MatchState next) {
    ArrayList<Edge> edges = new ArrayList<Edge>();
    Edge lastEdge = null;
    while (next != null) {
        Edge edge = next.getEdge();
        if (edge != lastEdge) {
            edges.add(edge);
            lastEdge = edge;
        }
        next = next.parent;
    }
    Collections.reverse(edges);
    return edges;
}
Also used : ArrayList(java.util.ArrayList) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge)

Example 62 with Edge

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

the class ElevationModule method assignMissingElevations.

/**
 * Assign missing elevations by interpolating from nearby points with known
 * elevation; also handle osm ele tags
 */
private void assignMissingElevations(Graph graph, List<StreetEdge> edgesWithElevation, HashMap<Vertex, Double> knownElevations) {
    log.debug("Assigning missing elevations");
    BinHeap<ElevationRepairState> pq = new BinHeap<ElevationRepairState>();
    // elevation for each vertex (known or interpolated)
    // knownElevations will be null if there are no ElevationPoints in the data
    // for instance, with the Shapefile loader.)
    HashMap<Vertex, Double> elevations;
    if (knownElevations != null)
        elevations = (HashMap<Vertex, Double>) knownElevations.clone();
    else
        elevations = new HashMap<Vertex, Double>();
    HashSet<Vertex> closed = new HashSet<Vertex>();
    // initialize queue with all vertices which already have known elevation
    for (StreetEdge e : edgesWithElevation) {
        PackedCoordinateSequence profile = e.getElevationProfile();
        if (!elevations.containsKey(e.getFromVertex())) {
            double firstElevation = profile.getOrdinate(0, 1);
            ElevationRepairState state = new ElevationRepairState(null, null, e.getFromVertex(), 0, firstElevation);
            pq.insert(state, 0);
            elevations.put(e.getFromVertex(), firstElevation);
        }
        if (!elevations.containsKey(e.getToVertex())) {
            double lastElevation = profile.getOrdinate(profile.size() - 1, 1);
            ElevationRepairState state = new ElevationRepairState(null, null, e.getToVertex(), 0, lastElevation);
            pq.insert(state, 0);
            elevations.put(e.getToVertex(), lastElevation);
        }
    }
    // back pointers through the region of unknown elevation, setting elevations via interpolation.
    while (!pq.empty()) {
        ElevationRepairState state = pq.extract_min();
        if (closed.contains(state.vertex))
            continue;
        closed.add(state.vertex);
        ElevationRepairState curState = state;
        Vertex initialVertex = null;
        while (curState != null) {
            initialVertex = curState.vertex;
            curState = curState.backState;
        }
        double bestDistance = Double.MAX_VALUE;
        double bestElevation = 0;
        for (Edge e : state.vertex.getOutgoing()) {
            if (!(e instanceof StreetEdge)) {
                continue;
            }
            StreetEdge edge = (StreetEdge) e;
            Vertex tov = e.getToVertex();
            if (tov == initialVertex)
                continue;
            Double elevation = elevations.get(tov);
            if (elevation != null) {
                double distance = e.getDistance();
                if (distance < bestDistance) {
                    bestDistance = distance;
                    bestElevation = elevation;
                }
            } else {
                // continue
                ElevationRepairState newState = new ElevationRepairState(edge, state, tov, e.getDistance() + state.distance, state.initialElevation);
                pq.insert(newState, e.getDistance() + state.distance);
            }
        }
        for (Edge e : state.vertex.getIncoming()) {
            if (!(e instanceof StreetEdge)) {
                continue;
            }
            StreetEdge edge = (StreetEdge) e;
            Vertex fromv = e.getFromVertex();
            if (fromv == initialVertex)
                continue;
            Double elevation = elevations.get(fromv);
            if (elevation != null) {
                double distance = e.getDistance();
                if (distance < bestDistance) {
                    bestDistance = distance;
                    bestElevation = elevation;
                }
            } else {
                // continue
                ElevationRepairState newState = new ElevationRepairState(edge, state, fromv, e.getDistance() + state.distance, state.initialElevation);
                pq.insert(newState, e.getDistance() + state.distance);
            }
        }
        // in the case of islands missing elevation (and some other cases)
        if (bestDistance == Double.MAX_VALUE && state.distance > 2000) {
            log.warn("While propagating elevations, hit 2km distance limit at " + state.vertex);
            bestDistance = state.distance;
            bestElevation = state.initialElevation;
        }
        if (bestDistance != Double.MAX_VALUE) {
            // we have found a second vertex with elevation, so we can interpolate the elevation
            // for this point
            double totalDistance = bestDistance + state.distance;
            // trace backwards, setting states as we go
            while (true) {
                // all the way out to edge lengths
                if (totalDistance == 0)
                    elevations.put(state.vertex, bestElevation);
                else {
                    double elevation = (bestElevation * state.distance + state.initialElevation * bestDistance) / totalDistance;
                    elevations.put(state.vertex, elevation);
                }
                if (state.backState == null)
                    break;
                bestDistance += state.backEdge.getDistance();
                state = state.backState;
                if (elevations.containsKey(state.vertex))
                    break;
            }
        }
    }
    // do actual assignments
    for (Vertex v : graph.getVertices()) {
        Double fromElevation = elevations.get(v);
        for (Edge e : v.getOutgoing()) {
            if (e instanceof StreetWithElevationEdge) {
                StreetWithElevationEdge edge = ((StreetWithElevationEdge) e);
                Double toElevation = elevations.get(edge.getToVertex());
                if (fromElevation == null || toElevation == null) {
                    if (!edge.isElevationFlattened() && !edge.isSlopeOverride())
                        log.warn("Unexpectedly missing elevation for edge " + edge);
                    continue;
                }
                if (edge.getElevationProfile() != null && edge.getElevationProfile().size() > 2) {
                    continue;
                }
                Coordinate[] coords = new Coordinate[2];
                coords[0] = new Coordinate(0, fromElevation);
                coords[1] = new Coordinate(edge.getDistance(), toElevation);
                PackedCoordinateSequence profile = new PackedCoordinateSequence.Double(coords);
                if (edge.setElevationProfile(profile, true)) {
                    log.trace(graph.addBuilderAnnotation(new ElevationFlattened(edge)));
                }
            }
        }
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) HashMap(java.util.HashMap) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) BinHeap(org.opentripplanner.common.pqueue.BinHeap) StreetWithElevationEdge(org.opentripplanner.routing.edgetype.StreetWithElevationEdge) Coordinate(com.vividsolutions.jts.geom.Coordinate) ElevationFlattened(org.opentripplanner.graph_builder.annotation.ElevationFlattened) StreetWithElevationEdge(org.opentripplanner.routing.edgetype.StreetWithElevationEdge) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet) PackedCoordinateSequence(org.opentripplanner.common.geometry.PackedCoordinateSequence)

Example 63 with Edge

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

the class PlatformLinker method isEndpoint.

private boolean isEndpoint(OsmVertex ov) {
    boolean isCandidate = false;
    Vertex start = null;
    for (Edge e : ov.getIncoming()) {
        if (e instanceof StreetEdge && !(e instanceof AreaEdge)) {
            StreetEdge se = (StreetEdge) e;
            if (Arrays.asList(1, 2, 3).contains(se.getPermission().code)) {
                isCandidate = true;
                start = se.getFromVertex();
                break;
            }
        }
    }
    if (isCandidate && start != null) {
        boolean isEndpoint = true;
        for (Edge se : ov.getOutgoing()) {
            if (!se.getToVertex().getCoordinate().equals(start.getCoordinate()) && !(se instanceof AreaEdge)) {
                isEndpoint = false;
            }
        }
        return isEndpoint;
    }
    return false;
}
Also used : OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex) Vertex(org.opentripplanner.routing.graph.Vertex) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) AreaEdge(org.opentripplanner.routing.edgetype.AreaEdge) Edge(org.opentripplanner.routing.graph.Edge) AreaEdge(org.opentripplanner.routing.edgetype.AreaEdge)

Example 64 with Edge

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

the class UnconnectedStop method fulfillDemands.

/**
 * @retrun return true if the stop is not connected to any street
 */
@Override
public boolean fulfillDemands(TransitStop ts, Graph graph) {
    List<Edge> outgoingStreets = ts.getOutgoingStreetEdges();
    boolean hasStreetLink = false;
    for (Edge e : ts.getIncoming()) {
        if (e instanceof StreetTransitLink || e instanceof PathwayEdge) {
            hasStreetLink = true;
            break;
        }
    }
    if (!hasStreetLink) {
        // TODO: see what if there is incoming and not outgoing
        for (Edge e : ts.getOutgoing()) {
            if (e instanceof StreetTransitLink) {
                hasStreetLink = true;
                break;
            }
        }
    }
    return !(hasStreetLink || (outgoingStreets.size() > 0));
}
Also used : StreetTransitLink(org.opentripplanner.routing.edgetype.StreetTransitLink) PathwayEdge(org.opentripplanner.routing.edgetype.PathwayEdge) PathwayEdge(org.opentripplanner.routing.edgetype.PathwayEdge) Edge(org.opentripplanner.routing.graph.Edge)

Example 65 with Edge

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

the class InterleavedBidirectionalHeuristic method doSomeWork.

/**
 * Move backward N steps through the transit network.
 * This improves the heuristic's knowledge of the transit network as seen from the target,
 * making its lower bounds on path weight progressively more accurate.
 */
@Override
public void doSomeWork() {
    if (finished)
        return;
    for (int i = 0; i < HEURISTIC_STEPS_PER_MAIN_STEP; ++i) {
        if (transitQueue.empty()) {
            finished = true;
            break;
        }
        int uWeight = (int) transitQueue.peek_min_key();
        Vertex u = transitQueue.extract_min();
        // The weight of the queue head is uniformly increasing.
        // This is the highest weight ever seen for a closed vertex.
        maxWeightSeen = uWeight;
        // Now that this vertex is closed, we can store its weight for use as a lower bound / heuristic value.
        // We don't implement decrease-key operations though, so check whether a smaller value is already known.
        double uWeightOld = postBoardingWeights.get(u);
        if (uWeight < uWeightOld) {
            // Including when uWeightOld is infinite because the vertex is not yet closed.
            postBoardingWeights.put(u, uWeight);
        } else {
            // The vertex was already closed. This time it necessarily has a higher weight, so skip it.
            continue;
        }
        // When the main search is arriveBy the heuristic search looks at OUTgoing edges.
        for (Edge e : routingRequest.arriveBy ? u.getOutgoing() : u.getIncoming()) {
            // Do not enter streets in this phase, which should only touch transit.
            if (e instanceof StreetTransitLink) {
                continue;
            }
            Vertex v = routingRequest.arriveBy ? e.getToVertex() : e.getFromVertex();
            double edgeWeight = e.weightLowerBound(routingRequest);
            // this saves time by not reverse-exploring those routes and avoids maxFound of INF.
            if (Double.isInfinite(edgeWeight)) {
                continue;
            }
            double vWeight = uWeight + edgeWeight;
            double vWeightOld = postBoardingWeights.get(v);
            if (vWeight < vWeightOld) {
                // Should only happen when vWeightOld is infinite because it is not yet closed.
                transitQueue.insert(v, vWeight);
            }
        }
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) StreetTransitLink(org.opentripplanner.routing.edgetype.StreetTransitLink) 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