【问题标题】:How do I save centroids in a shapefile, together with a geometry?如何将质心与几何图形一起保存在 shapefile 中?
【发布时间】:2019-01-16 21:22:13
【问题描述】:

我计算了一些GeoDataFrame的质心

M["centroid"] = M.centroid

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

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

但司机抱怨我无法将Point 与我的几何图形一起保存。我想没关系,但我想知道使用geopandas

将控制信息与其他地理信息一起存储的最佳方式是什么

【问题讨论】:

    标签: python pandas shapely geopandas


    【解决方案1】:

    因为 GeoPandas 不允许您保存两个几何列(在创建 shapefile 时),我建议将 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")
    

    【讨论】:

    • 小注释:您不需要迭代,因为您可以直接访问 x/y 坐标:M.centroid.xM.centroid.y(或将 M.centroid 保存到临时变量以避免计算两次)
    猜你喜欢
    • 1970-01-01
    • 2020-09-29
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 2018-03-10
    • 1970-01-01
    • 2019-06-10
    相关资源
    最近更新 更多