/
GHRequest.java
346 lines (292 loc) · 10.6 KB
/
GHRequest.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper;
import com.graphhopper.routing.util.HintsMap;
import com.graphhopper.util.Helper;
import com.graphhopper.util.shapes.GHPoint;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
/**
* GraphHopper request wrapper to simplify requesting GraphHopper.
*
* @author Peter Karich
* @author ratrun
*/
public class GHRequest {
private List<GHPoint> points;
// todo #1934: keep this here or put it into hints, and even more important: remove vehicle+weighting from
// hints?
private String profile = "";
private final HintsMap hints = new HintsMap();
// List of favored start (1st element) and arrival heading (all other).
// Headings are north based azimuth (clockwise) in (0, 360) or NaN for equal preference
private List<Double> favoredHeadings;
private List<String> pointHints = new ArrayList<>();
private List<String> curbsides = new ArrayList<>();
private List<String> snapPreventions = new ArrayList<>();
private List<String> pathDetails = new ArrayList<>();
private String algo = "";
private boolean possibleToAdd = false;
private Locale locale = Locale.US;
public GHRequest() {
this(5);
}
public GHRequest(int size) {
points = new ArrayList<>(size);
favoredHeadings = new ArrayList<>(size);
possibleToAdd = true;
}
/**
* Set routing request from specified startPlace (fromLat, fromLon) to endPlace (toLat, toLon)
* with a preferred start and end heading. Headings are north based azimuth (clockwise) in (0,
* 360) or NaN for equal preference.
*/
public GHRequest(double fromLat, double fromLon, double toLat, double toLon,
double startHeading, double endHeading) {
this(new GHPoint(fromLat, fromLon), new GHPoint(toLat, toLon), startHeading, endHeading);
}
/**
* Set routing request from specified startPlace (fromLat, fromLon) to endPlace (toLat, toLon)
*/
public GHRequest(double fromLat, double fromLon, double toLat, double toLon) {
this(new GHPoint(fromLat, fromLon), new GHPoint(toLat, toLon));
}
/**
* Set routing request from specified startPlace to endPlace with a preferred start and end
* heading. Headings are north based azimuth (clockwise) in (0, 360) or NaN for equal preference
*/
public GHRequest(GHPoint startPlace, GHPoint endPlace, double startHeading, double endHeading) {
if (startPlace == null)
throw new IllegalStateException("'from' cannot be null");
if (endPlace == null)
throw new IllegalStateException("'to' cannot be null");
points = new ArrayList<>(2);
points.add(startPlace);
points.add(endPlace);
favoredHeadings = new ArrayList<>(2);
validateAzimuthValue(startHeading);
favoredHeadings.add(startHeading);
validateAzimuthValue(endHeading);
favoredHeadings.add(endHeading);
}
public GHRequest(GHPoint startPlace, GHPoint endPlace) {
this(startPlace, endPlace, Double.NaN, Double.NaN);
}
/**
* Set routing request
*
* @param points List of stopover points in order: start, 1st stop, 2nd stop, ..., end
* @param favoredHeadings List of favored headings for starting (start point) and arrival (via
* and end points) Headings are north based azimuth (clockwise) in (0, 360) or NaN for equal
*/
public GHRequest(List<GHPoint> points, List<Double> favoredHeadings) {
if (points.size() != favoredHeadings.size())
throw new IllegalArgumentException("Size of headings (" + favoredHeadings.size()
+ ") must match size of points (" + points.size() + ")");
for (Double heading : favoredHeadings) {
validateAzimuthValue(heading);
}
this.points = points;
this.favoredHeadings = favoredHeadings;
}
/**
* Set routing request
*
* @param points List of stopover points in order: start, 1st stop, 2nd stop, ..., end
*/
public GHRequest(List<GHPoint> points) {
this(points, Collections.nCopies(points.size(), Double.NaN));
}
/**
* Add stopover point to routing request.
*
* @param point geographical position (see GHPoint)
* @param favoredHeading north based azimuth (clockwise) in (0, 360) or NaN for equal preference
*/
public GHRequest addPoint(GHPoint point, double favoredHeading) {
if (point == null)
throw new IllegalArgumentException("point cannot be null");
if (!possibleToAdd)
throw new IllegalStateException("Please call empty constructor if you intent to use "
+ "more than two places via addPoint method.");
points.add(point);
validateAzimuthValue(favoredHeading);
favoredHeadings.add(favoredHeading);
return this;
}
/**
* Add stopover point to routing request.
*
* @param point geographical position (see GHPoint)
*/
public GHRequest addPoint(GHPoint point) {
addPoint(point, Double.NaN);
return this;
}
public void setPoints(List<GHPoint> points) {
this.points = points;
if (favoredHeadings.isEmpty())
this.favoredHeadings = Collections.nCopies(points.size(), Double.NaN);
}
public void setHeadings(List<Double> favoredHeadings) {
this.favoredHeadings = favoredHeadings;
}
/**
* @return north based azimuth (clockwise) in (0, 360) or NaN for equal preference
*/
public double getFavoredHeading(int i) {
if (favoredHeadings.size() != points.size())
throw new IllegalStateException("Wrong size of headings " + favoredHeadings.size() + " vs. point count " + points.size());
return favoredHeadings.get(i);
}
/**
* @return if there exist a preferred heading for start/via/end point i
*/
public boolean hasFavoredHeading(int i) {
if (i >= favoredHeadings.size())
return false;
return !Double.isNaN(favoredHeadings.get(i));
}
private void validateAzimuthValue(double heading) {
// heading must be in (0, 360) oder NaN
if (!Double.isNaN(heading) && (Double.compare(heading, 360) > 0 || Double.compare(heading, 0) < 0))
throw new IllegalArgumentException("Heading " + heading + " must be in range (0,360) or NaN");
}
public List<GHPoint> getPoints() {
return points;
}
public String getAlgorithm() {
return algo;
}
/**
* For possible values see AlgorithmOptions.*
*/
public GHRequest setAlgorithm(String algo) {
if (algo != null)
this.algo = Helper.camelCaseToUnderScore(algo);
return this;
}
public Locale getLocale() {
return locale;
}
public GHRequest setLocale(Locale locale) {
if (locale != null)
this.locale = locale;
return this;
}
public GHRequest setLocale(String localeStr) {
return setLocale(Helper.getLocale(localeStr));
}
public String getProfile() {
return profile;
}
public GHRequest setProfile(String profile) {
this.profile = profile;
return this;
}
public String getWeighting() {
return hints.getWeighting();
}
/**
* By default it supports fastest and shortest. Or specify empty to use default.
*/
public GHRequest setWeighting(String w) {
hints.setWeighting(w);
return this;
}
public String getVehicle() {
return hints.getVehicle();
}
/**
* Specify car, bike or foot. Or specify empty to use default.
*/
public GHRequest setVehicle(String vehicle) {
hints.setVehicle(vehicle);
return this;
}
public HintsMap getHints() {
return hints;
}
/**
* This method sets a key value pair in the hints and is unrelated to the setPointHints method.
* It is mainly used for deserialization with Jackson.
*
* @see #setPointHints(List)
*/
public GHRequest putHint(String fieldName, Object value) {
this.hints.putObject(fieldName, value);
return this;
}
public GHRequest setPointHints(List<String> pointHints) {
this.pointHints = pointHints;
return this;
}
public List<String> getPointHints() {
return pointHints;
}
public boolean hasPointHints() {
return pointHints.size() == points.size() && !points.isEmpty();
}
public GHRequest setCurbsides(List<String> curbsides) {
this.curbsides = curbsides;
return this;
}
public List<String> getCurbsides() {
return curbsides;
}
public boolean hasCurbsides() {
return curbsides.size() == points.size() && !points.isEmpty();
}
public GHRequest setSnapPreventions(List<String> snapPreventions) {
this.snapPreventions = snapPreventions;
return this;
}
public boolean hasSnapPreventions() {
return !snapPreventions.isEmpty();
}
public List<String> getSnapPreventions() {
return snapPreventions;
}
public GHRequest setPathDetails(List<String> pathDetails) {
this.pathDetails = pathDetails;
return this;
}
public List<String> getPathDetails() {
return this.pathDetails;
}
@Override
public String toString() {
String res = "";
for (GHPoint point : points) {
if (res.isEmpty()) {
res = point.toString();
} else {
res += "; " + point.toString();
}
}
if (!algo.isEmpty())
res += " (" + algo + ")";
if (!pathDetails.isEmpty())
res += " (PathDetails: " + pathDetails + ")";
if (!hints.isEmpty())
res += " (Hints:" + hints + ")";
return res;
}
}