3

I am trying to make a custom function in the QGIS expression editor that takes three arguments. Below is the tested standalone function, which I would like to integrate:

import math

def qfull_circle(DN, kb, I):
    #Input: DN in mm, kb in mm, I in ppm
    #Output: Q in l/s
    viscosity = 1.31 * math.pow(10,-6)
    roughness = kb / 1000
    Acircle = (DN * DN * math.pi / 4) * math.pow(10,-6)
    Xroot = math.pow((2 * 9.81 * DN * math.pow(10,-3) * I * math.pow(10,-3)),0.5)
    fraction1 = (2.51 * viscosity) / (DN * math.pow(10,-3) * Xroot)
    fraction2 = roughness / (3.71 * DN * math.pow(10,-3))
    return Acircle * (-2 * math.log(fraction1 + fraction2, 10) * Xroot) * math.pow(10,3)

Unfortunately, I cannot make it work. Any assistance on how to define it within the function editor is highly welcome.

3
  • I notice that two of your variables, Acircle and Xroot, turned light blue in the code block, which might indicate that those variable names are already defined (and thus not available for you to use). See if it works with something like myAcircle and myXroot.
    – csk
    Aug 26, 2019 at 16:30
  • 1
    Could you edit your question so that it only include the question and then insert the solution in the answer section below? :)
    – Joseph
    Aug 27, 2019 at 9:26
  • Changing variable names does not lead to a different result. The error message turns out as Parser Errors: qfull_circle function is called with wrong number of arguments. Expected 1 but got 3.
    – Sebastian
    Aug 27, 2019 at 18:43

1 Answer 1

1

When considering Register custom qgsfunction in pyqgis I altered the header to

from qgis.utils import qgsfunction
from qgis.core import QgsExpression
import math

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

def qfull_circle(DN, kb, I, feature, parent):

Now it's working.

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.