在Pandas数据框架中使用pyproj在投影之间进行转换



这无疑是一个"只见树木不见森林"的时刻。我盯着这段代码看了一个小时,还是看不出我做错了什么。我知道它就在我眼前,但我就是看不见!

我正在尝试使用Python在两个地理坐标系统之间进行转换。

我有经度(x轴)和纬度(y轴)值,想转换为OSGB 1936。对于单个点,我可以执行以下操作:

import numpy as np
import pandas as pd
import shapefile
import pyproj
inProj = pyproj.Proj(init='epsg:4326')
outProj = pyproj.Proj(init='epsg:27700')
x1,y1 = (-2.772048, 53.364265)
x2,y2 = pyproj.transform(inProj,outProj,x1,y1)
print(x1,y1)
print(x2,y2)

生成以下内容:

-2.772048 53.364265
348721.01039783185 385543.95241055806

这似乎是合理的,并表明经度-2.772048被转换成坐标348721.0103978。

实际上,我想在Pandas数据框架中这样做。dataframe包含包含经度和纬度的列,我想添加另外两个包含转换后的坐标的列(称为newLong和newLat)。

一个典型的数据框架可能是:

    latitude  longitude
0  53.364265  -2.772048
1  53.632481  -2.816242
2  53.644596  -2.970592

我写的代码是:

import numpy as np
import pandas as pd
import shapefile
import pyproj
inProj = pyproj.Proj(init='epsg:4326')
outProj = pyproj.Proj(init='epsg:27700')
df = pd.DataFrame({'longitude':[-2.772048,-2.816242,-2.970592],'latitude':[53.364265,53.632481,53.644596]})
def convertCoords(row):
    x2,y2 = pyproj.transform(inProj,outProj,row['longitude'],row['latitude'])
    return pd.Series({'newLong':x2,'newLat':y2})
df[['newLong','newLat']] = df.apply(convertCoords,axis=1)
print(df)

生产:

    latitude  longitude        newLong         newLat
0  53.364265  -2.772048  385543.952411  348721.010398
1  53.632481  -2.816242  415416.003113  346121.990302
2  53.644596  -2.970592  416892.024217  335933.971216

但是现在看起来newLong和newLat值混淆了(与上面所示的单点转换结果相比)。

我把我的电线交叉在哪里产生这个结果?

当您执行df[['newLong','newLat']] = df.apply(convertCoords,axis=1)时,您正在索引df.apply输出的列。但是,列的顺序是任意的,因为您的序列是使用字典定义的(本质上是无序的)。

您可以选择返回具有固定列顺序的Series:

return pd.Series([x2, y2])

或者,如果您想保持convertCoords输出标记,那么您可以使用.join来组合结果:

return pd.Series({'newLong':x2,'newLat':y2})
...
df = df.join(df.apply(convertCoords, axis=1))

请注意,pyprojtransform函数也接受arrays,这在处理大数据帧时非常有用,并且比使用lambda/apply函数要快得多

import pandas as pd
from pyproj import Proj, transform
inProj, outProj = Proj(init='epsg:4326'), Proj(init='epsg:27700')
df['newLon'], df['newLat'] = transform(inProj, outProj, df['longitude'].tolist(), df['longitude'].tolist())

最新更新