4

After my previous question Weighted values of field in QGIS, I successfully adapted the following formula in Expression dialogue

"Value" / (sum("Value") / (count(CASE WHEN "Value" >= 1 THEN +1 END)))

In QGIS in the Attribute Table, there are many fields with values for which I count using this formula.

However, I don't want to repeat all long action and therefore I need to create a function in the Function editor. How can I implement this?

0

1 Answer 1

3

Use the following function in the 'Function Editor'.

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')

def calculate_values(value, feature, parent):
    layer = qgis.utils.iface.activeLayer()
    sum_values = sum(filter(None,[f['Value'] for f in layer.getFeatures()]))
    count_value = sum(1 for x in filter(None,[f['Value'] for f in layer.getFeatures()]))
    if value:
        return float(value)/sum_values/count_value
    else:
        return NULL

The result of the function will give a desired output.

result


References:

0

Your Answer

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

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