/
CarFlagEncoder.java
325 lines (282 loc) · 10.7 KB
/
CarFlagEncoder.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
/*
* Licensed to GraphHopper and Peter Karich under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper 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 java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.graphhopper.reader.OSMRelation;
import com.graphhopper.reader.OSMWay;
import com.graphhopper.util.Helper;
import com.graphhopper.util.PMap;
import java.util.*;
/**
* Defines bit layout for cars. (speed, access, ferries, ...)
* <p>
* @author Peter Karich
* @author Nop
*/
public class CarFlagEncoder extends AbstractFlagEncoder
{
protected final Map<String, Integer> trackTypeSpeedMap = new HashMap<String, Integer>();
protected final Set<String> badSurfaceSpeedMap = new HashSet<String>();
/**
* A map which associates string to speed. Get some impression:
* http://www.itoworld.com/map/124#fullscreen
* http://wiki.openstreetmap.org/wiki/OSM_tags_for_routing/Maxspeed
*/
protected final Map<String, Integer> defaultSpeedMap = new HashMap<String, Integer>();
public CarFlagEncoder()
{
this(5, 5, 0);
}
public CarFlagEncoder( PMap properties )
{
this((int) properties.getLong("speedBits", 5),
properties.getDouble("speedFactor", 5),
properties.getBool("turnCosts", false) ? 1 : 0);
this.properties = properties;
this.setBlockFords(properties.getBool("blockFords", true));
}
public CarFlagEncoder( String propertiesStr )
{
this(new PMap(propertiesStr));
}
public CarFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts )
{
super(speedBits, speedFactor, maxTurnCosts);
restrictions.addAll(Arrays.asList("motorcar", "motor_vehicle", "vehicle", "access"));
restrictedValues.add("private");
restrictedValues.add("agricultural");
restrictedValues.add("forestry");
restrictedValues.add("no");
restrictedValues.add("restricted");
restrictedValues.add("delivery");
restrictedValues.add("military");
intendedValues.add("yes");
intendedValues.add("permissive");
potentialBarriers.add("gate");
potentialBarriers.add("lift_gate");
potentialBarriers.add("kissing_gate");
potentialBarriers.add("swing_gate");
absoluteBarriers.add("bollard");
absoluteBarriers.add("stile");
absoluteBarriers.add("turnstile");
absoluteBarriers.add("cycle_barrier");
absoluteBarriers.add("motorcycle_barrier");
absoluteBarriers.add("block");
trackTypeSpeedMap.put("grade1", 20); // paved
trackTypeSpeedMap.put("grade2", 15); // now unpaved - gravel mixed with ...
trackTypeSpeedMap.put("grade3", 10); // ... hard and soft materials
trackTypeSpeedMap.put("grade4", 5); // ... some hard or compressed materials
trackTypeSpeedMap.put("grade5", 5); // ... no hard materials. soil/sand/grass
badSurfaceSpeedMap.add("cobblestone");
badSurfaceSpeedMap.add("grass_paver");
badSurfaceSpeedMap.add("gravel");
badSurfaceSpeedMap.add("sand");
badSurfaceSpeedMap.add("paving_stones");
badSurfaceSpeedMap.add("dirt");
badSurfaceSpeedMap.add("ground");
badSurfaceSpeedMap.add("grass");
maxPossibleSpeed = 140;
// autobahn
defaultSpeedMap.put("motorway", 100);
defaultSpeedMap.put("motorway_link", 70);
defaultSpeedMap.put("motorroad", 90);
// bundesstraße
defaultSpeedMap.put("trunk", 70);
defaultSpeedMap.put("trunk_link", 65);
// linking bigger town
defaultSpeedMap.put("primary", 65);
defaultSpeedMap.put("primary_link", 60);
// linking towns + villages
defaultSpeedMap.put("secondary", 60);
defaultSpeedMap.put("secondary_link", 50);
// streets without middle line separation
defaultSpeedMap.put("tertiary", 50);
defaultSpeedMap.put("tertiary_link", 40);
defaultSpeedMap.put("unclassified", 30);
defaultSpeedMap.put("residential", 30);
// spielstraße
defaultSpeedMap.put("living_street", 5);
defaultSpeedMap.put("service", 20);
// unknown road
defaultSpeedMap.put("road", 20);
// forestry stuff
defaultSpeedMap.put("track", 15);
}
@Override
public int getVersion()
{
return 1;
}
/**
* Define the place of the speedBits in the edge flags for car.
*/
@Override
public int defineWayBits( int index, int shift )
{
// first two bits are reserved for route handling in superclass
shift = super.defineWayBits(index, shift);
speedEncoder = new EncodedDoubleValue("Speed", shift, speedBits, speedFactor, defaultSpeedMap.get("secondary"),
maxPossibleSpeed);
return shift + speedEncoder.getBits();
}
protected double getSpeed( OSMWay way )
{
String highwayValue = way.getTag("highway");
Integer speed = defaultSpeedMap.get(highwayValue);
if (speed == null)
throw new IllegalStateException(toString() + ", no speed found for: " + highwayValue + ", tags: " + way);
if (highwayValue.equals("track"))
{
String tt = way.getTag("tracktype");
if (!Helper.isEmpty(tt))
{
Integer tInt = trackTypeSpeedMap.get(tt);
if (tInt != null)
speed = tInt;
}
}
return speed;
}
@Override
public long acceptWay( OSMWay way )
{
String highwayValue = way.getTag("highway");
if (highwayValue == null)
{
if (way.hasTag("route", ferries))
{
String motorcarTag = way.getTag("motorcar");
if (motorcarTag == null)
motorcarTag = way.getTag("motor_vehicle");
if (motorcarTag == null && !way.hasTag("foot") && !way.hasTag("bicycle") || "yes".equals(motorcarTag))
return acceptBit | ferryBit;
}
return 0;
}
if ("track".equals(highwayValue))
{
String tt = way.getTag("tracktype");
if (tt != null && !tt.equals("grade1") && !tt.equals("grade2") && !tt.equals("grade3"))
return 0;
}
if (!defaultSpeedMap.containsKey(highwayValue))
return 0;
if (way.hasTag("impassable", "yes") || way.hasTag("status", "impassable"))
return 0;
// multiple restrictions needs special handling compared to foot and bike, see also motorcycle
String firstValue = way.getFirstPriorityTag(restrictions);
if (!firstValue.isEmpty())
{
if (restrictedValues.contains(firstValue))
return 0;
if (intendedValues.contains(firstValue))
return acceptBit;
}
// do not drive street cars into fords
if (isBlockFords() && ("ford".equals(highwayValue) || way.hasTag("ford")))
return 0;
// do not drive cars over railways (sometimes incorrectly mapped!)
if (way.hasTag("railway") && !way.hasTag("railway", acceptedRailways))
return 0;
return acceptBit;
}
@Override
public long handleRelationTags( OSMRelation relation, long oldRelationFlags )
{
return oldRelationFlags;
}
@Override
public long handleWayTags( OSMWay way, long allowed, long relationFlags )
{
if (!isAccept(allowed))
return 0;
long encoded;
if (!isFerry(allowed))
{
// get assumed speed from highway type
double speed = getSpeed(way);
speed = applyMaxSpeed(way, speed, true);
// limit speed to max 30 km/h if bad surface
if (speed > 30 && way.hasTag("surface", badSurfaceSpeedMap))
speed = 30;
encoded = setSpeed(0, speed);
boolean isRoundabout = way.hasTag("junction", "roundabout");
if (isRoundabout)
encoded = setBool(encoded, K_ROUNDABOUT, true);
boolean isOneway = way.hasTag("oneway", oneways)
|| way.hasTag("vehicle:backward")
|| way.hasTag("vehicle:forward")
|| way.hasTag("motor_vehicle:backward")
|| way.hasTag("motor_vehicle:forward");
if (isOneway || isRoundabout)
{
boolean isBackward = way.hasTag("oneway", "-1")
|| way.hasTag("vehicle:forward", "no")
|| way.hasTag("motor_vehicle:forward", "no");
if (isBackward)
encoded |= backwardBit;
else
encoded |= forwardBit;
} else
encoded |= directionBitMask;
} else
{
encoded = handleFerryTags(way, defaultSpeedMap.get("living_street"), defaultSpeedMap.get("service"), defaultSpeedMap.get("residential"));
encoded |= directionBitMask;
}
return encoded;
}
public String getWayInfo( OSMWay way )
{
String str = "";
String highwayValue = way.getTag("highway");
// for now only motorway links
if ("motorway_link".equals(highwayValue))
{
String destination = way.getTag("destination");
if (!Helper.isEmpty(destination))
{
int counter = 0;
for (String d : destination.split(";"))
{
if (d.trim().isEmpty())
continue;
if (counter > 0)
str += ", ";
str += d.trim();
counter++;
}
}
}
if (str.isEmpty())
return str;
// I18N
if (str.contains(","))
return "destinations: " + str;
else
return "destination: " + str;
}
@Override
public String toString()
{
return "car";
}
}