Search in sources :

Example 6 with QueryGraph

use of com.graphhopper.routing.querygraph.QueryGraph in project graphhopper by graphhopper.

the class CHTurnCostTest method test_issue1593_full.

@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void test_issue1593_full(String algo) {
    // 6   5
    // 1<-x-4-x-3
    // ||    |
    // |x7   x8
    // ||   /
    // 2---
    NodeAccess na = graph.getNodeAccess();
    na.setNode(0, 49.407117, 9.701306);
    na.setNode(1, 49.406914, 9.703393);
    na.setNode(2, 49.404004, 9.709110);
    na.setNode(3, 49.400160, 9.708787);
    na.setNode(4, 49.400883, 9.706347);
    EdgeIteratorState edge0 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(4, 3).setDistance(194.063000));
    EdgeIteratorState edge1 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(525.106000));
    EdgeIteratorState edge2 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 2).setDistance(525.106000));
    EdgeIteratorState edge3 = GHUtility.setSpeed(60, true, false, encoder, graph.edge(4, 1).setDistance(703.778000));
    EdgeIteratorState edge4 = GHUtility.setSpeed(60, true, true, encoder, graph.edge(2, 4).setDistance(400.509000));
    // cannot go 4-2-1 and 1-2-4 (at least when using edge1, there is still edge2!)
    setRestriction(edge4, edge1, 2);
    setRestriction(edge1, edge4, 2);
    // cannot go 3-4-1
    setRestriction(edge0, edge3, 4);
    graph.freeze();
    LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
    index.prepareIndex();
    List<GHPoint> points = Arrays.asList(// 8 (on edge4)
    new GHPoint(49.401669187194116, 9.706821649608745), // 5 (on edge0)
    new GHPoint(49.40056349818417, 9.70767186472369), // 7 (on edge2)
    new GHPoint(49.406580835146556, 9.704665738628218), // 6 (on edge3)
    new GHPoint(49.40107534698834, 9.702248694088528));
    List<Snap> snaps = new ArrayList<>(points.size());
    for (GHPoint point : points) {
        snaps.add(index.findClosest(point.getLat(), point.getLon(), EdgeFilter.ALL_EDGES));
    }
    automaticPrepareCH();
    QueryGraph queryGraph = QueryGraph.create(chGraph.getBaseGraph(), snaps);
    RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(chGraph, queryGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
    Path path = chAlgo.calcPath(5, 6);
    // there should not be a path from 5 to 6, because first we cannot go directly 5-4-6, so we need to go left
    // to 8. then at 2 we cannot go on edge 1 because of another turn restriction, but we can go on edge 2 so we
    // travel via the virtual node 7 to node 1. From there we cannot go to 6 because of the one-way so we go back
    // to node 2 (no u-turn because of the duplicate edge) on edge1. And this is were the journey ends: we cannot
    // go to 8 because of the turn restriction from edge1 to edge4 -> there should not be a path!
    assertFalse(path.isFound(), "there should not be a path, but found: " + path.calcNodes());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) IntArrayList(com.carrotsearch.hppc.IntArrayList) Snap(com.graphhopper.storage.index.Snap) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) GHPoint(com.graphhopper.util.shapes.GHPoint) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with QueryGraph

use of com.graphhopper.routing.querygraph.QueryGraph in project graphhopper by graphhopper.

the class CHTurnCostTest method testFiniteUTurnCost_virtualViaNode.

@ParameterizedTest
@ValueSource(strings = { DIJKSTRA_BI, ASTAR_BI })
public void testFiniteUTurnCost_virtualViaNode(String algo) {
    // if there is an extra virtual node it can be possible to do a u-turn that otherwise would not be possible
    // and so there can be a difference between CH and non-CH... therefore u-turns at virtual nodes are forbidden
    // 4->3->2->1-x-0
    // |
    // 5->6
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(4, 3).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(3, 2).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(2, 1).setDistance(0));
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(1, 0).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(1, 5).setDistance(0));
    GHUtility.setSpeed(60, true, false, encoder, graph.edge(5, 6).setDistance(0));
    updateDistancesFor(graph, 4, 0.1, 0.0);
    updateDistancesFor(graph, 3, 0.1, 0.1);
    updateDistancesFor(graph, 2, 0.1, 0.2);
    updateDistancesFor(graph, 1, 0.1, 0.3);
    updateDistancesFor(graph, 0, 0.1, 0.4);
    updateDistancesFor(graph, 5, 0.0, 0.3);
    updateDistancesFor(graph, 6, 0.0, 0.4);
    // not allowed to turn right at node 1 -> we have to take a u-turn at node 0 (or at the virtual node...)
    setRestriction(2, 1, 5);
    graph.freeze();
    chConfig = chConfigs.get(1);
    prepareCH(0, 1, 2, 3, 4, 5, 6);
    LocationIndexTree index = new LocationIndexTree(graph, new RAMDirectory());
    index.prepareIndex();
    GHPoint virtualPoint = new GHPoint(0.1, 0.35);
    Snap snap = index.findClosest(virtualPoint.lat, virtualPoint.lon, EdgeFilter.ALL_EDGES);
    QueryGraph chQueryGraph = QueryGraph.create(graph, snap);
    assertEquals(3, snap.getClosestEdge().getEdge());
    QueryRoutingCHGraph routingCHGraph = new QueryRoutingCHGraph(chGraph, chQueryGraph);
    RoutingAlgorithm chAlgo = new CHRoutingAlgorithmFactory(routingCHGraph).createAlgo(new PMap().putObject(ALGORITHM, algo));
    Path path = chAlgo.calcPath(4, 6);
    assertTrue(path.isFound());
    assertEquals(IntArrayList.from(4, 3, 2, 1, 0, 1, 5, 6), path.calcNodes());
    Snap snap2 = index.findClosest(virtualPoint.lat, virtualPoint.lon, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, snap2);
    assertEquals(3, snap2.getClosestEdge().getEdge());
    Weighting w = queryGraph.wrapWeighting(chConfig.getWeighting());
    Dijkstra dijkstra = new Dijkstra(queryGraph, w, TraversalMode.EDGE_BASED);
    Path dijkstraPath = dijkstra.calcPath(4, 6);
    assertEquals(IntArrayList.from(4, 3, 2, 1, 7, 0, 7, 1, 5, 6), dijkstraPath.calcNodes());
    assertEquals(dijkstraPath.getWeight(), path.getWeight(), 1.e-2);
    assertEquals(dijkstraPath.getDistance(), path.getDistance(), 1.e-2);
    assertEquals(dijkstraPath.getTime(), path.getTime());
}
Also used : RoutingAlgorithm(com.graphhopper.routing.RoutingAlgorithm) Path(com.graphhopper.routing.Path) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) GHPoint(com.graphhopper.util.shapes.GHPoint) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Dijkstra(com.graphhopper.routing.Dijkstra) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with QueryGraph

use of com.graphhopper.routing.querygraph.QueryGraph in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method getBaseGraph.

@Test
public void getBaseGraph() {
    GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(10));
    graph.freeze();
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    QueryGraph queryGraph = QueryGraph.create(graph, Collections.<Snap>emptyList());
    assertSame(graph.getBaseGraph(), routingCHGraph.getBaseGraph());
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    assertSame(queryGraph, queryCHGraph.getBaseGraph());
}
Also used : QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 9 with QueryGraph

use of com.graphhopper.routing.querygraph.QueryGraph in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method withVirtualEdges.

@Test
public void withVirtualEdges() {
    // 2 3
    // 0-x-1-2
    // 3
    na.setNode(0, 50.00, 10.00);
    na.setNode(1, 50.00, 10.10);
    na.setNode(2, 50.00, 10.20);
    EdgeIteratorState edge = addEdge(graph, 0, 1);
    addEdge(graph, 1, 2);
    graph.freeze();
    assertEquals(2, graph.getEdges());
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    Snap snap = new Snap(50.00, 10.05);
    snap.setClosestEdge(edge);
    snap.setWayIndex(0);
    snap.setSnappedPosition(Snap.Position.EDGE);
    snap.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    QueryGraph queryGraph = QueryGraph.create(graph, Collections.singletonList(snap));
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    assertEquals(4, queryCHGraph.getNodes());
    assertEquals(2 + 4, queryCHGraph.getEdges());
    assertNodesConnected(queryCHGraph, 1, 2, true);
    // virtual edges at virtual node 3
    assertNodesConnected(queryCHGraph, 0, 3, true);
    assertNodesConnected(queryCHGraph, 3, 1, true);
    // out-iter at real node
    RoutingCHEdgeIterator outIter = queryCHGraph.createOutEdgeExplorer().setBaseNode(2);
    assertNextEdge(outIter, 2, 1, 1);
    assertEnd(outIter);
    // in-iter at real node
    RoutingCHEdgeIterator inIter = queryCHGraph.createInEdgeExplorer().setBaseNode(2);
    assertNextEdge(inIter, 2, 1, 1);
    assertEnd(inIter);
    // out-iter at real node next to virtual node
    outIter = queryCHGraph.createOutEdgeExplorer().setBaseNode(0);
    assertNextEdge(outIter, 0, 3, 2);
    assertEnd(outIter);
    // in-iter at real node next to virtual node
    inIter = queryCHGraph.createInEdgeExplorer().setBaseNode(1);
    assertNextEdge(inIter, 1, 3, 3);
    assertNextEdge(inIter, 1, 2, 1);
    assertEnd(inIter);
    // out-iter at virtual node
    outIter = queryCHGraph.createOutEdgeExplorer().setBaseNode(3);
    assertNextEdge(outIter, 3, 0, 2);
    assertNextEdge(outIter, 3, 1, 3);
    assertEnd(outIter);
    // in-iter at virtual node
    inIter = queryCHGraph.createInEdgeExplorer().setBaseNode(3);
    assertNextEdge(inIter, 3, 0, 2);
    assertNextEdge(inIter, 3, 1, 3);
    assertEnd(inIter);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Example 10 with QueryGraph

use of com.graphhopper.routing.querygraph.QueryGraph in project graphhopper by graphhopper.

the class QueryRoutingCHGraphTest method getLevel.

@Test
public void getLevel() {
    // 0-x-1
    na.setNode(0, 50.00, 10.00);
    na.setNode(1, 50.00, 10.10);
    EdgeIteratorState edge = addEdge(graph, 0, 1);
    graph.freeze();
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    CHStorageBuilder chBuilder = new CHStorageBuilder(chStore);
    chBuilder.setLevel(0, 5);
    chBuilder.setLevel(1, 7);
    Snap snap = new Snap(50.00, 10.05);
    snap.setClosestEdge(edge);
    snap.setWayIndex(0);
    snap.setSnappedPosition(Snap.Position.EDGE);
    snap.calcSnappedPoint(DistancePlaneProjection.DIST_PLANE);
    QueryGraph queryGraph = QueryGraph.create(graph, Collections.singletonList(snap));
    QueryRoutingCHGraph queryCHGraph = new QueryRoutingCHGraph(routingCHGraph, queryGraph);
    assertEquals(5, queryCHGraph.getLevel(0));
    assertEquals(7, queryCHGraph.getLevel(1));
    assertEquals(Integer.MAX_VALUE, queryCHGraph.getLevel(2));
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Test(org.junit.jupiter.api.Test)

Aggregations

QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)37 Snap (com.graphhopper.storage.index.Snap)32 Test (org.junit.jupiter.api.Test)18 QueryRoutingCHGraph (com.graphhopper.routing.querygraph.QueryRoutingCHGraph)17 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)16 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)11 Path (com.graphhopper.routing.Path)9 Weighting (com.graphhopper.routing.weighting.Weighting)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)6 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)6 PMap (com.graphhopper.util.PMap)6 ArrayList (java.util.ArrayList)6 GraphHopper (com.graphhopper.GraphHopper)5 GHPoint (com.graphhopper.util.shapes.GHPoint)5 ValueSource (org.junit.jupiter.params.provider.ValueSource)5 Graph (com.graphhopper.storage.Graph)4 LocationIndex (com.graphhopper.storage.index.LocationIndex)4 GHResponse (com.graphhopper.GHResponse)3 ResponsePath (com.graphhopper.ResponsePath)3