I'm using pyqgis within an standalone application.
Now I wanted to create a custom expression to to use as filterExpression in rule of
ruleBasedRendererV2. (I tried the renderer with existing functions before and everything worked fine!)
qgisfunction (bitwise AND-function - as there is not such a function yet) looks like this:
from qgis.utils import qgsfunction @qgsfunction(args="auto", group='Python') def bitwise_and(value1, value2, feature, parent): return value1 & value2
I also tried to register it, via:
from qgis.core import QgsExpression QgsExpression.registerFunction(bitwise_and)
Testing the expression via:
expressionString = 'bitwise_and(2, 2) = 2' exp = QgsExpression(expressionString) if exp.hasParserError(): print(exp.parserErrorString())
2=2 so always
True) works after registering function, and fails if not. But still, I can not use the expression within a rule...
Right now, the file containing the function lies on top in my project folder, which is automatically included to PATH, isn't it?! (According to Nathan Woodrow, file can be placed anywhere in PATH - see https://nathanw.net/2012/11/10/user-defined-expression-functions-for-qgis/).
UPDATE: Registering of function works so far, with code from above - now the problem lies in function itself...
do_nothing getting two arguments works fine in my code:
@qgsfunction(args="auto", group='Python') def do_nothing(value1, value2, feature, parent): return value1
expString = 'do_nothing(2, 2) = 2'
Simple change of
add_one does not work any longer:
@qgsfunction(args="auto", group='Python') def add_one(value1, value2, feature, parent): return value1 + 1
calling it via
expString = 'add_one(2, 2) = 3'.
That's why I think it has something to do with returning type?!