Tony's Site

Google Maps Coordinates

How to use Google Maps API with co-ordinates given in National Grid eastings and northings

For my work on the East Cheshire Hospice Christmas Tree Collection website I wanted to include some maps using the Google Maps API. The problem was that my data was held in GB National Grid eastings and northings whereas Google Maps works in latitude and longitude. I needed to find a way of converting.

This was made more complicated because it turns out there is more than one definition of latitude and longitude. This is explained in rather more detail than I ever wanted on the Ordnance Survey website. Google uses WGS84 whereas the National Grid uses OSGB36. If you ignore the difference your conversions can be 100m or so out. Whether this matters depends on your application. To do the job properly you need to do a two step conversion - WGS84 to/from OSGB36 to/from easting/northing.

The solutions below are based upon my limited understanding of the problem and the tools used to solve. I make no claims for elegance or efficiency but they do seem to work. The answers they give are consistent with the Ordnance Survey converter.

If you know of a better answer please contact me.

Client Side

The first solution I found works client side using Javascript functions. The source for this I found here under the heading "client side script to convert from WGS84 LatLon to British National Grid". If anyone can provide an attribution for this I'd be happy to include it. As noted above two steps are needed to make a conversion. Here is some example javascript.

function ENtoLL84(easting, northing) {
// Returns object with attributes lat and lon
     var vlatlng = NEtoLL(easting,northing);
     return OGBToWGS84(vlatlng.lat,vlatlng.lon,0);
}

function LL84toEN(lat, lon) {
// Returns object with attributes east and north
     var vlatlon = WGS84ToOGB(lat, lon, 0);
     return LLtoNE(vlatlon.lat,vlatlon.lon);
}

Server Side

Subsequently I wanted to do this server side in Python. I found the package pyproj but documentation for this seemed a bit thin. Again a two stage conversion is needed. I found help here and here. This is my code:

from pyproj import Proj,transform

v84 = Proj(proj="latlong",towgs84="0,0,0",ellps="WGS84")
v36 = Proj(proj="latlong", k=0.9996012717, ellps="airy", towgs84="446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894")
vgrid = Proj(init="world:bng")

def ENtoLL84(easting,northing):
# Returns (longitude,latitude) tuple
     vlon36, vlat36 = vgrid(easting,northing,inverse=True)
     return transform(v36,v84,vlon36,vlat36)

def LL84toEN(longitude,latitude):
# Returns (easting,northing) tuple
     vlon36, vlat36 = transform(v84,v36,longitude,latitude)
     return vgrid(vlon36,vlat36)