Showing posts with label terrain. Show all posts
Showing posts with label terrain. Show all posts

Monday, 10 April 2023

The longest line (part 2)

In my last blog piece I wrote about my attempt to find a longer straight line in Great Britain that doesn't cross a public road than the one identified by Ordnance Survey in their 2019 blog. I did this using their OS Open Roads dataset, and I excluded public roads because that's what I think makes most sense. I found a longer line in a different area, though I definitely wouldn't recommend trying to walk it but I would recommend watching this video. I wasn't quite satisfied that my previous line was the definitive single longest line possible so I spent a bit more time on it and now I'm back to report my results. All of this was done in QGIS, using open data from Ordnance Survey - and just a bit of curiosity. The original OS piece was in response to a question on Twitter but I also found it very interesting from a methods point of view. And now here's a map of the single longest straight line I think you can possibly find between roads in Great Britain. I'll say a bit more about the method below, for the nerds. Here's the web map of the new longest line. And here's the Scottish Outdoor Access Code.

What I now believe is the definitive 'longest line'

Was I wrong at first? Well, no, in the sense that I was still correct in stating that I'd found a longer line - in a different area - than the original OS line, but I didn't quite find the single longest line possible. The line I have found is roughly in the same location though and this is all just a mix of curiosity, mathematical geometry and fun so there's no big issue here. Look for longest-line-new in this folder (gpkg, shp and geojson formats) if you want to load up the line in your GIS software.

But I would strongly suggest that if anyone wants to walk this route they should be extremely careful and only attempt in good weather, and if they are very experienced in Highland terrain (e.g. thigh-deep heather, nasty, tussocky, holey land, murderous midgies, etc). I know myself how hard it can be to walk over this terrain and how much pain it can cause. Many difficult walks over the years have proven this, including a particularly bad decision in Knoydart a few years ago where it took us about an hour to walk 500 metres over what looked like flat ground!

In doing all this I also found a much longer line in England than the one identified in the original OS piece - over 35km vs 29km. This was the case even if I used the file that has the restricted roads in it. I also found a longer one for Wales too but in both these cases there is no right to roam so I don't want to encourage trespassing.

You can read about the method below, but here are a few images from my map adventures.

All areas > 100 sq km between roads

Generating sets of 'longest line' candidates

I got to about 35km straight line in England

Yes, I got carried away with myself again

What about the distance over terrain? Well, that's important so I did the same as last time. I took the longest line, plonked it on top of the OS Terrain 50 digital elevation model and then generated a distance for the line. Obviously if I had even more accurate terrain data (Terrain 5 for example) then the distance would be a bit more.

So, a nice wee 50 mile stroll

This was an interesting methodological challenge and I think I've found the definitive longest line you can travel along a straight line without crossing any public roads in the UK. See my original blog post on this for more on the background.

A revised web map - I think this is the final answer


The method - let me know if you have a better, super-quick method :)

In theory the task is easy. Just draw a straight line inside the biggest gap in the road network, defined using the OS Open Roads dataset. It's quite easy just to eyeball the biggest gaps in the road network and come to a pretty good answer but this is not sufficient if you want to be systematic and definitive. I didn't just eyeball it the first time, there was more to it than that, but also I wasn't completely exhaustive either. So, here's what I did this time, in brief. Note that the data is just for Great Britain but the longest line is the longest in the UK because there are no gaps in the road network in Northern Ireland anywhere near this big.

I'm reporting my method for non-restricted roads, but I also did it with the entire OS Open Roads dataset, including restricted roads - just for fun.

  1. Download the latest OS Open Roads dataset and add it to QGIS.
  2. I decided to polygonize the entire OS Open Roads dataset because of a suggestion by John Murray, and this was pretty useful. This was also pretty quick and simple on my computer.
  3. Then I decided to deal with the roads a bit differently, because they needed to be cut out of the 'between roads' polygons I made so that I could fit my lines entirely within the polygon geometry. But also because the OS Open Roads data is just centre lines and they have no thickness. So I approximated here and buffered the roads to 10 metre width, and dissolved the result. Then I ran a Difference on them to end up with a final set of polygons - lots of big 'no roads here' polygons. I only did this for areas of 100 sq km or more - for all road types this gave me 116 areas and when I removed restricted roads I ended up with 141 areas. 87 out of 141 are in Scotland.
  4. Then I needed a simple way to identify the longest line within polygons. This is in theory very easy but also you could waste a lot of time computing vertex-vertex distances here that you don't need to - you're only really interested in the furthest vertices from each other that can be connected with a line entirely within the polygon. So for this I used the Python console in QGIS and a little snippet I found on StackExchange (of course). I've copied this below in case it ever disappears from there. With thousands of vertices this can take a long time so try it one shape at a time if you are doing it yourself. You could just draw a set of lines to/from selected vertices but this is also a bit of a pain, particularly because it won't avoid the roads.
  5. How could I be sure which intra-road area contains the longest line? Well it's pretty easy to see visually but to be sure I created some minimum bounding circles and computed the radius and then the longest line was still clearly in my original Monadhliath Mountains area, so that was good. You can see a bit of this in one of the screenshots above.
  6. Then it was a case of doing some checks, double checks and then writing this short note.

import itertools

layer = iface.activeLayer() #Click layer in tree

#Create empty line layer
vl = QgsVectorLayer("LineString?crs={}&index=yes".format(layer.crs().authid()), "Longest_line", "memory")
provider = vl.dataProvider()

#For each polygon find the longest line that is within the polygon
for feat in layer.getFeatures():
    verts = [v for v in feat.geometry().vertices()] #List all vertices
    all_lines = []
    for p1,p2 in itertools.combinations(verts, 2): #For every combination of two vertices
        all_lines.append(QgsGeometry.fromPolyline([p1,p2])) #Create a line
    all_lines = [line for line in all_lines if line.within(feat.geometry())] #Check if line is within polygon
    if len(all_lines)>0:
        longest_line = max(all_lines, key=lambda x: x.length()) #Find longest line
        #Create a line feature from the longest line within polygon
        f = QgsFeature()
        f.setGeometry(longest_line)
        provider.addFeature(f)

QgsProject.instance().addMapLayer(vl)

Thursday, 6 April 2023

The longest straight line in Great Britain (without crossing a public road)

In short, I believe I've found a longer straight line without crossing a public road than the line identified by Ordnance Survey in 2019. Important stuff, clearly. Let me explain.

**Update, 8 April 2023: I've added a Part 2 to this now, where I find an even longer line in the same area. Also, be very careful if you try this route because it won't be at all easy and you may also be eaten alive by midgies, as well as all the other hazards like bogs, hills, snow, cloud and so on.**

This is a longer straight line than the one below

Back in 2018 someone asked Ordnance Survey a question on Twitter, as follows:

"what (and where) is the longest distance you can walk in a straight line in England/Wales/Scotland without crossing a road (defined as a paved surface for vehicular use)??"

Note 'a paved surface for vehicular use'. This is the most important bit for me and the reason I'm coming back to it again after years of working with this data and never quite finding time to investigate, until now.

It's an excellent question. The answer the OS team provided was then turned into a blog post on the OS website in early 2019, and you can read that here but see below for their map. There's also a fantastic short film of Calum Maclean and Jenny Graham walking the route. 

71.5km or 44.43 miles

Now, let me say a little bit more. I'm from the Highlands, know lots of this area pretty well from my days cycling around it on my mountain bike when I was a teenager, as well as loads of other trips over the years for all sorts of reasons. I also spend my days looking at map data because that's a big part of my job and because of that one thing troubled me a bit about this answer from Ordnance Survey. 

To be more precise, once again, it's the 'paved surface for vehicular use' part of the answer. I'll explain all below and my maps are very rough and quick but you'll hopefully see what I mean. 


The roads data

Ordnance Survey provide loads of great open data these days, and one of those products is their OS Open Roads product. Load it into your GIS software and it looks like the image below. You can see from just eyeballing it where the biggest gaps in the road network are - unsurprisingly, all in the north of Scotland. There are some gaps in England and Wales too but here I'm trying to find the longest straight line between roads in Great Britain so I'm focusing on the north of Scotland. The longest line in Great Britain is also the longest in the whole UK, because the density of roads in Northern Ireland is such that there's nowhere even remotely as long as the line above there.

All roads in Great Britain


This OS roads file is about 2GB in size and is really useful. However, when I've run different kinds of network analyses on it I sometimes run into problems because it also includes roads that are not public. These are typically flagged as 'Restricted Local Access Roads', and I've shown some of these in the zoomed-in map below in purple. You may think by looking that these are 'roads' but in reality they are usually unpaved private tracks but more on that further down the page. 

Restricted 'roads' in purple here

What do these restricted roads look like on the ground? Well, you can take a look via Google street view a lot of the time, and you can also get a sense for what public roads are just by hovering the street view icon over the map, like I've done below.

A much better representation of public roads

For example, if you travel down the road along the River Findhorn, you'll eventually get to the end of the public road. You can of course keep going on foot or on your bike because of the laws on access in Scotland but you can't drive down these routes (unless you have permission). 


The end of the public road


Another 'Restricted Local Access Road' - see what I mean?

If you download the roads file and then make public roads one colour (blue) and then the restricted roads a different colour and then make a head-splitting gif of the result, this is what you get. Study this for a while and then you start to wonder if you can find a longer line than the OS one from 2019. Sometimes restricted access roads are in places like caravan parks, cemeteries, and the like - but in the Highlands they are often in the hills too.

Loads of restricted access roads (purple) on the map

Using what I would consider to be the 'correct' OS roads file - i.e. the one in keeping with the original question and in keeping with the concept of public roads - I can get a longer line in the original Cairngorms route too, see below. Let's worry about distance over undulating ground later on.

I can get a distance over 74km (straight line) this way

But with my glasses on I could see a potentially longer route, that just missed a couple of public roads, this time to the west of the A9 instead of the east. That's the map I posted at the start, and again below.

From the A9 to near Fort William, a nice walk

I've added terrain to this one, to show how tough this would be

Here's a height profile for anyone mad enough to be thinking about doing this route. It looks horrendously tiring and almost certainly quite dangerous unless you're a bit of an expert. Units are in metres below and the max is just about 3,000 feet, with lots of ups and downs. How much distance does this actually add to the straight line? 

Well, it's never as much as I'd think and it depends upon the accuracy of your terrain model but using the publicly available OS Terrain 50 dataset I get a distance of 77.0 km / 47.8 miles for this so a bit longer than straight line distance. I calculated this in QGIS using the Saga 'Profiles from lines' tool. 

A nice wee stroll

Anyway, that's what I get when I look at the data. A bit longer than the OS answer, but that's because I've tried to stick to the spirit of the original question. Get a different result? Found a mistake? Feel free to let me know.

Not easy

All these options are over 75km, and none are easy!

Data source: © Crown copyright and database rights 2023. © OpenStreetMap and contributors

Want the line file to play around with? It's in this folder, in geojson, gpkg, gpx and shp formats. File name is longest-straight-line-candidate followed by the various file extensions for each type.

Interactive map version of my original line 

How could you systematically, definitively find the longest possible route? With some difficulty but it could be done. My map above involved converting OS open roads to polygons, then vertices then connecting vertices then measuring the longest lines and so on. It's not a trivial problem to solve with 100% accuracy, at least not for me.

Wednesday, 28 December 2016

Interactive Terrain Mapping in QGIS

During last year's winter break I did a little animation of Arctic sea ice, which eventually ended up being shown in slightly modified format in Svalbard Museum - and they even sent me a nice hat for my efforts. This year's spare time project was to look a bit more at terrain mapping in QGIS, with a purpose. Since I'm a Highlander in exile I dream of the hills quite a lot, and the mountains in particular. If you follow mountain weather reports for the Highlands, you'll hear of fairly regular avalanches. They may not have the ferocity of avalanches elsewhere in the world, but they can be deadly. The Scottish Avalanche Information Service do a great job of recording, documenting, mapping and educating here so I thought I'd add this data to some 3D interactives using Minoru Akagi's Qgis2threejs QGIS plugin and Ordnance Survey Open Data. If that sounds like gobbledygook to you, never mind - just look at the little animation below to see one of the outputs. The red dots are the locations of recorded avalanches since 1991. You can also play around with it in your web browser - it's quite good fun.


Avalanche data is from the Scottish Avalanche Information Service

I extracted the avalanche data from the SAIS webmap and just plotted the locations in QGIS. I decided to focus on an area I know relatively well - the Cairngorm mountain area. It's not high by world standards, at 1,245m/4,085 feet (6th highest in the UK), but at 57 degrees north and nothing much protecting it from the fierce winter weather, conditions up there can be extreme. In fact, I remember growing up sitting many times on the old chairlift as it swung in the wind! Not for the faint hearted. Anyway, as is the way with these things, the area I wanted to look at was split between map tiles, as you can see below in this QGIS screenshot.

I think there must be a name for the law of split map tiles

Methods-wise, I just patched together a few map tiles, clipped out the area I wanted to focus on, overlaid the avalanche location data, generated a hillshade with an azimuth of 180 and elevation of 27 (to simulate shadows at noon on 26 December), and then added some Ordnance Survey map tile data on top of it. I also did a version with Google satellite imagery, just to see what kind of result I could get. I've posted a little gif of the satellite version below, followed by more of the images. You can also play around with a smaller version of this in your web browser. It should also work on phones, but you might have to use three fingers to pan around.

This uses Google satellite imagery - interactive here

What you'll see below in the next five images are just screenshots from the final interactive from my web browser. You should see them in larger size if you click on them. I've modified some of the settings slightly but basically it's quite an easy thing to do in QGIS with the Qgis2threejs plugin. The updated document on this (18 Oct 2016) is also available in a single, handy pdf. If you want to use this tool, you'll find lots of useful tips in here.


Full view of the final interactive - looking north

Focused on Loch Avon (pronounced Loch A'an) 

This shows the main areas of avalanche activity in one view 

This is a view with the Lairig Ghru pass in the foreground

Another more vertical view of the southern part of the area

I like the effect here with the hillshade layer providing some sense of depth and shadow. There is no vertical exaggeration here and in the interactive version you can zoom in quite far before it becomes pixellated. I also wanted to see what kind of quality I could get by adding in a Google satellite layer and then exporting that. For this, I used the OpenLayers plugin in QGIS (though QuickMapServices is also really good for this). The final version was pretty big as I exported at a high resolution, but here are some snapshots.

The full view of the satellite version - click to enlarge

Loch Etchachan - watch this video!

A different view, showing a cluster of recorded avalanches

The resolution here is actually quite good

This is a view from 'over the back' of the ski area

One final overview image, just for completeness

As I said above, I also did a little extract of this focusing on a smaller area around Loch Avon. The satellite imagery changes part way along the loch but it's still quite pleasing. The interactive version works pretty well in the browser and the image quality here is also pretty good (I exported this at 200% in the Qgis2threejs settings).


Loch Avon - a nice little video from March 2016

It's not always winter here - take a look

Along the way, I almost forgot why I started to map this - the purpose was to plot some interesting and important data on top of the terrain in order to try to understand more about which areas are particularly prone to avalanches. Along the way, I learned more about Mike Spencer's snow hydrology research and PhD (more on that here) and found out some stuff that will help me update my teaching material on this topic. I did begin to look at the Ben Nevis area as well (below), but I decided to focus on the Cairngorms instead this time.

25 years of avalanche locations (red dots) for the Nevis Range

That's all for now. Thanks to the kind of data collected by the SAIS, Ordnance Survey Open Data, and great tools like QGIS and Qgis2threejs, it's becoming much easier to explore, analyse, visualise and understand important datasets. That is kind of what I was attempting here, as a little holiday experiment.

Thanks for reading.


Map data: Contains OS data © Crown copyright and database right 2016

Sunday, 20 March 2016

Ben Nevis 'gains a metre'

The news that the UK's highest mountain has 'gained a metre' was relatively big news here this week. It just so happens that this coincided with me teaching a bit of 3D GIS last Friday. So, since I'm a) Scottish, b) interested in mountains and c) have a hard drive with all the data on it, this means that I did of course have to produce a 3D animation of Ben Nevis using Ordnance Survey Open Data. So, here's what I've done, just for fun. Unlike some other mountains, and as some news outlets suggested, Ben Nevis didn't grow, it has just been measured with more accurate equipment by the national mapping agency. In global terms it's still a bit of a midget (now 1,345 metres compared to 1,344 on the old maps) but at nearly 57 degrees north and with nothing to shelter you from a few thousand miles of North Atlantic weather, it's not to be underestimated!



I also did an interactive version if you want to explore it yourself. For this, I used the excellent Qgis2threejs plugin created by Minoru Akagi. It might load a little slowly in your browser but it should work okay and allow you to explore the terrain. It works pretty well for me, even on my phone.

Try it yourself here


I also extracted a few static images from ArcScene, as you can see below. If you do happen to visit, you'll be lucky if you actually see Ben Nevis as it's normally shrouded in cloud but on a good day you can see it towering over Fort William in all its glory. If you want to see some really great Scottish mountain imagery, check out Iain Cameron on twitter.

Fort William, a few miles away, is just about at sea level

You can get a sense of the scale of the Nevis massif here

The view from 'over the back', looking down towards Fort William


For a much more beautiful video take a look at this view of the Càrn Mòr Dearg arête in winter. Absolutely stunning. 

The video from this point is really fantastic

Technical details: I did this in ArcScene using the following OS layers: Terrain 50, tile NN17 and OS VectorMap District (Raster), also tile NN17. There is no vertical exaggeration on the elevation. I just enabled animated rotation and then set it spinning quite slowly and then used the Animation toolbar in ArcScene to record the view. I then exported this (to the default avi) and then uploaded to YouTube and added some titles.