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;
}
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;
}
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");
}
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);
}
}
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));
}
Aggregations