Tuesday, 30 June 2015

qgis - How to optimize QgsFeatureRequest with filter expression


I have a point PostGIS layer (my_layer) which has more than 66000 features. I am getting the features I want but it takes too long. My code is:


    resultList = []
req = QgsFeatureRequest().setFilterExpression(' "some_field_name" = \'some_value\' ')
for feat in my_layer.getFeatures(req):

name = feat.attribute("some_other_field")
resultList.append(name)

How can I optimize it so that I can get the results quicker?



Answer



QGIS API provides you with a couple of ways for optimizing features requests.


In your case, if you don't need geometry and the rest of attributes in the result, you can:



  • Use flag NoGeometry (see docs).

  • Set subset of attributes you really need using setSubsetOfAttributes() (see docs).



That should speed your request up.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...