4

How do I get features by their attributes (similar to Iqueryfilter in ArcObjects) in QGIS using Python?

Instead of getting all features and filter it manually, is there any option to use whereclause to filter it out?

For Example: I have a field name called 'Counties'. It has more than fifty thousand of features i.e. not possible to fetch all features and filter it because of time consuming. So i can query it by using iqueryfilter.whereclause = 'Counties = Norwich' in arcobjects.

I need a similar thing in PyQGIS.

1 Answer 1

5

Update Please read this answer for a safer method of achieving the same https://gis.stackexchange.com/a/74940/9839

In case you experience performance issues you may still resort to original method described below.

The only way of forwarding a WHERE clause to the data provider (backend) at the moment is to use the setSubsetString method of QgsDataProvider

If you have a QgsVectorLayer assigned to a variable vl :

vl.setSubsetString( 'Counties = "Norwich"' )

This causes heavy side-effects, because the filter will then apply to the whole layer and not only to your query, so you have to carefully consider if this is really an option for your use case!

Any other option will require a seperate connection to the database or adding a new layer with a subsetString as outlined above.

0

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