Skip to content

Instantly share code, notes, and snippets.

@elisehuard
Last active June 21, 2018 07:15
Show Gist options
  • Save elisehuard/10259449 to your computer and use it in GitHub Desktop.
Save elisehuard/10259449 to your computer and use it in GitHub Desktop.
# fixed version of http://altons.github.io/python/2012/12/01/converting-northing-and-easting-to-latitude-and-longitude/
# - filenames
# - data type (pyproj transform doesn't like pandas series)
import os
import pandas as pd
import pyproj
import re
listfiles = os.listdir("codepo/Data/CSV")
pieces = []
columns = ['pstcode','positional_quality_indicator','eastings','northings','country_code','nhs_regional_ha_code',
'nhs_ha_code','admin_county_code','admin_district_code','admin_ward_code']
print listfiles
for f in listfiles:
path = "codepo/Data/CSV/%s" % f
frame=pd.read_csv(path, names = columns)
frame['filename']=f
pieces.append(frame)
postcodes = pd.concat(pieces, ignore_index=True)
sample = postcodes
def proj_transform(df):
#bng = pyproj.Proj(init='epsg:27700')
bng = pyproj.Proj("+init=EPSG:27700")
#wgs84 = pyproj.Proj(init='epsg:4326')
wgs84 = pyproj.Proj("+init=EPSG:4326")
lats = pd.Series()
lons = pd.Series()
for idx, val in enumerate(df['eastings']):
lon, lat = pyproj.transform(bng,wgs84,df['eastings'][idx], df['northings'][idx])
lats.set_value(idx, lat)
lons.set_value(idx, lon)
df['lat'] = lats
df['lon'] = lons
return df
sample2 = proj_transform(sample)
sample2.to_csv('uk-postcodes-latlng.csv',index=False)
@dominijk
Copy link

Resolved through stack overflow, I had some NaN values in the data. More reading on that here for others reference.

I also had an issue with RuntimeError: non-convergent inverse meridional dist

Resolved by code below;

def proj_transform2(df): 
    bng = pyproj.Proj("+init=EPSG:27700") 
    wgs84 = pyproj.Proj("+init=EPSG:4326") 
    arr = map(lambda x: pyproj.transform(bng, wgs84, x[0], x[1]), 
    zip(df['eastings'], df['northings'])) 
    lons, lats = map(array, zip(*arr)) 
    df['lat'] = lats 
    df['lon'] = lons
    return df

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment