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

JGH
44.4k3 gold badges49 silver badges95 bronze badges
asked Mar 22, 2021 at 18:39

2 Answers 2

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
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 
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Mar 23, 2021 at 20:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.