Skip to main content
2 of 2
deleted 9 characters in body; edited title
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Getting features by attribute using PyQgis?

How can I get a feature's geometry from selection by attribute?

For instance from a point layer I want to get the geometry by the field CODICE filter (CODICE = A001).

I tried with:

layer = QgsMapLayerRegistry.instance().mapLayersByName("My_layer")[0]
selection = layer.getFeatures(QgsFeatureRequest(QgsExpression("\"CODICE\" = 'A001'")))
geom = selection.geometry()

It doesn't work, and the python console error is:

AttributeError: 'QgsFeatureIterator' object has no attribute 'geometry'

Chlorine
  • 63
  • 1
  • 3