Showing posts with label shetland. Show all posts
Showing posts with label shetland. Show all posts

Sunday, 5 March 2023

Geometry generators in QGIS

So, you've heard about the mysterious 🌏 geometry generator πŸŒin QGIS and you want to know more? You're in the right place. No idea what I'm talking about but you do use QGIS? Well you're still probably in the right place because I'm going to explain things here, with examples you can try yourself. I've put this post together for anyone who really wants to know what a geometry generator in QGIS can do. So before going any further here's a map of US states where I have used a geometry generator style to move Alaska and Hawaii below the lower 48 states as they often appear on US maps. Read all the way down to the bottom of the post if you also want to know how to use a geometry generator expression to create an entirely new layer where AK and HI are in the location shown below permanently!

The expression moves Alaska and Hawaii

Geometry generator? Generators?

Let me say a little about terminology here, because it can be a bit confusing hearing people talk about 'geometry generator', 'the geometry generator', 'geometry generators' and various other terms. Don't worry about the precise term, it's all the same. Regardless of precise terminology The Geometry Generator in QGIS allows you to use expressions to create new points, lines or polygons on the fly during the rendering process. 

Hmm, okay. What does this actually mean? It means that if you have, say, a shapefile of US states and you want to move Alaska and Hawaii to a position below the continental states you can do this using a geometry generator expression - no new shapefile is created and you aren't editing the original file, it's all done 'on the fly' - i.e. you make changes to the appearance of the layer without actually having to modify it's original properties or edit any features. That's why geometry generator appears within the Symbology option in map layers in QGIS.

So using a geometry generator is simply about changing the symbology of a layer in QGIS by using an expression, but without having to a) create any new files or b) edit the existing layer in any way? Yes, that's basically it.

I've found that the best way to understand what the geometry generator can do in QGIS is to have a go yourself, so with that in mind see if you can download the file below and add it to a new QGIS project. Once you've done that, go into the layer's Symbology and then once you're there click on Geometry Generator to look at the expression I've used to move Alaska and Hawaii. You can then edit the settings to see what happens.

Just note that in the example layer above the layer itself is in a coordinate reference system that uses metres, so the large numbers in the geometry generator expression (see below) refer to metres. If you are doing this kind of thing with a layer that has degrees as the map unit you will need to be aware of it. So, for example, you may want to move Alaska just a few degrees south, and not 1200000 as in the example I've given you!

Hopefully this makes things a bit clearer


Some more geometry generator examples

If you search 'geometry generator' in the blog search box here you'll find some examples, but they are quite complex if you're new to it, so let me share some simpler ones now, starting by saying a little more about the US example I've provided above. In the first image below you can see where Alaska and Hawaii actually are (in yellow) and where I've moved them to using a geometry generator expression - and note that I've also scaled Alaska down 50% and rotated them so they look like other maps where AK and HI have been moved.

 

Original position vs new geometry generator position

Once again, this explains the expression used to move AK and HI

Loads of examples that have AK and HI below the lower 48

So what about other examples? Well I actually created a US lower 48 states file with loads of different geometry generator expressions in it that you can have a play around with, as well as a video that shows you how to do it. Here's the file - just download it and add it to a fresh, blank QGIS project:



Look out for the dancers at the end of the video above, by the way.

So, watch the video, download the file and then you'll see in much more detail than I can say here how things work. But, if you did want to make Texas much bigger - and only Texas - this is how you'd do it.

BIG TEXAS!

If you wanted to take things a lot further and do strange stuff, see below - the point here is that once you know how it works you can use your imagination to do all sorts of things. Note that for all the expressions I've used you will be able to replicate them if you are on the latest version of QGIS (currently 3.22 or above).

An up to date version of QGIS is required

Okay, here's a version where I've got a plain US states layer in the background and then one on top that uses a geometry generator expression to create little needle shapes that relate to population change between the 2010 and 2020 US Census. I have a much more detailed post on this kind of thing here.

See, you can do all sorts of cool stuff

Since I'm in the UK, and since people sometimes move Shetland, you may find it useful to know how to do that if you were working with a UK local authorities file. If you want to replicate this, then here's the UK districts layer

I made Shetland 5 times bigger, just because

Note that in the example above I also added a box round Shetland - a rounded box - if you look closely you'll see that I have two separate geometry generator symbols in the one layer here. See below for the expression I used to add the box - it's basically the same as the one I used to move Shetland but then I added a bounding box and a buffer distance too.

Shetland is indeed in a box, but it's also 5x bigger here

For loads more examples, take a look at the video I posted above, which relates to the US lower 48 states layer I created with TONS of example expression in it that you can play with. This is a great way to learn more about the possibilities of geometry generator in QGIS. Here are a couple of screenshots showing you what's in that file. All you need to do is delete the -- in front of any of the expressions so that only one expression is active at a time and then you'll see what it does. 

 

I put together loads of different expressions in this file

Tons of different potential uses for this


Wait, I can actually create new layers using geometry generator?!?! Yes!

This is a very important section. Yes, you can use geometry generator to style a layer to make it appear totally different from its original shape but if you want to create an entirely new layer that permanently looks different, based on a geometry generator expression then you can do this. Let's say you wanted to create a new US states layer where Alaska and Hawaii are always located below the lower 48 states. You can do this easily in QGIS by opening the Processing toolbox and then searching for 'Geometry by expression' and then you enter your geometry generator expression, choose where to save your new layer to and then that's that! It's amazingly easy and also very useful.

Create a new, modified layer using Geometry by expression

If you wanted to create a new shapefile, say - or any other type of geo file - with Alaska and Hawaii permanently moved below the lower 48 states, you could just take the original US file I shared at the start of this post, then copy/paste the expression into the Geometry by expression tool as shown above and then run it. See below for a screenshot of where I've done this in QGIS myself.

A layer with AK and HI permanently moved

Help! My shapes are disappearing

You may find that when using geometry generator expression in symbology that things disappear. This can be confusing and worrying and confusing. It happens when you zoom in too far, basically. What do I mean? Well, in the case of the US example above, if you zoom in to the lower 48 states then you will find that Alaska and Hawaii disappear because you have zoomed in to a map extent beyond their actual  position. To get round this, in the Print Layout in QGIS, you can just set the map frame much wider so that it includes the original positions of AK and HI, for example - as shown below. In the main map canvas in QGIS, in this case, just don't zoom in too far or at least move the lower 48 states to the lower right of the map canvas. Once you know what causes this it's easy to deal with and then if it's really causing problems you can just use Geometry by expression to create a layer with the shapes actually moved and then this won't be an issue anyway!

Stop AK and HI disappearing


You may also find that your shapes (points, lines, polygons) disappear when you use a geometry generator expression even if you do it correctly and not for the zoom-in-too-far reason above. In this case it will be because you have used an expression such as centroid($geometry) on a polygon layer to display the polygon centroids but you have forgotten to change the geometry type - as shown in the screenshot below. So, if you are using a geometry generator expression to change the geometry type (e.g. polygons to points) you need to let QGIS know but all it involves is clicking on the Geometry type drop-down and then picking the right one. 

Sometimes you also need to change the Geometry type



Where can I find out more?

There aren't a huge number of resources out there on using geometry generators in QGIS. Well, maybe there are but they are kind of scattered across the internet and they vary in quality. But if you do want to know a bit more and see examples of all sorts of interesting use cases then here are a few resources I recommend.


And of course you will also find lots of great geometry generator stuff if you search twitter. I have particularly enjoyed the examples from Keith Jenkins.


Monday, 1 August 2016

How long is the coastline of Great Britain?

This is a bit of a long read, so if you really want to know the answer to the question in the title of this post, it's very simple: it depends upon how you measure it. Or, you could say that the coastline of the island of Great Britain is infinitely long. But this doesn't really help anyone who wants to walk or kayak or swim round this island, so I'll attempt to answer the question here. Take a look at the image below and you'll see that I've calculated the distance of the coastline round the island of Great Britain as 11,023 miles. 

Quite a lot of coastline for a small island

But hold on a minute, I also calculated it again and got an answer of 3,876 miles, as you can see below. What's going on here? Well, the first image is an extremely detailed digitised representation of the coastline of Great Britain and surrounding islands (bearing in mind 'detailed' is a relative concept). This first map is represented by 2,282,000 individual vertices which create the polygons you see in the image above. 

In the second map, only 0.1% of these vertices are retained, so the geographical features you see below are represnted by 2,282 individual vertices. You can't see much different between the two at the scale you view them at here but if you were trying to navigate your way into a harbour or sea loch on the west coast of Scotland, for example, it would make a big difference. Click the first image to enlarge it and then compare it to the next one and you will see some differences, but nothing too drastic.

The coastline length is a function of how you measure it

At this point, you might be thinking 'hasn't this got something to do with fractals and Benoit Mandelbrot?' - and you'd be right. He wrote a very famous paper in Science in 1967 on exactly this topic, entitled 'How long is the coast of Britain'. The answer is that there really is no definitive answer - it's all about how you measure it. But let's say you want to swim or kayak around the coastline of Great Britain and nearby islands. How far would you have to travel? I tried to calculate this based on a 1km distance from the shoreline and concluded that it could be done by covering fewer than 2,000 miles - even though the coastline seems to be a lot longer. After all, you wouldn't want to go in and out of every little cove and estuary.

Be my guest

I created a little gif based on different ways of measuring the British coastline, starting off with a file that included 100% of the vertices from my original Ordnance Survey map layer (see notes below for more on this). I then created files with fewer and fewer vertices retained, all the way down to a non-sensical shape which retained hardly any of the original points. This is what I got - at 2 seconds per frame (note '% of vertices retained' figure in each image):

Coastline length at different measurement scales

It's a bit difficult to see the difference between some of these images at this scale, so I also zoomed in to the west coast of Scotland to produce another little animation. This time, you can really see more of the difference between the layers I produced. The figures on the graphics indicate what percentage of the original vertices were retained in each case. Below, this, I have also provided a still image with different versions of the coast overlaid on top of each other, just to demonstrate the impact of reducing the number of vertices on the representation of the coastline, and hence its length.


This shows Morar, Mallaig and Loch Nevis 

Each line represents a different level of generalisation

I then decided to take a smaller island and extract the individual vertices (also known as nodes) that make up the shapes you see in the maps above. For this, I chose the Isle of Skye because it's one of the biggest British islands and the coast is highly irregular and indented. Using the version of the original shapefile where I retained 1% of the original vertices, Skye is represented by 772 individual nodes joined together to make a single polygon, as you can see below.

This produces a pretty good approximation of the coastline of Skye for most purposes. At this resolution, the coastline of Skye comes in at 330 miles (530km), compared to 456 miles (733km) at the original resolution. But of course we need to remember that if we had digitised around every single rock around the coastline the length would be nearly infinite. If you measured the coastline with a matchstick, for example, you'll get an extremely high value (and a sore back).

Skye represented with a polygon comprised of 772 vertices

Here's what this looks like when you show them one by one, in an animated gif - just to give you an idea of how it is plotted spatially. This is shown at 15ms per frame, so the dot fairly zooms around the coastline. All of this also gives you a little insight into how a GIS deals with geometry and what goes into the shapes that you see on your screen. It also helps explain why the very detailed, highly accurate spatial data files we can download from Ordnance Survey aren't always the most appropriate ones to use in small scale mapping. Or, maybe I just wanted to make another geogif, but either way I think I learned something.

A dot going round the Isle of Skye at 99,000 mph (forever)


So, how long is the coastline of Great Britain? Well, if you want to swim or kayak around all islands then you should think about training for a distance of around 2,000 miles and if you want to walk the coastline of Great Britain only then it's most likely going to be a bit more, or maybe a bit less - but that depends upon how you plan your route. Despite all the uncertainty, however, I think we can all agree that you'll need to go more than 1,024 miles.


Yes, this is Britain (kind of)

Last of all, I also did a little gif showing the 174 vertices of Great Britain when the file is massively reduced - so I'll end with this.


Another one, just for fun


Notes: I used the OS OpenData Boundary Line product for the coastline. This was a polyline file so I converted it to a polygon and then generalised it several times using the Visvalingam algorithm in mapshaper. Contains OS data © Crown copyright and database right 2015. You'll see if you search online that my measurements are close to those of others - so I'm at least as right or wrong as some people. If you're interested, you might want to look up the coastline paradox as well and, of course, Lewis Fry Richardson. Other big British islands? After the island of Great Britain, it's Lewis and Harris at 741 miles of coastline (1,193km), the mainland of Shetland at 692 miles (1,113km), Skye at 456 miles (733km) and North Uist at 334 miles (537km). Remember that this refers to coastline length and not land area.