Creating a map series using Atlas
In this recipe, we will use the Print Composer Atlas functionality to automatically create a PDF map book with a series of maps.
To follow this recipe, load zipcodes_wake.shp and geology.shp from our sample data. In the following screenshots, the zipcodes_wake layer was styled with a simple white border, while the geology layer is styled with random colors.
The Print Composer Atlas feature will create one map for each feature in the so-called Coverage layer. In this recipe, the zipcodes layer will serve as a Coverage layer, and we will create one map for each zipcode feature:
- Click on the New Print Composer button or press Ctrl + P to get started. You will be prompted to set a title for the new composer. This can be left empty if you want QGIS to generate a title automatically.
- Click on the Add new map button and drag open a rectangle on the composer page to create a map item for the main map.
- To activate the Atlas functionality, we enable the map item's Controlled by atlas checkbox. The following screenshot shows the fully configured map's item properties. In the Controlled by atlas section, we can select which zoom mode Atlas should use:
- Margin around feature: This is the most flexible option, which tells Atlas to zoom to the feature while keeping the specified margin percentage around the feature.
- Predefined scale (best fit): This tells Atlas to use the one predefined project scale (configurable in Project Properties | General | Project scales) where the feature best fits in.
- Fixed scale: This keeps the same scale for all maps of the series; the scale is configured in the map's Main properties, that is, 100,000 in the following screenshot:
- Next, we add a label for the title using the Add new label button. This title label will display the zip code polygon feature's
NAME value which will be automatically updated by Atlas for each map in the series. To achieve this, we insert the following expression in the input field of the label item's Main properties: - To finalize the Atlas configuration, we need to go to the Atlas generation tab. There, we first have to enable the Generate an atlas checkbox. This activates the Configuration section, where we can pick the Coverage layer and set it to the
zipcodes_wake layer, as shown in the following screenshot. - To preview the Atlas output, we can now click on the Preview Atlas button. This button is only active if the Generate an atlas checkbox in the Atlas generation tab is enabled. Once the preview mode is active, you can step through the map series using the arrow buttons right besides the Preview Atlas button.
- When we are happy with the preview, we can export the map series. The output behavior is controlled by the configuration in the Atlas generation tab's Output section, which you can also see in the following screenshot. Atlas supports exporting to separate image, SVG, or PDF files. Activate the Single file export when possible option to combine all maps into one PDF and click on the Export Atlas as PDF button, as shown in the following screenshot:
The Atlas feature provides access to a series of variables related to the current feature. We already used this to display the NAME value of the current feature in the title label using the [%attribute( @atlas_feature, 'NAME' ) %] expression. Besides @atlas_feature, you have access to the following variables:
@atlas_feature: This is the feature ID of the current Atlas feature. This makes it possible to use this information in rules to, for example, hide or highlight features based on their ID.@atlas_geometry: This is the geometry of the current Atlas feature and can be used in rules to, for example, only show features of other layers if their geometry intersects the Atlas feature geometry.@atlas_featurenumber: This is the number of the current Atlas feature.@atlas_totalfeatures: This is the total number of features in the Atlas coverage layer.
Overview maps are a great way to provide context to more detailed main maps. To add an overview map (as shown in the upper-right corner of the composition in the following screenshot), you need to add a second map item to the composition. To turn this map item into an overview map, go to Item properties | Overviews and click on the button with the green plus sign. This will add an Overview 1 entry and enable the Draw "Overview 1" overview configuration GUI:
- Map frame: The Map frame drop-down list enables us to define the main map that should be referenced by the overview map. By default, the map items are named
Map 0, Map 1, Map 2, and so on, depending on the order they were added to the composition. Therefore, we will select the Map 0 entry if the main map was the first item that was added to the composition. - Frame style: The Change … button can be used to choose a style for the overview frame. Usually, this will be a simple fill with transparency.
- Blending mode: These are supported by overview frames, as explained in detail in the Understanding the feature and layer blending modes recipe.
- Invert overview: Enable the Invert overview checkbox if you want to apply the overview frame style to the areas outside the extent of the main map.
- Center on overview: Enable the Center on overview checkbox if you want the overview map to automatically pan to center on the extent of the main map.