0

My script to compute distance in my network works. Now I want to loop over all features of a point layer to produce multiple distance calculations.
Just iterating over the features work of course and prints all of them correctly. When I add the rest of the script QGIS shuts down immediately after the first iteration, but I am not able to recover an error message.

for f in points_layer.getFeatures():

points_to_tie = [QgsPoint(f['longitudeO'], f['latitudeOr']), QgsPoint(f['longitudeD'], f['latitudeDe'])]

#PROGRESS:
#count += 1
#print "calculating number %s" % count

tiedPoints = director.makeGraph(builder, points_to_tie)
graph = builder.graph()

startId = graph.findVertex(tiedPoints[0])
endId   = graph.findVertex(tiedPoints[-1])
(dtree, dcost) = QgsGraphAnalyzer.dijkstra(graph, startId, 0)
print "Travel time from %s to %s is %s hours" % (f['municipOri'], f['municipDes'], dcost[endId])
del graph 
del tiedPoints
del points_to_tie
del startId
del endId
del dtree
del dcost

I thought this question was a good hint, but it doesn't solve it when I delete everything at the end of the loop.
Is there an obvious mistake that I am making?

0

As it turns out, the problem was that I loaded the network layer outside of the loop, which is something that python doesn't like apparently (I still don't know why). The solution is to load everything within the loop; apart from the layer that you are iterating over of course.

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.