I'd like to write an expression function in QGIS 2.18 to select features based on the value of their attributes. How can I access attribute values within my function?
I have noticed that in QGIS 2.18.x, the selection does not seem to work when accessing feature attributes as feature['attribute']
.
For example, I tried the following functions to select features from Natural Earth's Populated Places Simple dataset. The three functions below select all the capital cities in QGIS 2.14.12, but no features are selected in QGIS 2.18.7.
@qgsfunction(args=0, group='Custom')
def select_country_capitals_1(value1, feature, parent):
""" Select all the capital cities """
return feature['featurecla'] == 'Admin-0 capital'
@qgsfunction(args=0, group='Custom')
def select_country_capitals_2(value1, feature, parent):
return feature[3] == 'Admin-0 capital'
@qgsfunction(args=0, group='Custom')
def select_country_capitals_3(value1, feature, parent):
return feature.attributes()[3] == 'Admin-0 capital'
Agreed that the above is possible with a simple query "featurecla" = 'Admin-0 capital'
from the expression engine, but I would like to access attribute values within the function and perform some further computation.