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.

Wednesday 11 January 2023

Label callouts in QGIS

This is a short tutorial about how to display label callouts (sometimes referred to as 'leader lines') in QGIS. This has been possible for a while, but not everyone knows it's possible, or how to do it. So long as you're using QGIS version 3.10 or later, you create callouts for your labels very easily. Before going any further, here's an example of a map where label callouts would be useful (below). And here's the map layer I'm using for this tutorial, in case you want to follow along with the same data.

Very crowded labelling in the Northeastern United States

In the example above, some labels are okay, but in New England in particular things are a bit crowded, and in some other cases as well we might need to move things around. And note that I won't bother making things look particularly pretty here at all, this is all about the method.

So, here's two useful things to know for now - this is just for information right now, we'll look at how to change things further below.

  • The Label Toolbar (it looks like the image below, and if you don't see it you can turn it on via the View menu then Toolbars). We can place labels wherever we like using the Label Toolbar.
  • Where to find the Callouts options for a layer. For this, you need to go to Layer Properties (easiest done via a double-click on the layer name in the Layers Panel, or via a right-click then Properties... then Labels and then the separate Callouts section - again, see below for a screenshot of how it looks, in this case in QGIS 3.26).
Yes, this is what it looks like!

Here's where you find the Callouts options

Okay, but why would we want to use callouts? 

Well, we want to make labels easier to read and because some states are small (NH, VT, etc) we just can't fit the labels on top of - or inside - the shape of the state. Not everywhere can be a Texas or a Colorado! And that's fine.

Also, anyone who has done any mapping - or even looked at a map - knows that labelling is super important. Done well it can make a massive positive difference. Done particularly badly and it could ruin a map.


Let's draw some Callouts

Now I'm going to add some callouts to my US lower 48 states layer that I showed you above. This is done in Layer Properties > Labels for the layer you want the callouts on. So the first thing I'll do in Layer Properties is tick the Draw callouts box (below) and then click OK to activate callouts. Oh, boo. Nothing happens, but that's actually fine. 

This is how we make callouts active

Next I'm going to use the Move a Label, Diagram or Callout button to move some of the labels in the Northeastern United States. It looks like this (below) in QGIS version 3.26 and should look the same - or very similar - on your version of QGIS too.

This is the important button!

Next, I hit the Move a Label, Diagram or Callout button as shown above, then I select the first label I want to move, just by clicking on it - in my case I did this for Vermont and as soon as I did I got a pop-up, which unless you know what it's asking you can be baffling! It's not a problem, just keep reading for how to deal with this.

This little window (shown below) is QGIS asking you to use a unique column in your attribute table so that QGIS can store the X and Y coordinates of where you move your labels to, more or less. But you don't really need do know that so just click OK when you see the Auxiliary Storage: Choose Primary Key window, as shown below. It should just be on fid by default but really unless you're into this kind of thing you can click OK. At the bottom of the blog post I tell you how you can reset this, if you need to.

You can just click OK here

Then, making sure you've still got the Move a Label, Diagram or Callout button selected, you can move a label to somewhere where it has more space - so it's easier to read. This takes a bit of time, judgement and skill but don't worry if you don't get it right first time.

See below for where I've done this for lots of states - quite messily I might add. But that's fine at first, you can experiment with it later. At this stage I'd usually move some of the other state labels as well, to improve upon their positions - e.g. Florida, California, etc. This won't add a leader line because that only appears if you move the label further away from the feature.

We've made some progress! Oh yeah

But let's say we want curved callouts, because they're nice, right? 

Let's try that instead. To do this, we go back into the Layer Properties, then Labels and then in the Callouts section we change the Style from Simple lines to Curved lines, as shown below. 

If you don't see all the same options as me, it's more than likely because you're using an earlier version of QGIS - or perhaps a later one. At this point you can also experiment with different curve options, like Curvature (a higher value here = a bendier line). For now I'm just going to accept the defaults and hit OK. You can always experiment with the settings later, including the different anchor points options.

The different callout options

Curvature, and other options

I've gone with curved callouts in the map below, and I've moved the labels a bit too, in an attempt to try and tidy things up.

Staring to look a little better now

Couldn't we just use state abbreviations in these states, like NH for New Hampshire? Yes we could. But sometimes we want full names, plus not everything can be abbreviated. 

What about callout colours, widths, font sizes, styles and so on? Good question. You can go back into Layer Properties and then Labels and then change all that in the Text and Callouts section, as shown below when I did it. I used a callout stroke width of 0.1 instead of 0.3 and I changed the font, added a buffer and a little shadow too.

This is starting to look a little better

What if, say, we want the callouts to point to the edge of the state rather than the middle? Well, that's something we can change in the callout options by changing the anchor points, as I've done below where I set the Feature anchor point to Point on Exterior.

The callout now points to the edge of the shape, not the middle

As you can see, I've gone a bit wild with the colours now (below), but just for fun. But you can also see that I've tried to make the labels all neat and tidy and get the callouts looking good too.

This is more like it - at least in terms of the callouts

So, to recap on this bit...

  • Make sure you can see the Label Toolbar
  • Turn Callouts on via the Layer Properties, in the Labels section, and pick what kind of callout you want.
  • Experiment with placement, as well as font styles, line styles and so on.
  • Experiment a bit more.

Other things to think about - e.g. different kinds of data

The example above uses US states, but there are lots of cases in countries, regions and cities across the world where such an approach is useful - e.g. where some labels will fit within features but some won't and you need to find a solution. 

Sometimes callouts are the solution. But sometimes we might want to have callouts on every feature. We can do that too, but it's not necessarily that obvious at first so let's have a look.

To turn callouts on for every feature (e.g. points, lines or polygons) - and actually see the callout lines...
  1. Go to Layer Properties for the layer you want the callouts on.
  2. Go to the Labels section and turn labels on.
  3. Within the Labels section go to Callouts and turn them on.
  4. I tend to use curved lines rather than straight lines, but it's up to you - it all depends.
  5. Now go to the Placement section, still within Labels - it should be below Callouts (see screenshot below).
  6. By default, the Distance in Placement will be 0, so when you turn callouts on you won't actually see a line unless you increase the Distance. The Distance here refers to how far the label is away from the feature (e.g. a point).
  7. You probably won't want to have callouts on every feature most of the time, but that's what this does. See below for an example with a US cities layer. Once you click OK to exit Layer Properties you'll see the result.
Placement is very important with callouts

We don't always want callouts, but sometimes we do

Once you've done this, you can of course move labels around using the Label Toolbar, just like you did before.


Other useful/important stuff

Okay, so we've got some callouts now, and that's fine. But as you can probably tell there are LOADS of other things you can tweak with labels so I'm not going to cover everything. It's just a case of trying this and then experimenting with all the options. However, there are a few extra useful and/or important things I think you should know.

Once I've manually moved labels using the Label Toolbar, how do I reset the label positions to get them back to how they were originally?

A good question! Further above when we started to move labels I mentioned how you get that mysterious Auxiliary Storage: Choose Primary Key window pop up when you start to move labels. I said to click OK, and that was fine because it works. But you may not have noticed that it changes the label placement settings under Data defined, as shown below. To reset this, just click the X and the Y and hit Deactivate and you should be back to the original default placement.

Not a lot of people realise this is here


When I turn on labels the callout goes to the centre of a point feature and I don't like that!

Okay, yes, this is a good thing to know how to fix. You can see below in my US cities example how the callout goes right into the centre of the dot. I don't want this so I need to change some settings.

Callout goes to centre of the point feature

Offset from feature is what you need to edit here

As you can see from the screenshot above, I've edited the Offset from feature setting to move the callout line away from the point a little bit - I think it looks better this way. You can also experiment with lots of other settings here, depending upon what you want to achieve.


What do Manhattan lines look like? 

Another good question. They look just like the example below. They are called Manhattan because of the street grid in Manhattan, which is all right angles - well, a lot of it. 

Just take time to experiment here

Okay, that's it for now. This post is a bit messy but I'll fix any errors or clarify anything that isn't clear if anyone gets in touch. See below for a little animation of different callout types - using a dataset for US cities. I wouldn't normally need to use callouts here, so this is just for demonstration purposes.

Hope you find this useful!

But I wouldn't usually use callouts in this case!