Friday 7 November 2014

Automatic map production with QGIS

This longer post explains how to automate map production in QGIS using the atlas generation tool. It's based on QGIS 2.4 but will work in later versions and some earlier versions too. I've found this tool immensely useful and a great time saver so I'm sharing the method here. Similar outcomes are possible in ArcMap's Data Driven Pages but I find that the rendering quality of QGIS is better, so I use this approach. Before going any further, here are some example outputs from the atlas tool - it shows some recent mayoral election results from Toronto, which I saw on twitter via Patrick Cain. You can download the PDF mapbook and the individual images files below.

Download the complete map book of results (150MB PDF)

Download individual image files for the whole city (58MB)

I'll explain how you can produce multiple maps and also how I achieved some of the effects in the image above. This tutorial tells you how to produce one map per page. For multiple maps per page (as above) I'll say more at the end. There are two data layers in my map - one layer for the 44 wards of Toronto and one for the election results. The election results were posted on CartoDB by Zack Taylor from the University of Toronto Scarborough with his interactive map so that's the data I'm using here. I've used this dataset because a) it's very interesting and b) I wanted to compare a small number of variables across single areas of a city and produce a map book from it.

I added my layers to QGIS and then I styled them as I wanted. Basically, this involved copying the ward boundary layer and making the top one just a hollow black outline style by changing the symbology to 'No Brush' and a black outline of about 0.5 width. Here's what I did to create the glow effect round each ward... On the copy of the ward layer I symbolised it using the 'Inverted Polygon' and then in the Sub-renderer options in the same window I selected 'Rule-based' (screenshot below).

I then clicked to edit the rule so that my map looked as I want it to when I move to the atlas production phase in the Print Composer later on. You just need to select the colour patch and then click on Edit Rule (the little pencil and paper icon, as shown below). I wanted to make sure there was a glow effect around each individual ward when the atlas was zoomed to that feature so using tips from Nyall Dawson on shapeburst fill styles in QGIS 2.4, Hugo Mercier on inverted polygons and Nathan Woodrow on highlighting current atlas features I managed to achieve this - note the text in the Filter box in the second image below. You'll see that in the second screenshot I've symbolised the layer using a black to white shapeburst (this creates the black glow effect outside the active polygon, since we're using inverted polygon symbology). 

The final thing I did was to go back to the Fill colour patch on the second image below and change the transparency to 33%. This means that you'll be able to see surrounding areas but with a 'lights off' effect. You might find that when you do this your layer completely disappears but don't worry; it will come back on when you move to the Print Composer.

I then symbolised my Toronto 2014 mayoral election results layer using a graduated colour scheme using 'Pretty Breaks' so that it had breaks at 10%, 20%, 30% and so on. I then made sure I saved my project (!) and moved to the print composer (CTRL+P) and added a map to my page. 

This is where it gets a little confusing if you're new to it. Once the map is added, go to the Atlas generation tab on the right and click the 'Generate an atlas' box. You then need to set a 'Coverage layer'. This serves as the positioning device for each atlas page, so that when you produce an atlas QGIS will zoom to the extent of each feature to create a map for that area. So, in this example, I specified the coverage layer as the Ward boundary layer (the top one) because I wanted a map for each ward. Still on the Atlas generation tab, go down to Output and you can set to sort the atlas pages by features from your coverage layer. In my case I ticked the 'Sort by' box and used the NAME field so that my atlas would be organised alphabetically by neighbourhood name. Nothing will happen yet. I then went back to the Item properties tab and scrolled down until I could see 'Controlled by atlas' and then clicked that box. 

There are a number of options here but I chose a 'Margin around feature' of 25%. This means each map will zoom to your area and leave a nice margin around the ward - useful if you want to leave space for other map elements such as a legend. To finally make something happen, go to the Atlas menu and then select 'Preview Atlas' - the map will then zoom to your first feature - as you can see in the example below. If you then click on the blue Atlas preview arrows you can preview successive pages and the map will zoom to each feature in turn. You can also see that the inverted polygon glow is now active only for each active atlas feature (that's what the $id = $atlasfeatureid filter does).

I then added in a text item which would serve as a title but I wanted this to automatically change as I moved through the atlas so once I added a text item I then deleted the default text and clicked on 'Insert an expression' below the text box and used the Function list to insert the 'NAME' field in the Expression box (as below). What this means is that as you go through each successive page on the Atlas, QGIS will enter the name of the ward currently in view in the atlas. A good tip here is that you should size your text box and font appropriately so that the longest name in your dataset still fits within the box. You can spot mistakes in the Atlas preview but if you're generating hundreds of pages this is not always practical. 

If you close your project and open it up again, remember that you'll have to go to Atlas, Preview Atlas again. Now you just need to take some time to add any other map items you want, such as a legend, north arrow, and so on. Scale bars are a bit problematic but I don't normally use them for this kind of map. At this stage you'll want to check the Composition tab to check on the export resolution. Obviously, the higher it is, the longer it will take to export.

Finally, go back to the Atlas generation tab and look down to the Output options. By default, QGIS will call your individual map exports 'output_1', 'output_2' and so on but you can click the little expression button here and choose to name your individual files using a field from your Coverage layer. I did this when I exported my maps by using the NAME field and each file now has the name of a Toronto neighbourhood: immensely useful when you're generating hundreds of maps.

If you click the 'Single file export when possible' it means that (e.g.) you'll get one big PDF rather than individual ones for each area in your Coverage layer. I chose this option in the example at the start with 4 maps per page, but it creates large file sizes.

That's pretty much it. I have not explained everything here in relation to the basics because I'm really aiming this at people with experience of QGIS but feel free to get in touch if you need any tips. This kind of thing can also be done programatically in R, but since I don't have the coding skills of Alex Singleton et al. I'm using QGIS. 

Finally, here's the zipped folder (58MB) containing 44 neighbourhood maps of the Toronto 2014 Mayoral Elections - with 4 maps on each page relating to % voting Ford, Tory, Chow and Other candidates. Feel free to use these as you wish. I didn't do this as a Toronto mapping project but the results do look quite nice in my opinion.

People in this ward seem to like John Tory

Want multiple maps on each atlas page, each of which show a different variable?
To achieve the effect I have in the images at the top of this post, all I did was create a smaller map item in Print Composer using the symbology I wanted for the electoral results layer (e.g. % voting Ford) and then adding a legend for that frame and then locking the layers for that map item (see below). I would then create a duplicate of the electoral results layer, add a new map frame to Print Composer and repeat the process. Just re-symbolising the same layer will not work. Once I am happy with how a map looks, I simply lock the layers on Item properties so that it doesn't change. I wanted to show a little inset map to show the general location of the ward within the city of Toronto and I did this in a very similar way. The main different here is that in the 'Controlled by atlas' options I used a fixed scale.

Notes: apologies if I've made any of this too complicated or if I've missed anything. Do get in touch if you notice any errors. I'm sorry if the map projection is not the one Toronto natives would use or if I've made any other foreigner gaffes. The point of this was really to demonstrate the technique using an interesting recent dataset so hopefully I've achieved that.