Converting UK OS Grid References to Latitude and Longitude

As covered in my last post I've been looking at location based services for mobile devices. In that post I discussed how to find a set of nearest points of interest based on the latitude and longitude reported by an Android phone.

I also mentioned, for my example, I was looking at UK rail stations. The names and Ordnance Survey grid references of stations are published as part of the NaPTAN data set available through data.gov.uk.

GPS devices, including those in mobile phones, work in terms of Latitude and Longitude and not OS grid references. I could of course have used OS grid references as part of my nearest location algorithm, however since the aim is to work for mobile devices using GPS, then I would still need to convert back and forth between latitude and longitude and OS grid references. Therefore, I decided to do the conversion up by pre-processing the NaPTAN data.

There is an additional problem, however. It turns out that using latitude and longitude is not quite as straight-forward as one might imagine. For accuracy you need to know which geodetic system you are working with. Geodetic systems differ since they use different ellipsoid models to compensate for the fact that the earth is not a perfect sphere.

In particular, GPS uses WGS-84 which is a system that provides good overall accuracy across the globe (since it's a global system), however the UK Ordnance Survey use OSGB36 which provides better accuracy in the UK. A latitude and longitude coordinate pair in WSG-84 can actually be over 100 metres from the same coordinate pair in OSGB36.

After some research I found a couple of really useful pages by Chris Veness. One that converts UK OS grid references to OSGB36 latitude and longitude coordinates, and another that converts OSGB36 coordinates to WSG-84.

I translated these to Java (they are originally in JavaScript) and used them to convert the station data, I plugged the data into my kd-tree and then I could search for stations based on a given WSG-84 latitude and longitude pair. It was very satisfying to see the stations displayed in the correct position within Google Maps.

This site uses cookies. Continue to use the site as normal if you are happy with this, or read more about cookies and how to manage them.

X