Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to 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?

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. Similar thing i need in PyQgis. kindly let me know whether its doable

share|improve this question
Can you provide some context around this question? What are you really trying to achieve? What have you already tried? What worked for you? What didn't work the way you expected. You can just edit your question (click "edit" above) to include this information. – BradHards Apr 20 '13 at 5:44
@underdark I'm not sure this should be closed. It's not really the same. I think they are looking for something where they only return the data using a query from the layer, a bit like getFeatures can do. – Nathan W Apr 22 '13 at 0:20
If that's the case @venkat, please clarify the question or open a new one with more details. – underdark Apr 22 '13 at 8:32
@NathanW yes you are correct. i need only return the data using a query from the layer. could you please provide me any example in pyqgis? – venkat Apr 22 '13 at 9:52
@underdark could you reopen this please? – Nathan W Apr 22 '13 at 10:48

The QGIS expression engine is able to do this using the QgsFeatureRequest.setFilterExpression( unicode ) method (Since QGIS 2.2)

request = QgsFeatureRequest().setFilterExpression( u'"Counties" = \'Norwich\'' )
it = l.getFeatures( request )

Starting with QGIS 2.10 it is even possible that filtering in such a way will give you some extra performance over other types of filtering (like python implementations).

Basically this applies if the following three conditions are met:

  • You are using a layer with the postgis provider
  • Your expression is not overly complicated (things like >, =, IN, NOT NULL ... are supported)
  • You have enabled this feature in Settings > Options > Data Sources > Data Source Handling > Execute expression on postgres server-side
  • The performance benefit is optimal with appropriate indexes on the database tables
share|improve this answer

This post - which could be considered an answer to a duplicate question - details how to fetch all attributes from a layer. The author describes the process you are looking for as filtering the data manually once it is returned. It's a pretty complete reference and their link should really help you out.

share|improve this answer
This not a duplicate question. i dont want to fetch all attributes from a layer. Filter it first and then i want to fetch the features which are come under the filter criteria. i.e the performance is much better. – venkat Apr 22 '13 at 9:37

By using a sql query it also easily possible with ogr. You could execute this code for instance in the QGIS python console or a standalone script.

from osgeo import ogr

path = "path to your shapefile.shp"
ID = "FieldID" # For instance 'Countries' 
datasource = ogr.Open(str(path)) # your datasource

layer = datasource.GetLayer(0) # Import layer 0 --> only works with shapefiles
layerName = str( layer.GetName() )# Save the Layersname first

# Do the sql query
# Selects all features from a layer datasource where Field Countries is equal to 'Germany'
layers = datasource.ExecuteSQL("SELECT * FROM %s WHERE %s = '%s'" % (layerName, ID, 'Germany') )
res = []
for i in range(0,layers.GetFeatureCount()):
   f = layers.GetFeature(i)
   g = f.GetGeometryRef()

# res now contains the measured area of each feature where the attribute ID has the value 'Germany'
share|improve this answer

Specifying SQL filters are not yet supported using QGIS API as of version 1.9.

As I understand from this mailing list article, support for "native provider's SQL" would only be in a future release.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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