mapping dom mapping dom - 7 months ago 43
Python Question

Function to convert coordinates in pandas series and append as additional series

I'm looking to take as series of co-ordinates stored in a pandas dataframe and define a function that will go through each entry, transform it (BNG Easting Northing to Lat and Long) and save it to a new column in same row. This function by Elise Huard looks like it should do just this

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['Easting']):
lon, lat = pyproj.transform(bng,wgs84,df['Easting'][idx], df['Northing'][idx])
lats.set_value(idx, lat)
lons.set_value(idx, lon)
df['lat'] = lats
df['lon'] = lons
return df


but im getting the following error once i try to run the function. Any advice on what might be causing it or an alternate approach as work round.

RuntimeError: non-convergent inverse meridional dist


Sample of data used;

Site Reference LA Reference Start Date Easting Northing
0 380500145 NaN 20130101 105175.0 105175.0
1 380500128 NaN 20060331 104000.0 104000.0
2 380500085 NaN 20030401 105055.0 105055.0
3 380500008 NaN 19980930 108480.0 108480.0
4 380500009 NaN 19980930 105415.0 105415.0
5 380500136 SHLAA20100101 105081.0 105081.0
6 380500038 NaN 19980930 105818.0 105818.0

Answer

Assuming pyproj.transform works correctly on single (easting, northing) coordinate pairs, then instead of:

for idx, val in enumerate(df['Easting']):
    lon, lat = pyproj.transform(bng,wgs84,df['Easting'][idx], df['Northing'][idx])
    lats.set_value(idx, lat)
    lons.set_value(idx, lon)

Try:

lons, lats = map(lambda x: pyproj.transform(bng, wgs84, x[0], x[1]),
                 zip(df['Easting'], df['Northing']))

And leave the rest unchanged.

Edit

This works:

arr = map(lambda x: pyproj.transform(bng, wgs84, x[0], x[1]),
          zip(df['eastings'], df['northings']))
lons, lats = map(array, zip(*arr))