Skip to content

Treat maxspeed=none more carefully, clean up maxspeed parsing - #3077

Merged
easbar merged 15 commits into
masterfrom
maxspeed_none
Nov 6, 2024
Merged

easbar merged 15 commits into
masterfrom
maxspeed_none

Conversation

@easbar

@easbar easbar commented Nov 2, 2024 •

Copy link
Copy Markdown
Member

According to the wikimaxspeed=none means 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=none will 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.

@easbar
easbar requested a review from karussell November 2, 2024 21:11
double tmp = OSMValueExtractor.stringToKmh(tags.getValue());
if (Double.isNaN(tmp))
throw new IllegalStateException("illegal maxspeed " + tags.getValue());
if (tmp == OSMValueExtractor.MAXSPEED_NONE)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@karussell karussell Nov 5, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In DefaultMaxSpeedParser there is a separate parseInt method. Is this something different again or is it applied to the same data as here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, true. There is no none in the data.

But the json file does not use km/h, mph etc.

It uses mph.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Comment on lines -20 to -29
/**
* @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;
}

/**

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far we did not consider maxspeed:forward/backward here (but OSMMaxSpeedParser.parseMaxSpeed does).

}

if (value <= 0) {
if (value < 0) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't maxspeed=0 a tagging error?

@easbar easbar Nov 4, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example here there is a maxspeed=2 tag that leads to unrealistic speeds (and even routes).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #3074. I think we can just ignore anything well below walking speed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@karussell karussell Nov 4, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@easbar

easbar commented Nov 2, 2024 •

Copy link
Copy Markdown
Member Author

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.

@otbutz

otbutz commented Nov 4, 2024

Copy link
Copy Markdown
Contributor

and on the Island of Man

The tagging for the Isle of Man is most likely valid. There is no national speed limit.

@easbar

easbar commented Nov 4, 2024

Copy link
Copy Markdown
Member Author

The tagging for the Isle of Man is most likely valid. There is no national speed limit.

Ok, I see, that's correct. I would still ignore maxspeed=none for a road like this.

}

if (value <= 0) {
if (value < 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't maxspeed=0 a tagging error?

Comment on lines 83 to 88
"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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there maxspeed tags with none or <5 in the data or has this a different reason?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After 1cde434 (no longer ignore maxspeed=none for primary) there is no change left.

Comment on lines -32 to -34
protected static boolean isValidSpeed(double speed) {
return !Double.isNaN(speed);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now replaced with speed != MaxSpeed.UNSET_SPEED?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@karussell karussell Nov 5, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@easbar easbar Nov 5, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed and moved it

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"))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@easbar
easbar merged commit 8aefb69 into master Nov 6, 2024
@easbar
easbar deleted the maxspeed_none branch November 6, 2024 15:12
@karussell karussell added this to the 11.0 milestone Nov 7, 2024

This branch was previously deployed

1 inactive deployment
benchmarks — 8b4eb7b7 Deployed Nov 6, 2024 by easbar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

maxspeed=0.5 on a way triggers exception when using bicycle profile

3 participants