【问题标题】:Save Tuple Containing Polygon Coordinates to Shapefile Using Geopandas [closed]使用 Geopandas 将包含多边形坐标的元组保存到 Shapefile [关闭]
【发布时间】:2020-02-01 01:06:03
【问题描述】:

我在将包含多边形顶点坐标的元组转换为 shapefile 时遇到问题。

元组对我来说是一种非常陌生的格式;如果它在数据框中,我可以使用 geopandas 轻松完成。

shape= ({'type': 'Polygon',
  'coordinates': [[(-148.7285301097261, 60.42704276401832),
    (-148.7285301097261, 60.42693172262919),
    (-148.7285856304207, 60.42693172262919),
    (-148.72830802694787, 60.42704276401832),
    (-148.7285301097261, 60.42704276401832)]]},
 1.0)

我无法通过pd.DataFrame(shape) 转换为数据帧;不能子集元组以通过shape['coordinates']pd.DataFrame(list(shape)) 访问坐标。我已经查看了thisthis,但我一直坚持从元组结构中获取坐标!

给定此处显示的结构元组,我如何创建 shapefile(通过 Geopandas)?

【问题讨论】:

标签: pandas tuples shapefile geopandas shapely


【解决方案1】:

您应该能够通过读取元组的第一个元素将其转换为pandas DataFrame

pd.DataFrame(shape[0]).explode('coordinates')

Out[1]: 
      type                               coordinates
0  Polygon   (-148.7285301097261, 60.42704276401832)
0  Polygon   (-148.7285301097261, 60.42693172262919)
0  Polygon   (-148.7285856304207, 60.42693172262919)
0  Polygon  (-148.72830802694787, 60.42704276401832)
0  Polygon   (-148.7285301097261, 60.42704276401832)

如果你需要拆分成xy你可以从系列中取出物品:

df = pd.DataFrame(shape[0]).explode('coordinates').reset_index(drop=True)

df = df.join(df['coordinates'].apply(pd.Series)).rename(columns={0:'x', 1:'y'}).drop('coordinates', axis=1)

Out[2]: 
      type           x          y
0  Polygon -148.728530  60.427043
1  Polygon -148.728530  60.426932
2  Polygon -148.728586  60.426932
3  Polygon -148.728308  60.427043
4  Polygon -148.728530  60.427043

【讨论】:

    猜你喜欢
    • 2013-04-04
    • 2020-09-18
    • 2018-04-22
    • 1970-01-01
    • 2021-04-01
    • 2013-01-24
    • 2020-09-04
    • 2014-10-25
    相关资源
    最近更新 更多