[Qgis-user] QGIS custom python function with zero or more args

Stefan Giese (WhereGroup) stefan.giese at wheregroup.com
Fri Mar 9 02:25:10 PST 2018


Hi Benjamin,
you can do it like this example which only makes a sum of 2 values. Two 
aruments or only one...:
----------------------
@qgsfunction(-1, 'Python')
def add_values( values, feature, parent):
	if len(values) == 2:
		return values[0]+values[1]
	elif len(values) == 1:
		return values[0]+5
------------------------
@qgsfunction(-1, 'Python') says that you will pass a list of arguments 
and the function will be in the Python rider.
In the code, you can check the number of arguments with getting the 
length of the list, and then use your default values...

hope this helps
stefan


---
Mit freundlichen Grüßen
Stefan Giese
Projektleiter/Consultant
***********************************
Treffen Sie uns auf der
FOSSGIS Konferenz 2018
21.-24. März 2018 in Bonn
https://fossgis-konferenz.de/2018/
***********************************
WhereGroup GmbH & Co. KG
Schwimmbadstr. 2
79100 Freiburg
Germany

Fon: +49 (0)761 / 519 102 - 61
Fax: +49 (0)761 / 519 102 - 11

stefan.giese at wheregroup.com
www.wheregroup.com
Amtsgericht Bonn, HRA 6788
-------------------------------
Komplementärin:
WhereGroup Verwaltungs GmbH
vertreten durch:
Olaf Knopp, Peter Stamm
-------------------------------

Am 2018-03-09 09:18, schrieb Fuenfer-Koenigstein.Benjamin2 at swm.de:
> Hi all,
> 
> I would like to write a custom python function for QGIS 2.18 that
> takes zero or one argument. The reason is that in most cases I can use
> a default value in the function, in some cases I would like to enter
> the value as argument. For python I know that you can prefix args in
> functions to make them optional, e.g. def func(*arg). For QGIS
> functions this will not work because there always have to be the args
> feature and parent in last two positions. Has anyone experience with
> QGIS functions that take zero or more arguments? Thanks for any help!
> 
> Regards
> Benjamin
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user



More information about the Qgis-user mailing list