This page explains and demonstrates with working scripts how to read Ordnance Survey Terrain 50 (OST50) elevation data for use in an on-line map or similar geo-app.
OST50 can be obtained from Ordnance Survey free for use under their Open Data Terms & Conditions:
The area covered is all of England, Scotland, & Wales including offshore islands, but not including Northern Ireland, nor, unlike its predecessor Landform Panorama, the Isle Of Man. OST50 data comes as ASCII text files called tiles, each of which begins with five header lines giving details about the data lines that follow. There are 200 data lines each with 200 columns, separated by spaces, of floating point numbers representing elevation, giving a 200 x 200 grid with 50m spacing. Note that, unlike the SRTM and OS's earlier Landform Panorama datasets, the edge rows and columns do NOT overlap and replicate the edge rows & colummns of neighbouring tiles. Also unlike SRTM and LP, the coordinates mark the centre of each cell, not the SE corner of it. Note also that within each tile the data is stored in row-major order (x easting within y northing), and, like an image, from the top of the tile to the bottom.
Some points to note about using Terrain 50 in this manner:
|Terrain 50||All Files||ASCII tiles only||OS Zips|
Ordnance Survey Terrain 50 Tile Map
These are two versions of fundamentally the same underlying Python script. The first is designed to run as a standard CGI script on a webserver, the second as a Google App. Both use OST50 tiles, which by default are expected to be in a sub-directory called OST50 beneath that where the script is hosted. Debugging output is HTML5 compliant and requires two other files, an HTML template, which varies at least slightly from script to script and therefore is named after the script to distinguish it from others1, and an invariant CSS stylesheet, by default named Minimal.css.
1 As it happens, here the only difference between these two HTML templates is the line that loads the stylesheet.
Obviously, in both cases these defaults can be changed to suit individual requirements, but the scripts (for the OST50 data and template paths) and/or the templates (for the stylesheet path) then must be edited accordingly.
Both scripts are self-documented. Instructions for use are included as leading comments, and also are displayed in response to errors in calling them.
If using one of these scripts, and scarcity of available server or web space so demands, the tiles can be left zipped, as, when looking for a tile, the scripts will look first for an unzipped version of it, but if that is not found they will then look for a zipped version. Inevitably however, leaving the data in its original zips will slow things down a little.
A profile point is returned for each intermediate OST50 cell up to 100 (the empirically determined maximum realistically displayable using a URL to the Google Charts API to draw the profile). If a profile path crosses more than 100 cells, then each of the 100 points returned is the average, or maximum if given the mx switch, of the cells crossed by the path that are closest to that sample point - that is, the profile returned is derived from all the cells it crosses, not just a sample of them.
The profiles returned follow a straight line on an OS map, and therefore will not normally be an accurate geodesic.
For both script / template versions the stylesheet lists as follows:
It is beyond the scope of this article to document and support CGI. If required,
novices are referred to their webhost's online documentation and support for how to enable
Python CGI on their webservers. This article merely gives the basics for setting
up this CGI script. However, two points to note are to ensure that the
'shebang', the first commented line containing the path to the Python interpreter, is
correctly set, and that on Linux-hosted systems the execute attribute is set on the script
file, for example by using
chmod +x <path to file>.
By default the script would normally be put in the /cgi-bin directory off the site root. The default expected directory structure is therefore …
/cgi-bin/OST50/<*.asc or *.zip OST50 data files in their original directory layout>
By default this is expected to be in the directory /cgi-bin off site root. The webserver CGI version lists as follows:
By default this is expected to be in the directory /Resources/Templates off site root and have the same base name as the Python script with an .html extension. The webserver CGI version lists as follows:
Right-click this link and choose Save As for a zip of the three files required for Server-side CGI: OST50ServerCGI.zip
It is beyond the scope of this article to document and support Google Apps. Novices are referred to Google's online documentation and the self-help groups they host. This article merely gives the basics for setting up this Google App, assuming you have already created a default app called <tag>ost50 where <tag> is an id of your own choosing.
You may wish to investigate using the app's free quota, 5GB at the time of writing, of Google Cloud Storage (GCS) to store the tiles, so that they can be shared between app versions rather than having a separate copy of invariant data for each one. However, GCS is significantly slower than local file storage, particularly where a path goes over a sea area with no tiles, though this particular case could probably be significantly improved by adjusting retry parameters.
In addition to the default contents of the app's app.yaml file, it will
will need to import both the jinja2 and webapp2 libraries,
and to declare a static handler for the CSS file, as follows:
- name: jinja2
- name: webapp2
- url: /Resources/(.*\.css)
By default the app script would normally be put in the root directory of the app space, giving a default expected directory structure of …
/OST50/<*.asc or *.zip OST50 data files in their original directory layout>
By default this is expected to be in the root directory of the Google App space. The Google App version lists as follows:
By default this is expected to be in the directory /Resources/Templates off root of the Google App space and have the same base name as the Python script with an .html extension. The Google App version lists as follows:
Right-click this link and choose Save As for a zip of the three files required for a Google App: OST50GoogleApp.zip
Unfortunately, the only convenient way that I know of displaying a profile, the Google Static Image Charts API, has been deprecated since 2012. As per this discussion in their forum, they have made soothing noises, but these remain unofficial, and there is still no definite word on any possible replacement. If it were to go, this wouldn't be the first time that they've broken my work in this manner, and, as I don't suppose it will be the last, ideally what is needed is some sort of standard library that can be installed on one's own site, but I don't know of anything remotely comparable to the ease of just feeding Google a suitable URL.
However, at the time of writing in June 2015, it's still running, so here's how to use the service. It really is as simple as taking the URL returned by either Python script, replacing ~Width~ and ~Height~ by suitable dimensions (pixels), ensuring that the two when multiplied together come to less than Google's limit of 300,000, and then making the result the src attribute of an img tag in your web-page. Here's a demo profile between the Outer Hebrides and St Kilda, which also demonstrates the lack of a flat sea discussed above …
var imgWidth = 650;
var imgHeight = 250;
function callBack( data )
profData = data;
profData.url = profData.url.replace("~Width~",""+ imgWidth +"").replace("~Height~",""+ imgHeight +"");
var img = document.createElement("img");
img.id = "ProfImg"
img.style.width = imgWidth + "px"
img.style.height = imgHeight + "px"
img.src = profData.url
document.getElementById("ProfDiv").appendChild( img );