Skip to content

Instantly share code, notes, and snippets.

@rbanick
Last active March 5, 2024 09:06
Show Gist options
  • Save rbanick/4ed6e53078d7340f30853df277c54b03 to your computer and use it in GitHub Desktop.
Save rbanick/4ed6e53078d7340f30853df277c54b03 to your computer and use it in GitHub Desktop.
QGIS Advanced Atlas Techniques

Background

QGIS's Atlas feature and ArcGIS's Data Driven Pages allow you to create a series of maps, one per geography, based off a single map style. Titles, labels, and styles within the maps can be created based on the attributes of key layers and/or the scale of the map, imparting some customization and flexibility to an otherwise automated process. Atlas geographies can be points, polygons, and scales can be adjusted.

But what if you want to create multiple maps for a single geography, or multiple maps for multiple geographies? How can you vary not just the extent but the contents of what's shown?

This walkthrough will detail two complementary methods for filtering the display of datasets across multiple geographies using QGIS's Atlas Tool. This is a deep dive into a few specific Atlas techniques that I thought were poorly documented elsewhere and useful, not an exhaustive guide to the Atlas Tool. I have no idea if any or all of these techniques can be replicated using ArcGIS's Data Driven Pages.

1 - File setup

We'll assume you have three basic types of data in this tutorial

  • Stable background layers, which are turned on for every map
  • Dynamic background layers, which we want to switch on and off or style differently for different atlas features
  • The atlas coverage layer, which defines the geographies and features we want to create maps of, one per feature

Load all of these layers into QGIS for an initial look at the data.

Duplicate atlas geographies for desired number of maps

The principal method for creating multiple maps per geography relies on exploiting the Atlas Tool's desire to create one map per feature. The basic idea is to create multiple copies of each coverage layer feature, one copy per map you want to create. I have scaled this process to hundreds of maps per feature, for thousands of maps total, without significant problems.

The easiest way to do this is to enable editing of your feature dataset, select the features of interest, copy them, and paste them the number of times you want to iterate over them. For large groups of features this may be too time consuming and programmatic methods via Python, R, etc. should be used. This is relatively trivial to do in Python using Geopandas.

In our example we will be turning on and off one geotiff per feature. The sample Python code below shows how to duplicate features according to the length of a list of geotiffs. We use the country outline of Bangladesh as a minimal example.

# load in a country variable
country = 'Bangladesh'
ctry = gpd.read_file("Country_Masks.gpkg",layer=country)

# make a variable corresponding to the total number of tifs
tif_len = len(tiflist)

# copy tiflist for each feature in ctry (1 if ctry is the country outline)
tiflist = tiflist * len(ctry)

# replicate each feature in ctry for each tif
ctry = ctry.loc[ctry.index.repeat(tif_len)]

Load atlas-dependent labels into atlas feature column

QGIS's Layout Manager automatically enables querying of Atlas attribute table columns in descriptive text. The format is [%column_name%]. This is normally used to display the name of the administrative area or feature site, e.g. "Province 1". By preparing a column with descriptive labels for each copy of the coverage layer features being iterated over, we can label the outputs for our readers.

Our example shows geotiffs by date, so I prepared a datelabel column in the Bangladesh layer attribute table displaying the date corresponding to a feature. This was then called in the atlas title in the layout as [%datelabel%].

Load atlas-dependent layers into atlas feature column

This step is the most crucial and least well-documented elsewhere. QGIS provides a "data-driven labeling" option within the Layout Manager which can turn on and off specified layers. This option overrides whatever setup is specified in the main map screen. The dialogue will accept free text layer names or columns from the Atlas coverage layer's attribute table. By specifying different layer names within the relevant attribute column of copied features, these layers can be toggled on/off while creating maps, automatically generating multiple types of maps per feature.

The following attribute table setup

data_driven_layer_attributes

Can be specified in the data driven layers dialogue

Attribute defined layer visibility

Resulting in the following maps...

two maps of same area

This could be used for turning on and off sets of vectors layers instead of geotiffs if desired. Be sure that the layer names exactly match the names specified in the atlas coverage layer's attribute table as QGIS will enable these layers only. Miss an underscore or capital and it will ignore the layer and your atlas maps will look wrong. Since generating hundreds or thousands of maps can take some time this is a time-consuming mistake!

2 - Map styling

Illustrator is my preferred mapping software; given a choice, my workflow only uses QGIS to export unstyled layers at the correct spatial extent, with a scale bar, and everything else happens in AI. This presents me with a problem when making Atlases, which by their nature are rasters and involve too many maps to style 1-by-1 in AI. Since Atlases also involve calling attention to specific features in a sequence, there are specific design considerations I usually have in mind as well.

Below are a few technical tricks and stylistic tips for how to make maps look nice using Atlases. You'll never get a map in QGIS (or ArcGIS) looking quite as good as you could get it in AI but you can make something pretty nice-to-look-at with a little effort. Note that these are well-documented in a few other places so I'm not going to linger on them exhaustively.

Atlas variables

Here we explore a second, complementary method for adjusting the display of data according to the atlas feature and extent.

QGIS automatically generates several system variables related to atlases. These provide useful handles for manipulating data display. The ones I use most frequently are:

  • @atlas_featureid
  • @atlas_geometry

@atlas_featureid corresponds to the atlas feature's index number, which the system generates in the background. This is useful for referencing the atlas feature but can be tricky as the ID doesn't correspond to any displayed attribute and the system doesn't expose the ID easily; it's somewhat hidden away in the layout manager.

@atlas_geometry corresponds to the actual spatial geometry of the atlas and can be used for spatial analysis. By querying against @atlas_geometry labels, polygons, and other features you only want to show inside or outside of the Atlas feature extent can be turned on and off using Rule-based Symbology. The following string will only show (or label) features whose centroid intersects the Atlas extent. Using a centroid makes sure neighboring polygons are not included. Note that if your atlas feature is a point the spatial querying abilities will be relatively limited.

Intersects(centroid($geometry), @atlas_geometry )

Style layers and label according to a spatial filter on atlas geometries

For my atlases I often employ the above filter in a rule-based style that imposes a semi-transparent white inverted polygon style on matching fields. This gives a nice "pop" to the atlas feature by partially masking surrounding polygons. I often use this in combination with filters on labels, borders styles, and point features; play around with what works best for your maps.

invert_innershadow_atlasgeometry

A more dramatic variation on this is to use inverted drop shadows or starburst fills. To get an inverted drop shadow, invert the "inner shadow" -- this works better with Atlases than an actual drop shadow. I personally prefer the subtlety of drop shadows, which establish a basic depth of field in a map, to starbursts, but you can find your own style.

Province 1 - Deumai - 524 1 03 3 001

This map of Deumai in Nepal uses an inverted inner shadow to create a drop shadow effect on the municipality, and an inverted white shaded polygon to subtley white out the surrounding polygons

Conclusion

This is far from a complete guide to Atlas creation. A quick Google will turn up lots of resources for further learning. Just be careful about the QGIS version number as the Atlas Tool has evolved over time. Some of the more esoteric and interesting dives into Atlas feature creation are found on Stack Exchange comments, make sure to root around on there. Part of my inspiration for typing this up was to preserve some crucial knowledge that was a few comments deep on SE.

If you want to know more about making your maps in QGIS look nice, the ESAPV GIS team has a "QGIS Map Design 2" guide under the P/Teams/Reference_docs folder. Dig into it, there's lots of good learning there.

@rbanick
Copy link
Author

rbanick commented Jun 1, 2020

data_driven_layer_attributes

@rbanick
Copy link
Author

rbanick commented Jun 1, 2020

data_driven_layer_display

@rbanick
Copy link
Author

rbanick commented Jun 1, 2020

invert_innershadow_atlasgeometry

@rbanick
Copy link
Author

rbanick commented Jun 2, 2020

Province 1 - Deumai - 524 1 03 3 001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment