2

I defined a new function SetPopulation() using the Function editor. I have used it in the Field calculator, and it works as desired.

SetPopulation available as an expression.

I have a model (deliberately very simple for testing purposes) in which I wish to use this new SetPopulation() function in the Advanced Python field calculator to calculate the values of a new attribute created by the Advanced Python field calculator.

SetPopulation used in Advanced Python field calculator in model

Why does the the Advanced Python field calculator algorithm in the model not recognize SetPopulation() as a legitimate expression?

enter image description here

1 Answer 1

2

Because your SetPopulation() function is a QGIS function, not a Python function. The code entered in the "Formula" for the "Advanced python field calculator" must be valid Python code (not QGIS expression format).

What you want to use here is the standard "Field calculator" algorithm - that one uses a standard QGIS expression and your custom function should work fine.

2
  • So could I instead write the Python code that I used to define SetPopulation in the Formula field of the Advanced Python field calculator?...I don't feel like this approach is as elegant, but I'd be interested to know simply because I have not found many examples of using the Advanced Python field calculator...in any case, thanks, @ndawson, it does indeed work fine now!
    – james
    Commented May 16, 2018 at 21:30
  • Yes, that would also be possible. Just depends on your overall workflow which approach is more suitable.
    – ndawson
    Commented May 17, 2018 at 1:27

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.