Search in sources :

Example 21 with QueryGraph

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

the class LowLevelAPIExample method useContractionHierarchiesToMakeQueriesFaster.

public static void useContractionHierarchiesToMakeQueriesFaster() {
    // Creating and saving the graph
    FlagEncoder encoder = new CarFlagEncoder();
    EncodingManager em = EncodingManager.create(encoder);
    Weighting weighting = new FastestWeighting(encoder);
    CHConfig chConfig = CHConfig.nodeBased("my_profile", weighting);
    GraphHopperStorage graph = new GraphBuilder(em).setRAM(graphLocation, true).create();
    graph.flush();
    // Set node coordinates and build location index
    NodeAccess na = graph.getNodeAccess();
    graph.edge(0, 1).set(encoder.getAccessEnc(), true).set(encoder.getAverageSpeedEnc(), 10).setDistance(1020);
    na.setNode(0, 15.15, 20.20);
    na.setNode(1, 15.25, 20.21);
    // Prepare the graph for fast querying ...
    graph.freeze();
    PrepareContractionHierarchies pch = PrepareContractionHierarchies.fromGraphHopperStorage(graph, chConfig);
    PrepareContractionHierarchies.Result pchRes = pch.doWork();
    RoutingCHGraph chGraph = graph.createCHGraph(pchRes.getCHStorage(), pchRes.getCHConfig());
    // create location index
    LocationIndexTree index = new LocationIndexTree(graph, graph.getDirectory());
    index.prepareIndex();
    // calculate a path with location index
    Snap fromSnap = index.findClosest(15.15, 20.20, EdgeFilter.ALL_EDGES);
    Snap toSnap = index.findClosest(15.25, 20.21, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, fromSnap, toSnap);
    BidirRoutingAlgorithm algo = new CHRoutingAlgorithmFactory(chGraph, queryGraph).createAlgo(new PMap());
    Path path = algo.calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
    assert Helper.round(path.getDistance(), -2) == 1000;
}
Also used : Path(com.graphhopper.routing.Path) PrepareContractionHierarchies(com.graphhopper.routing.ch.PrepareContractionHierarchies) PMap(com.graphhopper.util.PMap) Snap(com.graphhopper.storage.index.Snap) BidirRoutingAlgorithm(com.graphhopper.routing.BidirRoutingAlgorithm) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) CHRoutingAlgorithmFactory(com.graphhopper.routing.ch.CHRoutingAlgorithmFactory) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Example 22 with QueryGraph

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

the class LowLevelAPIExample method createAndSaveGraph.

public static void createAndSaveGraph() {
    FlagEncoder encoder = new CarFlagEncoder();
    EncodingManager em = EncodingManager.create(encoder);
    GraphHopperStorage graph = new GraphBuilder(em).setRAM(graphLocation, true).create();
    // Make a weighted edge between two nodes and set average speed to 50km/h
    EdgeIteratorState edge = graph.edge(0, 1).setDistance(1234).set(encoder.getAverageSpeedEnc(), 50);
    // Set node coordinates and build location index
    NodeAccess na = graph.getNodeAccess();
    graph.edge(0, 1).set(encoder.getAccessEnc(), true).set(encoder.getAverageSpeedEnc(), 10).setDistance(1530);
    na.setNode(0, 15.15, 20.20);
    na.setNode(1, 15.25, 20.21);
    LocationIndexTree index = new LocationIndexTree(graph, graph.getDirectory());
    index.prepareIndex();
    // Flush the graph and location index to disk
    graph.flush();
    index.flush();
    graph.close();
    index.close();
    // Load the graph ... can be also in a different code location
    graph = new GraphBuilder(em).setRAM(graphLocation, true).build();
    graph.loadExisting();
    // Load the location index
    index = new LocationIndexTree(graph.getBaseGraph(), graph.getDirectory());
    if (!index.loadExisting())
        throw new IllegalStateException("location index cannot be loaded!");
    // calculate with location index
    Snap fromSnap = index.findClosest(15.15, 20.20, EdgeFilter.ALL_EDGES);
    Snap toSnap = index.findClosest(15.25, 20.21, EdgeFilter.ALL_EDGES);
    QueryGraph queryGraph = QueryGraph.create(graph, fromSnap, toSnap);
    Weighting weighting = new FastestWeighting(encoder);
    Path path = new Dijkstra(queryGraph, weighting, TraversalMode.NODE_BASED).calcPath(fromSnap.getClosestNode(), toSnap.getClosestNode());
    assert Helper.round(path.getDistance(), -2) == 1500;
    // calculate without location index (get the fromId and toId nodes from other code parts)
    path = new Dijkstra(graph, weighting, TraversalMode.NODE_BASED).calcPath(0, 1);
    assert Helper.round(path.getDistance(), -2) == 1500;
}
Also used : Path(com.graphhopper.routing.Path) Snap(com.graphhopper.storage.index.Snap) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Dijkstra(com.graphhopper.routing.Dijkstra) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Example 23 with QueryGraph

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

the class DirectedRoutingTest method randomGraph_withQueryGraph.

/**
 * Similar to {@link #randomGraph}, but using the {@link QueryGraph} as it is done in real usage.
 */
@ParameterizedTest
@ArgumentsSource(RepeatedFixtureProvider.class)
public void randomGraph_withQueryGraph(Fixture f) {
    final long seed = System.nanoTime();
    final int numQueries = 50;
    // we may not use an offset when query graph is involved, otherwise traveling via virtual edges will not be
    // the same as taking the direct edge!
    double pOffset = 0;
    Random rnd = new Random(seed);
    GHUtility.buildRandomGraph(f.graph, rnd, 50, 2.2, true, true, f.encoder.getAccessEnc(), f.encoder.getAverageSpeedEnc(), null, 0.7, 0.8, pOffset);
    GHUtility.addRandomTurnCosts(f.graph, seed, f.encodingManager, f.encoder, f.maxTurnCosts, f.turnCostStorage);
    // GHUtility.printGraphForUnitTest(graph, encoder);
    f.preProcessGraph();
    LocationIndexTree index = new LocationIndexTree(f.graph, f.dir);
    index.prepareIndex();
    List<String> strictViolations = new ArrayList<>();
    for (int i = 0; i < numQueries; i++) {
        List<Snap> snaps = createRandomSnaps(f.graph.getBounds(), index, rnd, 2, true, EdgeFilter.ALL_EDGES);
        QueryGraph queryGraph = QueryGraph.create(f.graph, snaps);
        int source = snaps.get(0).getClosestNode();
        int target = snaps.get(1).getClosestNode();
        Random tmpRnd1 = new Random(seed);
        int sourceOutEdge = getSourceOutEdge(tmpRnd1, source, queryGraph);
        int targetInEdge = getTargetInEdge(tmpRnd1, target, queryGraph);
        Random tmpRnd2 = new Random(seed);
        int chSourceOutEdge = getSourceOutEdge(tmpRnd2, source, queryGraph);
        int chTargetInEdge = getTargetInEdge(tmpRnd2, target, queryGraph);
        Path refPath = new DijkstraBidirectionRef(queryGraph, ((Graph) queryGraph).wrapWeighting(f.weighting), TraversalMode.EDGE_BASED).calcPath(source, target, sourceOutEdge, targetInEdge);
        Path path = f.createAlgo(queryGraph).calcPath(source, target, chSourceOutEdge, chTargetInEdge);
        // do not check nodes, because there can be ambiguity when there are zero weight loops
        strictViolations.addAll(comparePaths(refPath, path, source, target, false, seed));
    }
    // is wrong and we fail
    if (strictViolations.size() > Math.max(1, 0.05 * numQueries)) {
        fail("Too many strict violations, with seed: " + seed + " - " + strictViolations.size() + " / " + numQueries);
    }
}
Also used : ArrayList(java.util.ArrayList) Snap(com.graphhopper.storage.index.Snap) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) QueryRoutingCHGraph(com.graphhopper.routing.querygraph.QueryRoutingCHGraph) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) Random(java.util.Random) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 24 with QueryGraph

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

the class HeadingResolverTest method withQueryGraph.

@Test
public void withQueryGraph() {
    // 2
    // 0 -x- 1
    FlagEncoder encoder = new CarFlagEncoder();
    EncodingManager em = EncodingManager.create(encoder);
    GraphHopperStorage graph = new GraphBuilder(em).create();
    NodeAccess na = graph.getNodeAccess();
    na.setNode(0, 48.8611, 1.2194);
    na.setNode(1, 48.8538, 2.3950);
    EdgeIteratorState edge = GHUtility.setSpeed(60, true, true, encoder, graph.edge(0, 1).setDistance(10));
    Snap snap = createSnap(edge, 48.859, 2.00, 0);
    QueryGraph queryGraph = QueryGraph.create(graph, snap);
    HeadingResolver resolver = new HeadingResolver(queryGraph);
    // if the heading points East we get the Western edge 0->2
    assertEquals("0->2", queryGraph.getEdgeIteratorState(1, Integer.MIN_VALUE).toString());
    assertEquals(IntArrayList.from(1), resolver.getEdgesWithDifferentHeading(2, 90));
    // if the heading points West we get the Eastern edge 2->1
    assertEquals("2->1", queryGraph.getEdgeIteratorState(2, Integer.MIN_VALUE).toString());
    assertEquals(IntArrayList.from(2), resolver.getEdgesWithDifferentHeading(2, 270));
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) NodeAccess(com.graphhopper.storage.NodeAccess) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) GraphBuilder(com.graphhopper.storage.GraphBuilder) Snap(com.graphhopper.storage.index.Snap) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.jupiter.api.Test)

Example 25 with QueryGraph

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

the class QueryRoutingCHGraphTest method getEdgeIteratorState.

@Test
public void getEdgeIteratorState() {
    // /---\
    // 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();
    CHConfig chConfig = CHConfig.edgeBased("x", weighting);
    CHStorage chStore = graph.createCHStorage(chConfig);
    RoutingCHGraph routingCHGraph = graph.createCHGraph(chStore, chConfig);
    CHStorageBuilder chBuilder = new CHStorageBuilder(chStore);
    chBuilder.setIdentityLevels();
    chBuilder.addShortcutEdgeBased(0, 2, PrepareEncoder.getScFwdDir(), 20, 0, 1, 0, 1);
    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);
    assertGetEdgeIteratorState(queryCHGraph, 1, 2, 1);
    assertGetEdgeIteratorShortcut(queryCHGraph, 0, 2, 0, 1);
    // the orig edge corresponds to the edge id of the edge in the (base) query graph
    assertGetEdgeIteratorState(queryCHGraph, 0, 3, 2);
    assertGetEdgeIteratorState(queryCHGraph, 3, 0, 2);
    assertGetEdgeIteratorState(queryCHGraph, 1, 3, 3);
    assertGetEdgeIteratorState(queryCHGraph, 3, 1, 3);
}
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