You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid streets entirely with (legal) maxspeed higher than threshold #2840
We recently added top_speed to all (motorized) models where roads with higher estimated (not max) speeds are slightly penalized.
However, there are instances where it's mandatory that vehicles don't enter roads which have a legal speed limit above a certain threshold. Apparently that's applicable for electric bikes/other "lower" electric vehicle types in US/CA. In parameter terms this could be legal_speed or similar.
Would you be generally okay with a contribution like that?
I can see speed_limit is already collected from maxspeed OSM tag and put into EdgeInfo.
There a few questions:
I think maxspeed:forward/backward should be collected too (?!)
Would the check go into DynamicCost::Allowed(Reverse)? I can't see EdgeInfo being used there (or in any other part of the actual path finding), but I'm quite sure you'd not like to extend the DirectedEdge interface with that info!?
Obviously maxspeed (and more so their directional siblings) are still fairly rare, so country-/roadclass-specific defaults would be nice to get some better coverage of speed_limit (I know it's been on your TODO for a while..). Would you accept a contribution in a first step without that enhancement? I'd be happy to look into it at some point, but I couldn't take care of that right now tbh.
True, another attribute that'll never see > 1 permille coverage;) Via maxspeed and a legal_speed request param would be more flexible I think. Though I understand that that's quite a special thing to ask for..
I think maxspeed:forward/backward should be collected too (?!)
On this one - a space compromise has been made here. There aren't that many singly digitized roads that have differing speed limits in opposite directions. By storing speed_limit on EdgeInfo, we only need one copy per road. The penalty to this space saving is that we can't support the 0.001% of roads that need different limits in opposite directions.
@gknisely can correct me if im wrong but, we do support different speeds in different directions. its stored in directededge::speed. i forget if enhancer messes with this speed though so its possible its there but then gets modified later in the pipeline but technically we do support different speeds in different directions along the same stretch of singly digitized road
Yes, for actual speed that's right @kevinkreiser. For speed_limit (which I'd be interested in here) it's not. But @danpat is right, it's a fraction of roads compared to those having a maxspeed tag. Also very secondary requirement.
More interesting is how one would optimally get the speed_limit from edge_info, so it's available during the expansion. Would it have to go into the directededge interface? Eventually we'd have the country-/roadclass-specific defaults for legal speed_limit from the admin db so it could make sense? However, not so sure how many other use cases there are for legal speed limits..
yea, anything used in pathfinding (expansion) would need to be in DirectedEdge. Having to look up EdgeInfo (which is not tightly packed in adjacent memory like the directed edges that leave a node) would I think be unacceptable performance loss.
Hmpf was forgetting about the Lua step there.. Thanks for the pointers @kevinkreiser ! Is it an option for you to store a speed_limit on the directed edge? Not on the computer right now, but I think there are 64 bit spare. Would it be worth 8 of those? 😇
hmm maybe im misunderstanding what you are asking @nilsnolde but i thought the pointers i linked above proved that if the speed_type is SpeedType::kTagged then the stored speed IS the speed limit. in fact the only time the speed isnt the speed limit is when there is no maxspeed tag at all. what am i missing here?
Yeah I should’ve stopped to listen.. was too focused on getting an answer before the weekend to what I thought the question was 😕 thanks for slamming it in my face😊
So in most cases, does this make speed_limit on EdgeInfo is redundant? If a maxspeed tag doesn't exist, then it won't be set, if maxspeed does exist, then speed_type will let us know that DirectedEdge::speed_ contains the value.
So to get the actual speed limit back without going to the edgeinfo object we can simply reverse this transform. Honestly it seems like a bit of an oversight. I highly doubt that adding that if block costing would dramatically change the runtime of the path algorithms. We probably could have just let the speed limit out of edge info and added the surface penalty on the fly in costing.
Seems like an artifact of hysterical raisins or plans that never fully came to be. I mean maybe we can even refactor a bit to make it so that we always get speed limit out of the directed edge and forget about edge_info while still keeping the tile format backwards compatible. I have to brainstorm that a bit. The other option is to leave the format as is and as i said about undo the transform to get the speed limit.
Yea, as @kevinkreiser says this was added to support future enhancements and perhaps did not need to occur. The thought was that adding speed_limit to EdgeInfo would protect in future cases where the DirectedEdge speed might be modified from the OSM tagged speed limit. The idea was that the speed in DirectedEdge would provide the base speed used in costing for routes in absence of any traffic based speeds - even if this speed was not the actual speed limit. The thought was that speed limits would always be available even if not used in any costing methods.
Having looked at it again, it seems to me there actually isn't a way currently to reliably retrieve the actual maxspeed from an edge: if maxspeed:advisory or maxspeed:practical is set, it's also a "tagged speed" and is favored over maxspeed:
Of course, it's pretty unlikely: combined those two tags occur ~60k times currently. And the good thing is they should definitely underestimate the legal max speed, so in worst case it'd take an unnecessary detour, rather than routing through forbidden roads.
But still, I'd prefer an accurate implementation. So I ended up extending the DirectedEdge interface with max_speed_ and using up 8 of the last 9 spare bits, then checking in Cost::Allowed(Reverse) if the set legal speed is higher than the edge's max_speed_. Less invasive ideas welcome. I can PR soon if you'd accept smth like that.
Activity
danpat commentedon Feb 5, 2021
OSM has a specific tag for roads where a actual minimum travel speed is required:
https://wiki.openstreetmap.org/wiki/Key:minspeed
AFAIK, this tag isn't parsed or stored anywhere, as it's not that common.
nilsnolde commentedon Feb 5, 2021
True, another attribute that'll never see > 1 permille coverage;) Via
maxspeedand alegal_speedrequest param would be more flexible I think. Though I understand that that's quite a special thing to ask for..danpat commentedon Feb 5, 2021
On this one - a space compromise has been made here. There aren't that many singly digitized roads that have differing speed limits in opposite directions. By storing
speed_limitonEdgeInfo, we only need one copy per road. The penalty to this space saving is that we can't support the 0.001% of roads that need different limits in opposite directions.kevinkreiser commentedon Feb 5, 2021
@gknisely can correct me if im wrong but, we do support different speeds in different directions. its stored in directededge::speed. i forget if enhancer messes with this speed though so its possible its there but then gets modified later in the pipeline but technically we do support different speeds in different directions along the same stretch of singly digitized road
nilsnolde commentedon Feb 5, 2021
Yes, for actual speed that's right @kevinkreiser. For
speed_limit(which I'd be interested in here) it's not. But @danpat is right, it's a fraction of roads compared to those having amaxspeedtag. Also very secondary requirement.More interesting is how one would optimally get the speed_limit from edge_info, so it's available during the expansion. Would it have to go into the directededge interface? Eventually we'd have the country-/roadclass-specific defaults for legal
speed_limitfrom the admin db so it could make sense? However, not so sure how many other use cases there are for legal speed limits..dnesbitt61 commentedon Feb 5, 2021
yea, anything used in pathfinding (expansion) would need to be in DirectedEdge. Having to look up EdgeInfo (which is not tightly packed in adjacent memory like the directed edges that leave a node) would I think be unacceptable performance loss.
kevinkreiser commentedon Feb 5, 2021
we parse the bidirectional speed
https://github.com/valhalla/valhalla/blob/master/lua/graph.lua#L1472-L1473
we store it in the binary memmapped way object
https://github.com/valhalla/valhalla/blob/master/src/mjolnir/pbfgraphparser.cc#L417-L425
we then look it up to store it in the tile
https://github.com/valhalla/valhalla/blob/master/src/mjolnir/graphbuilder.cc#L516-L520
https://github.com/valhalla/valhalla/blob/master/src/mjolnir/graphbuilder.cc#L689
to tell if you actually have a speed limit you must also check if its a "tagged" speed, which gets set when we make the edge:
https://github.com/valhalla/valhalla/blob/master/src/mjolnir/directededgebuilder.cc#L76
nilsnolde commentedon Feb 5, 2021
Hmpf was forgetting about the Lua step there.. Thanks for the pointers @kevinkreiser ! Is it an option for you to store a speed_limit on the directed edge? Not on the computer right now, but I think there are 64 bit spare. Would it be worth 8 of those? 😇
dnesbitt61 commentedon Feb 5, 2021
the 64 bits is in DirectedEdgeExt which is not integrated with any path algorithms (as far as I know).
kevinkreiser commentedon Feb 6, 2021
hmm maybe im misunderstanding what you are asking @nilsnolde but i thought the pointers i linked above proved that if the
speed_typeisSpeedType::kTaggedthen the stored speed IS the speed limit. in fact the only time the speed isnt the speed limit is when there is no maxspeed tag at all. what am i missing here?nilsnolde commentedon Feb 6, 2021
Yeah I should’ve stopped to listen.. was too focused on getting an answer before the weekend to what I thought the question was 😕 thanks for slamming it in my face😊
danpat commentedon Feb 6, 2021
So in most cases, does this make
speed_limitonEdgeInfois redundant? If amaxspeedtag doesn't exist, then it won't be set, ifmaxspeeddoes exist, thenspeed_typewill let us know thatDirectedEdge::speed_contains the value.When is
EdgeInfo::speed_limituseful?kevinkreiser commentedon Feb 6, 2021
As I was saying at the very beginning I'm pretty sure we "enhance" the speed to account for urban vs rural speed limit adherence. Basically we make urban ones slower and rural ones faster. The good news is, for the tagged (ie speed limit speeds) this update is very deterministic and doesnt even care about urban vs rural: https://github.com/valhalla/valhalla/blob/master/src/mjolnir/graphenhancer.cc#L116-L150
So to get the actual speed limit back without going to the edgeinfo object we can simply reverse this transform. Honestly it seems like a bit of an oversight. I highly doubt that adding that if block costing would dramatically change the runtime of the path algorithms. We probably could have just let the speed limit out of edge info and added the surface penalty on the fly in costing.
Seems like an artifact of hysterical raisins or plans that never fully came to be. I mean maybe we can even refactor a bit to make it so that we always get speed limit out of the directed edge and forget about edge_info while still keeping the tile format backwards compatible. I have to brainstorm that a bit. The other option is to leave the format as is and as i said about undo the transform to get the speed limit.
dnesbitt61 commentedon Feb 6, 2021
Yea, as @kevinkreiser says this was added to support future enhancements and perhaps did not need to occur. The thought was that adding speed_limit to EdgeInfo would protect in future cases where the DirectedEdge speed might be modified from the OSM tagged speed limit. The idea was that the speed in DirectedEdge would provide the base speed used in costing for routes in absence of any traffic based speeds - even if this speed was not the actual speed limit. The thought was that speed limits would always be available even if not used in any costing methods.
nilsnolde commentedon Feb 28, 2021
Having looked at it again, it seems to me there actually isn't a way currently to reliably retrieve the actual
maxspeedfrom an edge: ifmaxspeed:advisoryormaxspeed:practicalis set, it's also a "tagged speed" and is favored overmaxspeed:valhalla/src/mjolnir/pbfgraphparser.cc
Lines 1413 to 1423 in 19fa661
Of course, it's pretty unlikely: combined those two tags occur ~60k times currently. And the good thing is they should definitely underestimate the legal max speed, so in worst case it'd take an unnecessary detour, rather than routing through forbidden roads.
But still, I'd prefer an accurate implementation. So I ended up extending the DirectedEdge interface with
max_speed_and using up 8 of the last 9 spare bits, then checking inCost::Allowed(Reverse)if the set legal speed is higher than the edge'smax_speed_. Less invasive ideas welcome. I can PR soon if you'd accept smth like that.nilsnolde commentedon Feb 18, 2023
super niche case, I take it back:)