4

I've a QGIS project with several vector layers that I need to be represented in a layout I've prepared one by one.

I can manually make visible and select one layer at a time, so that the layout after a refresh shows the desired layer map and legend (where I've selected only show items in the selected map in the interface). I'm able to select layers by name, and also 'activate' them, but I'm not finding a way to set visibility and active in a PyQGIS script for the layers one by one to automate the export of the layers maps with the layout.

4
  • Have you had a look at map themes?
    – Erik
    Commented Oct 22, 2021 at 14:18
  • Not really in depth, but my goal is to be able to produce in the shortest time 20+ exported layouts
    – stefan0n
    Commented Oct 22, 2021 at 15:13
  • And you deem map themes not suited because .... ?
    – Erik
    Commented Oct 22, 2021 at 17:50
  • There is a suggestion here: gis.stackexchange.com/questions/203393/… that might point to a solution for you. It reminded me a bit of some things I've done with Data Driven Pages, although an equivalent to the ArcGIS page definition would be nice in QGIS. Erik's suggestion is interesting; I wonder if map themes could be used in place of the layer listing in the suggestion.
    – John
    Commented Oct 22, 2021 at 19:30

2 Answers 2

2

There is a solution to roll on layer you use in your qgis project.

It is based on a mix between this answer and this answer.

The goal is to bend the atlas function to roll to the layer you have in your project.

  1. set the name of the layers to be all different.
  2. create a layer that is named atlas as non geographic layer with no attribute. Then create as many features in this layer as there is layer in your project.
  3. open a layout, then add your map frame. Then activate atlas and use the layer created before as atlas coverage. Then in the map propreties > Layers set the layers to be locked and to be data driven.

enter image description here

  1. Then an expression helper pops out. Set it to give the name of each layer in your project based on the atlas number. This formula may work well :
layer_property(array_get(@layers, @atlas_featurenumber -1) , 'name')
  1. Then you may set the name of the atlas page or anything else and export the atlas.
2
  • 1
    This is a good solution, the only problem is that the formula for the name of each layer isn't working for me, as it seems that @atlas_featurenumber is always zero, and layers do not show, or are messed up. I've solved adding a column in the 'atlas' layer filled with the names of layer I need to show for each page of the atlas, separated by the | character.
    – stefan0n
    Commented Oct 22, 2021 at 23:43
  • It seems strange that it doesn't work. Writting the layers name is fine but there is two limiit : it takes time if you have many layers and you can have miswritting issue. As a mixed option you can create a field "id" as number that you use to replace @atlas_featurenumber Commented Oct 23, 2021 at 16:57
1

Step 4 in Corentin Lemaitre works if you use this expression:

layer_property(array_get(@layers,  array_find(array_foreach ( 
    @layers,
    layer_property(@element,'name')
  ),   @atlas_pagename  )) ,'name' )

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