[For a modified version, go to this article.]
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. The steps are described for Windows, but it wouldn’t be difficult to develop a similar procedure for 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 batch file 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 batch file 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 batch file
The batch file needed to create projects and snapshots from your template consists of three 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 settings for QGIS. Check if OSGEO4W_ROOT refers to the correct folder on your system. The third 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;
- an external program is used to replace eacht occurrence of ‘@REPLACEFIELD@’ with the field name;
- QGIS is started to generate a snapshot for the project (as a PNG-file) and then closed again;
- the PNGW-file which is also generated is deleted (remove this line in the batch file if you want to keep them).
Click on the next image, to get the content of the sample batch file in text format. Copy the commands in a text editor (Notepad, WordPad), adapt it to your needs and save it as a file with the extension ‘.bat’.
Step 4: Execute batch file
Execute your new batch file by double clicking on it from the Windows Explorer. Watch the command line window for status or error messages. Open the snapshot folder to check the results. If you select the ‘minitiature’ view, you get a quick overview of the maps.
Unfortunately, there are some problems with snapshots in the latest version of QGIS (1.8.0). In this article only one field was changed for each new map. For pie or bar charts both the template and the batch file would have to be more complicated. Download ZIP-file with full example