Using Map Themes in QGIS

Map themes can seem daunting at first, but once you get used to them, combining them with expressions ( and variables ) can make your workflow a lot faster and make your work easier. So what are map themes? how can you use them easily? and why should you?



What Are Map Themes?

Let's say you want to make multiple maps, each showing the same layers, but with different symbology or with another layer added to each of them. Map themes are made specifically for that, it's basically saving your place in a way, the project remembers which layers are turned on, and that's it. The current symbology of the layers isn't saved, so watch that when you change it, the theme will remain the same theme.

For example, if we have 3 layers: the OpenStreetMap basemap, a landuse layer, and a buildings layer we can set that as a map theme. Adding another layer or switching the basemap off and turning a satellite basemap on can be a second theme, there's a good example exactly like that in Lutra Consultings' Input's documentation.

Simply turn on the layers you want included in the theme, click the Manage Map Themes ( the eye icon ) in the Layers Panel, and select Add Theme...


 

How Can We Use Themes To Change Styles?

Let's say I have a map of the world, and I want to create a print layout with multiple maps, one with just the countries, one with another layer for lakes, one with airports and a last one with railroads. Let's also say, I want to use a different color for the countries polygons in each of those maps.

First step is we create a theme for each of those maps.

Now that we have themes, I'll create an expression to change the color of the ne_countries layer. In the Layer Styling Panel we'll select the symbol, and edit the expression for the fill color.


The expression will use 2 functions if and is_layer_visible, combining these two, we can check if a layer is visible and return different values when the layer is visible or not. the values here can be colors in HEX or arrays that combine values for red, green and blue.

My expression looks like this:

    if(
       is_layer_visible('ne_airports'),
       '#be7396', /* background for airports */
    if(
       is_layer_visible('ne_railroads'),
       '#ffffff', /* background for railroads */
    if(
       is_layer_visible('ne_lakes'),
       '#766d2d', /* background for lakes and rivers */
        '#beb297' /* default background */
            )
        )
    )

All it does, is check what layer is visible ( or turned on ) and returns a different color. You could also create multiple symbols for your layer and change the fill style or the opacity of all of them with a similar expression. I also recommend defining variables for frequently used colors, or even just project colors, that way you can remember the names, and not the exact notation of your colors, names like @building_gray or @lake_blue are easier to remember.
You can learn more about variables in my recent post about the expression engine and variables.

Now that that each theme has a different background, we can set each map object in our print layout to use a map theme. Simply add or select a map object, and under Layers select Follow map theme and select the one you want to use.


 

And that's it, fairly simple to set up, uses a simple expression, but can make your work a lot easier, especially when setting up print layouts with multiple maps. Remember that themes are useful if you want to use Lutra Consulting's Input app for field data collection since using themes is the only way to turn layers on and off in it.

 

Comments