I have a QGIS project with multiple layouts (in total about 30) in my layout manager. I used of the "Follow map theme" then "Lock layers" options in the Map item properties to make sure that each layout is displaying the correct layer. However after I am done making all the layouts I would like to make sure that they are displaying the correct layers.
Is there a way in QGIS to verify which layer is being displayed in the layout? Making all the layouts manually is prone to error. This option is absolutely necessary to make big map making projects possible.
I am also open to using the Python API to do this.
I have made some progress:
project = QgsProject.instance()
manager = project.layoutManager()
layouts = manager.layouts
layout = layouts[0]
items = layout.items()
#find map object
for item in items:
if isinstance(item, qgis._core.QgsLayoutItemMap):
map_object = item
#Here need to find map object somehow
layers = map_object.layers
The above will extract the layers as QgsVectorLayer
or QgsRasterLayer
objects. Unfortunately this is not enough, I would also like to extract the position of the QgsVectorLayer
in the layer tree. I found out how to go down a layer tree from this post. However when I get down to the object of interest it returns it as a QgsLayerTreeLayer
.
It is possible to extract the underlying layer from the tree layer by doing this:
layer_from_layer_tree = tree_layer.layer()
And it is apparently possible to also do this:
layer_from_layer_tree==layer_from_map_layout
>>> True
But I am not sure if this is the recommended way to compare two layer objects, and if objects which are not equal will return False
.
In any case, I think it is very convoluted. Verifying which layers are in your layout should be a basic functionality available directly in the GUI.