OS Terrain 50 Elevation Data in Rust & PHP
This page has two demos of my PHP code to read elevation data from the freely-available Ordnance Survey OS Terrain 50 data which provides elevation data for Great Britain. Rust code is also available.
So why use OS Terrain 50 data when SRTM data is readily available?
- the OS data is provided every 50m as opposed to SRTM's 90m data set
- the OS data has a 4m root mean square error compared with 9.73m RMSE for SRTM (though this figure varies wildly in different publications)
- the OS data matches UK OS map spot heights much more closely
- the OS data is updated annually from regular aerial surveys (SRTM data is basically static)
I should note that worldwide SRTM 30m data now exists. This was only available for the USA when I wrote my SRTM 90m code back in around 2010. I believe the 30m data is based on the original Shuttle data but has been backfilled and mixed with other data sources to provide the increased accuracy. I have no plans to write code for SRTM 30m data because the OS data better fits my needs.
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 called osterrain50 which converts the OS 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 a 2020 Mac mini M1 it retrieved 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 here 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.