3

I try to make a print-layout consisting of 2 maps-items. I only use one layer, but I want to picture it in different styles.

First, as it is when loaded, and on the second map in another style (which I load from a file). I used a template for the layout production (https://opensourceoptions.com/blog/pyqgis-create-and-print-a-map-layout-with-python/) an adapted it. Then I red the advice from bbsea (How to lock LayoutItem layers in QGIS 3.0 using python?) and used map.storeCurrentLayerStyles(), still it is not working.

To put it in pictures, this is what I want:

enter image description here

but this is what I get:

enter image description here

Here is the code I use:

from qgis.PyQt import QtGui

layer_list = iface.mapCanvas().layers()
project = QgsProject.instance()
manager = project.layoutManager()
layoutName = 'Test1'
layouts_list = manager.printLayouts()
layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName(layoutName)
manager.addLayout(layout)
layer = layer_list[0]

# first map:
map = QgsLayoutItemMap(layout)
map.setRect(60, 20, 60, 20)
ms = QgsMapSettings()
ms.setLayers([layer]) 
rect = QgsRectangle(ms.fullExtent())
rect.scale(1.0)
ms.setExtent(rect)
map.setExtent(rect)
map.setBackgroundColor(QColor(255, 255, 255, 0))
layout.addLayoutItem(map)
map.attemptMove(QgsLayoutPoint(5, 5, QgsUnitTypes.LayoutMillimeters))
map.attemptResize(QgsLayoutSize(90, 90, QgsUnitTypes.LayoutMillimeters))
map.storeCurrentLayerStyles()

# second map:
map2 = QgsLayoutItemMap(layout)
map2.setRect(60, 20, 60, 20)
ms2 = QgsMapSettings()
layer.loadNamedStyle('/home/..../style.qml')
layer.triggerRepaint()
ms2.setLayers([layer])
rect2 = QgsRectangle(ms2.fullExtent())
rect2.scale(1.0)
ms2.setExtent(rect2)
map2.setExtent(rect2)
map2.setBackgroundColor(QColor(255, 255, 255, 0))
layout.addLayoutItem(map2)
map2.attemptMove(QgsLayoutPoint(95, 95, QgsUnitTypes.LayoutMillimeters))
map2.attemptResize(QgsLayoutSize(90, 90, QgsUnitTypes.LayoutMillimeters))
map2.storeCurrentLayerStyles()

layout = manager.layoutByName(layoutName)
exporter = QgsLayoutExporter(layout)
fn = '/home/c/mapTest.pdf'
exporter.exportToPdf(fn, QgsLayoutExporter.PdfExportSettings())

How can I lock the layer-style in?

3

1 Answer 1

1

I'm afraid that I can't really provide an intelligent or scientific explanation of the problem here, because I tried modifying your code and I was not able to get a satisfactory result.

But it works for me with the following code snippet which I tested on a similar data set to what you have shown.

project = QgsProject.instance()
layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName('Test1')
project.layoutManager().addLayout(layout)

layer_list = iface.mapCanvas().layers()
layer = layer_list[0]

map1 = QgsLayoutItemMap(layout)
map1.attemptMove(QgsLayoutPoint(5,50, QgsUnitTypes.LayoutMillimeters))
map1.attemptResize(QgsLayoutSize(125,85, QgsUnitTypes.LayoutMillimeters))
map1.setExtent(layer.extent())
map1.setLayers([layer])
layout.addLayoutItem(map1)


map1.storeCurrentLayerStyles()
map1.setKeepLayerSet(True)
map1.setKeepLayerStyles(True)


map2 = QgsLayoutItemMap(layout)
map2.attemptMove(QgsLayoutPoint(150,50, QgsUnitTypes.LayoutMillimeters))
map2.attemptResize(QgsLayoutSize(125,85, QgsUnitTypes.LayoutMillimeters))

#change path to your own saved style file
layer.loadNamedStyle('C:\\Users\\Ben\\Desktop\\Layer_styles\\test_style.qml')
layer.triggerRepaint()
map2.setExtent(layer.extent())
map2.setLayers([layer])
layout.addLayoutItem(map2)

The result is:

enter image description here

The required api call seems to be:

map1.setKeepLayerStyles(True)

Because if I just comment out that single line, the stored style for map 1 is not locked and changes to follow the new style applied as in the problem you describe.

1
  • It works : ) Thank you VERY much!
    – Carina
    Commented Sep 10, 2021 at 4:58

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.