﻿ Convert between Latitude/Longitude & OS National Grid Reference points

# Movable Type Scripts

## Convert between Latitude/Longitude & OS National Grid References

Some people have asked me about converting between latitude/longitude & Ordnance Survey grid references. The maths is extraordinarily complex (and way beyond me!), but the Ordnance Survey explain the resulting formulae very clearly in Annex C of their Guide to coordinate systems in Great Britain.

OS Grid References are based on 100km grid squares identified by letter-pairs, followed by digits which identify a sub-square within the grid square, as explained on the OS Interactive Guide to the National Grid. 6-digit references identify 100m grid squares; 8 digits identify 10m grid squares, and 10 digits identify 1m squares. TG51401317 represents a 10m box with its (south-west) origin 51.40km across, 13.17km up within the TG square.

Functional demo

Enter OS grid references or latitude/longitude values into the test boxes to try out the calculations:

 OS Grid Ref ≡ Lat/Lon (WGS84) (SW corner of grid square) Lat/Lon (OSGB36) no longer used (since 2014)

As the surface of the earth is curved, and maps are flat, mapping involves a projection of the curved surface onto a flat surface. The Ordnance Survey grid is a transverse Mercator projection.

Note that, at a fine level of accuracy, there are different ways of measuring latitude & longitude. Ordnance Survey grid references are based on ‘OSGB-36’, which is based on an ellipsoidal model of the earth’s surface which is a good fit to the UK. GPS systems generally use the world-wide ‘WGS-84’, based on an ellipsoidal model which is a best approximation to the entire earth. At Greenwich, these differ by about 126m, which is why the Greenwich meridian is not at 0° longitude (they coincide somewhere in the Atlantic ocean; there’s more on Wikipedia). However, the while grid references remain based on OSGB-36, the Ordnance Survey no longer use OSGB-36 latitudes & longitudes, so it is now of purely historical interest (yeah, pretty confusing). I have written some separate notes on converting between OSGB-36 & WGS-84.

Note also that an alternative way of expressing OS Grid References is as all-numeric eastings and northings. As square TG is six squares across, three squares up within the grid, grid reference TG 5140 1317 can also be expressed as 65140,31317.

Aside from the transformation maths, the other tricky bit of the script is converting grid letter-pairs to/from numeric eastings & northings. To follow what’s going on, it is worth noting that the letter-pairs define a 5x5 grid of 5x5 sub-grids; the eastings & northings work from a ‘false origin’ at grid square SV, which is displaced from grid square AA by 10 squares E, 19 squares N, with the northing axis inverted; and letter ‘I’ is skipped. OS Grid References apply to the UK only.

For other scripts for calculating distances, bearings, etc between latitude/longitude points, see my Lat/Long page. I have also written a script for calculating distances between OS grid reference points.

Display calculation results as: deg/min/sec decimal degrees

See below for the source code of the JavaScript implementation, also available on GitHub. Note I use Greek letters in variables representing maths symbols conventionally presented as Greek letters: I value the great benefit in legibility over the minor inconvenience in typing (if you encounter any problems, ensure your `<head>` includes `<meta charset="utf-8">`).

With its untyped C-style syntax, JavaScript reads remarkably close to pseudo-code: exposing the algorithms with a minimum of syntactic distractions. These functions should be simple to translate into other languages if required, though can also be used as-is in browsers and Node.js.

Note that this uses the published formulation due to Thomas, Redfearn, etc; I am presenting this version as it directly implements the Ordnance Survey documentation, but the Krüger formulation (as used in my UTM conversion scripts) is superior for transverse Mercator projections.

I offer these scripts for free use and adaptation to balance my debt to the open-source info-verse. You are welcome to re-use these scripts [under an MIT licence, without any warranty express or implied] provided solely that you retain my copyright notice and a link to this page.

If you need any advice or development work done, I am available for consultancy.

If you have any queries or find any problems, contact me at ku.oc.epyt-elbavom@oeg-stpircs.