如何将质心与几何一起保存在 shapefile 中?
How do I save centroids in a shapefile, together with a geometry?
我计算了一些GeoDataFrame
的质心
M["centroid"] = M.centroid
现在,我想将该列保存在 shp
文件中,然后
M[["centroid","geometry"]].to_file("mfile.shp")
但是 driver 抱怨我无法将 Point
与我的几何图形一起保存。我想没关系,但我想知道使用 geopandas
将 controid 信息与其他地理信息一起存储的最佳方法是什么
因为 GeoPandas 不允许您保存两个几何列(创建 shapefile 时),我建议将 xy 坐标保存到每个质心的列中。从那里你总是可以很容易地得到 shapely.Point.
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")
我计算了一些GeoDataFrame
M["centroid"] = M.centroid
现在,我想将该列保存在 shp
文件中,然后
M[["centroid","geometry"]].to_file("mfile.shp")
但是 driver 抱怨我无法将 Point
与我的几何图形一起保存。我想没关系,但我想知道使用 geopandas
因为 GeoPandas 不允许您保存两个几何列(创建 shapefile 时),我建议将 xy 坐标保存到每个质心的列中。从那里你总是可以很容易地得到 shapely.Point.
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")