I am trying to start with PyQGIS and make a simple script in the processing toolbox.
##theVector=vector
theVec = processing.getObject(theVector)
features = theVec.getFeatures()
now, if I'd like to iterate over every Object I'd write:
for feature in features:
but I just want to get the first object, and NOT interate over everyone..
I have tried:
f1 = features.get(1)
and get
'QgsFeatureIterator' object has no attribute 'get' See log for more details
How do I get just one feature?
Best Answer
It is a pure Python problem not limited to PyQGIS. I learn Python before using PyQGIS and the Help of PyQGIS.
In pure Python examine the attributes of
feature
with the dir() function:And there is no
get
here, hence the error:QgsFeatureIterator' object has no attribute 'get' See log for more details
when you tryf1 = features.get(1)
.In contrats,
features
has the__iter__
method which means that you can use afor loop
, look at Understanding Python Iterables and IteratorsSo
for feature in features
is the same asfeatures.next(), features.next(),...
Therefore the answer of Underdark
features.next()
is a pure Python problem, not limited to the Help of PyQGIS (The same kind of problem existed with Avenue, even with the Help)