Skip to content

Commit 2207576

Browse files
committed
Fix/update low-level-api.md
1 parent 5ac8d78 commit 2207576

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

docs/core/low-level-api.md

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,34 @@ create new virtual nodes or if close enough use the existing junction node.
4747
```java
4848
FlagEncoder encoder = new CarFlagEncoder();
4949
EncodingManager em = EncodingManager.create(encoder);
50-
GraphBuilder gb = new GraphBuilder(em).setLocation("graphhopper_folder").setStore(true);
51-
GraphStorage graph = gb.create();
50+
GraphHopperStorage graph = new GraphBuilder(em).setRAM("graphhopper_folder", true).create();
5251
// Make a weighted edge between two nodes.
5352
EdgeIteratorState edge = graph.edge(fromId, toId);
5453
edge.setDistance(distance);
55-
edge.setFlags(encoder.setProperties(speed, true, true));
56-
// Flush to disc
54+
edge.set(encoder.getAverageSpeedEnc(), 50);
55+
56+
// Set node coordinates and build location index
57+
NodeAccess na = graph.getNodeAccess();
58+
na.setNode(5, 15.15, 20.20);
59+
na.setNode(10, 15.25, 20.21);
60+
LocationIndexTree index = new LocationIndexTree(graph, graph.getDirectory());
61+
index.prepareIndex();
62+
63+
// Flush the graph and location index to disk
5764
graph.flush();
65+
index.flush();
5866
```
5967

6068
### Load the graph
6169

6270
```java
63-
...
64-
GraphStorage graph = gb.load();
65-
// Load index
66-
LocationIndex index = new LocationIndexTree(graph.getBaseGraph(), new RAMDirectory("graphhopper_folder", true));
71+
FlagEncoder encoder = new CarFlagEncoder();
72+
EncodingManager em = EncodingManager.create(encoder);
73+
GraphHopperStorage graph = new GraphBuilder(em).setRAM("graphhopper_folder", true).build();
74+
graph.loadExisting();
75+
76+
// Load the location index
77+
LocationIndex index = new LocationIndexTree(graph.getBaseGraph(), graph.getDirectory());
6778
if (!index.loadExisting())
6879
throw new IllegalStateException("location index cannot be loaded!");
6980
```
@@ -74,55 +85,51 @@ if (!index.loadExisting())
7485
QueryResult fromQR = index.findClosest(latitudeFrom, longituteFrom, EdgeFilter.ALL_EDGES);
7586
QueryResult toQR = index.findClosest(latitudeTo, longituteTo, EdgeFilter.ALL_EDGES);
7687
QueryGraph queryGraph = QueryGraph.create(graph, fromQR, toQR);
77-
Path path = new Dijkstra(queryGraph, encoder).calcPath(fromQR.getClosestNode(), toQR.getClosestNode());
88+
Weighting weighting = new FastestWeighting(encoder);
89+
Path path = new Dijkstra(queryGraph, weighting).calcPath(fromQR.getClosestNode(), toQR.getClosestNode());
7890
```
7991

8092
### Calculate Path without LocationIndex
8193

8294
```java
8395
// get the fromId and toId nodes from other code parts
84-
Path path = new Dijkstra(graph, encoder).calcPath(fromId, toId);
96+
Path path = new Dijkstra(graph, weighting).calcPath(fromId, toId);
8597
```
8698

8799
### Use Contraction Hierarchies to make queries faster
88100

89101
```java
90102
// Creating and saving the graph
91-
GraphBuilder gb = new GraphBuilder(em).
92-
setLocation("graphhopper_folder").
93-
setStore(true).
94-
setCHGraph(true);
95-
GraphHopperStorage graph = gb.create();
96-
// Create a new edge between two nodes, set access, distance, speed, geometry, ..
97-
EdgeIteratorState edge = graph.edge(fromId, toId);
98-
...
103+
FlagEncoder encoder = new CarFlagEncoder();
104+
EncodingManager em = EncodingManager.create(encoder);
105+
Weighting weighting = new FastestWeighting(encoder);
106+
CHConfig chConfig = CHConfig.nodeBased("my_profile", weighting);
107+
GraphHopperStorage graph = new GraphBuilder(em)
108+
.setRAM("graphhopper_folder", true)
109+
// need to setup CH at time of graph creation here!
110+
.setCHConfigs(chConfig)
111+
.create();
112+
113+
// ... (not shown) set edges and nodes/coordinates as above
99114

100115
// Prepare the graph for fast querying ...
101-
TraversalMode tMode = TraversalMode.NODE_BASED;
102-
CHConfig chConfig = CHConfig.nodeBased("chGraphName", weighting);
103-
PrepareContractionHierarchies pch = PrepareContractionHierarchies.fromGraphHopperStorage(ghStorage, chConfig);
116+
PrepareContractionHierarchies pch = PrepareContractionHierarchies.fromGraphHopperStorage(graph, chConfig);
104117
pch.doWork();
105118

106119
// flush after preparation!
107120
graph.flush();
108121

109-
// Load and use the graph
110-
GraphStorage graph = gb.load();
111-
112-
// Load index
113-
LocationIndex index = new LocationIndexTree(graph.getBaseGraph(), new RAMDirectory("graphhopper_folder", true));
114-
if (!index.loadExisting())
115-
throw new IllegalStateException("location index cannot be loaded!");
122+
// get the CH graph
123+
RoutingCHGraph chGraph = graph.getRoutingCHGraph("my_profile");
116124

117-
// calculate path is identical
118-
QueryResult fromQR = index.findClosest(latitudeFrom, longituteFrom, EdgeFilter.ALL_EDGES);
119-
QueryResult toQR = index.findClosest(latitudeTo, longituteTo, EdgeFilter.ALL_EDGES);
120-
QueryGraph queryGraph = QueryGraph.create(graph, fromQR, toQR);
125+
// calculate a path without location index
126+
BidirRoutingAlgorithm algo = new CHRoutingAlgorithmFactory(chGraph).createAlgo(new PMap());
127+
algo.calcPath(fromId, toId);
121128

122-
// create the algorithm using the PrepareContractionHierarchies object
123-
AlgorithmOptions algoOpts = AlgorithmOptions.start().
124-
algorithm(Parameters.Algorithms.DIJKSTRA_BI).traversalMode(tMode).weighting(weighting).
125-
build();
126-
RoutingAlgorithm algorithm = pch.getRoutingAlgorithmFactory().createAlgo(queryGraph, algoOpts);
127-
Path path = algorithm.calcPath(fromQR.getClosestNode(), toQR.getClosestNode());
129+
// calculate a path with location index
130+
QueryGraph queryGraph = QueryGraph.create(graph, fromQR, toQR); // use index as shown above
131+
BidirRoutingAlgorithm algo = new CHRoutingAlgorithmFactory(chGraph, queryGraph).createAlgo(new PMap());
132+
algo.calcPath(fromQR.getClosestNode(), toQR.getClosestNode());
128133
```
134+
135+
**See GraphHopper's many unit tests for up-to date and more detailed usage examples of the low level API!**

0 commit comments

Comments
 (0)