Saturday, 15 September 2018

python - QGIS Expression function


I would like to write a Python expression function which calculates the average of columns (variable length parameters), skipping NULL values.


@qgsfunction(args=-1, group='Custom')
def col_avg(vals, feature, parent):
v = [ x for x in vals if x is not NULL]
if len(v):
return sum(v) / len(v)
return 0

If there is a NULL value among the input parameters the result is NULL, too. If I use an expression in Select by Expression for example:



my_col is not NULL

it works. I use QGIS 2.18.7 on Ubuntu 16.04


* addition * Here is an image, how I call my function: enter image description here




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