繁体   English   中英

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

[英]How to convert geospatial coordinates dataFrame to native x,y projection?

我有一个以下数据框,纬度和经度是地理坐标系中的纬度和经度。 我正在尝试将这些坐标系转换为原生 (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

感谢您提供任何帮助。

不幸的是, 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

您可以遍历 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM