I'm trying to compute the shortest path between points using the QGIS networkanalysis library. But the graph.findVertex always return -1.

Basically what I'm trying to do is : 1/ Given a set of points, create a grid. The size of the grid is define by the poins bounding box 2/ transform all the cells' grid to point (centroid) 3/ For each cell's centroid compute the shortest path with the set of points

My script works fine with no error, but the graph.findVertex() function always return -1. It's like my points are not tied to the graph...

My code is available on pastebin : http://pastebin.com/u6n4tSkz

Do you know what I'm doing wrong ?

  • Can you confirm that graph and grid are in the same CRS?
    – underdark
    Commented Aug 1, 2015 at 8:15
  • Hi @underdark, thanks for your help. The CRS iis the same for both layers (EPSG:32740). This is one of the first I've checked, but unfortunately it was not that simple :( Commented Aug 3, 2015 at 12:04

1 Answer 1


Check if your graph contains the points you later want to find using findVertex. According to http://qgis.org/api/qgsgraph_8cpp_source.html, the routing graph has to contain the exact point:

72 int QgsGraph::findVertex( const QgsPoint& pt ) const
73 {
74   int i = 0;
75   for ( i = 0; i < mGraphVertexes.size(); ++i )
76   {
77     if ( mGraphVertexes[ i ].point() == pt )
78     {
79       return i;
80     }
81   }
82   return -1;
83 }

Try and check if the tying in makeGraph failed:

Note if tiedPoints[i]==QgsPoint(0.0,0.0) then tied failed.

  • Hi undertak, the number of points seems to be good. If I do print len(gridCentroidFeats) & print len(processing.features(self.pointLayer)) the total number is 770 + 12. And if I do a print len(tiedPoints) the result is 782. So all the points seem to be here. Commented Aug 5, 2015 at 8:35
  • Thanks to your suggestion I've tried to loop on the tiedPoints to check the geometry. So if I do a "for tiedPoint in tiedPoints: print tiedPoint.wellKnownText()" the result is POINT(0 -0), POINT(0 -0), POINT(0 -0)... Commented Aug 5, 2015 at 8:44
  • Hi underdark, do you have any idea why the makeGraph function has failed ? I've got no errors and the code seems to be fine... Thanks for your help Commented Aug 6, 2015 at 3:41
  • Maybe the distances are too big. Can you share test data?
    – underdark
    Commented Aug 6, 2015 at 8:51
  • Hi Underdark. Here you will find the data, a qgis project and the python code : dl.dropboxusercontent.com/u/96280295/shared_data.zip Thanks :) Commented Aug 6, 2015 at 9:37

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.