Bug report #16424

Feature selection using custom python expression functions

Added by Simran Khare about 7 years ago. Updated about 7 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:Expressions
Affected QGIS version:2.18.5 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:24333

Description

This is regarding selecting features by expression in QGIS 2.18.x. The selection does not seem to work when we try to access the feature attributes in our custom expression function.

For example, we would expect the following function to select all features with non-null osm_ids. Upon executing the function in the expression engine, the output preview is 'true', but no features are selected.

@qgsfunction(args=0, group='Custom')
def fun3(value1, feature, parent):
if feature['osm_id']:
return True
else:
return False

The above function works fine in QGIS 2.14.12, but not in 2.18.5.

Attached is the pbf file and some Python functions I tested with. Please add the point vector layer from 'tulersee.pbf' and call fun3() to reproduce the error.

turlersee.pbf - Small pbf file (181 KB) Simran Khare, 2017-04-07 04:34 AM

test.py Magnifier - test.py has some simple expression functions (934 Bytes) Simran Khare, 2017-04-07 04:34 AM

History

#1 Updated by Giovanni Manghi about 7 years ago

  • Easy fix? set to No
  • Regression? set to No

#2 Updated by Simran Khare about 7 years ago

  • Status changed from Open to Closed

This is not a bug, but a slight difference in custom python expression functions in QGIS 2.18.
In version 2.18, any feature attribute that we intend to use within the function must be passed as an argument to the function.

So, for example, a function that accesses the 'osm_id' field should be modified as follows and called as fun("osm_id"). In this case the features are selected.

@qgsfunction(args='auto', group='Custom')
def fun(field, feature, parent):

if feature['osm_id']:

return True

else:

return False

Thanks to this answer: https://gis.stackexchange.com/a/238226/94852

Also available in: Atom PDF