Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dominijk/d8fc70a7999f4ee9a0b47473a92785ce to your computer and use it in GitHub Desktop.
Save dominijk/d8fc70a7999f4ee9a0b47473a92785ce 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
#getting errrors with this section, resolved through discussions in link below
#http://stackoverflow.com/questions/36851845/function-to-convert-coordinates-in-pandas-series-and-append-as-additional-series
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
Author

Updated code as comments sections on the original, fixed by community via StackOverflow

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