Repository navigation
Treat maxspeed=none more carefully, clean up maxspeed parsing - #3077
Conversation
| double tmp = OSMValueExtractor.stringToKmh(tags.getValue()); | ||
| if (Double.isNaN(tmp)) | ||
| throw new IllegalStateException("illegal maxspeed " + tags.getValue()); | ||
| if (tmp == OSMValueExtractor.MAXSPEED_NONE) |
There was a problem hiding this comment.
Not entirely sure here tbh. Can you check this @karussell? Also we do not seem to make use of the maxspeed:forward/backward tags here, which is why I did not use OSMMaxSpeedParser.parseMaxSpeed here yet.
There was a problem hiding this comment.
As this method precalculates the kmh value only for the country speed limit data which does not contain maxspeed:forward/backward. So it should be fine to use OSMMaxSpeedParser.parseMaxSpeed (as those cases won't happen)
There was a problem hiding this comment.
Ok, but there is no ReaderWay here. Just the tags. If this method parses a different data source maybe we can just keep using stringToKmh? And probably do not need to handle maxspeed=none?
There was a problem hiding this comment.
But the json file does not use km/h, mph etc. so maybe there is hardly anything to parse and we do not even need stringToKmh?
There was a problem hiding this comment.
In DefaultMaxSpeedParser there is a separate parseInt method. Is this something different again or is it applied to the same data as here?
There was a problem hiding this comment.
Yes, true. There is no none in the data.
But the json file does not use km/h, mph etc.
It uses mph.
There was a problem hiding this comment.
In DefaultMaxSpeedParser there is a separate parseInt method. Is this something different again or is it applied to the same data as here?
It uses the integer value which is precalculated and stored here (newTags.put(tags.getKey(), "" + Math.round(tmp));)
I think I did this instead of newTags.put(tags.getKey(), tmp), because of the limited interface of the library: tags: kotlin.collections.Map<kotlin.String, kotlin.String>
| /** | ||
| * @return {@link Double#NaN} if no maxspeed found | ||
| */ | ||
| public static double getMaxSpeed(ReaderWay way, boolean bwd) { | ||
| double maxSpeed = OSMValueExtractor.stringToKmh(way.getTag("maxspeed")); | ||
| double directedMaxSpeed = OSMValueExtractor.stringToKmh(way.getTag(bwd ? "maxspeed:backward" : "maxspeed:forward")); | ||
| return isValidSpeed(directedMaxSpeed) ? directedMaxSpeed : maxSpeed; | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
I think I was able to replace this by OSMMaxSpeedParser.parseMaxSpeed
| protected double applyBadSurfaceSpeed(ReaderWay way, double speed) { | ||
| // limit speed if bad surface | ||
| if (badSurfaceSpeed > 0 && isValidSpeed(speed) && speed > badSurfaceSpeed) { | ||
| if (badSurfaceSpeed > 0 && speed > badSurfaceSpeed) { |
There was a problem hiding this comment.
Not sure how speed could be NaN here before.
| if (externalAccess == null) | ||
| throw new IllegalArgumentException("Call init before using " + getClass().getName()); | ||
| double maxSpeed = stringToKmh(way.getTag("maxspeed")); | ||
| double maxSpeed = Math.max(OSMMaxSpeedParser.parseMaxSpeed(way, false), OSMMaxSpeedParser.parseMaxSpeed(way, true)); |
There was a problem hiding this comment.
So far we did not consider maxspeed:forward/backward here (but OSMMaxSpeedParser.parseMaxSpeed does).
| } | ||
|
|
||
| if (value <= 0) { | ||
| if (value < 0) { |
There was a problem hiding this comment.
For some reason we turned maxspeed=0 into UNSET_SPEED and maxspeed=0.1 to 0 so far. Now maxspeed=0 is also turned into 0.
There was a problem hiding this comment.
Isn't maxspeed=0 a tagging error?
There was a problem hiding this comment.
Yes, probably. Like here? But so is 0.1? What should we do about maxspeed=0 or maxspeed=0.x, or maxspeed=1 and even maxspeed=2? Simply ignore/act as if these tags weren't there?
There was a problem hiding this comment.
For example here there is a maxspeed=2 tag that leads to unrealistic speeds (and even routes).
There was a problem hiding this comment.
See #3074. I think we can just ignore anything well below walking speed.
There was a problem hiding this comment.
Even low maxspeeds that are around walking speeds should be ignored in most cases? Is there any real-world example where speeds are limited for pedestrians?
There was a problem hiding this comment.
Is there any real-world example where speeds are limited for pedestrians?
I'm only aware of limits for motorized traffic.
Even low maxspeeds that are around walking speeds should be ignored in most cases?
Let's say there's a road that is limited to walking speed for motorized traffic. The problem is that it's up to the creativity of the tagger how this ends up in OSM. Some people might not know about maxspeed=walk and simply tag what's their interpretation of a walking speed. If we ignore values like 4 or 5 km/h just because they don't fit our interpretation of around 6 km/h, we could quickly overestimate the maximum speed on that route.
In essence:
- walk'ish speed of around 4-6 -> normalize to our definition of 6km/h
- anything below -> ignore
The interpretation also varies by country. e.g in the Netherlands it's quite high with 15km/h
There was a problem hiding this comment.
Even low maxspeeds that are around walking speeds should be ignored in most cases?
I think I saw a 5km/h speed limit somewhere in Germany :) (like on factory premises?), but probably not too relevant for an ETA of a route.
There was a problem hiding this comment.
I think I saw a 5km/h speed limit somewhere in Germany :) (like on factory premises?), but probably not too relevant for an ETA of a route.
We also use 2 as speed factor for the maxspeed encoded value so we cannot resolve 5km/h anyway :)
Let's say there's a road that is limited to walking speed for motorized traffic. The problem is that it's up to the creativity of the tagger how this ends up in OSM.
Yes, there is probably no reliable method to distinguish very low maxspeed tags that are supposed to indicate walking speed from those that are simply bugs like my example with a maxspeed=2 tag on a primary road above. For the routing speeds one has to carefully decide whether the maxspeed tag should be taken seriously or rather be overruled by the highway tag or other tags.
For highways that are not paths or footways I found using overpass/taginfo:
| maxspeed | count |
|---|---|
| 0 | 94 |
| 1 | 461 |
| 2 | 163 |
| 3 | 322 |
| 4 | 138 |
| 5 | ~40k |
| 6 | 1668 (the vast majority in Germany) |
| 7 | 6232 (almost exclusively in Germany and some in Sweden) |
| 5mph (~8km/h) | ~40k |
| walk | 4764 (also mostly Germany) |
I'd say we should treat everything below and including 4km/h as a bug and ignore it.
walk'ish speed of around 4-6 -> normalize to our definition of 6km/h
I don't think we need to normalize these. We somewhat normalize the values already because we only store the maxspeed in steps of 2km/h and such small differences won't affect the routing much.
There was a problem hiding this comment.
I just realized that maxspeed = 3 mph is just under the the limit of 5 I used and found some traffic signs using 3mph so I changed the limit to 4.8.
| double maxSpeed = getMaxSpeed(way, bwd); | ||
| return Math.min(140, isValidSpeed(maxSpeed) ? Math.max(1, maxSpeed * 0.9) : speed); | ||
| double maxSpeed = OSMMaxSpeedParser.parseMaxSpeed(way, bwd); | ||
| return maxSpeed != MaxSpeed.UNSET_SPEED ? Math.max(1, maxSpeed * 0.9) : speed; |
There was a problem hiding this comment.
Here I slightly changed the speed. The maximum (finite) value returned by OSMMaxSpeedParser.parseMaxSpeed is 150 so I do not see a reason to limit it to 140 again. Since we multiply by 0.9 the resulting speed for motorways tagged as maxspeed=none will be 150*0.9=135 while previously it was Math.min(140, UNSET_SPEED)=140.
|
Btw, yes, in Germany there are roads other than the Autobahn (motorway/trunk) where, by law, there is (still) no speed restriction (drivers can legally go as fast as they can). But for GraphHopper this does not play a role IMO, because no sane router will suggest speeds higher than 130 anyway. |
The tagging for the Isle of Man is most likely valid. There is no national speed limit. |
| } | ||
|
|
||
| if (value <= 0) { | ||
| if (value < 0) { |
There was a problem hiding this comment.
Isn't maxspeed=0 a tagging error?
| "94,-1,algorithm=" + DIJKSTRA_BI, | ||
| "106,-1,algorithm=" + ASTAR_BI, | ||
| "30834,1,ch.disable=true&algorithm=" + DIJKSTRA, | ||
| "21174,1,ch.disable=true&algorithm=" + ASTAR, | ||
| "14846,1,ch.disable=true&algorithm=" + DIJKSTRA_BI, | ||
| "10538,1,ch.disable=true&algorithm=" + ASTAR_BI |
There was a problem hiding this comment.
Are there maxspeed tags with none or <5 in the data or has this a different reason?
There was a problem hiding this comment.
I think this is because there are maxspeed=none tags in the data. Also note that the changed numbers are expected numbers of visited nodes, not routing times or distances.
There was a problem hiding this comment.
After 1cde434 (no longer ignore maxspeed=none for primary) there is no change left.
| protected static boolean isValidSpeed(double speed) { | ||
| return !Double.isNaN(speed); | ||
| } |
There was a problem hiding this comment.
This is now replaced with speed != MaxSpeed.UNSET_SPEED?
There was a problem hiding this comment.
Yes, or simply with Double.isNaN. We have stringToKmh which uses Double.NaN to indicate a missing or invalid maxspeed value. And there is parseMaxSpeed which uses infinity in case there is no tagged limit. I found isValidSpeed rather confusing, because we use different values to indicate (in)validity in different parts of the code.
There was a problem hiding this comment.
I see.
missing or invalid maxspeed value
The problem is that stringToKmh is unrelated to maxspeed. Probably UNSET instead of NaN wouldn't make sense there?
There was a problem hiding this comment.
It looks like it is unrelated to maxspeed, but really it is not, because it handles maxspeed-things like maxspeed=walk/none and ignores small maxspeeds. If we could replace the usages of stringToKmh except the ones in the maxspeed parser maybe we can rename and move it (see the other comments about DefaultMaxSpeedParser and MaxSpeedCalculator)?
| if (maxSpeed != MAXSPEED_MISSING && maxSpeed != MAXSPEED_NONE) | ||
| // there is no actual use for maxspeeds above 150 so we simply truncate here | ||
| return Math.min(MAXSPEED_150, maxSpeed); | ||
| else if (maxSpeed == MAXSPEED_NONE && way.hasTag("highway", "motorway", "motorway_link", "trunk", "trunk_link", "primary")) |
There was a problem hiding this comment.
highway=primary+maxspeed=none is very rare (less than 120 worldwide, ~60 in Germany, most others in Isle of Man). Technically they might be (and are mostly) correct, so I decided to not ignore them here. Otherwise mappers would be forced to map something like maxspeed=100 (or higher) to correctly set the maxspeed, but this would be a workaround.
# Conflicts: # CHANGELOG.md
According to the wiki
maxspeed=nonemeans there actually is no speed limit. However, there are quite a few cases in OSM where it is used differently (most likely because mappers do not know). Take for example this living_street where GraphHopper sets the maxspeed to 150 currently. There are many more examples in and around Norwich, England and on the Island of Man.Here I changed the maxspeed parsing such that
maxspeed=nonewill be ignored except on motorways and trunks. Maybe we could even ignore it everywhere except for the German Autobahn.While I was at it I felt like consolidating the maxspeed parsing a bit as well.
Fixes #3070 as well.