1

How to get a particular feature using an expression in PyQGIS?

For example, there are 100 houses (with 100 entries in Attributes Table). I want to get only one house with field name ("name"=="John's House") and make get selected that feature in the map view.

Taras
  • 32,823
  • 4
  • 66
  • 137
Devenepali
  • 365
  • 2
  • 10

1 Answers1

7
layer = iface.activeLayer()

layer.selectByExpression('"name"=\'John's House\'', QgsVectorLayer.SetSelection)

selection = layer.selectedFeatures()

print(selection)

References:

Taras
  • 32,823
  • 4
  • 66
  • 137