# -*- coding: utf-8 -*- """ *************************************************************************** qgsfunction.py --------------------- Date : May 2018 Copyright : (C) 2018 by Denis Rouzaud Email : denis@opengis.ch *************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * *************************************************************************** """ import inspect import string from builtins import str from qgis.PyQt.QtCore import QCoreApplication from qgis._core import QgsExpressionFunction, QgsExpression, QgsMessageLog, QgsFeatureRequest def register_function(function, arg_count, group, usesgeometry=False, referenced_columns=[QgsFeatureRequest.ALL_ATTRIBUTES], **kwargs): """ Register a Python function to be used as a expression function. Functions should take (values, feature, parent) as args: Example: def myfunc(values, feature, parent): pass They can also shortcut naming feature and parent args by using *args if they are not needed in the function. Example: def myfunc(values, *args): pass Functions should return a value compatible with QVariant Eval errors can be raised using parent.setEvalErrorString("Error message") :param function: :param arg_count: :param group: :param usesgeometry: :return: """ class QgsPyExpressionFunction(QgsExpressionFunction): def __init__(self, func, name, args, group, helptext='', usesGeometry=True, referencedColumns=QgsFeatureRequest.ALL_ATTRIBUTES, expandargs=False): QgsExpressionFunction.__init__(self, name, args, group, helptext) self.function = func self.expandargs = expandargs self.uses_geometry = usesGeometry self.referenced_columns = referencedColumns def func(self, values, context, parent, node): feature = None if context: feature = context.feature() try: if self.expandargs: values.append(feature) values.append(parent) if inspect.getargspec(self.function).args[-1] == 'context': values.append(context) return self.function(*values) else: if inspect.getargspec(self.function).args[-1] == 'context': self.function(values, feature, parent, context) return self.function(values, feature, parent) except Exception as ex: parent.setEvalErrorString(str(ex)) return None def usesGeometry(self, node): return self.uses_geometry def referencedColumns(self, node): return self.referenced_columns helptemplate = string.Template("""