@@ -47,23 +47,34 @@ create new virtual nodes or if close enough use the existing junction node.
47
47
``` java
48
48
FlagEncoder encoder = new CarFlagEncoder ();
49
49
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();
52
51
// Make a weighted edge between two nodes.
53
52
EdgeIteratorState edge = graph. edge(fromId, toId);
54
53
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
57
64
graph. flush();
65
+ index. flush();
58
66
```
59
67
60
68
### Load the graph
61
69
62
70
``` 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());
67
78
if (! index. loadExisting())
68
79
throw new IllegalStateException (" location index cannot be loaded!" );
69
80
```
@@ -74,55 +85,51 @@ if (!index.loadExisting())
74
85
QueryResult fromQR = index. findClosest(latitudeFrom, longituteFrom, EdgeFilter . ALL_EDGES );
75
86
QueryResult toQR = index. findClosest(latitudeTo, longituteTo, EdgeFilter . ALL_EDGES );
76
87
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());
78
90
```
79
91
80
92
### Calculate Path without LocationIndex
81
93
82
94
``` java
83
95
// 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);
85
97
```
86
98
87
99
### Use Contraction Hierarchies to make queries faster
88
100
89
101
``` java
90
102
// 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
99
114
100
115
// 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);
104
117
pch. doWork();
105
118
106
119
// flush after preparation!
107
120
graph. flush();
108
121
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" );
116
124
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);
121
128
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());
128
133
```
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