如何将地理空间坐标dataFrame转换为原生x,y投影?

时间:2017-03-14 03:07:28

标签: python pandas dataframe coordinates geospatial

我有一个以下数据帧,lat和lon是地理坐标系统中的纬度和经度。我试图将这些坐标系转换为原生(x,y)投影。

我已经尝试过单点pyproj,但是如何处理包含数千行的整个数据帧。

       time                 lat        lon 
     0 2011-01-31 02:41:00  18.504273  -66.009332
     1 2011-01-31 02:42:00  18.504673  -66.006225

我想尝试这样的事情:

       time                 lat        lon        x_Projn    y_Projn
     0 2011-01-31 02:41:00  18.504273  -66.009332 resp_x_val resp_y_val
     1 2011-01-31 02:42:00  18.504673  -66.006225 resp_x_val resp_y_val
     and so on...

以下是我尝试使用lat / lon到x,y系统的代码:

      from pyproj import Proj, transform

      inProj = Proj(init='epsg:4326')
      outProj = Proj(init='epsg:3857')
      x1,y1 = -105.150271116, 39.7278572773
      x2,y2 = transform(inProj,outProj,x1,y1)
      print (x2,y2)

输出:

      -11705274.637407782 4826473.692203013

感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:1)

不幸的是,pyproj只能逐点转换。我想这样的事情应该有效:

import pandas as pd
from pyproj import Proj, transform

inProj = Proj(init='epsg:4326')
outProj = Proj(init='epsg:3857')

def towgs84(row):
    return pd.Series(transform(inProj, outProj, row["lat"], row["lon"]))

wsg84_df = df.apply(towgs84, axis=1)  # new coord dataframe with two columns

答案 1 :(得分:0)

您可以遍历pandas数据框中的行,转换每行的经度和纬度值,用第一和第二坐标值创建两个列表,然后将这些列表变成数据框中原始的新列。也许不是最漂亮,但这为我完成了工作。

from pyproj import Proj, transform

M1s = [] #initiate empty list for 1st coordinate value
M2s = [] #initiate empty list for 2nd coordinate value

for index, row in df.iterrows(): #iterate over rows in the dataframe
    long = row["Longitude (decimal degrees)"] #get the longitude for one row
    lat = row["Latitude (decimal degrees)"] #get the latitude for one row
    M1 = (transform(Proj(init='epsg:4326'), Proj(init='epsg:3857'), long, lat))[0] #get the 1st coordinate
    M2 = (transform(Proj(init='epsg:4326'), Proj(init='epsg:3857'), long, lat))[1] #get the second coordinate
    M1s.append(M1) #append 1st coordinate to list
    M2s.append(M2) #append second coordinate to list

df['M1'] = M1s #new dataframe column with 1st coordinate
df['M2'] = M2s #new dataframe columne with second coordinate