代码之家  ›  专栏  ›  技术社区  ›  Dervin Thunk

如何将质心与几何体一起保存在形状文件中?

  •  1
  • Dervin Thunk  · 技术社区  · 7 年前

    我计算了一些 GeoDataFrame

    M["centroid"] = M.centroid
    

    现在,我想将该列保存在 shp 文件,然后做

    M[["centroid","geometry"]].to_file("mfile.shp")
    

    但是司机抱怨我救不了一个 Point 和我的几何。我想没关系,但我想知道在使用其他地理信息的同时存储有争议的信息的最好方法是什么 geopandas

    1 回复  |  直到 7 年前
        1
  •  2
  •   swatchai    7 年前

    因为geopandas不允许保存两个几何列(创建shapefiles时),我建议将xy坐标保存到每个形心的列中。从这一点上你总是可以很容易地得到形状。

    for index, row in M.iterrows():
        centroid_coords = row.geometry.centroid.coords.xy
        M.loc[index, 'cen_x'] = centroid_coords[0][0]
        M.loc[index, 'cen_y'] = centroid_coords[1][0]
    
    M[["cen_x", "cen_y", "geometry"]].to_file("mfile.shp")
    

    根据以下评论进行编辑(谢谢,没有意识到):

    temp = M.centroid
    
    M['cen_x'] = temp.x
    M['cen_y'] = temp.y
    
    M[["cen_x", "cen_y", "geometry"]].to_file("mfile.shp")