I've used pyqgis to calculate the shortest path through a network between two points. I want to turn this shortest path into a polyline.
I've managed to use the QGIS dijkstra function to calculate a shortest path between two points in a network.
If I print the resulting variable point_route as a string, it prints a list of coordinates.
Where do I go from here?
Is there a(nother) well-documented python library that I can use to turn this list of coordinates into a single polyline shapefile? I can't seem to find/understand the right instructions for pyqgis.
Relevant code
(copied from the shortest path chapter of 'pyqgis developer cookbook'):
(tree, cost) = QgsGraphAnalyzer.dijkstra(graph, startId, 0)
if tree[endId] == -1:
print "Path not found"
else:
point_route = []
curPos = endId
while curPos != startId:
point_route.append(graph.vertex(graph.arc(tree[curPos]).inVertex()).point())
curPos = graph.arc(tree[curPos]).outVertex()
print "appended " + str(curPos)
point_route.append(tStart)
Best Answer
The result of the PyQGIS script is a list of points (point_route)
In this example, the point_route result is (7 points between
pStart = QgsPoint(110579.17281126765, 116585.26070464966)
, point 7, andpStop = QgsPoint(111819.28396552203, 116854.75854462344))
, point 22)If you want to transform these points in a LineString
And you can use pyQGIS to save the result to a shapefile (in green).
But if you want a pure Python script you don't need QGIS (standalone script), simply use the module NetworkX (many examples in GIS SE) in combination with the module Shapely
And you can use NetworkX, Fiona or pyshp (as Clubdebambos propose) to save the result to a shapefile