Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
How many ids did it miss? –  R.K. May 21 '13 at 3:57

2 Answers 2

up vote 0 down vote accepted

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())
share|improve this answer

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())
share|improve this answer
    
Thank you very much. Done –  user1573901 May 21 '13 at 9:26
    
@user1573901: no problem! Please consider accepting the answer (click the tick mark under the down arrow) if it solved your problem. –  bananafish May 21 '13 at 21:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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