Showing posts with label blue. Show all posts
Showing posts with label blue. Show all posts

Thursday, 19 January 2023

Animating a proportional symbol map in QGIS

Many people who read this blog will be familiar with Karim Douïeb's fantastic 'transition between surface area of US counties and their associated population' map, shown below.

I wanted to see if I could create something a bit like this in QGIS, using a geometry generator approach, so that's what this post is about. My end result is shown below, and read on for more information about my method. See the notes at the bottom of the page for a bit more detail. The version embedded below will be a bit small, so here's the full size, high-resolution mp4 if you want to take a closer look at it.



What's a geometry generator in QGIS?

In QGIS you can style layers in the normal way, as in any GIS software - e.g. by using a blue fill on a polygon and a white outline - or you can use the geometry generator option to style a layer in a different way. For example, if you had a set of points representing populations, you could turn them into spikes with the height based on their populations. 

You could display lines as a series of points, and all kinds of other options, all without having to create any new files. Or you could convert areas into proportional symbols, as I've done above.

Okay, so you may have read this and still be like 'what the what? what the heck does this mean?' and that's pretty normal actually because it's not necessarily an easy concept to grasp without seeing examples. That's why I created a sample file where you can drop it straight into QGIS and play with different geometry generator styles, as in the tweet/ thread below.


You can get really pretty fancy with geometry generators in QGIS and do all sorts of interesting things, some of which you can easily find if you search for geometry generator qgis on Twitter. 

The thread below is another example of what I'm talking about here - it ends up with me making a tartan t-shirt based on a geometry generator style and some point data in QGIS!


Okay, fine, but could we create something like Karim's animated map in QGIS? Well, I tried and I think I got reasonably close, so read on for more about it.

Polygons to proportional symbols with QGIS geometry generator + Atlas

Okay, first things first - if you want to replicate this style, you can find the QGIS project file plus the individual map layers in my training data folder - directs links to each file are provided below. Whereas Karim did this for the entire US, my example only includes the lower 48 states. If you already know what you're doing with QGIS this will be fairly straightforward but anyone with a bit of experience should be able to download the files and replicate my steps. Here are the files you'd need to replicate my work.

If you want to replicate my project exactly, you'll also need to have the Comfortaa font installed because that's what I used for the labels and in the Print Layout. You'll also need to open the Print Layout in the project and make sure you activate the QGIS Atlas (via the Atlas menu then Preview Atlas). I did this in QGIS version 3.24 so if you're having any issues try using the same version, or a later one. Last of all, if you want to add the QGIS logo like I have, you will need to change the file path for the Picture in the print layout. I put the QGIS svg logo file in the same folder as the files above.

As you move through the Atlas page by page, each of the counties changes from a polygon to a proportional symbol, from west to east, with the size of the symbol based on the number of votes in each county. This is not perfect, but it's a more proportional representation and it's all about testing the concept here. This is what it looks like half way through the set of QGIS atlas frames.

Half way through the animation

Once I got everything set up, I exported each page of the Atlas as a single png and then combined it into an mp4 as an animation, using ffmpeg. I've written about that process in a previous post. For the final version I added a pause at the end, then reversed it so it loops back and forth.

So what about the QGIS project file itself? Well, if you download it you'll see it contains:
  • A US states layer, to show the state outlines - simple black colour outline
  • A US cities layer, filtered to show a selection of cities across the US, and also labelled
  • A grey US county backdrop layer so that when the polygons turn to proportional symbols we still have some background behind it
  • And on top of the grey US county layer we have the red/blue county layer showing which party won in each county at the 2020 US election. This is the layer that has the geometry generator style in it. 
  • Plus the QGIS logo svg in the bottom right of the print layout. 
If you want to look at the all-important geometry generator style, just go to the Layer Properties for the red/blue us-election-2020-lower-48 layer and then double-click on the blue colour patch symbol then Geometry Generator to see the geometry generator style for the blue areas and then do the same for the red areas, as shown below. 

You can also access this via the Symbol dropdown in Layer Properties


This is the geometry generator style I used - it's pretty basic, but works well:

CASE

WHEN x($geometry) <  @atlas_pagename 

THEN  buffer(centroid( $geometry),7000+ "total_votes"/30 ,50)

ELSE

$geometry

END

What does it do, and how does it work? Well, the first bit after the WHEN uses the x coordinate of county centroids to change the shapes from west to east - this is because I used the longitude of each county as the Atlas page name. That's why @atlas_pagename appears in the expression. You'll need to think about this for a while if you're not used to how the Atlas tool works in QGIS but if you look at the Print Layout you'll see each page is named using x($geometry).

The next bit draws a buffer around the centroid of each county, to 7000 metres, then scales it using the total vote count, divided by 30 (otherwise the circles are HUGE, but I did experiment with this and scaled it loads of different ways). The 50 value just makes the circle smoother. Why 7000 metres for the buffer? Well, if it's much smaller then you can't actually see it at the scale I'm mapping at. 

The ELSE $geometry bit just says to QGIS to draw the polygon as usual until the Atlas page name (which contains the longitude) meets the WHEN condition above.

Confusing? Yes, probably if you've not used this before but feel free to download the project and have a go.

You'll also notice that in the Atlas tab I've done some other things, like set the page name using the format 0001, 0002, 0003, 0004 and so on. 

This is what the Atlas layout looks like at frame 2000, below.

What it looks like in the QGIS Print Layout


Okay, so can we create an animated proportional symbol map in QGIS? Yes, we can. 

Is it as cool as Karim's? No, but it does demonstrate the power of QGIS geometry generator styles and shows the kinds of things that can be done - and that's what this post is all about. 

This is example is fairly simple really but if you've not used QGIS in this way before it can be quite confusing - that's why I shared the simple geometry generator file plus the more complex project file shown here.

Want to replicate this but can't quite get it all to work? Feel free to get in touch.

Notes

If you want to actually create new geomtries (e.g. a new shapefile or geopackage) using a geometry generator expression, you can do it via 'Geometry by expression' in the Processing Toolbox.

Are the proportional symbols sized exactly to match the population? No, because the size is based on the buffer radius, but if you use a bit more clever maths in the expression you can get it spot-on. But if you do this, the smallest symbols will likely disappear.

What's the point of all this? The point is to test what kind of things are possible with geometry generator styles in QGIS and to share this with people who might be interested.

That's all for now. I may add further notes if there are any questions but I'm putting this post here as a more permanent resource for anyone interested in how to do this kind of stuff.

Thursday, 9 September 2021

10km City Squares

In 2020 I wrote a piece on here along with some maps showing 10km by 10km (that's 6.21 miles) city squares for places across Great Britain. I'm fascinated by the different forms and types of urban development we see on the ground, so I'm back for more - this time adding in blue and green to the grey of building footprints. You may even have seen my Twitter thread on this, but here I'm sharing some final versions of these grey-green-blue maps and also saying a bit more about what they actually are - and are not. But first, let's look at a few of the final map outputs for some interesting places. There are currently maps for 199 different places across Great Britain in the project folder. Sadly I don't have the data for Northern Ireland, otherwise I'd have included that too.

























What are all the different colours?

The grey bits are buildings, roads and railways. The blue bits are surface water (lochs, lakes, ponds, rivers and the like) as well as coastal water. The green bits are less straightforward in that they do not represent every bit of green in a place - and are not intended to. They are the areas included in the OS Open Greenspace dataset from Ordnance Survey. You can read more about it on the dedicated webpage for it but in short it includes public parks, playing fields, sports facilities, play areas, allotments and more. So, things like some large green areas (e.g. national parks, gardens, and the like) are not green on these maps. What I am trying to do here is compare like-with-like across the country to examine density, the patterning of urban fabric, water coverage and a particular kind of green. Nothing too profound but something I find particularly interesting.


My town isn't included - you scoundrel!

I can in theory do this for about 43,000 places across Great Britain so if the place you want to see has been overlooked and is not included in the web folder then feel free to get in touch and I might be able to add it. Alternatively, strike me off your Christmas card list and consider me a non-person from this point onwards.


Can I print one of these and hang one on my wall?

Be my guest. I have outputted them at 300dpi (png files) so they should print at a reasonable size. I may yet add some to my print store but not sure yet.


How did you make these?

I downloaded the map data from Ordnance Survey and then put the layouts together in QGIS. I then automated the exporting of each map using the QGIS Atlas function. If you're desperately keen to learn how to do such a thing then I also do training on that. Once you get one layout set up, it's fairly straightforward to export a big batch, so long as your computer is up to it. See below for how they look in QGIS just before I export them.

This is how it looks in QGIS Print Layout

Why did you choose 10km?

Partly because it's a nice round figure within which a lot of towns and central cities fit, and partly because 20km was too big and 5km was too small. I chose it through trial and error and it works reasonably well for most places.


That's all for now. Happy browsing.



Sunday, 14 April 2019

Suffering from Brexhaustion? Have some more maps

This is the latest instalment in my series of ill-advised mapping adventures. I really don't know why, so long after that EU referendum thing, I am posting this today but I think it must be a mixture of being on Brexopilot and the fact that nobody seems to have mapped all the ward-level EU referendum data that Martin Rosenbaum of the BBC published in February 2017. I say 'nobody' but Owen Boswarva did map Bristol at the time, so I've borrowed some of his ideas for my maps. Note: there aren't maps of everywhere - only some areas released ward-level results data. Here's a little screenshot of the folder with all the maps. Scroll down to see the big versions.


Some explanation first, to describe what you are looking at. I used the ward results file at the bottom of Martin Rosenbaum's February 2017 piece to make these maps. I used shades of yellow for remain and shades of blue for leave. In both cases I used the yellow and blue from the original BBC maps as most people are familiar with that colour scheme. I mapped all 61 local authority areas that had results for all wards (36 'Leave' areas, 25 'Remain' areas). This covers about 15% of the UK but there is a good mix of area types so it should be somewhat representative of the wider picture. Here's a little visual explainer of what the maps show.

I think the local patterns are pretty interesting

The best way to see if an area you are interested in is in the map set is just to scroll down, because I've put all the maps below, in alphabetical order. You can also get them from the Google Drive folder, which by the way you can also get to by using this shorter and easier to remember url: bit.ly/brexit-wards-2016. At the very bottom of this post I've added a few more words, should you be desperate enough to read them.
































































Notes: I thought I would do this when the data came out in February 2017 but other things got in the way. With the recent Brextension (sorry) I thought I might as well finish the job I didn't start back then. Why? Partly because I was curious about what the local patterns actually looked like on the ground - apparent anomalies, concentrations, strongholds, that kind of thing - but also because I wondered whether there ware many 'islands of Leave' or 'archipelagos of Remain' and that kind of thing. I think if you look at the maps you can see a bit of this. I have used local authority boundaries because that is how the vote was conducted and counted, but I did also have a look at it by constituency (and then thought better of it).

Three Nottingham constituencies (East - Ind, North - Lab, South - Lab)


I was also interested in whether there were neighbouring wards that are really different in their voting. It turns out there are a few but not that many. If you're interested in this subject and haven't already read Martin Rosenbaum's piece on these local results, do take a look - it's really good. I did do a bit of analysis using the same data at the end of 2017, but that was just a few scatterplots. I should note, as Martin Rosenbaum does in his article, that this data was obtained thanks to a major data collection exercise carried out by George Greenwood.

I really would quite like to never hear the word 'Brexit' ever again though. Not because I have warm, fuzzy feelings for the EU but because it's all we ever hear about. So I thought I'd join in the fun and do some almost entirely unnecessary local Brexit voting pattern maps, even if it is just for the historical record. Though, actually, all this might be useful to people over the next six months, even if it's only for national media vox pop location planning (on which note, the KFC on the A127 in Southend seems like a good spot). Happy Halloween etc.


One of a few spots where near things are not that related