pgrouting icon indicating copy to clipboard operation
pgrouting copied to clipboard

Refer to previous edges for cost function

Open illpack opened this issue 6 years ago • 5 comments

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,

illpack avatar Nov 13 '18 16:11 illpack

Nothing of the kind can be done with current pgRouting (nor in the near future)

cvvergara avatar Nov 14 '18 21:11 cvvergara

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

flother avatar Nov 15 '18 09:11 flother

maybe using trsp

cvvergara avatar Nov 15 '18 11:11 cvvergara

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

image

Somehow lagging within the function does not yield the expected results. Is it worth trying with a different function?

Thanks!

illpack avatar Nov 15 '18 14:11 illpack

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

cvvergara avatar Nov 24 '18 18:11 cvvergara