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


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.

| improve this answer | |
  • 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 May 16 '18 at 21:30
  • Yes, that would also be possible. Just depends on your overall workflow which approach is more suitable. – ndawson May 17 '18 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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