I would like to create and populate a virtual field in a QGIS vector layer attribute table using a python script. I think the easiest way to do this is to create a custom expression function, and, then use this function to populate the virtual field. How do I instantiate a custom expression function from within a python script? What I have tried with Python 3 from a Python IDE is:
from qgis.core import *
@qgsfunction(args=0, group='Custom', usesGeometry=False)
def a_function():
return "'Test'"
QgsExpression.registerFunction(a_function)
print(QgsExpression(a_function()).evaluate())
This returns a TypeError:
print(QgsExpression(a_function()).evaluate())
TypeError: 'QgsPyExpressionFunction' object is not callable
No comments:
Post a Comment