-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
FootFlagEncoder.java
392 lines (335 loc) · 14.5 KB
/
FootFlagEncoder.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
/*
* 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.util;
import com.graphhopper.reader.OSMTurnRelation;
import com.graphhopper.reader.ReaderRelation;
import com.graphhopper.reader.ReaderWay;
import com.graphhopper.routing.profiles.DecimalEncodedValue;
import com.graphhopper.routing.profiles.EncodedValue;
import com.graphhopper.routing.profiles.UnsignedDecimalEncodedValue;
import com.graphhopper.routing.weighting.PriorityWeighting;
import com.graphhopper.storage.IntsRef;
import com.graphhopper.util.PMap;
import java.util.*;
import static com.graphhopper.routing.util.EncodingManager.getKey;
import static com.graphhopper.routing.util.PriorityCode.*;
/**
* Defines bit layout for pedestrians (speed, access, surface, ...). Here we put a penalty on unsafe
* roads only. If you wish to also prefer routes due to beauty like hiking routes use the
* HikeFlagEncoder instead.
* <p>
*
* @author Peter Karich
* @author Nop
* @author Karl Hübner
*/
public class FootFlagEncoder extends AbstractFlagEncoder {
static final int SLOW_SPEED = 2;
static final int MEAN_SPEED = 5;
static final int FERRY_SPEED = 15;
final Set<String> safeHighwayTags = new HashSet<>();
final Set<String> allowedHighwayTags = new HashSet<>();
final Set<String> avoidHighwayTags = new HashSet<>();
// convert network tag of hiking routes into a way route code
final Map<String, Integer> hikingNetworkToCode = new HashMap<>();
protected HashSet<String> sidewalkValues = new HashSet<>(5);
protected HashSet<String> sidewalksNoValues = new HashSet<>(5);
protected boolean speedTwoDirections;
private DecimalEncodedValue priorityWayEncoder;
private EncodedValueOld relationCodeEncoder;
/**
* Should be only instantiated via EncodingManager
*/
public FootFlagEncoder() {
this(4, 1);
}
public FootFlagEncoder(PMap properties) {
this((int) properties.getLong("speed_bits", 4),
properties.getDouble("speed_factor", 1));
this.setBlockFords(properties.getBool("block_fords", true));
this.speedTwoDirections = properties.getBool("speed_two_directions", false);
}
public FootFlagEncoder(String propertiesStr) {
this(new PMap(propertiesStr));
}
public FootFlagEncoder(int speedBits, double speedFactor) {
super(speedBits, speedFactor, 0);
restrictions.addAll(Arrays.asList("foot", "access"));
restrictedValues.add("private");
restrictedValues.add("no");
restrictedValues.add("restricted");
restrictedValues.add("military");
restrictedValues.add("emergency");
intendedValues.add("yes");
intendedValues.add("designated");
intendedValues.add("official");
intendedValues.add("permissive");
sidewalksNoValues.add("no");
sidewalksNoValues.add("none");
// see #712
sidewalksNoValues.add("separate");
sidewalkValues.add("yes");
sidewalkValues.add("both");
sidewalkValues.add("left");
sidewalkValues.add("right");
setBlockByDefault(false);
absoluteBarriers.add("fence");
potentialBarriers.add("gate");
potentialBarriers.add("cattle_grid");
safeHighwayTags.add("footway");
safeHighwayTags.add("path");
safeHighwayTags.add("steps");
safeHighwayTags.add("pedestrian");
safeHighwayTags.add("living_street");
safeHighwayTags.add("track");
safeHighwayTags.add("residential");
safeHighwayTags.add("service");
safeHighwayTags.add("platform");
avoidHighwayTags.add("trunk");
avoidHighwayTags.add("trunk_link");
avoidHighwayTags.add("primary");
avoidHighwayTags.add("primary_link");
avoidHighwayTags.add("secondary");
avoidHighwayTags.add("secondary_link");
avoidHighwayTags.add("tertiary");
avoidHighwayTags.add("tertiary_link");
// for now no explicit avoiding #257
//avoidHighwayTags.add("cycleway");
allowedHighwayTags.addAll(safeHighwayTags);
allowedHighwayTags.addAll(avoidHighwayTags);
allowedHighwayTags.add("cycleway");
allowedHighwayTags.add("unclassified");
allowedHighwayTags.add("road");
// disallowed in some countries
//allowedHighwayTags.add("bridleway");
hikingNetworkToCode.put("iwn", UNCHANGED.getValue());
hikingNetworkToCode.put("nwn", UNCHANGED.getValue());
hikingNetworkToCode.put("rwn", UNCHANGED.getValue());
hikingNetworkToCode.put("lwn", UNCHANGED.getValue());
maxPossibleSpeed = FERRY_SPEED;
speedDefault = MEAN_SPEED;
init();
}
@Override
public int getVersion() {
return 5;
}
@Override
public void createEncodedValues(List<EncodedValue> registerNewEncodedValue, String prefix, int index) {
// first two bits are reserved for route handling in superclass
super.createEncodedValues(registerNewEncodedValue, prefix, index);
// larger value required - ferries are faster than pedestrians
registerNewEncodedValue.add(avgSpeedEnc = new UnsignedDecimalEncodedValue(getKey(prefix, "average_speed"), speedBits, speedFactor, speedTwoDirections));
registerNewEncodedValue.add(priorityWayEncoder = new UnsignedDecimalEncodedValue(getKey(prefix, "priority"), 3, PriorityCode.getFactor(1), speedTwoDirections));
}
@Override
public int defineRelationBits(int index, int shift) {
relationCodeEncoder = new EncodedValueOld("RelationCode", shift, 3, 1, 0, 7);
return shift + relationCodeEncoder.getBits();
}
/**
* Foot flag encoder does not provide any turn cost / restrictions
*/
@Override
public int defineTurnBits(int index, int shift) {
return shift;
}
/**
* Foot flag encoder does not provide any turn cost / restrictions
* <p>
*
* @return <code>false</code>
*/
@Override
public boolean isTurnRestricted(long flags) {
return false;
}
/**
* Foot flag encoder does not provide any turn cost / restrictions
* <p>
*
* @return 0
*/
@Override
public double getTurnCost(long flag) {
return 0;
}
@Override
public long getTurnFlags(boolean restricted, double costs) {
return 0;
}
@Override
public boolean acceptsTurnRelation(OSMTurnRelation relation) {
return false;
}
/**
* Some ways are okay but not separate for pedestrians.
*/
@Override
public EncodingManager.Access getAccess(ReaderWay way) {
String highwayValue = way.getTag("highway");
if (highwayValue == null) {
EncodingManager.Access acceptPotentially = EncodingManager.Access.CAN_SKIP;
if (way.hasTag("route", ferries)) {
String footTag = way.getTag("foot");
if (footTag == null || intendedValues.contains(footTag))
acceptPotentially = EncodingManager.Access.FERRY;
}
// special case not for all acceptedRailways, only platform
if (way.hasTag("railway", "platform"))
acceptPotentially = EncodingManager.Access.WAY;
if (way.hasTag("man_made", "pier"))
acceptPotentially = EncodingManager.Access.WAY;
if (!acceptPotentially.canSkip()) {
if (way.hasTag(restrictions, restrictedValues) && !getConditionalTagInspector().isRestrictedWayConditionallyPermitted(way))
return EncodingManager.Access.CAN_SKIP;
return acceptPotentially;
}
return EncodingManager.Access.CAN_SKIP;
}
String sacScale = way.getTag("sac_scale");
if (sacScale != null) {
if (!"hiking".equals(sacScale) && !"mountain_hiking".equals(sacScale)
&& !"demanding_mountain_hiking".equals(sacScale) && !"alpine_hiking".equals(sacScale))
// other scales are too dangerous, see http://wiki.openstreetmap.org/wiki/Key:sac_scale
return EncodingManager.Access.CAN_SKIP;
}
// no need to evaluate ferries or fords - already included here
if (way.hasTag("foot", intendedValues))
return EncodingManager.Access.WAY;
// check access restrictions
if (way.hasTag(restrictions, restrictedValues) && !getConditionalTagInspector().isRestrictedWayConditionallyPermitted(way))
return EncodingManager.Access.CAN_SKIP;
if (way.hasTag("sidewalk", sidewalkValues))
return EncodingManager.Access.WAY;
if (!allowedHighwayTags.contains(highwayValue))
return EncodingManager.Access.CAN_SKIP;
if (way.hasTag("motorroad", "yes"))
return EncodingManager.Access.CAN_SKIP;
// do not get our feet wet, "yes" is already included above
if (isBlockFords() && (way.hasTag("highway", "ford") || way.hasTag("ford")))
return EncodingManager.Access.CAN_SKIP;
if (getConditionalTagInspector().isPermittedWayConditionallyRestricted(way))
return EncodingManager.Access.CAN_SKIP;
return EncodingManager.Access.WAY;
}
@Override
public long handleRelationTags(long oldRelationFlags, ReaderRelation relation) {
int code = 0;
if (relation.hasTag("route", "hiking") || relation.hasTag("route", "foot")) {
Integer val = hikingNetworkToCode.get(relation.getTag("network"));
if (val != null)
code = val;
else
code = hikingNetworkToCode.get("lwn");
} else if (relation.hasTag("route", "ferry")) {
code = PriorityCode.AVOID_IF_POSSIBLE.getValue();
}
int oldCode = (int) relationCodeEncoder.getValue(oldRelationFlags);
if (oldCode < code)
return relationCodeEncoder.setValue(0, code);
return oldRelationFlags;
}
@Override
public IntsRef handleWayTags(IntsRef edgeFlags, ReaderWay way, EncodingManager.Access access, long relationFlags) {
if (access.canSkip())
return edgeFlags;
accessEnc.setBool(false, edgeFlags, true);
accessEnc.setBool(true, edgeFlags, true);
if (!access.isFerry()) {
String sacScale = way.getTag("sac_scale");
if (sacScale != null) {
setSpeed(edgeFlags, true, true, "hiking".equals(sacScale)? MEAN_SPEED : SLOW_SPEED);
} else {
setSpeed(edgeFlags, true, true, way.hasTag("highway", "steps")? MEAN_SPEED - 2 : MEAN_SPEED);
}
} else {
double ferrySpeed = getFerrySpeed(way);
setSpeed(edgeFlags, true, true, ferrySpeed);
}
int priorityFromRelation = 0;
if (relationFlags != 0)
priorityFromRelation = (int) relationCodeEncoder.getValue(relationFlags);
priorityWayEncoder.setDecimal(false, edgeFlags, PriorityCode.getFactor(handlePriority(way, priorityFromRelation)));
return edgeFlags;
}
void setSpeed(IntsRef edgeFlags, boolean fwd, boolean bwd, double speed) {
if (speed > getMaxSpeed())
speed = getMaxSpeed();
if (fwd)
avgSpeedEnc.setDecimal(false, edgeFlags, speed);
if (bwd && speedTwoDirections)
avgSpeedEnc.setDecimal(true, edgeFlags, speed);
}
protected int handlePriority(ReaderWay way, int priorityFromRelation) {
TreeMap<Double, Integer> weightToPrioMap = new TreeMap<>();
if (priorityFromRelation == 0)
weightToPrioMap.put(0d, UNCHANGED.getValue());
else
weightToPrioMap.put(110d, priorityFromRelation);
collect(way, weightToPrioMap);
// pick priority with biggest order value
return weightToPrioMap.lastEntry().getValue();
}
/**
* @param weightToPrioMap associate a weight with every priority. This sorted map allows
* subclasses to 'insert' more important priorities as well as overwrite determined priorities.
*/
void collect(ReaderWay way, TreeMap<Double, Integer> weightToPrioMap) {
String highway = way.getTag("highway");
if (way.hasTag("foot", "designated"))
weightToPrioMap.put(100d, PREFER.getValue());
double maxSpeed = getMaxSpeed(way);
if (safeHighwayTags.contains(highway) || maxSpeed > 0 && maxSpeed <= 20) {
weightToPrioMap.put(40d, PREFER.getValue());
if (way.hasTag("tunnel", intendedValues)) {
if (way.hasTag("sidewalk", sidewalksNoValues))
weightToPrioMap.put(40d, AVOID_IF_POSSIBLE.getValue());
else
weightToPrioMap.put(40d, UNCHANGED.getValue());
}
} else if (maxSpeed > 50 || avoidHighwayTags.contains(highway)) {
if (!way.hasTag("sidewalk", sidewalkValues))
weightToPrioMap.put(45d, AVOID_IF_POSSIBLE.getValue());
}
if (way.hasTag("bicycle", "official") || way.hasTag("bicycle", "designated"))
weightToPrioMap.put(44d, AVOID_IF_POSSIBLE.getValue());
}
@Override
public boolean supports(Class<?> feature) {
if (super.supports(feature))
return true;
return PriorityWeighting.class.isAssignableFrom(feature);
}
/*
* This method is a current hack, to allow ferries to be actually faster than our current storable maxSpeed.
*/
@Override
double getSpeed(boolean reverse, IntsRef edgeFlags) {
double speed = super.getSpeed(reverse, edgeFlags);
if (speed == getMaxSpeed()) {
// We cannot be sure if it was a long or a short trip
return SHORT_TRIP_FERRY_SPEED;
}
return speed;
}
@Override
public String toString() {
return "foot";
}
}