1

I have a road network data where roads(edges) are specified with 3 values to identify the category of road(one way).

  • Road with value 0 can be routed in both direction.
  • Road with value 1 can be routed in the directed way.
  • Road with value -1 can be routed in opposite direction. enter image description here

As seen in above picture. Blue edges can be routed in both direction, green edges should directed in the same direction how they are digitised and edges with red colors should be directed in opposite direction of the arrow.

I am trying to get route from one node to another using pgRouting on the above data.

pgRouting works fine if it is given as undirected graph with bellow query.

select seq, node, edge, pt.cost, agg_cost, st_astext(the_geom) as geom from pgr_dijkstra('select id as id, source, target,cost from ways_noded', 60414, 68634, directed := false) as pt join ways_noded rd on pt.edge = rd.id;

But I am facing problem with routing with one ways in directed graph. Above query with directed:=true considers the edges which are directed towards same direction. I am not able to direct with the edges which are mentioned as two ways(0) also.

OSM data also has a one_way field and this link says 0 for both ways, 1 for one way and -1 for reverse way and they also have an additional value 2 for specifying 2 way roads.

Can anyone help me in routing through one way and reverse ways? I found this link but didn't get much info on directed graph.

1 Answer 1

5

You would need to prepare your data beforehand. Add cost and reverse_cost columns to your ways_noded table. Then, you populate them using the way length if usable in this direction, or an insanely high value, as described in the the link you provided, if it is not usable in this direction.

update ways_noded
set cost = length(the_geom)
where directionindicator in (0,1);

update ways_noded
set cost = 1000000
where directionindicator =-1;

update ways_noded
set reverse_cost = length(the_geom)
where directionindicator in (0,-1);

update ways_noded
set reverse_cost= 1000000
where directionindicator =1;

At last, you call pgr_dijkstra using a directed network.

select seq, node, edge, pt.cost, agg_cost, st_astext(the_geom) as geom 
from pgr_dijkstra('select id as id, source, target,cost, reverse_cost 
                   from ways_noded', 60414, 68634, directed := true) as pt 
join ways_noded rd on pt.edge = rd.id;
1
  • I needed to use st_length(the_geom::geography,true) to calculate line string length for my geometries.
    – Aparichith
    Commented Oct 16, 2017 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.