【问题标题】:Add coordinates to points after changing a df CRS in Geopandas在 Geopandas 中更改 df CRS 后向点添加坐标
【发布时间】:2019-07-14 12:11:08
【问题描述】:

基本上这个想法是在不使用 Qgis 的情况下自动化一些工作流程。

我未能获得与 Geopandas 中 Qgis 功能“向点添加坐标”类似的结果,该功能允许您在当前投影中获取点的 x、y 坐标并为表格创建新属性。

所以我有一组我玩过的点。原始 shapefile 的 CRS 是 epsg 2154 (Lambert 93)。我需要以与 Google 地图兼容的格式获取经纬度。

Google 将 epsg 3857 用于 Google 地图。

points = pd.DataFrame({'Id': ['001', '002', '003'],'geometry': ['POINT (909149.3986619939 6881986.232659903)', 'POINT (909649.3986619939 6882486.232659903)', 'POINT (909149.3986619939 6882486.232659903)']})

这个想法是切换到 epsg 3857 (wgs84),然后从那里创建用 wgs84 坐标填充的纬度/经度列,例如 47.357955,1.7317783。

所以我所做的是明显改变了 CRS: pointswgs84 = points.to_crs(espg=3857)

然后

pointswgs84['lon'] = pointswgs84.geometry.x

pointswgs84['lat'] = pointswgs84.geometry.y

但是我的纬度/经度列随后会填充与原始点数据框对应的坐标:

points = pd.DataFrame({'Id': ['001', '002', '003'],'geometry':['POINT (909149.3986619939 6881986.232659903)', 'POINT (909649.3986619939 6882486.232659903)', 'POINT (909149.3986619939 6882486.232659903)'],'long': ['6881986.232659903', '6882486.232659903', '6882486.232659903'], 'lat': ['909149.3986619939', '909649.3986619939', '909149.3986619939']})

看起来我在这里遗漏了一些东西,但由于我对 Python 和 Geopandas 比较陌生,所以我不确定是什么......

感谢您的帮助。

【问题讨论】:

标签: python pandas gis qgis geopandas


【解决方案1】:

你可以试试:

import geopandas as gpd
from shapely.geometry import Point

points = pd.DataFrame({'Id': ['001', '002', '003'],
                       'geometry': [Point(909149.3986619939, 6881986.232659903), Point(909649.3986619939, 6882486.232659903), Point(909149.3986619939, 6882486.232659903)]})

gdf = gpd.GeoDataFrame(points, crs={'init': 'epsg:2154'}, geometry=points.geometry) # https://epsg.io/2154
gdf.geometry = gdf.geometry.to_crs({'init': 'epsg:3857'}) # https://epsg.io/3857

print(gdf)

结果:

    Id                                     geometry
0  001   POINT (652238.9846394559 6275490.88115861)
1  002  POINT (653026.8173626668 6276225.689838496)
2  003  POINT (652266.5262486706 6276253.346635176)

【讨论】:

  • 谢谢雷内!我的实际问题是以正确的格式获得lat long。而且我使用了错误的 CRS。我正在寻找类似 X = 275454,5593700000 到 long = 2,4744504077 的东西。但是我使用了错误的 epsg,我应该使用 espg 4326 而不是 3857。谢谢 =)
【解决方案2】:

好的,问题解决了。这是一个简单的 espg 问题。如果这可以帮助像我这样的任何其他新手,这是解决方案:

即使 Google 使用 espg 3857,如果您需要将点的 X、Y 转换为可以利用的东西(例如 url 参数),您也需要使用 espg 4326。

然后你就会以正确的方式获得你的长纬度。赞5.859162,49.003710.

所以:

第 1 步:将原始 shapefile 转换为 espg 4326

pointswgs84 = points.to_crs({'init': 'epsg:4326'})

第 2 步:只需提取 x 和 y 并将它们添加到新列中

pointswgs84['lon'] = pointswgs84.geometry.x

pointswgs84['lat'] = pointswgs84.geometry.y

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 2021-12-19
    • 2020-07-25
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多