Sunday 23 August 2015

How do I declare a QGIS custom expression function and use it in a single python script?


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

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...