/
InstructionsFromEdges.java
478 lines (413 loc) · 22 KB
/
InstructionsFromEdges.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
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
/*
* 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.routing;
import com.graphhopper.routing.profiles.*;
import com.graphhopper.routing.util.DefaultEdgeFilter;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.Graph;
import com.graphhopper.storage.IntsRef;
import com.graphhopper.storage.NodeAccess;
import com.graphhopper.util.*;
import com.graphhopper.util.shapes.GHPoint;
import static com.graphhopper.routing.util.EncodingManager.getKey;
/**
* This class calculates instructions from the edges in a Path.
*
* @author Peter Karich
* @author Robin Boldt
* @author jan soe
*/
public class InstructionsFromEdges implements Path.EdgeVisitor {
private final Weighting weighting;
private final FlagEncoder encoder;
private final NodeAccess nodeAccess;
private final Translation tr;
private final InstructionList ways;
private final EdgeExplorer outEdgeExplorer;
private final EdgeExplorer crossingExplorer;
private final BooleanEncodedValue roundaboutEnc;
private final BooleanEncodedValue accessEnc;
private final BooleanEncodedValue getOffBikeEnc;
private final EnumEncodedValue<RouteNetwork> bikeRouteEnc;
private final EnumEncodedValue<RoadClass> roadClassEnc;
private final EnumEncodedValue<RoadEnvironment> roadEnvEnc;
/*
* We need three points to make directions
*
* (1)----(2)
* /
* /
* (0)
*
* 0 is the node visited at t-2, 1 is the node visited
* at t-1 and 2 is the node being visited at instant t.
* orientation is the angle of the vector(1->2) expressed
* as atan2, while previousOrientation is the angle of the
* vector(0->1)
* Intuitively, if orientation is smaller than
* previousOrientation, then we have to turn right, while
* if it is greater we have to turn left. To make this
* algorithm work, we need to make the comparison by
* considering orientation belonging to the interval
* [ - pi + previousOrientation , + pi + previousOrientation ]
*/
private EdgeIteratorState prevEdge;
private double prevLat;
private double prevLon;
private double doublePrevLat, doublePrevLon; // Lat and Lon of node t-2
private int prevNode;
private double prevOrientation;
private double prevInstructionPrevOrientation = Double.NaN;
private Instruction prevInstruction;
private boolean prevInRoundabout;
private String prevName;
private String prevInstructionName;
private InstructionAnnotation prevAnnotation;
private final int MAX_U_TURN_DISTANCE = 35;
public InstructionsFromEdges(Graph graph, Weighting weighting, EncodedValueLookup evLookup,
Translation tr, InstructionList ways) {
this.encoder = weighting.getFlagEncoder();
this.weighting = weighting;
this.accessEnc = evLookup.getBooleanEncodedValue(getKey(encoder.toString(), "access"));
this.roundaboutEnc = evLookup.getBooleanEncodedValue(Roundabout.KEY);
// both EncodedValues are optional and only used when bike encoders are added
String key = getKey("bike", RouteNetwork.EV_SUFFIX);
this.bikeRouteEnc = evLookup.hasEncodedValue(key) ? evLookup.getEnumEncodedValue(key, RouteNetwork.class) : null;
this.getOffBikeEnc = evLookup.hasEncodedValue(GetOffBike.KEY) ? evLookup.getBooleanEncodedValue(GetOffBike.KEY) : null;
this.roadClassEnc = evLookup.getEnumEncodedValue(RoadClass.KEY, RoadClass.class);
this.roadEnvEnc = evLookup.getEnumEncodedValue(RoadEnvironment.KEY, RoadEnvironment.class);
this.nodeAccess = graph.getNodeAccess();
this.tr = tr;
this.ways = ways;
prevNode = -1;
prevInRoundabout = false;
prevName = null;
outEdgeExplorer = graph.createEdgeExplorer(DefaultEdgeFilter.outEdges(encoder));
crossingExplorer = graph.createEdgeExplorer(DefaultEdgeFilter.allEdges(encoder));
}
/**
* @return the list of instructions for this path.
*/
public static InstructionList calcInstructions(Path path, Graph graph, Weighting weighting, EncodedValueLookup evLookup, final Translation tr) {
final InstructionList ways = new InstructionList(tr);
if (path.isFound()) {
if (path.getSize() == 0) {
ways.add(new FinishInstruction(graph.getNodeAccess(), path.getEndNode()));
} else {
path.forEveryEdge(new InstructionsFromEdges(graph, weighting, evLookup, tr, ways));
}
}
return ways;
}
@Override
public void next(EdgeIteratorState edge, int index, int prevEdgeId) {
if (prevNode == -1) {
prevLat = this.nodeAccess.getLatitude(edge.getBaseNode());
prevLon = this.nodeAccess.getLongitude(edge.getBaseNode());
}
// baseNode is the current node and adjNode is the next
int adjNode = edge.getAdjNode();
int baseNode = edge.getBaseNode();
IntsRef flags = edge.getFlags();
double adjLat = nodeAccess.getLatitude(adjNode);
double adjLon = nodeAccess.getLongitude(adjNode);
double latitude, longitude;
PointList wayGeo = edge.fetchWayGeometry(3);
boolean isRoundabout = roundaboutEnc.getBool(false, flags);
if (wayGeo.getSize() <= 2) {
latitude = adjLat;
longitude = adjLon;
} else {
latitude = wayGeo.getLatitude(1);
longitude = wayGeo.getLongitude(1);
assert Double.compare(prevLat, nodeAccess.getLatitude(baseNode)) == 0;
assert Double.compare(prevLon, nodeAccess.getLongitude(baseNode)) == 0;
}
String name = edge.getName();
InstructionAnnotation annotation = InstructionAnnotation.EMPTY;
if (getOffBikeEnc != null) {
// only for bikes do
if (edge.get(roadClassEnc) == RoadClass.CYCLEWAY
|| bikeRouteEnc != null && edge.get(bikeRouteEnc) != RouteNetwork.OTHER) {
// for backward compatibility
annotation = new InstructionAnnotation(0, tr.tr("cycleway"));
} else if (edge.get(getOffBikeEnc)) {
annotation = new InstructionAnnotation(1, tr.tr("off_bike"));
}
} else if (edge.get(roadEnvEnc) == RoadEnvironment.FORD) {
annotation = new InstructionAnnotation(1, tr.tr("way_contains_ford"));
}
if ((prevName == null) && (!isRoundabout)) // very first instruction (if not in Roundabout)
{
int sign = Instruction.CONTINUE_ON_STREET;
prevInstruction = new Instruction(sign, name, annotation, new PointList(10, nodeAccess.is3D()));
double startLat = nodeAccess.getLat(baseNode);
double startLon = nodeAccess.getLon(baseNode);
double heading = Helper.ANGLE_CALC.calcAzimuth(startLat, startLon, latitude, longitude);
prevInstruction.setExtraInfo("heading", Helper.round(heading, 2));
ways.add(prevInstruction);
prevName = name;
prevAnnotation = annotation;
} else if (isRoundabout) {
// remark: names and annotations within roundabout are ignored
if (!prevInRoundabout) //just entered roundabout
{
int sign = Instruction.USE_ROUNDABOUT;
RoundaboutInstruction roundaboutInstruction = new RoundaboutInstruction(sign, name,
annotation, new PointList(10, nodeAccess.is3D()));
prevInstructionPrevOrientation = prevOrientation;
if (prevName != null) {
// check if there is an exit at the same node the roundabout was entered
EdgeIterator edgeIter = outEdgeExplorer.setBaseNode(baseNode);
while (edgeIter.next()) {
if ((edgeIter.getAdjNode() != prevNode)
&& !roundaboutEnc.getBool(false, edgeIter.getFlags())) {
roundaboutInstruction.increaseExitNumber();
break;
}
}
// previous orientation is last orientation before entering roundabout
prevOrientation = Helper.ANGLE_CALC.calcOrientation(doublePrevLat, doublePrevLon, prevLat, prevLon);
// calculate direction of entrance turn to determine direction of rotation
// right turn == counterclockwise and vice versa
double orientation = Helper.ANGLE_CALC.calcOrientation(prevLat, prevLon, latitude, longitude);
orientation = Helper.ANGLE_CALC.alignOrientation(prevOrientation, orientation);
double delta = (orientation - prevOrientation);
roundaboutInstruction.setDirOfRotation(delta);
} else // first instructions is roundabout instruction
{
prevOrientation = Helper.ANGLE_CALC.calcOrientation(prevLat, prevLon, latitude, longitude);
prevName = name;
prevAnnotation = annotation;
}
prevInstruction = roundaboutInstruction;
ways.add(prevInstruction);
}
// Add passed exits to instruction. A node is counted if there is at least one outgoing edge
// out of the roundabout
EdgeIterator edgeIter = outEdgeExplorer.setBaseNode(edge.getAdjNode());
while (edgeIter.next()) {
if (!roundaboutEnc.getBool(false, edgeIter.getFlags())) {
((RoundaboutInstruction) prevInstruction).increaseExitNumber();
break;
}
}
} else if (prevInRoundabout) //previously in roundabout but not anymore
{
prevInstruction.setName(name);
// calc angle between roundabout entrance and exit
double orientation = Helper.ANGLE_CALC.calcOrientation(prevLat, prevLon, latitude, longitude);
orientation = Helper.ANGLE_CALC.alignOrientation(prevOrientation, orientation);
double deltaInOut = (orientation - prevOrientation);
// calculate direction of exit turn to determine direction of rotation
// right turn == counterclockwise and vice versa
double recentOrientation = Helper.ANGLE_CALC.calcOrientation(doublePrevLat, doublePrevLon, prevLat, prevLon);
orientation = Helper.ANGLE_CALC.alignOrientation(recentOrientation, orientation);
double deltaOut = (orientation - recentOrientation);
prevInstruction = ((RoundaboutInstruction) prevInstruction)
.setRadian(deltaInOut)
.setDirOfRotation(deltaOut)
.setExited();
prevInstructionName = prevName;
prevName = name;
prevAnnotation = annotation;
} else {
int sign = getTurn(edge, baseNode, prevNode, adjNode, annotation, name);
if (sign != Instruction.IGNORE) {
/*
Check if the next instruction is likely to only be a short connector to execute a u-turn
--A->--
| <-- This is the short connector
--B-<--
Road A and Road B have to have the same name and roughly the same, but opposite orientation, otherwise we are assuming this is no u-turn.
Note: This approach only works if there a turn instruction fro A->Connector and Connector->B.
Currently we don't create a turn instruction if there is no other possible turn
We only create a u-turn if edge B is a one-way, see #1073 for more details.
*/
boolean isUTurn = false;
int uTurnType = Instruction.U_TURN_UNKNOWN;
if (!Double.isNaN(prevInstructionPrevOrientation)
&& prevInstruction.getDistance() < MAX_U_TURN_DISTANCE
&& (sign < 0) == (prevInstruction.getSign() < 0)
&& (Math.abs(sign) == Instruction.TURN_SLIGHT_RIGHT || Math.abs(sign) == Instruction.TURN_RIGHT || Math.abs(sign) == Instruction.TURN_SHARP_RIGHT)
&& (Math.abs(prevInstruction.getSign()) == Instruction.TURN_SLIGHT_RIGHT || Math.abs(prevInstruction.getSign()) == Instruction.TURN_RIGHT || Math.abs(prevInstruction.getSign()) == Instruction.TURN_SHARP_RIGHT)
&& edge.get(accessEnc) != edge.getReverse(accessEnc)
&& InstructionsHelper.isNameSimilar(prevInstructionName, name)) {
// Chances are good that this is a u-turn, we only need to check if the orientation matches
GHPoint point = InstructionsHelper.getPointForOrientationCalculation(edge, nodeAccess);
double lat = point.getLat();
double lon = point.getLon();
double currentOrientation = Helper.ANGLE_CALC.calcOrientation(prevLat, prevLon, lat, lon, false);
double diff = Math.abs(prevInstructionPrevOrientation - currentOrientation);
if (diff > (Math.PI * .9) && diff < (Math.PI * 1.1)) {
isUTurn = true;
if (sign < 0) {
uTurnType = Instruction.U_TURN_LEFT;
} else {
uTurnType = Instruction.U_TURN_RIGHT;
}
}
}
if (isUTurn) {
prevInstruction.setSign(uTurnType);
prevInstruction.setName(name);
} else {
prevInstruction = new Instruction(sign, name, annotation, new PointList(10, nodeAccess.is3D()));
// Remember the Orientation and name of the road, before doing this maneuver
prevInstructionPrevOrientation = prevOrientation;
prevInstructionName = prevName;
ways.add(prevInstruction);
prevAnnotation = annotation;
}
}
// Update the prevName, since we don't always create an instruction on name changes the previous
// name can be an old name. This leads to incorrect turn instructions due to name changes
prevName = name;
}
updatePointsAndInstruction(edge, wayGeo);
if (wayGeo.getSize() <= 2) {
doublePrevLat = prevLat;
doublePrevLon = prevLon;
} else {
int beforeLast = wayGeo.getSize() - 2;
doublePrevLat = wayGeo.getLatitude(beforeLast);
doublePrevLon = wayGeo.getLongitude(beforeLast);
}
prevInRoundabout = isRoundabout;
prevNode = baseNode;
prevLat = adjLat;
prevLon = adjLon;
prevEdge = edge;
}
@Override
public void finish() {
if (prevInRoundabout) {
// calc angle between roundabout entrance and finish
double orientation = Helper.ANGLE_CALC.calcOrientation(doublePrevLat, doublePrevLon, prevLat, prevLon);
orientation = Helper.ANGLE_CALC.alignOrientation(prevOrientation, orientation);
double delta = (orientation - prevOrientation);
((RoundaboutInstruction) prevInstruction).setRadian(delta);
}
Instruction finishInstruction = new FinishInstruction(nodeAccess, prevEdge.getAdjNode());
// This is the heading how the edge ended
finishInstruction.setExtraInfo("last_heading", Helper.ANGLE_CALC.calcAzimuth(doublePrevLat, doublePrevLon, prevLat, prevLon));
ways.add(finishInstruction);
}
private int getTurn(EdgeIteratorState edge, int baseNode, int prevNode, int adjNode, InstructionAnnotation annotation, String name) {
GHPoint point = InstructionsHelper.getPointForOrientationCalculation(edge, nodeAccess);
double lat = point.getLat();
double lon = point.getLon();
prevOrientation = Helper.ANGLE_CALC.calcOrientation(doublePrevLat, doublePrevLon, prevLat, prevLon);
int sign = InstructionsHelper.calculateSign(prevLat, prevLon, lat, lon, prevOrientation);
boolean forceInstruction = false;
if (!annotation.equals(prevAnnotation) && !annotation.isEmpty()) {
forceInstruction = true;
}
InstructionsOutgoingEdges outgoingEdges = new InstructionsOutgoingEdges(prevEdge, edge, encoder, crossingExplorer, nodeAccess, prevNode, baseNode, adjNode);
int nrOfPossibleTurns = outgoingEdges.nrOfAllowedOutgoingEdges();
// there is no other turn possible
if (nrOfPossibleTurns <= 1) {
if (Math.abs(sign) > 1 && outgoingEdges.nrOfAllOutgoingEdges() > 1) {
// This is an actual turn because |sign| > 1
// There could be some confusion, if we would not create a turn instruction, even though it is the only
// possible turn, also see #1048
// TODO if we see issue with this approach we could consider checking if the edge is a oneway
return sign;
}
return returnForcedInstructionOrIgnore(forceInstruction, sign);
}
// Very certain, this is a turn
if (Math.abs(sign) > 1) {
/*
* Don't show an instruction if the user is following a street, even though the street is
* bending. We should only do this, if following the street is the obvious choice.
*/
if (InstructionsHelper.isNameSimilar(name, prevName) && outgoingEdges.outgoingEdgesAreSlowerByFactor(2)) {
return returnForcedInstructionOrIgnore(forceInstruction, sign);
}
return sign;
}
/*
The current state is a bit uncertain. So we are going more or less straight sign < 2
So it really depends on the surrounding street if we need a turn instruction or not
In most cases this will be a simple follow the current street and we don't necessarily
need a turn instruction
*/
if (prevEdge == null) {
// TODO Should we log this case?
return sign;
}
IntsRef flag = edge.getFlags();
IntsRef prevFlag = prevEdge.getFlags();
boolean outgoingEdgesAreSlower = outgoingEdges.outgoingEdgesAreSlowerByFactor(1);
// There is at least one other possibility to turn, and we are almost going straight
// Check the other turns if one of them is also going almost straight
// If not, we don't need a turn instruction
EdgeIteratorState otherContinue = outgoingEdges.getOtherContinue(prevLat, prevLon, prevOrientation);
// Signs provide too less detail, so we use the delta for a precise comparision
double delta = InstructionsHelper.calculateOrientationDelta(prevLat, prevLon, lat, lon, prevOrientation);
// This state is bad! Two streets are going more or less straight
// Happens a lot for trunk_links
// For _links, comparing flags works quite good, as links usually have different speeds => different flags
if (otherContinue != null) {
//We are at a fork
if (!InstructionsHelper.isNameSimilar(name, prevName)
|| InstructionsHelper.isNameSimilar(otherContinue.getName(), prevName)
|| !prevFlag.equals(flag)
|| prevFlag.equals(otherContinue.getFlags())
|| !outgoingEdgesAreSlower) {
GHPoint tmpPoint = InstructionsHelper.getPointForOrientationCalculation(otherContinue, nodeAccess);
double otherDelta = InstructionsHelper.calculateOrientationDelta(prevLat, prevLon, tmpPoint.getLat(), tmpPoint.getLon(), prevOrientation);
// This is required to avoid keep left/right on the motorway at off-ramps/motorway_links
if (Math.abs(delta) < .1 && Math.abs(otherDelta) > .15 && InstructionsHelper.isNameSimilar(name, prevName)) {
return Instruction.CONTINUE_ON_STREET;
}
if (otherDelta < delta) {
return Instruction.KEEP_LEFT;
} else {
return Instruction.KEEP_RIGHT;
}
}
}
if (!outgoingEdgesAreSlower) {
if (Math.abs(delta) > .6
|| outgoingEdges.isLeavingCurrentStreet(prevName, name)) {
// Leave the current road -> create instruction
return sign;
}
}
return returnForcedInstructionOrIgnore(forceInstruction, sign);
}
private int returnForcedInstructionOrIgnore(boolean forceInstruction, int sign) {
if (forceInstruction)
return sign;
return Instruction.IGNORE;
}
private void updatePointsAndInstruction(EdgeIteratorState edge, PointList pl) {
// skip adjNode
int len = pl.size() - 1;
for (int i = 0; i < len; i++) {
prevInstruction.getPoints().add(pl, i);
}
double newDist = edge.getDistance();
prevInstruction.setDistance(newDist + prevInstruction.getDistance());
prevInstruction.setTime(weighting.calcMillis(edge, false, EdgeIterator.NO_EDGE)
+ prevInstruction.getTime());
}
}