Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
1  
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
1  
@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

4 Answers 4

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

it = l.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"Counties" = \'Norwich\'' ) )
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

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

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.
Example:

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.append(g.Area()) 

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

Your Answer

 
discard

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.