Search in sources :

Example 16 with Snap

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

the class QueryGraphTest method testWayGeometry_edge.

@Test
public void testWayGeometry_edge() {
    // drawn as horizontal linear graph for simplicity
    // 0 - * - x - * - 1
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 0, 0);
    na.setNode(1, 0.3, 0.3);
    GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(10)).setWayGeometry(Helper.createPointList(0.1, 0.1, 0.2, 0.2));
    LocationIndexTree locationIndex = new LocationIndexTree(g, new RAMDirectory());
    locationIndex.prepareIndex();
    Snap snap = locationIndex.findClosest(0.15, 0.15, AccessFilter.allEdges(encoder.getAccessEnc()));
    assertTrue(snap.isValid());
    assertEquals(EDGE, snap.getSnappedPosition(), "this test was supposed to test the Position.EDGE case");
    QueryGraph queryGraph = lookup(snap);
    EdgeIterator iter = queryGraph.createEdgeExplorer().setBaseNode(snap.getClosestNode());
    assertTrue(iter.next());
    assertEquals(0, iter.getAdjNode());
    assertEquals(1, iter.fetchWayGeometry(FetchMode.PILLAR_ONLY).size());
    assertEquals(2, iter.fetchWayGeometry(FetchMode.BASE_AND_PILLAR).size());
    assertEquals(2, iter.fetchWayGeometry(FetchMode.PILLAR_AND_ADJ).size());
    assertEquals(3, iter.fetchWayGeometry(FetchMode.ALL).size());
    assertEquals(Helper.createPointList(0.15, 0.15, 0.1, 0.1, 0.0, 0.0), iter.fetchWayGeometry(FetchMode.ALL));
    assertTrue(iter.next());
    assertEquals(1, iter.getAdjNode());
    assertEquals(1, iter.fetchWayGeometry(FetchMode.PILLAR_ONLY).size());
    assertEquals(2, iter.fetchWayGeometry(FetchMode.BASE_AND_PILLAR).size());
    assertEquals(2, iter.fetchWayGeometry(FetchMode.PILLAR_AND_ADJ).size());
    assertEquals(3, iter.fetchWayGeometry(FetchMode.ALL).size());
    assertEquals(Helper.createPointList(0.15, 0.15, 0.2, 0.2, 0.3, 0.3), iter.fetchWayGeometry(FetchMode.ALL));
    assertFalse(iter.next());
}
Also used : Snap(com.graphhopper.storage.index.Snap) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Test(org.junit.jupiter.api.Test)

Example 17 with Snap

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

the class QueryGraphTest method testIteration_Issue163.

@Test
public void testIteration_Issue163() {
    EdgeFilter outEdgeFilter = AccessFilter.outEdges(encodingManager.getEncoder("car").getAccessEnc());
    EdgeFilter inEdgeFilter = AccessFilter.inEdges(encodingManager.getEncoder("car").getAccessEnc());
    EdgeExplorer inExplorer = g.createEdgeExplorer(inEdgeFilter);
    EdgeExplorer outExplorer = g.createEdgeExplorer(outEdgeFilter);
    int nodeA = 0;
    int nodeB = 1;
    /* init test graph: one directional edge going from A to B, via virtual nodes C and D
         *
         *   (C)-(D)
         *  /       \
         * A         B
         */
    g.getNodeAccess().setNode(nodeA, 1, 0);
    g.getNodeAccess().setNode(nodeB, 1, 10);
    GHUtility.setSpeed(60, true, false, encoder, g.edge(nodeA, nodeB).setDistance(10)).setWayGeometry(Helper.createPointList(1.5, 3, 1.5, 7));
    // assert the behavior for classic edgeIterator
    assertEdgeIdsStayingEqual(inExplorer, outExplorer, nodeA, nodeB);
    // setup snaps
    EdgeIteratorState it = GHUtility.getEdge(g, nodeA, nodeB);
    Snap snap1 = createLocationResult(1.5, 3, it, 1, Snap.Position.EDGE);
    Snap snap2 = createLocationResult(1.5, 7, it, 2, Snap.Position.EDGE);
    QueryGraph q = lookup(Arrays.asList(snap1, snap2));
    int nodeC = snap1.getClosestNode();
    int nodeD = snap2.getClosestNode();
    inExplorer = q.createEdgeExplorer(inEdgeFilter);
    outExplorer = q.createEdgeExplorer(outEdgeFilter);
    // assert the same behavior for queryGraph
    assertEdgeIdsStayingEqual(inExplorer, outExplorer, nodeA, nodeC);
    assertEdgeIdsStayingEqual(inExplorer, outExplorer, nodeC, nodeD);
    assertEdgeIdsStayingEqual(inExplorer, outExplorer, nodeD, nodeB);
}
Also used : Snap(com.graphhopper.storage.index.Snap) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.jupiter.api.Test)

Example 18 with Snap

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

the class QueryGraphTest method testVirtualEdgeIds.

@Test
public void testVirtualEdgeIds() {
    // virtual nodes:     2
    // 0 - x - 1
    // virtual edges:   1   2
    FlagEncoder encoder = new CarFlagEncoder(new PMap().putObject("speed_two_directions", true));
    EncodingManager encodingManager = EncodingManager.create(encoder);
    DecimalEncodedValue speedEnc = encoder.getAverageSpeedEnc();
    Graph g = new GraphBuilder(encodingManager).create();
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 50.00, 10.10);
    na.setNode(1, 50.00, 10.20);
    double dist = DistanceCalcEarth.DIST_EARTH.calcDist(na.getLat(0), na.getLon(0), na.getLat(1), na.getLon(1));
    EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder, g.edge(0, 1).setDistance(dist));
    edge.set(speedEnc, 50);
    edge.setReverse(speedEnc, 100);
    // query graph
    Snap snap = createLocationResult(50.00, 10.15, edge, 0, EDGE);
    QueryGraph queryGraph = QueryGraph.create(g, snap);
    assertEquals(3, queryGraph.getNodes());
    assertEquals(5, queryGraph.getEdges());
    assertEquals(4, queryGraph.getVirtualEdges().size());
    EdgeIteratorState edge_0x = queryGraph.getEdgeIteratorState(1, 2);
    EdgeIteratorState edge_x0 = queryGraph.getEdgeIteratorState(1, 0);
    EdgeIteratorState edge_x1 = queryGraph.getEdgeIteratorState(2, 1);
    EdgeIteratorState edge_1x = queryGraph.getEdgeIteratorState(2, 2);
    assertNodes(edge_0x, 0, 2);
    assertNodes(edge_x0, 2, 0);
    assertNodes(edge_x1, 2, 1);
    assertNodes(edge_1x, 1, 2);
    // virtual edge IDs are 1 and 2
    assertEquals(1, edge_0x.getEdge());
    assertEquals(1, edge_x0.getEdge());
    assertEquals(2, edge_x1.getEdge());
    assertEquals(2, edge_1x.getEdge());
    // edge keys
    assertEquals(2, edge_0x.getEdgeKey());
    assertEquals(3, edge_x0.getEdgeKey());
    assertEquals(4, edge_x1.getEdgeKey());
    assertEquals(5, edge_1x.getEdgeKey());
    assertNodes(queryGraph.getEdgeIteratorStateForKey(2), 0, 2);
    assertNodes(queryGraph.getEdgeIteratorStateForKey(3), 2, 0);
    assertNodes(queryGraph.getEdgeIteratorStateForKey(4), 2, 1);
    assertNodes(queryGraph.getEdgeIteratorStateForKey(5), 1, 2);
    // internally each edge is represented by two edge states for the two directions
    assertSame(queryGraph.getVirtualEdges().get(0), edge_0x);
    assertSame(queryGraph.getVirtualEdges().get(1), edge_x0);
    assertSame(queryGraph.getVirtualEdges().get(2), edge_x1);
    assertSame(queryGraph.getVirtualEdges().get(3), edge_1x);
    for (EdgeIteratorState e : Arrays.asList(edge_0x, edge_x1)) {
        assertEquals(50, e.get(speedEnc), 1.e-6);
        assertEquals(100, e.getReverse(speedEnc), 1.e-6);
    }
    for (EdgeIteratorState e : Arrays.asList(edge_x0, edge_1x)) {
        assertEquals(100, e.get(speedEnc), 1.e-6);
        assertEquals(50, e.getReverse(speedEnc), 1.e-6);
    }
    try {
        queryGraph.getEdgeIteratorState(3, 2);
        fail("there should be an error");
    } catch (IndexOutOfBoundsException e) {
    // ok
    }
}
Also used : Snap(com.graphhopper.storage.index.Snap) DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) Test(org.junit.jupiter.api.Test)

Example 19 with Snap

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

the class QueryGraphTest method testInternalAPIOriginalEdgeKey.

@Test
public void testInternalAPIOriginalEdgeKey() {
    initGraph(g);
    EdgeExplorer explorer = g.createEdgeExplorer();
    EdgeIterator iter = explorer.setBaseNode(1);
    assertTrue(iter.next());
    int origEdgeId = iter.getEdge();
    Snap res = createLocationResult(2, 1.5, iter, 1, PILLAR);
    QueryGraph queryGraph = lookup(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)).getOriginalEdgeKey());
    assertTrue(iter.next());
    assertEquals(1, iter.getAdjNode());
    assertEquals(GHUtility.createEdgeKey(0, 1, origEdgeId, false), ((VirtualEdgeIteratorState) queryGraph.getEdgeIteratorState(iter.getEdge(), 1)).getOriginalEdgeKey());
}
Also used : Snap(com.graphhopper.storage.index.Snap) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.jupiter.api.Test)

Example 20 with Snap

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

the class QueryGraphTest method testMultipleVirtualNodes.

@Test
public void testMultipleVirtualNodes() {
    initGraph(g);
    // snap to edge which has pillar nodes
    EdgeIterator iter = g.createEdgeExplorer().setBaseNode(1);
    iter.next();
    Snap res1 = createLocationResult(2, 1.7, iter, 1, PILLAR);
    QueryGraph queryGraph = lookup(res1);
    assertEquals(new GHPoint(1.5, 1.5), res1.getSnappedPoint());
    assertEquals(3, res1.getClosestNode());
    assertEquals(3, getPoints(queryGraph, 0, 3).size());
    PointList pl = getPoints(queryGraph, 3, 1);
    assertEquals(2, pl.size());
    assertEquals(new GHPoint(1.5, 1.5), pl.get(0));
    assertEquals(new GHPoint(1, 2.5), pl.get(1));
    EdgeIteratorState edge = GHUtility.getEdge(queryGraph, 3, 1);
    assertNotNull(queryGraph.getEdgeIteratorState(edge.getEdge(), 3));
    assertNotNull(queryGraph.getEdgeIteratorState(edge.getEdge(), 1));
    edge = GHUtility.getEdge(queryGraph, 3, 0);
    assertNotNull(queryGraph.getEdgeIteratorState(edge.getEdge(), 3));
    assertNotNull(queryGraph.getEdgeIteratorState(edge.getEdge(), 0));
    // snap again => new virtual node on same edge!
    iter = g.createEdgeExplorer().setBaseNode(1);
    iter.next();
    res1 = createLocationResult(2, 1.7, iter, 1, PILLAR);
    Snap res2 = createLocationResult(1.5, 2, iter, 0, EDGE);
    queryGraph = lookup(Arrays.asList(res1, res2));
    assertEquals(4, res2.getClosestNode());
    assertEquals(new GHPoint(1.300019, 1.899962), res2.getSnappedPoint());
    assertEquals(3, res1.getClosestNode());
    assertEquals(new GHPoint(1.5, 1.5), res1.getSnappedPoint());
    assertEquals(3, getPoints(queryGraph, 3, 0).size());
    assertEquals(2, getPoints(queryGraph, 3, 4).size());
    assertEquals(2, getPoints(queryGraph, 4, 1).size());
    assertNull(GHUtility.getEdge(queryGraph, 4, 0));
    assertNull(GHUtility.getEdge(queryGraph, 3, 1));
}
Also used : Snap(com.graphhopper.storage.index.Snap) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.jupiter.api.Test)

Aggregations

Snap (com.graphhopper.storage.index.Snap)77 Test (org.junit.jupiter.api.Test)39 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)31 GHPoint (com.graphhopper.util.shapes.GHPoint)22 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)20 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)13 QueryRoutingCHGraph (com.graphhopper.routing.querygraph.QueryRoutingCHGraph)12 Weighting (com.graphhopper.routing.weighting.Weighting)11 ArrayList (java.util.ArrayList)11 Path (com.graphhopper.routing.Path)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)8 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)6 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)6 LocationIndex (com.graphhopper.storage.index.LocationIndex)6 IntArrayList (com.carrotsearch.hppc.IntArrayList)5 GraphHopper (com.graphhopper.GraphHopper)5 Profile (com.graphhopper.config.Profile)5 ValueSource (org.junit.jupiter.params.provider.ValueSource)5 DefaultSnapFilter (com.graphhopper.routing.util.DefaultSnapFilter)4