I am using pgr_ksp for finding multiple paths for single source and destination. I am wondering if 'source' and 'target' in a roads table are unique or not. because when i am doing routing on my road network table and displaying their geometry on the basis of their 'target' value, it is giving me extra link roads everywhere. This is the query i am using

   SELECT pt.gid, seq AS route, id1 AS node,  id2 AS edge, cost, geom as line
   FROM pgr_ksp(
   'SELECT gid as id, source::int4, target::int4, length::float8 as cost FROM my_table',
    2087, 1428, 2, false
    as di JOIN my_table pt
    ON di.id2 = pt.target

enter image description here

The red color is showing the output of the query. I know there is nothing wrong with the routing result but when i am trying to get the geometry, 'target' value is i think the only way to get it for display

enter image description here

I want the result as i am showing in the 2nd image with Red color, but right now i am getting extra road segments (shown in Green color) because i am choosing to display geometry from target value.

Kindly suggest me what to do in order to get better result


You should use id3 of the result instead of id2 because id3 is the edge id according to http://docs.pgrouting.org/dev/src/ksp/doc/index.html

  • thanks a lot... it worked for me. but now i have another issue and many other people also face this problem with pgr_ksp, sometimes prg_ksp returns null, i mean not a single route. First I thought when there is no alternative route then it gives NULL but its not the case. I have selected other source and destination with k=2 and its giving me single route... Why is it so?? – osqureshi Apr 10 '14 at 5:37
  • i have figured out that pgr_ksp is not giving me any route for some particular source and destinations because in that route "target" of first road segment is not matching the "source" of the second road segment. Do you have any solution for that? – osqureshi Apr 10 '14 at 5:53

source and target attribute contain node ID's. Each node ID should be unique, but it can appear multiple times in source and target column, because a node can be the connecting point for more than one road link.

Depending of the direction (defined by the geometry) of the road link the node ID is source or target.

  • kindly take a look on my query and image and suggest me a change that can work for me – osqureshi Apr 8 '14 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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