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
returnp.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()); // TODO PERF: cache bbox and edge wayGeometry for multiple area
}
This has a few drawbacks. On the one hand, we lose precision, since the full point list could in fact have a larger BBox than that of only the tower nodes:
And it adds computational overhead as discussed previously:
We currently use it for the block_area feature (GraphEdgeIdFinder) and, like you say, for the custom weighting areas. We use the tower node bounding box in if (!p.getBounds().intersects(bbox)) return false;, which is only a performance optimization that skips the more costly detailed intersection check for many edges. We then use the full geometry for the detailed check p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable());.
On the one hand, we lose precision, since the full point list could in fact have a larger BBox than that of only the tower nodes
I agree the current logic is not very intuitive. For example if p is a rectangle and the tower node bounding box intersects p it can happen that a road is affected by a custom area even though none of the road points lies within the area (false positive). On the other hand, if the roads geometry partly lies outside of the tower node bounding box, like in your picture, it can happen that a road is not affected even though some points are located within the area (false negative).
The false positives are only possible because of the rectangle optimization introduced here: #2326, right? Without the if (p.isRectangle()) true case we would only ever return true if p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()); was true, which is only true if one of the geometry points actually lies within p. And I think this is what we want: A road should be affected by a custom area if and only if at least one point is located within the area?!
So to actually find out which roads are affected by a custom model area we need to do the full check p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable());, but yes we can skip it if the real (geometry) bounding box does not intersect the polygon envelope bounding box. Currently we already skip it if the tower node bounding box does not intersect the polygon envelope bounding box, which is wrong in some cases (the false negatives I mentioned above).
And it adds computational overhead as discussed previously:
Actually I don't really think so. Let's say we used the full geometry bounding box in GHUtility.createBBox instead of the tower node bounding box we use currently. This would mean that more edges would be considered for the full p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()); check. So overall the area check could potentially be slower. But it would be more precise as we would get rid of the false negatives. I do not expect that reading the stored full bounding box would be any faster (or slower) than reading the tower node bounding box as we do now, because the fetchWayGeometry(FetchMode.TOWER_ONLY) call in GHUtility#createBBox does not 'fetch' the actual geometry anyway:
I think that maybe we should make the custom area check more precise (get rid of false positives and negatives), and this would require using the real geometry bounding box instead of the tower node bounding box, but this will make the custom area feature potentially slower rather than faster.
The code you quoted does include a (costly) fetchWayGeometry(FetchMode.ALL) call, but we cannot replace it with a stored bounding box, even if it was the full geometry bounding box. For this detailed check we actually want to check if any point lies within the polygon, so the bounding box wouldn't be sufficient.
So to me it seems like the real question is: Do we want to make the custom areas more precise? If yes we need to use the full bounding box for GHUtility#createBBox and to make sure this does not slow down the custom area feature it is probably a good idea to store the full bounding box. But overall this won't make the feature faster. If anything it will be slower.
If our goal is rather to make the custom area feature faster as requested in the forum discussion you mentioned storing the full bounding box won't help at all I think (unless we want to increase the precision without slowing it down). To speed up custom area routing we probably need to come up with something else and first we should have a reproducible example that shows how slow the routing really is.
Regarding actually storing the bounding box for each edge I found this related comment: #2320 (comment)
But overall this won't make the feature faster. If anything it will be slower.
Not necessarily as we currently fetch two random lat+lon reads in nodeAccess, which would be potentially replaced with a single read. But performance changes are often hard to guess :) (and easy to try)
I found this related comment: #2320 (comment)
.. and we could probably even somehow restrict this to 'curved' edges only, because for straight edges using the tower node coordinates as bounding box is often sufficient.
Yes, if the performance change is 0 this would be a nice storage optimization.
To speed up custom area routing we probably need to come up with something else and first we should have a reproducible example that shows how slow the routing really is.
Yes 👍
Probably we need some typical use cases that should be fast like single edge exclusions. E.g. for that we could fetch the edge-IDs. But first we would have to see how slow they are :)
And if there are several cases with a big slow down I can imagine we introduce another less precise helper method like likely_in_area instead of in_area :) Because especially for larger polygons the precision down to the edge geometry might not matter.
Because especially for larger polygons the precision down to the edge geometry might not matter.
Yes it really depends on the size of the areas. For small areas the current precision seems insufficient to me, but for big ones it probably does not really matter and we could even be less precise still, i.e. only check if one of the two tower nodes is in the area or not. By the way this could be an easy optimization: We could briefly check if one of the two tower nodes is in the area before we fetch the geometry. If yes return true and only otherwise do the full p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()); check.
For small areas the current precision seems insufficient to me
Agreed. Blocking individual edges is a feature that is still missing in CustomModels, but it can be substituted with small areas.
Maybe we should also take a step back and get some numbers on what's really causing the slowdown here. Since people seem to be complaining, this should be measurable.
Activity
easbar commentedon Jul 18, 2022
Ok, but the code where we use only the tower nodes to compute the bounding box is this method:
graphhopper/core/src/main/java/com/graphhopper/util/GHUtility.java
Lines 939 to 944 in 06b261a
We currently use it for the block_area feature (GraphEdgeIdFinder) and, like you say, for the custom weighting areas. We use the tower node bounding box in
if (!p.getBounds().intersects(bbox)) return false;, which is only a performance optimization that skips the more costly detailed intersection check for many edges. We then use the full geometry for the detailed checkp.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable());.I agree the current logic is not very intuitive. For example if p is a rectangle and the tower node bounding box intersects p it can happen that a road is affected by a custom area even though none of the road points lies within the area (false positive). On the other hand, if the roads geometry partly lies outside of the tower node bounding box, like in your picture, it can happen that a road is not affected even though some points are located within the area (false negative).
The false positives are only possible because of the rectangle optimization introduced here: #2326, right? Without the
if (p.isRectangle()) truecase we would only ever return true ifp.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable());was true, which is only true if one of the geometry points actually lies withinp. And I think this is what we want: A road should be affected by a custom area if and only if at least one point is located within the area?!So to actually find out which roads are affected by a custom model area we need to do the full check
p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable());, but yes we can skip it if the real (geometry) bounding box does not intersect the polygon envelope bounding box. Currently we already skip it if the tower node bounding box does not intersect the polygon envelope bounding box, which is wrong in some cases (the false negatives I mentioned above).Actually I don't really think so. Let's say we used the full geometry bounding box in
GHUtility.createBBoxinstead of the tower node bounding box we use currently. This would mean that more edges would be considered for the fullp.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable());check. So overall the area check could potentially be slower. But it would be more precise as we would get rid of the false negatives. I do not expect that reading the stored full bounding box would be any faster (or slower) than reading the tower node bounding box as we do now, because thefetchWayGeometry(FetchMode.TOWER_ONLY)call inGHUtility#createBBoxdoes not 'fetch' the actual geometry anyway:graphhopper/core/src/main/java/com/graphhopper/storage/BaseGraph.java
Lines 398 to 405 in 06b261a
I think that maybe we should make the custom area check more precise (get rid of false positives and negatives), and this would require using the real geometry bounding box instead of the tower node bounding box, but this will make the custom area feature potentially slower rather than faster.
The code you quoted does include a (costly)
fetchWayGeometry(FetchMode.ALL)call, but we cannot replace it with a stored bounding box, even if it was the full geometry bounding box. For this detailed check we actually want to check if any point lies within the polygon, so the bounding box wouldn't be sufficient.So to me it seems like the real question is: Do we want to make the custom areas more precise? If yes we need to use the full bounding box for
GHUtility#createBBoxand to make sure this does not slow down the custom area feature it is probably a good idea to store the full bounding box. But overall this won't make the feature faster. If anything it will be slower.If our goal is rather to make the custom area feature faster as requested in the forum discussion you mentioned storing the full bounding box won't help at all I think (unless we want to increase the precision without slowing it down). To speed up custom area routing we probably need to come up with something else and first we should have a reproducible example that shows how slow the routing really is.
Regarding actually storing the bounding box for each edge I found this related comment: #2320 (comment)
karussell commentedon Jul 18, 2022
Not necessarily as we currently fetch two random lat+lon reads in nodeAccess, which would be potentially replaced with a single read. But performance changes are often hard to guess :) (and easy to try)
Yes, if the performance change is 0 this would be a nice storage optimization.
Yes 👍
Probably we need some typical use cases that should be fast like single edge exclusions. E.g. for that we could fetch the edge-IDs. But first we would have to see how slow they are :)
And if there are several cases with a big slow down I can imagine we introduce another less precise helper method like
likely_in_areainstead ofin_area:) Because especially for larger polygons the precision down to the edge geometry might not matter.easbar commentedon Jul 18, 2022
Yes it really depends on the size of the areas. For small areas the current precision seems insufficient to me, but for big ones it probably does not really matter and we could even be less precise still, i.e. only check if one of the two tower nodes is in the area or not. By the way this could be an easy optimization: We could briefly check if one of the two tower nodes is in the area before we fetch the geometry. If yes return true and only otherwise do the full
p.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable());check.otbutz commentedon Jul 19, 2022
Agreed. Blocking individual edges is a feature that is still missing in CustomModels, but it can be substituted with small areas.
Maybe we should also take a step back and get some numbers on what's really causing the slowdown here. Since people seem to be complaining, this should be measurable.