如何通过 Shapely 找到与网格对齐的质心?

问题描述 投票:0回答:1

当我使用 shapely 查找质心时,即使我设置了精度,它也会得到小数值:

bbox = shapely.Polygon(coords)
shapely.set_precision(bbox, grid_size=1)
centroid = bbox.centroid
print(f'{centroid}')
# POINT (1125 1348.5)

如何告诉 Shapely 将质心捕捉到网格?在这种情况下,我希望点的每个暗淡四舍五入到最接近的整数。

computational-geometry shapely centroid geos
1个回答
0
投票

在质心上使用

set_precision

import shapely

bbox = shapely.box(0, 0, 9, 9)
centroid = bbox.centroid
print(f"{centroid}")
# POINT (4.5 4.5)

centroid_rounded = shapely.set_precision(centroid, grid_size=1)
print(f"{centroid_rounded}")
# POINT (5 5)
© www.soinside.com 2019 - 2024. All rights reserved.