pgrouting
pgrouting copied to clipboard
Refer to previous edges for cost function
I could not find such a thing in the docs:
I want to calculate cost based on the previous edge (for instance, get the path with the least number of turns). Is there a way to do this currently?
Something along these lines:
angle(parentEdge, currentEdge) AS cost
Thanks,
Nothing of the kind can be done with current pgRouting (nor in the near future)
Would PostgreSQL's window functions help you out here? If you already have an ordered set of edges and their geometries, you can get the angle between the current and previous rows with something like this:
ST_Angle(LAG(the_geom) OVER (), the_geom)
You'd need to make sure that the edges themselves are in the correct order (i.e. the end point of the first edge is the start point of the second edge).
maybe using trsp
@flother That was a good hint, thanks.
I tried that, first ordering the set of edges, then lagging -1 to calculate the angle.
Tested on a set of edges and it works fine.
However, it does not seem to yield valid angles when used inside pgr_dijkstra
.
The following image shows the resulting route, the tiny numbers are the cost returned by pgr_dijkstra
, the bold ones are angles applied to the resulting routing list (which seem to be correct)
Somehow lagging within the function does not yield the expected results. Is it worth trying with a different function?
Thanks!
Maybe another idea ... there is the experimental function on version 2.6: https://docs.pgrouting.org/2.6/en/pgr_lineGraphFull.html have additional edges for every turn that could be made across and give a cost based on angles to those additional edges