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.

closed as off-topic by ahmadhanb, Jochen Schwarze, BBG_GIS, BERA, whyzar Mar 15 at 13:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – ahmadhanb, Jochen Schwarze, BERA, whyzar
If this question can be reworded to fit the rules in the help center, please edit the question.

3
layer = iface.activeLayer()

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

selection = layer.selectedFeatures()

print(len(selection))

References:

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