
You can’t override the built-in methods.In the above example we could import userfunctions and never unregister because we plan on using it for the whole session.
DEFINITION OF QUANTUM GIS CODE
This mainly applies to plugins where the user might unload your plugin and the code is no longer available.

You can also see the $ sign in front of the function, this is because any functions that take no args are considered special and use the $ sign as a convention, this is all automatic when the function is registered.Īnd the result is: The label using the new function Nice! Notice also that the function doc string is used as the function help. Now open the label properties for the layer: The new function shown in the expression builder Lets open QGIS and run import userfunctions.py: Importing functions from userfunctions.py qgis/python folder, lets call it userfunctions.py (note you don’t have to save it here, anywhere that QGIS can find it will do. Now that we have that all done we can save it into a file in our. Get the geometry from the feature, check if it’s a polygon, if it is then count the number of vertices and return that number. Returns the number of vertices for a features geometry This procedure can also be launched from the QGIS environment, as described. values is a list of QVariants passed into the function, feature is the current QgsFeature, and parent is expression engine node (you use this to raise errors). Then the spatial extent of the mapset can be defined from the specifications. Any custom function must take (values, feature, parent) as python args.

So what does an empty function look like:ĭef vertices(values, feature, "Python") means we are defining a new vertices function that takes 0 args and lives in the “python” group in the expression builder UI. The qgsfunction decorator will take a normal Python function, wrap it up in the class used to define a function, and register it in the engine. Lets take a use case from Ujaval Gandhi and his example of counting vertices for each feature.įirst we need to import the new qgsfunction decorator function from qgis.utils.

Today I pushed a commit into master that adds the ability to define a function in Python (or C++), register it in the expression engine, then use it anywhere expressions are used. Thanks to the all the awesome work on the expression engine by Martin all this cool stuff is now possible. Ever since I added expression based labels, including the new expression builder UI, something that I always wanted to add is the ability to define custom user defined functions in Python (or C++) and use them in an expression. The expression engine is used for labels, rule based rendering, layer actions, field calculator, and atlas composer tags.
