forked from geofabrik/graphhopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutingExample.java
146 lines (121 loc) · 6.73 KB
/
RoutingExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.graphhopper.example;
import com.graphhopper.GHRequest;
import com.graphhopper.GHResponse;
import com.graphhopper.GraphHopper;
import com.graphhopper.ResponsePath;
import com.graphhopper.config.CHProfile;
import com.graphhopper.config.LMProfile;
import com.graphhopper.config.Profile;
import com.graphhopper.util.*;
import com.graphhopper.util.shapes.GHPoint;
import java.util.Locale;
import static com.graphhopper.json.Statement.If;
import static com.graphhopper.json.Statement.Op.LIMIT;
import static com.graphhopper.json.Statement.Op.MULTIPLY;
public class RoutingExample {
public static void main(String[] args) {
String relDir = args.length == 1 ? args[0] : "";
GraphHopper hopper = createGraphHopperInstance(relDir + "core/files/andorra.osm.pbf");
routing(hopper);
speedModeVersusFlexibleMode(hopper);
alternativeRoute(hopper);
customizableRouting(relDir + "core/files/andorra.osm.pbf");
// release resources to properly shutdown or start a new instance
hopper.close();
}
static GraphHopper createGraphHopperInstance(String ghLoc) {
GraphHopper hopper = new GraphHopper();
hopper.setOSMFile(ghLoc);
// specify where to store graphhopper files
hopper.setGraphHopperLocation("target/routing-graph-cache");
// add all encoded values that are used in the custom model, these are also available as path details or for client-side custom models
hopper.setEncodedValuesString("car_access, car_average_speed");
// see docs/core/profiles.md to learn more about profiles
hopper.setProfiles(new Profile("car").setCustomModel(GHUtility.loadCustomModelFromJar("car.json")));
// this enables speed mode for the profile we called car
hopper.getCHPreparationHandler().setCHProfiles(new CHProfile("car"));
// now this can take minutes if it imports or a few seconds for loading of course this is dependent on the area you import
hopper.importOrLoad();
return hopper;
}
public static void routing(GraphHopper hopper) {
// simple configuration of the request object
GHRequest req = new GHRequest(42.508552, 1.532936, 42.507508, 1.528773).
// note that we have to specify which profile we are using even when there is only one like here
setProfile("car").
// define the language for the turn instructions
setLocale(Locale.US);
GHResponse rsp = hopper.route(req);
// handle errors
if (rsp.hasErrors())
throw new RuntimeException(rsp.getErrors().toString());
// use the best path, see the GHResponse class for more possibilities.
ResponsePath path = rsp.getBest();
// points, distance in meters and time in millis of the full path
PointList pointList = path.getPoints();
double distance = path.getDistance();
long timeInMs = path.getTime();
Translation tr = hopper.getTranslationMap().getWithFallBack(Locale.UK);
InstructionList il = path.getInstructions();
// iterate over all turn instructions
for (Instruction instruction : il) {
// System.out.println("distance " + instruction.getDistance() + " for instruction: " + instruction.getTurnDescription(tr));
}
assert il.size() == 6;
assert Helper.round(path.getDistance(), -2) == 600;
}
public static void speedModeVersusFlexibleMode(GraphHopper hopper) {
GHRequest req = new GHRequest(42.508552, 1.532936, 42.507508, 1.528773).
setProfile("car").setAlgorithm(Parameters.Algorithms.ASTAR_BI).putHint(Parameters.CH.DISABLE, true);
GHResponse res = hopper.route(req);
if (res.hasErrors())
throw new RuntimeException(res.getErrors().toString());
assert Helper.round(res.getBest().getDistance(), -2) == 600;
}
public static void alternativeRoute(GraphHopper hopper) {
// calculate alternative routes between two points (supported with and without CH)
GHRequest req = new GHRequest().setProfile("car").
addPoint(new GHPoint(42.502904, 1.514714)).addPoint(new GHPoint(42.508774, 1.537094)).
setAlgorithm(Parameters.Algorithms.ALT_ROUTE);
req.getHints().putObject(Parameters.Algorithms.AltRoute.MAX_PATHS, 3);
GHResponse res = hopper.route(req);
if (res.hasErrors())
throw new RuntimeException(res.getErrors().toString());
assert res.getAll().size() == 2;
assert Helper.round(res.getBest().getDistance(), -2) == 2200;
}
/**
* To customize profiles in the config.yml file you can use a json or yml file or embed it directly. See this list:
* web/src/test/resources/com/graphhopper/application/resources and https://www.graphhopper.com/?s=customizable+routing
*/
public static void customizableRouting(String ghLoc) {
GraphHopper hopper = new GraphHopper();
hopper.setOSMFile(ghLoc);
hopper.setGraphHopperLocation("target/routing-custom-graph-cache");
hopper.setEncodedValuesString("car_access, car_average_speed");
hopper.setProfiles(new Profile("car_custom").setCustomModel(GHUtility.loadCustomModelFromJar("car.json")));
// The hybrid mode uses the "landmark algorithm" and is up to 15x faster than the flexible mode (Dijkstra).
// Still it is slower than the speed mode ("contraction hierarchies algorithm") ...
hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car_custom"));
hopper.importOrLoad();
// ... but for the hybrid mode we can customize the route calculation even at request time:
// 1. a request with default preferences
GHRequest req = new GHRequest().setProfile("car_custom").
addPoint(new GHPoint(42.506472, 1.522475)).addPoint(new GHPoint(42.513108, 1.536005));
GHResponse res = hopper.route(req);
if (res.hasErrors())
throw new RuntimeException(res.getErrors().toString());
assert Math.round(res.getBest().getTime() / 1000d) == 94;
// 2. now avoid the secondary road and reduce the maximum speed, see docs/core/custom-models.md for an in-depth explanation
// and also the blog posts https://www.graphhopper.com/?s=customizable+routing
CustomModel model = new CustomModel();
model.addToPriority(If("road_class == SECONDARY", MULTIPLY, "0.5"));
// unconditional limit to 20km/h
model.addToSpeed(If("true", LIMIT, "30"));
req.setCustomModel(model);
res = hopper.route(req);
if (res.hasErrors())
throw new RuntimeException(res.getErrors().toString());
assert Math.round(res.getBest().getTime() / 1000d) == 184;
}
}