0

I have some Python code to get the ids and georeference from a shapefile:

canvas = qgis.utils.iface.mapCanvas()
allLayers = canvas.layers()
nos = QgsFeature()
for i in allLayers:
    if i.name()=='path_nodes':
        path_nodes = i

nodedata  = path_nodes.dataProvider()

while nodedata.nextFeature(nos):
    i = i + 1
    print str(no.gid())

It does not return all of the ids. Can anyone help?

1
  • How many ids did it miss?
    – R.K.
    Commented May 21, 2013 at 3:57

2 Answers 2

2

In the while loop, no doesn't refer to anything, you need to assign it to the current feature. I don't know how the QGIS API works, so here's a somewhat ugly approach:

while True:
    try:
        no = nodedata.nextFeature(nos)
    except:
        break
    print str(no.gid())
1
  • 1
    @user1573901: no problem! Please consider accepting the answer (click the tick mark under the down arrow) if it solved your problem.
    – bananafish
    Commented May 21, 2013 at 21:28
0

As @bananafish noted, no should be nos. You might also want to set i=0 before the loop.

nos = QgsFeature()
while nodedata.nextFeature(nos):
    i = i + 1
    print str(nos.gid())

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.