Search code examples
pythoncoordinates

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.


Solution

  • 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