Generate a series of maps

[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.

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. 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.

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:

  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

13 thoughts on “Generate a series of maps”

  1. 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

  2. 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!

  3. Dear SW,
    I Thank you a lot, I was looking for this type of processes, programs!!!!
    I have to do the same type of work with a html output like e-atlas of maps
    and also i have to add iteration to obtain all maps by qgis-projects at
    time 0, time 1, time 2 and so on with (postgis database ).

    1)My first question: I suppose this process could be program in python?
    why do you prefer program in batch or bash for linux?

    2) Do you have some references about doc, guides that I am looking for ?
    “programming in batch for windows 7″
    and programming in bash for ubuntu?

    3) In windows 7 and XP
    at the moment, I only tested your first program for windows.
    I have installed QGIS-OSGeo4W-1.8.0-2-Setup
    I ve changed some paths that you can read on the .bat snapshotsproject file

    OSGEO4W_ROOT=C:\Program Files (x86)\Quantum GIS Lisboa1.8.0
    call “%OSGEO4W_ROOT%\apps\grass\grass-6.4.3RC2\etc\env.bat”

    I can obtain all projects but without “png” and “pngw files”! the
    snapshot directory is created (after deletting yours that you included in
    the zip file)!

    also, in xp I obtained all projects
    but i obtained error message ” the system can’t find the specified path”
    I tested your program(without png step) and I obtained an error :
    “– project” the system can’t be find

    Can you help me for those last steps of your batch program?

    4) In linux, you wrote that your program didn’t work with the “1.8 qgis”
    version”! and you tested your program under 1.9.0-Master.

    I know that the version 2.0 will be able on september after “FROG 2013 in
    Paris (OsGEO first french colloquium)”, but they said us that all old
    qgis-projects couldn’t work in the new 2.0 version of qgis!

    So I am not sure if the 1.9.0-master is the future new version 2.0 for non
    developer?
    I have work for the moment a lot on ubuntu 12.0 with qgis 1.8, pgadmin,
    postgis etc..

    thank you a lot
    I am going to look for some batch, ms-dos and bash books!
    have a nice week-end
    Anne Quesnel

    1. I was already familiar with DOS/Windows batch files (and to some extent with Linux shell scripts). It would take too much time to learn how to program in Python with the QGIS API. I didn’t use a general guide about batch or script files. I searched on the internet for specific examples. Bash is the same for all Linux distributions. If you are going to write your own batches or scripts, I advise to use a text editor with syntax highlighting (for Windows you could use Notepad++).

      There is a reported bug in the snapshot function of QGIS (the program freezes after opening the project; no snapshot is made). With QGIS 1.8 it only works on my Windows 8 pc (not on my Windows 7 netbook or my Linux system). With the QGIS 1.9-Master it only works on my openSUSE Linux system. This version wil eventually be released as QGIS 2.0.

      The error message you get, can possibly be solved by adding a double quote at the end of the line with the start command. I also use this in a newer version of my Windows batch file (search and replace is now done with sed.exe, already included in the QGIS installation). I don’t expect it to work with Windows 7 (because of the snapshot bug), but I would be interested to know if this works with Windows XP.

      1. Dear Sake,

        I had not a lot time until now today,
        so

        1) In windows 7 and XP
        *I have obtained with your new file.bat the same results with both systems!

        *I can obtain all projects in the subdirectory : projects
        and the index.html file in the subdir “snapshots”.
        but now, I can’t open the obtained project files from this new .bat file
        ==>QGIS met a problem and it has to shutdown the exe(i attached a new qgs
        project giving a crash)

        *but a good point! I can open the template1.qgs without problem for QGIS.exe

        *I always can’t obtain the “png” and “pngw files”

        *In xp there is no more Error message ” the system can’t find the
        specified path”

        I thank you a lot
        Best Regards,
        Anne
        PS what’s your job? profession? training?
        do you have a personal web site?

        1. The problem with the project files generated by my batch file, is that they are moved to a different folder, after the snapshots have been made (or should have). In de project file, the paths to the data files are relative paths, so QGIS can’t find the data. If you move the project files back, you should be able to open them from QGIS. It seems the snapshot function does not work with Windows XP either (like I described here a year ago: http://hub.qgis.org/issues/4076).

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>