如何计算点要素GeoDataFrame中全部点位集合的整体质心
你当前的代码调用GeoSeries.centroid是对序列中的每个单点分别计算质心,单点的质心就是其本身,因此得到的结果和原始点坐标一致。要计算所有点的整体质心,可参考以下两种方案:
方案1:使用geopandas内置方法计算平面整体质心
先将所有点合并为单个MultiPoint几何对象,再对该整体计算质心:
import geopandas filepath = r'Shiloh.json' gdf = geopandas.read_file(filepath) xyz = gdf['geometry'].to_crs('epsg:3587') # 合并GeoSeries中所有点为一个多点集合 all_points = xyz.unary_union # 计算整体质心 total_centroid = all_points.centroid print(total_centroid)
该方案返回的是二维质心,默认会丢失Z轴高程值。
方案2:手动计算带Z值的三维质心
如果需要保留高程维度的计算结果,直接对所有点的三个维度坐标取平均值即可:
import numpy as np # 提取所有点的坐标 coords_list = [list(point.coords)[0] for point in xyz] coords_arr = np.array(coords_list) # 各维度取平均得到三维质心 centroid_3d = coords_arr.mean(axis=0) print(f"三维整体质心:X={centroid_3d[0]:.3f}, Y={centroid_3d[1]:.3f}, Z={centroid_3d[2]:.3f}")
内容的提问来源于stack exchange,提问作者user3439702
相关产品推荐
相关产品推荐

