8

How can I build a feature request expression using variable inside expression? For example

values = [1, 2, 3]
for j in range( y ):
it = vl.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"field_name" = values[ j ]' ) )

or (I have also tried sthing like this)

it = vl.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"field_name" = values[%d]' % j) )

Where could someone study how to build these kind of expressions?

2 Answers 2

8

As starting point for studying I would use the QGIS Cookbook. For further reading with additional examples look at the books Joseph suggests in his answer.

As for your specific problem I would suggest string substitution like

it = vl.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"field_name" = {0}'.format(values[j])) )
0
2

There's a number of available books which help teach Python for QGIS. To name a few:

"The Pyqgis Programmer's Guide" by Gary Sherman

"QGIS Python Programming Cookbook" by Joel Lawhead

"Learning Geospatial Analysis with Python" by Joel Lawhead

"Mastering QGIS" by Kurt Menke, Luigi Pirelli, Richard Smith Jr & John Van Hoesen

Hope this helps!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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