OS Terrain 50 Elevation Data in Rust & PHP
This page has two demos of my PHP & Rust code to read elevation data from the freely-available Ordnance Survey OS Terrain 50 data which provides elevation data for Great Britain.
The main reason for using OS Terrain 50 data alongside OS maps is that it matches the heights on OS maps almost exactly. And the data gets updated from time to time from regular aerial surveys. But it's not all roses...
The problem with using OS Terrain 50 data
OS Terrain 50 data is published in specific binary formats for consumption by specific software. It's also available as thousands of small ASCII files which are messy and spacing-consuming to deal with. So I have written a small utility in Rust called osterrain50 which compresses all the ASCII data into one efficient binary data file. The file can be read in any language which supports binary file reads. So to get access to the data for use in (say) a web site, do this:
- download the OS Terrain 50 ASCII data zip file
- download my command-line utility application as either: and use it to unzip and convert the downloaded OS data zip file into one binary data file thus: $ ./osterrain50 path/to/downloaded_OS_data_zip_file
- download my PHP data reader class and mini demo on GitHub
- Amend the example code in the mini demo to suit your need
The Rust souce code for the command line application is also available on GitHub. Both the PHP & Rust repos have demo code showing how to to read the binary data file. They are both heavily commented to assist translation to other languages.
The code can also generate infill elevations every 50m between two or more locations (ideal for hike or ride elevation plotting). The Rust code in particular is extremely fast: on my Mac mini M1 it retrieves 18,485 elevations along a line between Niton Down (Isle of Wight) and Dùnan Mòr (Cape Wrath, Scotland) in just under 30ms. The PHP code (as demonstrated on this page) does the job in around 120ms on the same machine, which is pretty good considering it has the overhead of JavaScript calling the PHP code over HTTP.
OS Terrain 50 Demos
Demo 1
Here is a simple demo; try zooming in and mousing over spot heights (or tap-and-dragging them) to reveal the OS Terrain 50 elevation data for that location (a mouse is better here). What's happening is that the map coordinates under the mouse are fed to my code to look up the elevation for that location, which is then displayed in the box. Note how closely the retrieved elevation data matches the spot heights shown on the map.
Easting | Northing | Elevation |
---|---|---|
Demo 2
Click or tap any two points on the map to draw a line between two locations. This will display under the map all the elevation heights for every 50m between the two points. Click a third time to start over again. Try zooming out and clicking two locations a long way apart. The results are returned very rapidly in all cases. I use this code to populate the elevation charts for all the rides on the rides page