In the function editor, I am trying to make this function :
@qgsfunction(args='auto', group='Custom')
def size_reduction( x, feature, parent):
s = @map_scale/1000/x
y = if( s<1, s, 1+ln(s) )
return y
But I get the error shown below. Can anyone help me ?
An error occurred during execution of following code:
from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Custom')
def size_reduction( x, feature, parent):
s = @map_scale/1000/x
y = if( s return y
File "", line 6
s = @map_scale/1000/x
^
SyntaxError: invalid syntax
Python version: 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0]
QGIS version: 3.16.4-Hannover 'Hannover', exported
JGH
44.4k3 gold badges49 silver badges95 bronze badges
2 Answers 2
You can fetch the map scale within a function using
from qgis.utils import iface
[...]
iface.mapCanvas().scale()
answered Mar 22, 2021 at 19:23
Here is my working function
import math
from qgis.utils import iface
from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Custom')
def size_reduction( x, feature, parent):
s = iface.mapCanvas().scale()/1000/x
if s<1 :
y = s
else :
y = 1+math.log(s)
return y
lang-py