Search in sources :

Example 1 with BreadthFirstSearch

use of com.graphhopper.util.BreadthFirstSearch in project graphhopper by graphhopper.

the class GraphEdgeIdFinder method findEdgesInShape.

/**
 * This method fills the edgeIds hash with edgeIds found inside the specified shape
 */
public void findEdgesInShape(final GHIntHashSet edgeIds, final Shape shape, EdgeFilter filter) {
    GHPoint center = shape.getCenter();
    QueryResult qr = locationIndex.findClosest(center.getLat(), center.getLon(), filter);
    // TODO: if there is no street close to the center it'll fail although there are roads covered. Maybe we should check edge points or some random points in the Shape instead?
    if (!qr.isValid())
        throw new IllegalArgumentException("Shape " + shape + " does not cover graph");
    if (shape.contains(qr.getSnappedPoint().lat, qr.getSnappedPoint().lon))
        edgeIds.add(qr.getClosestEdge().getEdge());
    BreadthFirstSearch bfs = new BreadthFirstSearch() {

        final NodeAccess na = graph.getNodeAccess();

        final Shape localShape = shape;

        @Override
        protected boolean goFurther(int nodeId) {
            return localShape.contains(na.getLatitude(nodeId), na.getLongitude(nodeId));
        }

        @Override
        protected boolean checkAdjacent(EdgeIteratorState edge) {
            if (localShape.contains(na.getLatitude(edge.getAdjNode()), na.getLongitude(edge.getAdjNode()))) {
                edgeIds.add(edge.getEdge());
                return true;
            }
            return false;
        }
    };
    bfs.start(graph.createEdgeExplorer(filter), qr.getClosestNode());
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) Shape(com.graphhopper.util.shapes.Shape) BreadthFirstSearch(com.graphhopper.util.BreadthFirstSearch) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 2 with BreadthFirstSearch

use of com.graphhopper.util.BreadthFirstSearch in project graphhopper by graphhopper.

the class Location2IDQuadtree method findClosest.

@Override
public QueryResult findClosest(final double queryLat, final double queryLon, final EdgeFilter edgeFilter) {
    if (isClosed())
        throw new IllegalStateException("You need to create a new LocationIndex instance as it is already closed");
    if (edgeFilter != EdgeFilter.ALL_EDGES)
        throw new UnsupportedOperationException("edge filters are not yet implemented for " + Location2IDQuadtree.class.getSimpleName());
    // The following cases (e.g. dead ends or motorways crossing a normal way) could be problematic:
    // |     |
    // |  P  |
    // |  |  |< --- maxRasterWidth reached
    // \-----/
    /*
         * Problem: use additionally the 8 surrounding quadrants: There an error due to the raster
         * width. Because this index does not cover 100% of the graph you'll need to traverse the
         * graph until you find the real matching point or if you reach the raster width limit. And
         * there is a problem when using the raster limit as 'not found' indication and if you have
         * arbitrary requests e.g. from other systems (where points do not match exactly): Although
         * P is the closest point to the request one it could be that the raster limit is too short
         * to reach it via graph traversal:
         */
    long key = keyAlgo.encode(queryLat, queryLon);
    final int id = index.getInt(key * 4);
    double mainLat = nodeAccess.getLatitude(id);
    double mainLon = nodeAccess.getLongitude(id);
    final QueryResult res = new QueryResult(queryLat, queryLon);
    res.setClosestNode(id);
    res.setQueryDistance(distCalc.calcNormalizedDist(queryLat, queryLon, mainLat, mainLon));
    goFurtherHook(id);
    new BreadthFirstSearch() {

        @Override
        protected GHBitSet createBitSet() {
            return new GHTBitSet(10);
        }

        @Override
        protected boolean goFurther(int baseNode) {
            if (baseNode == id)
                return true;
            goFurtherHook(baseNode);
            double currLat = nodeAccess.getLatitude(baseNode);
            double currLon = nodeAccess.getLongitude(baseNode);
            double currNormedDist = distCalc.calcNormalizedDist(queryLat, queryLon, currLat, currLon);
            if (currNormedDist < res.getQueryDistance()) {
                res.setQueryDistance(currNormedDist);
                res.setClosestNode(baseNode);
                return true;
            }
            return currNormedDist < maxRasterWidth2InMeterNormed;
        }
    }.start(graph.createEdgeExplorer(), id);
    // denormalize distance
    res.setQueryDistance(distCalc.calcDenormalizedDist(res.getQueryDistance()));
    return res;
}
Also used : GHTBitSet(com.graphhopper.coll.GHTBitSet) BreadthFirstSearch(com.graphhopper.util.BreadthFirstSearch) GHBitSet(com.graphhopper.coll.GHBitSet) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 3 with BreadthFirstSearch

use of com.graphhopper.util.BreadthFirstSearch in project graphhopper by graphhopper.

the class AbstractEdgeElevationInterpolator method gatherOuterAndInnerNodeIds.

public void gatherOuterAndInnerNodeIds(final EdgeExplorer edgeExplorer, final EdgeIteratorState interpolatableEdge, final GHBitSet visitedEdgesIds, final IntSet outerNodeIds, final GHIntHashSet innerNodeIds) {
    final BreadthFirstSearch gatherOuterAndInnerNodeIdsSearch = new BreadthFirstSearch() {

        @Override
        protected boolean checkAdjacent(EdgeIteratorState edge) {
            visitedEdgesIds.add(edge.getEdge());
            final int baseNodeId = edge.getBaseNode();
            boolean isInterpolatableEdge = isInterpolatableEdge(edge);
            if (!isInterpolatableEdge) {
                innerNodeIds.remove(baseNodeId);
                outerNodeIds.add(baseNodeId);
            } else if (!outerNodeIds.contains(baseNodeId)) {
                innerNodeIds.add(baseNodeId);
            }
            return isInterpolatableEdge;
        }
    };
    gatherOuterAndInnerNodeIdsSearch.start(edgeExplorer, interpolatableEdge.getBaseNode());
}
Also used : BreadthFirstSearch(com.graphhopper.util.BreadthFirstSearch) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState)

Aggregations

BreadthFirstSearch (com.graphhopper.util.BreadthFirstSearch)3 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)2 GHPoint (com.graphhopper.util.shapes.GHPoint)2 GHBitSet (com.graphhopper.coll.GHBitSet)1 GHTBitSet (com.graphhopper.coll.GHTBitSet)1 QueryResult (com.graphhopper.storage.index.QueryResult)1 Shape (com.graphhopper.util.shapes.Shape)1