Python OSGB to WGR84

By | May 27, 2016

OSGB is the Ordnance Survey National Grid and is used for many grid references in the United Kingdom. This grid system works well for locations within the United Kingdom, but needs work if you need to combine coordinates from another system, or perhaps plot locations on open streetmap. In this post I look at python OSGB to WGS84 conversion.

Converting between OSGB and WGS84 is possible, but it is rather involved. Thankfully there are already some Python OSGB to WGR84 conversion modules that we can use.

Use the bng-latlon module

The OSGB grid system is also known as the British National Grid (BNG). The bng-latlon module, originally written by Hannah Fry, takes the hard work out of converting between OSGB and WGS84.

You can install the bng-latlon module with

pip install bng_latlon

and use it with:

from bng_to_latlon import OSGB36toWGS84
OSGB36toWGS84(538890, 177320)

which gives the output

(51.47779538331092, -0.0014016837826672265)

It is also possible to convert from WGS84 back to OSGB with:

from latlon_to_bng import WGS84toOSGB36
WGS84toOSGB36(51.4778, -0.0014)

which gives the output as OSGB coordinates:

(538890.1053365842, 177320.49650700082)

See how the bng-latlon package can be used to plot publicly available data.

Dealing With OSGB Letter Codes

One of the features (peculiarities?) of the OBGR system is that it can be represented by both letter or numbers. The bng-latlon package doesn’t account for these letter codes, so you may need to use a different module for Python OSGB to WGR84 conversion such as pyproj, or osgb.

Image: By cmglee, Strebe, MansLaughter, Alexrk2 from naturalearthdata, Pethrus and nandhp, CC BY-SA 3.0