Marisaz
Marisaz

Reputation: 85

Convert UK grid coordinates (X,Y) to latitude and longitude

I have a list of position coordinates given in UK grid reference format (X,Y) and I would like to convert them into latitude and longitude.

I am using OSGridConverter (python library) but is not converting it correctly. For example, one input for one location is X = 517393.6563 and Y = 194035.5469.

from OSGridConverter import grid2latlong
l=grid2latlong('TG 517393.6563 194035.5469')

The above, gives me an error: OSGridConverter error: Invalid grid reference

Therefore, which is wrong, I try:

>>>l=grid2latlong('TG 517393 194035')
>>>(l.latitude,l.longitude)
(52.71367793063314, 1.7297510074170983)

Which ends with a location out in UK, which is not correct. Most probably is due data formats, but I am not sure about how to solve it.

Upvotes: 5

Views: 3454

Answers (2)

Graham Monkman
Graham Monkman

Reputation: 434

Firstly, a definition: OS Grid Grid references (OSGGRs) refer to grid references prepended with the two letter grid identifiers, as explained rather well here.

As you found OSGGRs arn't floating point numbers. See the link above.

Moving on, the numbers for OSGGRs have their origin at the south westerly point of the grid - which is very different from the origin of the OSGB BNG projection (coords are easting and northings). See above link for great explanation.

You seem to be taking the BGGRs numeric portion as an Easting/Northing, which is incorrect.

So, armed with this knowledge, we can use OSGridConverter to convert from OSGGRs to eastings and northings using a conversion to lat long as an intermediary. But note the authors comments on errors at the bottom of the PyPI package page

import OSGridConverter
cvt_wgs84 =  OSGridConverter.grid2latlong('SN5117013189')
print(cvt_wgs84.latitude, cvt_wgs84.longitude)
51.79749210128498 -4.160451113839529

cvt_EN = OSGridConverter.latlong2grid(cvt_wgs84.latitude, cvt_wgs84.longitude)
print(cvt_EN.E,cvt_EN.N)
251116 213191

# Back to OSGGR, however, note the error of tens of meters.
str(cvt_EN)
'SN 51116 13191'

I did a quick 'by-eye' check by-map on the agreement between the OSGGR coords and the lat/long, and the error is in meters. May be acceptable to some.

Upvotes: 0

Jop Knoppers
Jop Knoppers

Reputation: 714

You should probably use something like pyproj:

import pyproj

crs_british = pyproj.Proj(init='EPSG:27700')
crs_wgs84 = pyproj.Proj(init='EPSG:4326')

long, lat = pyproj.transform(crs_british, crs_wgs84, 517393.6563, 194035.5469)
print(lat, long)

In your case this will give: 51.63289090467179, -0.3052119183057834

Upvotes: 5

Related Questions