QGIS Atlas – How to Loop on Layers in the QGIS Map Composer

atlasqgis

I'm working with 3.22.10-Białowieża and I need to create several maps of the same location, showing a different layer in each (i.e. heatmaps for different years). This is the workflow that I have set, but still needs a final fix:

  1. Create a layer (years) with no geometries and filled it with the names of the layers I want to use (an easy automated task, since the names are correlative: heatmap_1999, heatmap_2000…);
  2. Create a new print layout with the map extent I need;
  3. Create an atlas for that layout, using the years layer as coverage;
  4. Set the visibility of the layers in the Element properties of the map with the expression @atlas_pagename.
  5. Create the atlas preview, and it correctly shows each layer in each page.

So far, so good. However, I need to add a baselayer to the view and I've tried the following expression, but it doesn't work as expected:

'Base_map'||'|'||@atlas_pagename

Instead of this (obtained when the atlas preview is switched off):

enter image description here

I get this:

enter image description here

How can it be done?

Edit:

Here is a sample project (hope I've used the Project Packager complement properly). For the base layer I'm using a WMS and double-clicking on its name when constructing the expression, so misspelling shouldn't be the problem -> Well, it turns out yes: see the answer by @Babel below.

Best Answer

Use this syntax: 'Base_map|'||@atlas_pagename. However, in this case, the basemap is rendered above the atlas page and will cover it.

To see the basemap in the background and the atlas features above it, use the expression in switched order: @atlas_pagename || '|Base_map'

enter image description here

As became clear when you shared your project, the problem in your case was that your Base_map layer was not properlay named. You had:

@atlas_pagename || '|Callejero_76a67408_c17f_4fd4_9fe1_bb0ec136c33f'

Use the following expression, then it works:

@atlas_pagename || '|Callejero'

Be aware: To get the correct layer name, do not double click the layer's entry from the middle column (under Map Layers) in expression string builder, as this inserts the layer ID (name + sting), but rather use layer_property( [layer_id], 'name'), where insted of [layer_id], fill in the layer ID.

enter image description here

Related Question