In this section, we'll introduce how to loop in Python and how to apply loops to explore the content of the layer loaded in the previous paragraph. Write the following snippet in the Python Console, taking special care with the code indentation:
for feature in layer.getFeatures(): print("Feature %d has attributes and geometry:" % feature.id()) print(feature.attributes())
This will print a pattern like the following:
Feature with id 68 has attributes and geometry: [69, 9, 12.0, 'HAINES', 'Other']
Note
The layer.getFeatures()
method returns an object that can be iterated inside a for
Python instruction, getting a QgsFeature instance for every loop. The feature.attributes()
method returns a list (inside the brackets, []
) of the integer and Unicode strings (the u' values). The feature.geometry()
method returns QgsGeometry
, which is converted in QgsPoint
to be printed as a tuple (inside the ()
parenthesis) of coordinates.
It is strongly...