Generate series of maps with Python script

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.

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

  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 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. each occurrence of ‘@REPLACEFIELD@’ is replaced with the field name;
  3. a snapshot for the project is generated (as a PNG-file) and then closed again;
  4. 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.

Download

Download ZIP-file with full example.

11 thoughts on “Generate series of maps with Python script

  1. Pingback: Een serie kaarten genereren | www.qgis.nl

  2. Anne Quesnel

    Dear Sake ,
    I tested the .py file under window 7 and QGIS 1.8.
    I changed the path in the python file like this: “C:/Users/aquesnel/Documents/GIS DataBase/Sake_Wagenaar_Python/”
    I abtained all files. But the size of files are smaller than yours
    But I can’t open the .qgs file with QGIS
    An error message appeared : Impossible to start the program qwt5.dll is missing: try to install again the qgis program to correct the problem

    Reply
    1. Sake Wagenaar Post author

      The result of my first script is very dependent on the area displayed on the map canvas before the script is executed. That may explain the smaller images (but not the very large width). Try to zoom in to the layer extent before starting the script. I’m working on a new version of the script that is less sensitive to the extent of the map canvas before execution.

      I never got the error message you wrote about. The file qwt5.dll is a standard part of the QGIS installation. I don’t know a solution (maybe you can find a solution on http://gis.stackexchange.com). You shouldn’t try to open one of the generated project files from the ‘projects’ folder (move them one level up first, otherwise QGIS will crash).

      Reply
    1. Sake Wagenaar Post author

      I can’t see the project file you wrote about on your server (I don’t see files, only the content of the HTML file).

      Reply
  3. Anne Quesnel

    To see my project files ==> http://quesnela.free.fr/Python/projects

    I will take more time in september for finding a solution on http://gis.stackexchange.com).

    It’s work!! OK ==>To open one of the generated project files from the ‘projects’ folder (move
    them one level up first).

    With your new ZIP file containing a new version of the script.py and qgs-files (I can open both of them) .
    I got smaller maps again!
    Then I zoom the template layer before starting .py file and It’s work!!
    Thank you very much!
    Anne Q

    Reply
  4. Anne Quesnel

    Dear Sake,
    I am working on the new versions of psql (9.3) postgis (2.1.0) and Qgis (Dufour-2.0.1).
    I am testing again your files inside the Dufour Qgis Version
    In order to build my own file: “MapTitles” with its attributes like in your example: toelichting, geometrie, cr94421, …
    How do you find, or determine coordinates of the label point geometrie (1500 624500) ?
    then i will add a new text layer in my project to display the label “cr94421″
    Thank you
    Anne Q

    Reply
    1. Sake Wagenaar Post author

      You can find the coordinates for your titles by pointing with the mouse on the map canvas. On the status bar below the map canvas, you can see the coordinates change while you move the mouse.

      Reply
  5. Anne Quesnel

    I can reproduce the Python script, under Qgis (Dufour-2.0.1), a chorepleth map in my exemple.

    But with the Generate series of maps with Python script, how can we display a legend of the map (without the composer module), because you need legend for interpretation of maps.

    Best Regards
    Anne Q

    Reply

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>