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:
but this is what I get:
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?