-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathRoutingContext.java
More file actions
538 lines (468 loc) · 18.6 KB
/
Copy pathRoutingContext.java
File metadata and controls
538 lines (468 loc) · 18.6 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/**
* Container for routig configs
*
* @author ab
*/
package btools.router;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import btools.expressions.BExpressionContext;
import btools.expressions.BExpressionContextNode;
import btools.expressions.BExpressionContextWay;
import btools.mapaccess.GeometryDecoder;
import btools.mapaccess.MatchedWaypoint;
import btools.mapaccess.OsmLink;
import btools.mapaccess.OsmNode;
import btools.util.CheapAngleMeter;
import btools.util.CheapRuler;
public final class RoutingContext {
public void setAlternativeIdx(int idx) {
alternativeIdx = idx;
}
public int getAlternativeIdx(int min, int max) {
return alternativeIdx < min ? min : (alternativeIdx > max ? max : alternativeIdx);
}
public int alternativeIdx = 0;
public String localFunction;
public long profileTimestamp;
public Map<String, String> keyValues;
public String rawTrackPath;
public String rawAreaPath;
public String getProfileName() {
String name = localFunction == null ? "unknown" : localFunction;
if (name.endsWith(".brf")) name = name.substring(0, localFunction.length() - 4);
int idx = name.lastIndexOf(File.separatorChar);
if (idx >= 0) name = name.substring(idx + 1);
return name;
}
public BExpressionContextWay expctxWay;
public BExpressionContextNode expctxNode;
public String userOutputHeader;
public GeometryDecoder geometryDecoder = new GeometryDecoder();
public int memoryclass = 64;
public boolean carMode;
public boolean bikeMode;
public boolean footMode;
public boolean considerTurnRestrictions;
public boolean processUnusedTags;
public boolean forceSecondaryData;
public double pass1coefficient;
public double pass2coefficient;
public int elevationpenaltybuffer;
public int elevationmaxbuffer;
public int elevationbufferreduce;
public double cost1speed;
public double additionalcostfactor;
public double changetime;
public double buffertime;
public double waittimeadjustment;
public double inittimeadjustment;
public double starttimeoffset;
public boolean transitonly;
public double waypointCatchingRange;
public boolean correctMisplacedViaPoints;
public double correctMisplacedViaPointsDistance;
public boolean continueStraight;
public boolean useDynamicDistance;
public boolean buildBeelineOnRange;
public AreaInfo ai;
private void setModel(String className) {
if (className == null) {
pm = new StdModel();
} else {
try {
Class<?> clazz = Class.forName(className);
pm = (OsmPathModel) clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Cannot create path-model: " + e);
}
}
initModel();
}
public void initModel() {
pm.init(expctxWay, expctxNode, keyValues);
}
public long getKeyValueChecksum() {
long s = 0L;
if (keyValues != null) {
for (Map.Entry<String, String> e : keyValues.entrySet()) {
s += e.getKey().hashCode() + e.getValue().hashCode();
}
}
return s;
}
public void readGlobalConfig() {
BExpressionContext expctxGlobal = expctxWay; // just one of them...
setModel(expctxGlobal._modelClass);
carMode = 0.f != expctxGlobal.getVariableValue("validForCars", 0.f);
bikeMode = 0.f != expctxGlobal.getVariableValue("validForBikes", 0.f);
footMode = 0.f != expctxGlobal.getVariableValue("validForFoot", 0.f);
waypointCatchingRange = expctxGlobal.getVariableValue("waypointCatchingRange", 250.f);
// turn-restrictions not used per default for foot profiles
considerTurnRestrictions = 0.f != expctxGlobal.getVariableValue("considerTurnRestrictions", footMode ? 0.f : 1.f);
correctMisplacedViaPoints = 0.f != expctxGlobal.getVariableValue("correctMisplacedViaPoints", 0.f);
correctMisplacedViaPointsDistance = expctxGlobal.getVariableValue("correctMisplacedViaPointsDistance", 400.f); // 0 == don't use distance
continueStraight = 0.f != expctxGlobal.getVariableValue("continueStraight", 0.f);
// process tags not used in the profile (to have them in the data-tab)
processUnusedTags = 0.f != expctxGlobal.getVariableValue("processUnusedTags", 0.f);
forceSecondaryData = 0.f != expctxGlobal.getVariableValue("forceSecondaryData", 0.f);
pass1coefficient = expctxGlobal.getVariableValue("pass1coefficient", 1.5f);
pass2coefficient = expctxGlobal.getVariableValue("pass2coefficient", 0.f);
elevationpenaltybuffer = (int) (expctxGlobal.getVariableValue("elevationpenaltybuffer", 5.f) * 1000000);
elevationmaxbuffer = (int) (expctxGlobal.getVariableValue("elevationmaxbuffer", 10.f) * 1000000);
elevationbufferreduce = (int) (expctxGlobal.getVariableValue("elevationbufferreduce", 0.f) * 10000);
cost1speed = expctxGlobal.getVariableValue("cost1speed", 22.f);
additionalcostfactor = expctxGlobal.getVariableValue("additionalcostfactor", 1.5f);
changetime = expctxGlobal.getVariableValue("changetime", 180.f);
buffertime = expctxGlobal.getVariableValue("buffertime", 120.f);
waittimeadjustment = expctxGlobal.getVariableValue("waittimeadjustment", 0.9f);
inittimeadjustment = expctxGlobal.getVariableValue("inittimeadjustment", 0.2f);
starttimeoffset = expctxGlobal.getVariableValue("starttimeoffset", 0.f);
transitonly = expctxGlobal.getVariableValue("transitonly", 0.f) != 0.f;
showspeed = 0.f != expctxGlobal.getVariableValue("showspeed", 0.f);
showSpeedProfile = 0.f != expctxGlobal.getVariableValue("showSpeedProfile", 0.f);
inverseRouting = 0.f != expctxGlobal.getVariableValue("inverseRouting", 0.f);
showTime = 0.f != expctxGlobal.getVariableValue("showtime", 0.f);
int tiMode = (int) expctxGlobal.getVariableValue("turnInstructionMode", 0.f);
if (tiMode != 1) { // automatic selection from coordinate source
turnInstructionMode = tiMode;
}
turnInstructionCatchingRange = expctxGlobal.getVariableValue("turnInstructionCatchingRange", 40.f);
turnInstructionRoundabouts = expctxGlobal.getVariableValue("turnInstructionRoundabouts", footMode ? 0.f : 1.f) != 0.f;
// Speed computation model (for bikes)
// Total mass (biker + bike + luggages or hiker), in kg
totalMass = expctxGlobal.getVariableValue("totalMass", 90.f);
// Max speed (before braking), in km/h in profile and m/s in code
if (footMode) {
maxSpeed = expctxGlobal.getVariableValue("maxSpeed", 6.f) / 3.6;
} else {
maxSpeed = expctxGlobal.getVariableValue("maxSpeed", 45.f) / 3.6;
}
// Equivalent surface for wind, S * C_x, F = -1/2 * S * C_x * v^2 = - S_C_x * v^2
S_C_x = expctxGlobal.getVariableValue("S_C_x", 0.5f * 0.45f);
// Default resistance of the road, F = - m * g * C_r (for good quality road)
defaultC_r = expctxGlobal.getVariableValue("C_r", 0.01f);
// Constant power of the biker (in W)
bikerPower = expctxGlobal.getVariableValue("bikerPower", 100.f);
useDynamicDistance = expctxGlobal.getVariableValue("use_dynamic_range", 1f) == 1f;
buildBeelineOnRange = expctxGlobal.getVariableValue("add_beeline", 0f) == 1f;
boolean test = expctxGlobal.getVariableValue("check_start_way", 1f) == 1f;
if (!test) freeNoWays();
}
public void freeNoWays() {
BExpressionContext expctxGlobal = expctxWay;
if (expctxGlobal != null) expctxGlobal.freeNoWays();
}
public List<OsmNodeNamed> poipoints;
public List<OsmNodeNamed> nogopoints = null;
private List<OsmNodeNamed> nogopoints_all = null; // full list not filtered for wayoints-in-nogos
private List<OsmNodeNamed> keepnogopoints = null;
private OsmNodeNamed pendingEndpoint = null;
public Integer startDirection;
public boolean startDirectionValid;
public boolean forceUseStartDirection;
public Integer roundTripDistance;
public Integer roundTripDirectionAdd;
public Integer roundTripPoints;
public boolean allowSamewayback;
public CheapAngleMeter anglemeter = new CheapAngleMeter();
public double nogoCost = 0.;
public boolean isEndpoint = false;
public boolean shortestmatch = false;
public double wayfraction;
public int ilatshortest;
public int ilonshortest;
public boolean inverseDirection;
public boolean showspeed;
public boolean showSpeedProfile;
public boolean inverseRouting;
public boolean showTime;
public boolean hasDirectRouting;
public String outputFormat = "gpx";
public boolean exportWaypoints = false;
public boolean exportCorrectedWaypoints = false;
public OsmPrePath firstPrePath;
public int turnInstructionMode; // 0=none, 1=auto, 2=locus, 3=osmand, 4=comment-style, 5=gpsies-style
public double turnInstructionCatchingRange;
public boolean turnInstructionRoundabouts;
// Speed computation model (for bikes)
public double totalMass;
public double maxSpeed;
public double S_C_x;
public double defaultC_r;
public double bikerPower;
public static void prepareNogoPoints(List<OsmNodeNamed> nogos) {
for (OsmNodeNamed nogo : nogos) {
if (nogo instanceof OsmNogoPolygon) {
continue;
}
String s = nogo.name;
int idx = s.indexOf(' ');
if (idx > 0) s = s.substring(0, idx);
int ir = 20; // default radius
if (s.length() > 4) {
try {
ir = Integer.parseInt(s.substring(4));
} catch (Exception e) { /* ignore */ }
}
// Radius of the nogo point in meters
nogo.radius = ir;
}
}
/**
* restore the full nogolist previously saved by cleanNogoList
*/
public void restoreNogoList() {
nogopoints = nogopoints_all;
}
/**
* clean the nogolist (previoulsy saved by saveFullNogolist())
* by removing nogos with waypoints within
*
* @return true if all wayoints are all in the same (full-weigth) nogo area (triggering bee-line-mode)
*/
public void cleanNogoList(List<OsmNode> waypoints) {
nogopoints_all = nogopoints;
if (nogopoints == null) return;
List<OsmNodeNamed> nogos = new ArrayList<>();
for (OsmNodeNamed nogo : nogopoints) {
boolean goodGuy = true;
for (OsmNode wp : waypoints) {
if (wp.calcDistance(nogo) < nogo.radius
&& (!(nogo instanceof OsmNogoPolygon)
|| (((OsmNogoPolygon) nogo).isClosed
? ((OsmNogoPolygon) nogo).isWithin(wp.ilon, wp.ilat)
: ((OsmNogoPolygon) nogo).isOnPolyline(wp.ilon, wp.ilat)))) {
goodGuy = false;
}
}
if (goodGuy) nogos.add(nogo);
}
nogopoints = nogos.isEmpty() ? null : nogos;
}
public void checkMatchedWaypointAgainstNogos(List<MatchedWaypoint> matchedWaypoints) {
if (nogopoints == null) return;
int theSize = matchedWaypoints.size();
if (theSize<2) return;
int removed = 0;
List<MatchedWaypoint> newMatchedWaypoints = new ArrayList<>();
MatchedWaypoint prevMwp = null;
boolean prevMwpIsInside = false;
for (int i = 0; i < theSize; i++) {
MatchedWaypoint mwp = matchedWaypoints.get(i);
boolean isInsideNogo = false;
OsmNode wp = mwp.crosspoint;
for (OsmNodeNamed nogo : nogopoints) {
if (Double.isNaN(nogo.nogoWeight)
&& wp.calcDistance(nogo) < nogo.radius
&& (!(nogo instanceof OsmNogoPolygon)
|| (((OsmNogoPolygon) nogo).isClosed
? ((OsmNogoPolygon) nogo).isWithin(wp.ilon, wp.ilat)
: ((OsmNogoPolygon) nogo).isOnPolyline(wp.ilon, wp.ilat)))) {
isInsideNogo = true;
break;
}
}
if (isInsideNogo) {
boolean useAnyway = false;
if (prevMwp == null) useAnyway = true;
else if (mwp.wpttype == MatchedWaypoint.WAYPOINT_TYPE_DIRECT) useAnyway = true;
else if (prevMwp.wpttype == MatchedWaypoint.WAYPOINT_TYPE_DIRECT) useAnyway = true;
else if (prevMwpIsInside) useAnyway = true;
else if (i == theSize-1) {
throw new IllegalArgumentException("last wpt in restricted area ");
}
if (useAnyway) {
prevMwpIsInside = true;
newMatchedWaypoints.add(mwp);
} else {
removed++;
prevMwpIsInside = false;
}
} else {
prevMwpIsInside = false;
newMatchedWaypoints.add(mwp);
}
prevMwp = mwp;
}
if (newMatchedWaypoints.size() < 2) {
throw new IllegalArgumentException("a wpt in restricted area ");
}
if (removed > 0) {
matchedWaypoints.clear();
matchedWaypoints.addAll(newMatchedWaypoints);
}
}
public boolean allInOneNogo(List<OsmNode> waypoints) {
if (nogopoints == null) return false;
boolean allInTotal = false;
for (OsmNodeNamed nogo : nogopoints) {
boolean allIn = Double.isNaN(nogo.nogoWeight);
for (OsmNode wp : waypoints) {
int dist = wp.calcDistance(nogo);
if (dist < nogo.radius
&& (!(nogo instanceof OsmNogoPolygon)
|| (((OsmNogoPolygon) nogo).isClosed
? ((OsmNogoPolygon) nogo).isWithin(wp.ilon, wp.ilat)
: ((OsmNogoPolygon) nogo).isOnPolyline(wp.ilon, wp.ilat)))) {
continue;
}
allIn = false;
}
allInTotal |= allIn;
}
return allInTotal;
}
public long[] getNogoChecksums() {
long[] cs = new long[3];
int n = nogopoints == null ? 0 : nogopoints.size();
for (int i = 0; i < n; i++) {
OsmNodeNamed nogo = nogopoints.get(i);
cs[0] += nogo.ilon;
cs[1] += nogo.ilat;
// 10 is an arbitrary constant to get sub-integer precision in the checksum
cs[2] += (long) (nogo.radius * 10.);
}
return cs;
}
public void setWaypoint(OsmNodeNamed wp, boolean endpoint) {
setWaypoint(wp, null, endpoint);
}
public void setWaypoint(OsmNodeNamed wp, OsmNodeNamed pendingEndpoint, boolean endpoint) {
keepnogopoints = nogopoints;
nogopoints = new ArrayList<>();
nogopoints.add(wp);
if (keepnogopoints != null) nogopoints.addAll(keepnogopoints);
isEndpoint = endpoint;
this.pendingEndpoint = pendingEndpoint;
}
public boolean checkPendingEndpoint() {
if (pendingEndpoint != null) {
isEndpoint = true;
nogopoints.set(0, pendingEndpoint);
pendingEndpoint = null;
return true;
}
return false;
}
public void unsetWaypoint() {
nogopoints = keepnogopoints;
pendingEndpoint = null;
isEndpoint = false;
}
public int calcDistance(int lon1, int lat1, int lon2, int lat2) {
double[] lonlat2m = CheapRuler.getLonLatToMeterScales((lat1 + lat2) >> 1);
double dlon2m = lonlat2m[0];
double dlat2m = lonlat2m[1];
double dx = (lon2 - lon1) * dlon2m;
double dy = (lat2 - lat1) * dlat2m;
double d = Math.sqrt(dy * dy + dx * dx);
shortestmatch = false;
if (nogopoints != null && !nogopoints.isEmpty() && d > 0.) {
for (int ngidx = 0; ngidx < nogopoints.size(); ngidx++) {
OsmNodeNamed nogo = nogopoints.get(ngidx);
double x1 = (lon1 - nogo.ilon) * dlon2m;
double y1 = (lat1 - nogo.ilat) * dlat2m;
double x2 = (lon2 - nogo.ilon) * dlon2m;
double y2 = (lat2 - nogo.ilat) * dlat2m;
double r12 = x1 * x1 + y1 * y1;
double r22 = x2 * x2 + y2 * y2;
double radius = Math.abs(r12 < r22 ? y1 * dx - x1 * dy : y2 * dx - x2 * dy) / d;
if (radius < nogo.radius) { // 20m
double s1 = x1 * dx + y1 * dy;
double s2 = x2 * dx + y2 * dy;
if (s1 < 0.) {
s1 = -s1;
s2 = -s2;
}
if (s2 > 0.) {
radius = Math.sqrt(s1 < s2 ? r12 : r22);
if (radius > nogo.radius) continue;
}
if (nogo.isNogo) {
if (!(nogo instanceof OsmNogoPolygon)) { // nogo is a circle
if (Double.isNaN(nogo.nogoWeight)) {
// default nogo behaviour (ignore completely)
nogoCost = -1;
} else {
// nogo weight, compute distance within the circle
nogoCost = nogo.distanceWithinRadius(lon1, lat1, lon2, lat2, d) * nogo.nogoWeight;
}
} else if (((OsmNogoPolygon) nogo).intersects(lon1, lat1, lon2, lat2)) {
// nogo is a polyline/polygon, we have to check there is indeed
// an intersection in this case (radius check is not enough).
if (Double.isNaN(nogo.nogoWeight)) {
// default nogo behaviour (ignore completely)
nogoCost = -1;
} else {
if (((OsmNogoPolygon) nogo).isClosed) {
// compute distance within the polygon
nogoCost = ((OsmNogoPolygon) nogo).distanceWithinPolygon(lon1, lat1, lon2, lat2) * nogo.nogoWeight;
} else {
// for a polyline, just add a constant penalty
nogoCost = nogo.nogoWeight;
}
}
}
} else {
shortestmatch = true;
nogo.radius = radius; // shortest distance to way
// calculate remaining distance
if (s2 < 0.) {
wayfraction = -s2 / (d * d);
double xm = x2 - wayfraction * dx;
double ym = y2 - wayfraction * dy;
ilonshortest = (int) (xm / dlon2m + nogo.ilon);
ilatshortest = (int) (ym / dlat2m + nogo.ilat);
} else if (s1 > s2) {
wayfraction = 0.;
ilonshortest = lon2;
ilatshortest = lat2;
} else {
wayfraction = 1.;
ilonshortest = lon1;
ilatshortest = lat1;
}
// here it gets nasty: there can be nogo-points in the list
// *after* the shortest distance point. In case of a shortest-match
// we use the reduced way segment for nogo-matching, in order not
// to cut our escape-way if we placed a nogo just in front of where we are
if (isEndpoint) {
wayfraction = 1. - wayfraction;
lon2 = ilonshortest;
lat2 = ilatshortest;
} else {
nogoCost = 0.;
lon1 = ilonshortest;
lat1 = ilatshortest;
}
dx = (lon2 - lon1) * dlon2m;
dy = (lat2 - lat1) * dlat2m;
d = Math.sqrt(dy * dy + dx * dx);
}
}
}
}
return (int) Math.max(1.0, Math.round(d));
}
public OsmPathModel pm;
public OsmPrePath createPrePath(OsmPath origin, OsmLink link) {
OsmPrePath p = pm.createPrePath();
if (p != null) {
p.init(origin, link, this);
}
return p;
}
public OsmPath createPath(OsmLink link) {
OsmPath p = pm.createPath();
p.init(link);
return p;
}
public OsmPath createPath(OsmPath origin, OsmLink link, OsmTrack refTrack, boolean detailMode) {
OsmPath p = pm.createPath();
p.init(origin, link, refTrack, detailMode, this);
return p;
}
}