I am trying to use pgRouting in order to find the shortest route between two points. I realized that pgr_dijkstra() function is not able to find the route when the two points are far from each other or when they are not very straight. However, when I select the two points in a neighbourhood, it can give me the shortest path.

This is the roads table (edges):

enter image description here

and this is the table of nodes that pgrouting created with the ending of "_vertices_pgr":

enter image description here

Does any one know what is the problem? I used this query:

select pgr_dijkstra('select id , source, target, st_length(geom) as cost FROM q_roads_noded', 10237, 10551)

q_roads is the table of roads (edges)

The network is like this: I tried to find the shortest path between points with id: 10237 and 10551. But the function returned nothing!!!

enter image description here

  • Maybe it would help if you posted a picture of the network along which you are routing. Check the edges - are they actually broken at intersections or do they pass over each other? pgr_createTopology will not break lines at intersections for you, you have to do this yourself ahead of time.
    – wfgeo
    Aug 12, 2019 at 20:11
  • I added a photo of QGIS. Yes, All the lines are broken to segments in the intersections. I check it by selecing segments in QGIS. The function runs without any output: "Successfully run. Total query runtime: 71 msec. 0 rows affected."
    – milad
    Aug 12, 2019 at 20:27
  • Apparently this guy had a problem similar to me: stackoverflow.com/questions/32508581/pgrouting-not-find-routes
    – milad
    Aug 12, 2019 at 20:34
  • Note that by default pgr_dijkstra assumes a directed graph (see function parameter), so only traversing edges in source-> target direction is allowed; see if that's possible between those points.
    – geozelot
    Aug 12, 2019 at 21:43
  • 2
    When executing pgr_dijkstra(), pass it a parameter called directed which is equal to false. Every edge has an implicit "direction", which goes from the source node to the target node. if you do not specify that you can also travel from the target node to the source node, it will assume that the graph is direction, and it will not allow a routing to occur if i requires going "backwards" along an edge.
    – wfgeo
    Aug 13, 2019 at 7:53

1 Answer 1


My problem is solved by the solution that wfgeo and ThingumaBob gave me.

I changed the query as:

select * from pgr_dijkstra('select id , source, target,  cost FROM q_roads', 10237, 10551, false)

in fact the only difference with the previous one is adding false as the last argument. Previously, I did not write anything as last argument and it was true by default and it considered that the network has a direction! but now it finds all the routes.

take care of the last argument while using this function!!

Your Answer

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

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