2

I am programming a plugin for Qgis 3 from which I want to export a composer layout containing different maps that are supposed to have different extends and show different layers.

I am having trouble "fixating" a map before I make the changes required for the next one. When I use the composer interface, there is the option to use "Lock layers", which seems to do what I want. By looking into template files saved from the composer and the API documentation, I have figured out that QgsLayoutItemMap.setKeepLayerSet() is probably the corresponding method in python. The only problem is that it simply does not seem to work. The maps are not "fixated" and everytime I change something in the map canvas, it is still reflected in all maps contained in the layout.

Does anybody have any ideas what the problem might be?

    #Accessing the main map by recasting it as a QgsLayoutItemMap
    main_map = sip.cast(composition.itemById("Main"),QgsLayoutItemMap)
    main_map.zoomToExtent(raster_layer.extent())
    main_map.setKeepLayerSet(True)

    QgsLayoutExporter(composition).exportToImage( output_folder + "/test1.png", QgsLayoutExporter.ImageExportSettings())
    #Here the exported map does not show the background layers

    #Activating the background map
    root = QgsProject.instance().layerTreeRoot()
    for c in root.children():
        if c.name() == background_map.name():
            c.setItemVisibilityChecked(True)

    QgsLayoutExporter(composition).exportToImage( output_folder + "/test2.png", QgsLayoutExporter.ImageExportSettings())
    #And here it does...

1 Answer 1

2

From the QGIS API docs for QgsLayoutItemMap::setKeepLayerSet:

"This is just a GUI flag, and itself does not change which layers are rendered in the map. Instead, use setLayers() to control which layers are rendered."

So you need to call setLayers with a list of your desired layers instead.

0

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.