1

I am trying to archive the following: I have a print setup with 3 map canvases. I want to set the scale text for a specific map canvas (lets say its ID is "main_view").

In QGIS 2.14. I can use the normal scale bar and set its style to "numeric" and link it to my desired map canvas. However I have dynamic text in which the scale text will be integrated like this:

<b>Further Infos:</b> <br>[%"open_info"%]
Created: [% day( now())%].[% month(  now() )%].[% year(  now() )%]
Scale: [%$scale%]

So I can not use the transformed version from the scale bar. The command [%$scale%] yields just a 0 because I dont know how to link to my map specific canvas.

3
  • I think that this question/answer should solve this for you gis.stackexchange.com/questions/226436/…
    – Ian Turton
    Apr 11, 2017 at 13:29
  • thanks @iant for pointing me to this. I worked for my purpose!
    – LaughU
    Apr 11, 2017 at 14:33
  • Please post an answer so that others who find this can work out how to do it
    – Ian Turton
    Apr 11, 2017 at 14:59

2 Answers 2

10

In QGIS 3.0.3 I have insert the scale in a label text with the expression:

map_get(item_variables('**XXX**'), 'map_scale')

or rounded:

'1:'|| round(map_get(item_variables('**XXX**'), 'map_scale'),o)

Where **XXX** correspond to the Item ID of the map that You have to define before

1
  • Excellent...In my case I wanted the scale number to be represented in Spanish format, with zero decimal places. I assigned the "Item ID" of the Map in the Layout as "1".... The expression I used was: [%'Escala 1 : '|| format_number(map_get(item_variables('1'), 'map_scale'),0,'es')%] Thank you.....
    – Abs
    Jan 21, 2022 at 12:45
1

@iant pointed to a working answer I will just sum up the results, which you can find here, and give some additional explanations.

First you need to create a custom python expression function:

The code which worked for me:

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

@qgsfunction(args='auto', group='Custom')
def getScale(composerName, mapName, feature, parent):
    # dictionary to store QgsComposerView.composition() items by name
    compDict = {}
    for comp in iface.activeComposers():
        # workaround to get name: read it from window title
        compDict[comp.composerWindow().windowTitle()] = comp.composition()
    mapScale = 0 # default to be returned if scale not found
    if composerName in compDict:
        mapItem = compDict[composerName].getComposerItemById(mapName)
        if mapItem:
            mapScale = mapItem.scale()
    return mapScale

you insert this into the python expression see the qgis-tutorials for a detailed look. after this you can insert the following code into your label:

'scale' || getScale('Composer 1', 'Map 0')

The "Composer 1" argument is derived from the name under which you saved your print composer. The argument "Map 0" is the name of the map you want to see the scale text. If you want an integer instead of a float. you can return int(mapScale)

Your Answer

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

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