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

标签 python pandas shapely geopandas

我计算一些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/

相关文章:

python - 获取位于 Shapely 多边形内的所有格点

python - 如何用不同的 shell 调用 Popen?

python - 我应该如何安装和导入 simpletransformers?

python - 两个椭圆形(椭圆形)相交的面积?

Python:如何在两个数据帧之间进行操作?

python - Pandas:借助字典将变量子字符串从 A 列插入 B 列

python - 使用 Matplotlib 散点图绘制数字而不是点

python - 使用 pandas 查找时间序列数据中缺失的分钟数据

python - 相同的权限,不同的权利?

python - 如何使用 group by 检查数据框列中的值是否在另一个数据框的列中