gpt4 book ai didi

python - 如何将质心与几何图形一起保存在 shapefile 中?

转载 作者:行者123 更新时间:2023-12-01 01:17:21 25 4
gpt4 key购买 nike

我计算一些GeoDataFrame的质心

M["centroid"] = M.centroid

现在,我想将该列保存在 shp 文件中,然后执行

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

但司机提示我无法将与我的几何图形一起保存。我想这没问题,但我想知道使用 geopandas

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

最佳答案

因为 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")

关于python - 如何将质心与几何图形一起保存在 shapefile 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54200106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com