Rendering Print Layouts from QGIS Models

When automating GIS workflows, one often needs to automate the creation of cartographic outputs. The QGIS Model Designer allows you to build a workflow by combining multiple Processing algorithms. QGIS now includes several algorithms under the Cartography category that allow you to integrate the map creation process within your model. In this post, we will explore the Export print layout as image (or Export print layout as PDF) algorithm to automate the creation of a fire map. We will build a model that will automatically

  • Download the latest shapefile of active fires from FIRMS.
  • Extract fires intersecting the continental US.
  • Style the layer using a pre-configured QGIS style file.
  • Render a pre-configured Print Layout.

Whenever the model is run, it will output a map such as shown below.

If you want to fully customize the rendering of Print Layouts, you can use the PyQGIS API to write a custom Processing Algorithm. Check our post Exporting Print Layouts from Processing Scripts for an example.

Downloading Data

The QGIS Processing Toolbox includes a very useful algorithm named Download file. This algorithm can download data from any HTTP/FTP URL and respects QGIS’s proxy and network settings if configured. This makes it possible to have a QGIS model use this algorithm to fetch data from a remote server.

NASA’s Fire Information for Resource Management System (FIRMS) is a widely used system that provides near real-time data of fires globally. The detected fire locations are available to download in a variety of formats. For the model, we will get the shapefile for fires detected from VIIRS 375m / NOAA-20 for USA (Conterminous) and Hawaii. Right-click the desired link and copy the URL. The URL looks like this https://firms.modaps.eosdis.nasa.gov/data/active_fire/noaa-20-viirs-c2/shapes/zips/J1_VIIRS_C2_USA_contiguous_and_Hawaii_24h.zip. This is a dynamic URL that will always contain the latest fire detections.

We can test the Download file algorithm to see if QGIS can download it successfully. Open the algorithm from Processing Toolbox → File tools → Download file. Configure it with the URL of the required file. Choose any location on your computer as the File destination. Since our URL will download a zip file, make sure to name the output with a .zip extension.

Once you run the algorithm, you will have a new file named latest_fires.zip downloaded to the chosen location. This will be the first step in our model.

Filtering Data

We want to create a map of fires over the continental US. The FIRMS file may contain detections outside the region of interest. For the next step, we can use the Extract by Location algorithm to filter the detections using a shapefile of States provided by US Census.

Open the algorithm from Processing Toolbox → Vector selection → Extract by location. Configure the algorithm to extract features from the FIRMS layer by comparing to the features from the States layer.

The resulting layer will have detections over the continental US only.

Styling the Layer

As our goal is to create a cartographic output from a model, we need to style our layer dynamically. QGIS allows you to style a layer and save the style to a file in the QGIS Style File Format. These files have the .qml extension and can store all the information to apply a QGIS style to a layer. First, we apply the desired symbology using the QGIS Layer Styling Panel.

Once the layer is styled, right-click the layer and go to Properties. At the bottom of the dialog select Style → Save style. Choose a location on your system and save the file as fire.qml

We can test the saved style using the Processing Toolbox → Cartography → Set layer style algorithm. We will use this in the model to set the style of the filtered layer.

Rendering a Print Layout

As the final step in the workflow, we want to create a map with a title and the styled layer. Go to Project → New Print Layout. Name the layout as fire. Create the Print Layout as usual with the desired elements. You can incorporate QGIS expressions, such as now() to add a dynamic timestamp as a label.

Test the rendering of the layout using the Processing Toolbox → Cartography → Export print layout as image algorithm. Select fire as the Print Layout. Under the Advanced Parameters section, choose both the States and the Filtered layer as Map layers to assign to unlocked map item(s).

The algorithm will output a PNG image of the rendered print layout.

Building a Model

Now it’s time to put all the previous steps in a model using the Model Designer. The below image shows the workflow created using the QGIS Model Designer. (If you are new to the Model Designer, complete my step-by-step tutorial)

Since our model uses a Print Layout from a QGIS project, we can use the Save model in project option to save the model in the current project.

The saved model is available in the Project models folder in the Processing Toolbox.

For ease of sharing and data management, the best practice is to save your data and the QGIS project to a GeoPackage. We can save our project using Project → Save To → GeoPackage.

Executing the Model

Our entire workflow including data download, pre-processing, styling and map rendering has now been automated using the model. We can create a new map of the latest fires, by running the model from our project. Locate the geopackage from the QGIS Browser and expand it. Double-click the fire project to load it. The project will load the data layer along with the saved model. Double-click the create_map model to run the model.

Configure the model with the map layout and path to the QGIS Style File. Save the output as a PNG file on your system.

When you run the model, it will render the print layout with the latest fire data and create the map image below.

The completed model, the QGIS project with the Print Layout along with the style file can be downloaded from fire_map.zip

Want to level up your QGIS Automation skills? Check out our post on how to run QGIS Processing Algorithms and Models from the command line and watch the video demonstration of automating QGIS workflows below.

0 Comments

Leave a Comment

Leave a Reply