Search in sources :

Example 21 with QueryResult

use of com.graphhopper.storage.index.QueryResult 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 22 with QueryResult

use of com.graphhopper.storage.index.QueryResult in project PocketMaps by junjunguo.

the class NaviEngine method findClosestStreet.

private GeoPoint findClosestStreet(GeoPoint fromPos) {
    QueryResult pos = MapHandler.getMapHandler().getHopper().getLocationIndex().findClosest(fromPos.getLatitude(), fromPos.getLongitude(), EdgeFilter.ALL_EDGES);
    int n = pos.getClosestEdge().getBaseNode();
    NodeAccess nodeAccess = MapHandler.getMapHandler().getHopper().getGraphHopperStorage().getNodeAccess();
    GeoPoint gp = new GeoPoint(nodeAccess.getLat(n), nodeAccess.getLon(n));
    return gp;
}
Also used : GeoPoint(org.oscim.core.GeoPoint) QueryResult(com.graphhopper.storage.index.QueryResult) NodeAccess(com.graphhopper.storage.NodeAccess) GeoPoint(org.oscim.core.GeoPoint)

Example 23 with QueryResult

use of com.graphhopper.storage.index.QueryResult in project graphhopper by graphhopper.

the class QueryGraphTest method testGetEdgeProps.

@Test
public void testGetEdgeProps() {
    initGraph(g);
    EdgeIteratorState e1 = GHUtility.getEdge(g, 0, 2);
    QueryGraph queryGraph = new QueryGraph(g);
    QueryResult res1 = createLocationResult(0.5, 0, e1, 0, EDGE);
    queryGraph.lookup(Arrays.asList(res1));
    // get virtual edge
    e1 = GHUtility.getEdge(queryGraph, res1.getClosestNode(), 0);
    EdgeIteratorState e2 = queryGraph.getEdgeIteratorState(e1.getEdge(), Integer.MIN_VALUE);
    assertEquals(e1.getEdge(), e2.getEdge());
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) Test(org.junit.Test)

Example 24 with QueryResult

use of com.graphhopper.storage.index.QueryResult in project graphhopper by graphhopper.

the class QueryGraphTest method testInternalAPIOriginalTraversalKey.

@Test
public void testInternalAPIOriginalTraversalKey() {
    initGraph(g);
    EdgeExplorer explorer = g.createEdgeExplorer();
    QueryGraph queryGraph = new QueryGraph(g);
    EdgeIterator iter = explorer.setBaseNode(1);
    assertTrue(iter.next());
    int origEdgeId = iter.getEdge();
    QueryResult res = createLocationResult(2, 1.5, iter, 1, PILLAR);
    queryGraph.lookup(Arrays.asList(res));
    assertEquals(new GHPoint(1.5, 1.5), res.getSnappedPoint());
    assertEquals(3, res.getClosestNode());
    EdgeExplorer qGraphExplorer = queryGraph.createEdgeExplorer();
    iter = qGraphExplorer.setBaseNode(3);
    assertTrue(iter.next());
    assertEquals(0, iter.getAdjNode());
    assertEquals(GHUtility.createEdgeKey(1, 0, origEdgeId, false), ((VirtualEdgeIteratorState) queryGraph.getEdgeIteratorState(iter.getEdge(), 0)).getOriginalTraversalKey());
    assertTrue(iter.next());
    assertEquals(1, iter.getAdjNode());
    assertEquals(GHUtility.createEdgeKey(0, 1, origEdgeId, false), ((VirtualEdgeIteratorState) queryGraph.getEdgeIteratorState(iter.getEdge(), 1)).getOriginalTraversalKey());
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Example 25 with QueryResult

use of com.graphhopper.storage.index.QueryResult in project graphhopper by graphhopper.

the class QueryGraphTest method useEECache.

@Test
public void useEECache() {
    initGraph(g);
    EdgeExplorer explorer = g.createEdgeExplorer();
    EdgeIterator iter = explorer.setBaseNode(1);
    assertTrue(iter.next());
    QueryResult res = createLocationResult(2, 1.5, iter, 1, PILLAR);
    QueryGraph queryGraph = new QueryGraph(g).setUseEdgeExplorerCache(true);
    queryGraph.lookup(Arrays.asList(res));
    EdgeExplorer edgeExplorer = queryGraph.createEdgeExplorer();
    // using cache means same reference
    assertTrue(edgeExplorer == queryGraph.createEdgeExplorer());
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) Test(org.junit.Test)

Aggregations

QueryResult (com.graphhopper.storage.index.QueryResult)39 GHPoint (com.graphhopper.util.shapes.GHPoint)20 Test (org.junit.Test)20 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)4 LocationIndex (com.graphhopper.storage.index.LocationIndex)4 Weighting (com.graphhopper.routing.weighting.Weighting)3 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)3 GHRequest (com.graphhopper.GHRequest)2 GHResponse (com.graphhopper.GHResponse)2 EdgeFilter (com.graphhopper.routing.util.EdgeFilter)2 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)2 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)2 GHPoint3D (com.graphhopper.util.shapes.GHPoint3D)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 IntObjectMap (com.carrotsearch.hppc.IntObjectMap)1 Stop (com.conveyal.gtfs.model.Stop)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 GraphHopper (com.graphhopper.GraphHopper)1 GHIntObjectHashMap (com.graphhopper.coll.GHIntObjectHashMap)1