1

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

2 Answers 2

2

You can fetch the map scale within a function using

from qgis.utils import iface
[...]
iface.mapCanvas().scale()
0
0

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  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.