Repository navigation
Description
This issue ticket's purpose is to
- get feedback from you guys if inferring default speed limits is desired at all
- get feedback from you guys if the default speed limit data as provided is in a format you are able to make use of etc.
- finally, serve as a starting-area / feature request to implement this
Assumptions
The maximum allowed speed on any given road is an important factor to rate the costing of edges in the graph and travel time.
maxspeed coverage in OSM is sketchy and likely will always be: Currently, only about 12% of all roads have a speed limit tagged.
Additionally, maxspeed is just for cars. Other vehicles such as trucks, buses, motorhomes, cars with trailers or motorcycles will, depending on the road type, have different maximum speed limits that are often below the signed one.
Implicit speed limits
When speed limits are not signed, the default speed limits apply. (Maybe more so outside the United States. I heard that in the US, things are usually always explicitly signed.) Regardless of the reason why maxspeed is missing - nothing is signed or information is still missing - the actual speed limits can be inferred by looking at the legislation.
For example, in Hungary, the default speed limits are as follows
- 20 km/h on a living street
- 30 km/h on a bicycle road
- 50 km/h on any other urban road
- 90 km/h on a non-urban road, except for vehicles pulling a trailer, with a GVWR of 3.5t, buses or trucks for which it is 70 km/h
- 110 km/h on motorroads, except for the vehicle types mentioned above for which it is still 70 km/h
- 130 km/h on a motorway. For buses however, it's 100 km/h. For vehicles, pulling a trailer, with a GVWR of 3.5t or more and trucks, it is 80 km/h
So, if a road in Hungary is tagged with e.g. highway=living_street or motorroad=yes, we can easily infer the implicit speed limit from the above information. Hungary however is a rather easy example.
Sourcing implicit speed limits
The legislations as the one above have been collected over the years on the OSM wiki page Default speed limits in a well-researched (with links to the actual legislation) and machine-readable form. @ianthetechie and me have been working on a Python parser for this table that outputs the information in it as a JSON-file so that it can easily be consumed by (routing) software.
This is how it currently looks like: default_speeds.json
See how it looks for Hungary
"HU": [
{
"filter": "highway=living_street or living_street=yes",
"name": "living street",
"tags": {
"maxspeed": "20"
}
},
{
"filter": "bicycle_road=yes or cyclestreet=yes",
"name": "bicycle road",
"tags": {
"maxspeed": "30"
}
},
{
"filter": "{is_urban}",
"name": "urban",
"tags": {
"maxspeed": "50"
}
},
{
"tags": {
"maxspeed": "90",
"maxspeed:bus": "70",
"maxspeed:hgv": "70",
"maxspeed:conditional": "70 @ (weightrating>3.5); 70 @ (trailer)"
}
},
{
"filter": "motorroad=yes",
"name": "motorroad",
"tags": {
"maxspeed": "110",
"maxspeed:bus": "70",
"maxspeed:hgv": "70",
"maxspeed:conditional": "70 @ (weightrating>3.5); 70 @ (trailer)"
}
},
{
"filter": "highway~^motorway(_link)?$",
"name": "motorway",
"tags": {
"maxspeed": "130",
"maxspeed:bus": "100",
"maxspeed:hgv": "80",
"maxspeed:conditional": "80 @ (weightrating>3.5); 80 @ (trailer)"
}
}
]Inherent complexity
Already in the example for Hungary, the following becomes clear:
Filter-based mapping
There can not be a 1:1 mapping of highway=X to speed limit rules because
- there is no (single) tag for "urban road". It may be sourced from other information such as
source:maxspeed=HU:urbanand similar tags, by looking at the location oftraffic_sign=city_limitsigns, whether or not a road is in- or outside of a respective city limit boundary or fuzzily by looking at tags such aslit=yes/noand similar (because roads outside of settlements are usually not lit) - sometimes different tags are synonymous in OSM
- sometimes the legal road type matches multiple different tags on OSM
- sometimes the legal road type depends on such things as whether a road is lit or not (e.g. United Kingdom), whether the oncoming traffic is segregated (e.g. Germany) or whether the road is paved at all (e.g. Georgia, US)
Instead, it must be based on filtering by OSM tags. For example, the default speed limit in Illinois for roads where the oncoming traffic is segregated and there are 2 or more lanes in each direction would be 65 mph. For interstates, it would be 70 mph.
The filters (in pseudo-Overpass-Wizard, ~ meaning it would be a regex-match) for that would be:
oneway ~ ^yes|-1$ and lanes >= 2 and dual_carriageway != no-> 65 mphref ~ ^I and highway ~ ^motorway|motorway_link$-> 70 mph
To have a parser for such pseudo-Overpass-Wizard syntax adds quite some complexity, I believe.
Rules for when multiple road types match
For example, a bicycle road is also an urban road, so both filters match for an urban bicycle road. Which speed limit applies?
The wiki page explains the rules for which the right road type should be selected. This is not super complex, but still adds to it before one can make proper use of the data.
Reverse country/state geocoder
Not every motorway with a ref code starting with I is an US interstate highway. This rule may only be applied in the US. Thus, the router must know in which country/state any given road is located. (Should be no problem though if you use a database with geospatial extensions. I can give pointers.)
Advanced maxspeed and maxspeed:conditional parsing
The information in the default speed limits table is translated into OSM tags, including maxspeed:conditional and maxspeed:<vehicle type>. Thus, to interpret the limits correctly, it's necessary for the (router) software to also parse the information in these tags correctly.
Whether this adds complexity or not depends on if Graphhopper can already parse these, as these are normal OSM maxspeed tags.
Your Feedback necessary
So, the mentioned parser hasn't been worked on for quite some time because we need feedback from you. No point in creating something if it isn't used or doesn't even have the chance of being used. See the points at the very beginning of this ticket.
I may have some time in the near future to improve on this and also maintain the source data, so:
Do you think this data is useful? Can you work with this data? What is necessary to work with it? Do you have suggestions on how else this data could be represented? Do you have other questions?
I posted the same ticket also at Valhalla. Not at OSRM because it seems to be in maintenance mode.
Activity
otbutz commentedon Sep 23, 2021
This is already a solved problem. See CountryRule and our border polygons
We've a lack of country specific implementations though and things like urban/non-urban are yet to be solved.
westnordost commentedon Sep 23, 2021
Cool! I see you put the border polygons into a spatial index before accessing it, so the point-in-polygon check only needs to be done for one or a few polygons instead of all. If you need it even a bit faster, there is also https://github.com/westnordost/countryboundaries because in many cases, no pip-checks need to be done at all anymore. But this is a bit off topic I guess.
I see, so currently, Graphhopper doesn't know for any given (e.g.)
highway=unclassifiedwhether a speed limit of about 50 km/h (urban) or 70-100 km/h (rural) speed limit applies if none is given? How is this treated currently?There are some solutions for this. I think the easiest would be to start interpreting
source:maxspeed=...:urban-like tags but for a fuzzy match, other tags could be looked at too, e.g.shoulder(usually only outside settlements),sidewalk(usually only inside),lit(usually only inside) etc.otbutz commentedon Sep 23, 2021
The boundaries file of the JOSM project looks really good!
https://josm.openstreetmap.de/export/HEAD/josm/trunk/resources/data/boundaries.osm
@karussell @easbar
westnordost commentedon Sep 23, 2021
Yes, the boundaries file of JOSM is really precise (I contributed to it myself). The precision is about that one can be sure that every hamlet and maybe even every road is on the right side of the border.
otbutz commentedon Sep 23, 2021
Global default values which is kinda ugly:
graphhopper/core/src/main/java/com/graphhopper/routing/util/CarFlagEncoder.java
Lines 108 to 141 in e692138
We could pass our rules down to the OSMMaxSpeedParser to accomplish this.
Offtopic: i really dig your app. IMHO one of the best tools to easily contribute to OSM.
otbutz commentedon Sep 23, 2021
We could factor in landuse to detect urban roads. But that's a metric shitton of point-in-polygon lookups.
karussell commentedon Sep 23, 2021
We have some work done for our commercial Routing API (we called it "speedmodels") which we would like to open source. Basically we collected (and manually created) raster images from several data sources that define urban areas and then we have some logic to set the estimated speeds (based on urban/rural and road class etc). This is also what you see at GraphHopper Maps. And as we are working on reducing the differences of the open source routing engine and our Routing API this is probably the next big task for us and the community :)
One problem is also that this code is old, not easy to extend and limited to raster data, so maybe the best way would be to rewrite it from scratch to fill something like a
is_ruralencoding value.Every island solution will either cause problems or require a post-processing step where the neighboring street segments needs to be touched as well to avoid strange detours around these "single" obstacles. See the issue #382.
Kudos from me to you @westnordost too 👍 !
The problem is here more that the coverage for that is not great everywhere outside Europe.
westnordost commentedon Sep 23, 2021
Plus,
landuse=residentialis often cut to explicitly not include the roads and sidewalks, e.g. https://www.openstreetmap.org/#map=18/53.57070/9.97422If you are doing something geospatial, probably point-in-polygon checks with admin boundary relations... oh wait, nevermind, that won't work. https://www.openstreetmap.org/relation/445840#map=13/53.5076/9.5882
We really need a
is_urban=yes/notag in OSM, don't we? Currently, what comes closest to that iszone:traffic=DE:urbanwhich follows thesource:maxspeed/maxspeed:typescheme of<country_code>:<road_type>but as far as I understand, may also be set when amaxspeedsign is present because it just describes the location of the street, not what maxspeed should be valid in the absence of a sign (only). Anyway, I guess for router software, that difference in definition doesn't matter.karussell commentedon Sep 23, 2021
I would not want to set this tag on all OSM ways :) ... having the urban boundaries relations complete would fix this already, but even they are sparse for small towns even (not to mention hamlets etc). So I guess external sources (like "AI work" from satellite images) are our best source.
westnordost commentedon Sep 23, 2021
But how? This boundary obviously also includes rural roads https://www.openstreetmap.org/relation/445840#map=13/53.5076/9.5882
karussell commentedon Sep 23, 2021
Ok, they would fix it probably not 100% ;) but this would be already better than relying on some rough raster images (what we currently do) and likely the incorrectly contained (bigger) rural roads contain the maxspeed tag already. To make it really 100% we would need indeed a new relation defining an area where the default maxspeed should be considered to avoid tagging every single way.
Or the more pragmatic solution: we always use the urban boundaries and assume default maxspeed but every way, which would get an incorrect max. speed, would need an explicit maxspeed tag.
westnordost commentedon Sep 23, 2021
So, back the OP topic: Do you think the data in the wiki in the form currently shown in the JSON would be useful and could be made use of? What would need to be done do enable this? Do you for example have the capability to parse a filter syntax similar to the one used in the JSON already? Can you already interpret (all the) maxspeed conditional rules, and the
maxspeed:<vehicle_type>rules?otbutz commentedon Sep 23, 2021
The first step should be to harmonize min/max/avg speed handling. see #2266
We should probably deal with upper and lower bounds for max speed in country rules and derive the average speed from those values.
e.g:
This would allow us to use the values provided by the OSM wiki and yield more detailed avg speeds per country.
karussell commentedon Sep 23, 2021
Yes, but probably doable. Would it be possible to change this notation? Something without "full regex support" and just
*, like:highway*=motorway or highway*=motorway_link(I hope I correctly interpreted the regex match~?)Same for the oneway:
(oneway*=yes or oneway*=-1) and ...This way we could even use our internal janino engine to interpret the filters. (see our custom weightings for examples of them)
westnordost commentedon Sep 23, 2021
I think it would. Or replacing regexes with (... or .... or ... or). Looking at https://wiki.openstreetmap.org/wiki/Default_speed_limits#Road_Type_to_tag_filters_table the number of regex used is managable.
I do by the way have in StreetComplete (an even more complex) syntax parser and I guess it would be possible to outsource this into a library if there is interest. It is written in Kotlin though. This is also a JVM language, but comes with Kotlin Standard Library for JVM as default dependency which weights 1.4 MB.
An example of a more complex expression the parser is able to parse:
https://github.com/streetcomplete/StreetComplete/blob/master/app/src/main/java/de/westnordost/streetcomplete/quests/sidewalk/AddSidewalk.kt#L27-L47
29 remaining items
easbar commentedon Aug 22, 2022
Yes I thought the same and it looked pretty strange:
One of the files starts like this:
easbar commentedon Aug 22, 2022
Do you have an example where you used the artifact in an (otherwise Java-only) gradle project?
westnordost commentedon Aug 22, 2022
No. I've never used kotlin dependencies in Java projects, only the other way round. I'd also need to read up on that if there is anything special about it.
westnordost commentedon Aug 22, 2022
Did that decompiler decompile it to kotlin? Or is the above valid java (11) code?
easbar commentedon Aug 22, 2022
Good point :) It's Kotlin code. So I probably need some maven plugin that invokes the Kotlin compiler. Then again, no, because the jar only contains bytecode I thought?
westnordost commentedon Aug 22, 2022
Yeah, you should not need the kotlin compiler. I think you included the klib, not the jar? Not sure how to tell maven to take the jar
easbar commentedon Aug 22, 2022
I went two steps back and created a fresh Kotlin+Gradle project in IntelliJ, put
dependencies { implementation('de.westnordost:osm-legal-default-speeds:1.1') }in
build.gradleand got:Does that ring a bell?
westnordost commentedon Aug 22, 2022
No, doesn't ring a bell. I also do not quite understand what would be the problem and why "nativeApiElements" is even mentioned. There are no native api elements.
Anyway, at this point, it looks very much that this is a packaging problem of the library itself and not a maven configuration problem in your project, so I created an issue there. westnordost/osm-legal-default-speeds#2
In case you find anything, you are welcome to post anything on that ticket
westnordost commentedon Sep 28, 2022
The talks from the SotM are online, I've noticed: https://media.ccc.de/v/sotm2022-18524-inferring-default-speed-limits
otbutz commentedon Feb 15, 2023
What's the current state of the project? Would be nice if we could integrate this :)
westnordost commentedon Feb 15, 2023
It's done
karussell commentedon Jun 12, 2023
Integration done in #2810.