Search in sources :

Example 41 with Snap

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

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

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

the class LocationIndexExample method graphhopperLocationIndex.

public static void graphhopperLocationIndex(String relDir) {
    GraphHopper hopper = new GraphHopper();
    hopper.setProfiles(new Profile("car").setVehicle("car").setWeighting("fastest"));
    hopper.setOSMFile(relDir + "core/files/andorra.osm.pbf");
    hopper.setGraphHopperLocation("./target/locationindex-graph-cache");
    hopper.importOrLoad();
    LocationIndex index = hopper.getLocationIndex();
    // now you can fetch the closest edge via:
    Snap snap = index.findClosest(42.508552, 1.532936, EdgeFilter.ALL_EDGES);
    EdgeIteratorState edge = snap.getClosestEdge();
    assert edge.getName().equals("Avinguda Meritxell");
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) GraphHopper(com.graphhopper.GraphHopper) LocationIndex(com.graphhopper.storage.index.LocationIndex) Snap(com.graphhopper.storage.index.Snap) Profile(com.graphhopper.config.Profile)

Example 44 with Snap

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

use of com.graphhopper.storage.index.Snap 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)

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