1

I'm loading a layout template to a .qgs project and would like to set up the scale bar so that it has a round numeric scale (e.g. 1:5'000, or 1:25'000 but not 1:12'500). How do I do that?

Here is my piece of code:

import os
import processing
from qgis.core import *
from qgis.gui import *
import qgis.utils
from qgis.PyQt import QtGui
from qgis.PyQt.QtXml import QDomDocument

project = QgsProject.instance()
    project.setCrs( QgsCoordinateReferenceSystem(2056, QgsCoordinateReferenceSystem.EpsgCrsId) )
    project.setFileName(qgis_project_name)

project.write()

# load layout template
composition = QgsPrintLayout(project)
document = QDomDocument()

template_file = open('C:\\my_QGIS\\my_qgis_projects\\ramms_map\\RAMMS_Karte.qpt')
template_content = template_file.read()
template_file.close()
document.setContent(template_content)

composition.loadFromTemplate(document, QgsReadWriteContext()) 
project.layoutManager().addLayout(composition)

layout = project.layoutManager().layoutByName("RAMMS_Karte_layout_de")

# access the items contained in the layout
sel = layout.itemById("scale") # that's the id of the scalebar item
sel.??? --> here set the numeric number 1: xxxx

# my scalebar is connected to a map ("map1")


EDIT:

# here is the correct piece of code missing

layout = project.layoutManager().layoutByName("RAMMS_Karte_layout_de")

map1 = layout.itemById("map1")

scale_nr = map1.scale() # takes the scale of map1
if (scale_nr % 1000.0 == 0.0) == False: # if not divisible by 1000, it rounds the scale value to the next 1000
    scale_nr = int(round(scale_nr, -3))
map1.setScale(scale_nr) # sets the new scale


2 Answers 2

2

In this case, you have to change the map, no the scale, you can use this:

map = layout.itemById("principal map")
map.setScale(12500)

if you don't have a itemByID, the other option is this:

layout = project.layoutManager().layoutByName("RAMMS_Karte_layout_de")
page = layout.pageCollection()

for i in items:   
    if isinstance(i, QgsLayoutItemMap):
        i.setScale(12500)
2
  • Thanks that helped! I edited my question with the code I came up with!
    – SaskiaG
    Aug 28, 2019 at 9:25
  • You are welcome, I am glad it was useful for you. Sep 5, 2019 at 15:33
1

For corroborating what kind of result your code produces, I created a qpt file (for this template https://raw.githubusercontent.com/qgis/QGIS/master/tests/testdata/template.qpt) and I loaded it in QGIS 3.8 with following code:

project = QgsProject.instance()
composition = QgsPrintLayout(project)
document = QDomDocument()

template_file = open('/home/zeito/Templates/template.qpt')
template_content = template_file.read()
template_file.close()

document.setContent(template_content)

composition.loadFromTemplate(document, QgsReadWriteContext()) 
project.layoutManager().addLayout(composition)

layout = project.layoutManager().layoutByName("Composer 1")

sel = layout.itemById("scale")

print(type(sel))

where type printed for sel object was 'NoneType'. So, it doesn't exist any item named "scale" into QgsPrintLayout object (layout).

If you print layout.items() you have four types of objects:

>>>items = layout.items()
>>>items
[<qgis._core.QgsLayoutItemLabel object at 0x7f6a8c4ce0d8>, <qgis._core.QgsLayoutFrame object at 0x7f6a8c4ce288>, <PyQt5.QtWidgets.QGraphicsRectItem object at 0x7f6a8c4ce168>, <qgis._core.QgsLayoutItemPage object at 0x7f6afc0ae798>]

QgsLayoutItemLabel object (items[0]) have 'scale' and 'setScale' methods where I guess that you can set numeric scale 1:12,500.

In my case, default scale is 1.0:

>>>items[0]
<qgis._core.QgsLayoutItemLabel object at 0x7f6a8c4ce0d8>
>>>items[0].scale()
1.0

I hope that it helps.

1
  • Hi! the sel = layout.itemById("scale") line can work if you give an id name to your layout items! and my scale item had the name "scale", that's why it did not work for you :-)
    – SaskiaG
    Aug 28, 2019 at 9:23

Your Answer

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

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