Sometimes you need to produce a series of similar thematic maps. The only difference is the field used to colour areas or show graduated symbols. Classifications or maximal symbol sizes are identical. For large numbers of maps it is quite annoying to repeat the same actions in QGIS over and over again. Moreover, mistakes are easily made this way. This article explains how the production of large numbers of maps can be automated with a Python script. All files needed to try this method are available in a ZIP file. This is a modified version of a previous article. This new method works better and faster than the previous one en works the same for Windows and Linux.
- Create a QGIS-project with a map you want to use as a model for all the other maps.
- Create a template from this project.
- Prepare a Python script with the commands needed to produce the maps, and all necessary specifications, like the fields to be displayed and the template to be used.
- Execute the Python script from QGIS, to generate a series of QGIS-projects from the template and snapshots of each project.
Step 1: Create QGIS project
Create a QGIS project that can be used as a model for a series of maps. The example project shown below consists of three layers: a polygon shapefile and two point layers based on a text files, imported with the ‘Add delimited text file layer’ plugin. The first point layer contains the data for each region. The second point layer consists of only one point, located in the top left corner of the map canvas. It is used to show a unique identifyer on each map. You can skip this layer if you want. Save the project and remember the field name you used (in this case ‘cr94421’).
Step 2: Create template
Make a copy of the qgs-file you made in the previous step and call it ‘Template1.qgs’, for example. Open this copy with Notepad or Wordpad and search for the field name (in this case ‘cr94421’). Replace this field name with a new string like ‘@REPLACEFIELD@’. Don’t use automatic search and replace for this. Not all references to the field should be changed. How many references you will find, depends on the complexity of your map. If your map contains both circles and data labels, the project file will have more references to the same field.
Step 3: Prepare Python script
The Python script needed to create projects and snapshots from your template consists of two parts. The first part contains the specifications for the maps you want to generate:
- the name of your template;
- the suffix added to the names of the project and snapshot files (may be left empty);
- the fields to be used;
- the folder for the snapshots.
The second part contains the commands used to generate projects and snapshots. First the folder for the snapshots is made, if it doesn’t exist. Then for each field mentioned in the first part:
- a copy of the template is made, with a name based on the field name and the (optional) suffix;
- each occurrence of ‘@REPLACEFIELD@’ is replaced with the field name;
- a snapshot for the project is generated (as a PNG-file) and then closed again;
- the PNGw-file which is also generated is deleted (disable this line in the script file if you want to keep them).
Edit the file projectsnapshots.py from the ZIP-file with a text editor (Notepad, WordPad), adapt it to your needs and save it.
Step 4: Execute Python script
Start QGIS and open the Python Console (Plugins menu). Start the script with a command like: “execfile(‘C:/QGIS/Projects/PythonSnapshots/projectsnapshots.py’)”. Then open the snapshot folder to check the results. If you select the ‘minitiature’ view, you get a quick overview of the maps.