Generate a series of maps

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.

Procedure

  1. Create a QGIS-project with a map you want to use as a model for all the other maps.
  2. Create a template from this project.
  3. 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.
  4. 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.

  1. The first part contains the specifications for the maps you want to generate:
    1. the name of your template;
    2. the suffix added to the names of the project and snapshot files (may be left empty);
    3. the fields to be used;
    4. the folder for the snapshots.
  2. The second part contains settings for QGIS. Check if OSGEO4W_ROOT refers to the correct folder on your system.
  3. 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:
    1. a copy of the template is made, with a name based on the field name and the (optional) suffix;
    2. an external program is used to replace eacht occurrence of ‘@REPLACEFIELD@’ with the field name;
    3. QGIS is started to generate a snapshot for the project (as a PNG-file) and then closed again;
    4. 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.

Limitations

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

7 thoughts on “Generate a series of maps

  1. Edwin Liava'a

    Tonnes of thanks for this..greatly appreciated from the Pacific Islands QGIS users…cheers!

    Reply
  2. S3MP

    Tonnes of thanks – it works perfectly.

    I had to modify slightly the batch file to take into account spaces in the path:
    @echo off
    REM This batch file generates a series of QGIS-projects, based on a template. For each project a snapshot is generated.
    REM The program fart.exe is supposed to be in the same folder. Check if the location of qgis.exe is correct for your system.
    REM (Sake Wagenaar, 28 July 2012)

    SETLOCAL

    REM ==============================================================================
    REM CUSTOM DATA:
    REM ==============================================================================
    SET TEMPLATE=Template1.qgs
    SET SUFFIX=_test
    SET FIELDS=cr94421 cr94271 cr94301 cr94272 cr94281 cr94282 cr92513 cr91080 cr95672 cr94312
    SET SUBDIR=.\snapshots
    REM ==============================================================================

    REM ==============================================================================
    REM GENERAL SETTINGS FOR QGIS (from qgis.bat):
    REM ==============================================================================
    SET OSGEO4W_ROOT=C:\Program Files\_Carto\Quantum GIS Wroclaw
    call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
    echo "%OSGEO4W_ROOT%\bin\o4w_env.bat"
    call "%OSGEO4W_ROOT%\apps\grass\grass-6.4.2\etc\env.bat"
    @echo off
    path %PATH%;"%OSGEO4W_ROOT%\apps\qgis\bin";"%OSGEO4W_ROOT%\apps\grass\grass-6.4.2\lib"
    REM ==============================================================================

    REM ==============================================================================
    REM EXECUTION
    REM ==============================================================================
    IF NOT EXIST %SUBDIR% md %SUBDIR%
    for %%f in (%FIELDS%) do (
    echo --------------------------------
    echo PROJECT: %%f%SUFFIX%.qgs
    echo --------------------------------
    copy %TEMPLATE% %%f%SUFFIX%.qgs
    fart -c %%f%SUFFIX%.qgs @REPLACEFIELD@ %%f
    echo SNAPSHOT: %%f%SUFFIX%.png
    start "" /wait "%OSGEO4W_ROOT%\apps\qgis\bin\qgis.exe" --project %%f%SUFFIX%.qgs --snapshot %SUBDIR%\%%f%SUFFIX%.png --width 675 --height 770
    del %SUBDIR%\%%f%SUFFIX%.pngw
    )

    ENDLOCAL

    Reply
  3. Pedro Camargo

    Awesome work, but for some weird reason it stalls on my computer after having opened QGIS and never generates the PNGs… Any idea why??

    Many thanks!

    Reply
  4. Pingback: Reuse same style settings for chloropleth maps based on different datasets | Question and Answer

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>