簡體   English   中英

如何將質心和幾何一起保存在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")

但是駕駛員抱怨我無法將Point和幾何一起保存。 我想沒關系,但是我想知道使用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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM