使用 Geopandas 将包含多边形坐标的元组保存到 Shapefile

Save Tuple Containing Polygon Coordinates to Shapefile Using Geopandas

我在将包含多边形顶点坐标的元组转换为 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)转换为dataframe;无法通过 shape['coordinates']pd.DataFrame(list(shape)) 对元组进行子集访问坐标。我已经查看了 , and this,但我仍然坚持从元组结构中获取坐标!

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

您应该可以通过读取元组的第一个元素将其转换为 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