Last active
June 21, 2018 07:15
-
-
Save elisehuard/10259449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;