查找顶点为lat, lon的多边形质心



试图用Shapely找到多边形的质心,但我不明白如何告诉库顶点是(lon, lat)。也许我需要设置一个投影?

下面是我的代码:
from shapely.geometry import Polygon, Point
# LON, LAT
vertices = [
Point(-79.8726944444444, 8.68505555555556),
Point(-79.8733888888889, 8.50419444444444),
Point(-79.54552777777779, 8.68386111111111),
Point(-79.54622222222221, 8.503),
Point(-79.8726944444444, 8.68505555555556),
]
p1 = Polygon(vertices)
centroid = p1.centroid
print(centroid)
# POINT (-1805804163717.8823 6592764402.930745) 

结果显然是错误的。

这是一个简并形状。它是自己交叉的领结。shape不能处理这个

shape允许多边形在某一点将自身缩小到零宽度。如果您通过包含中心点来重新组织数据,它就可以工作。

import matplotlib.pyplot as plt
from shapely.geometry import Polygon, Point
# LON, LAT
vertices = [
(-79.8726944444444, 8.68505555555556),
(-79.8733888888889, 8.50419444444444),
(-79.7091111111111, 8.594625),
(-79.54622222222221, 8.503),
(-79.54552777777779, 8.68386111111111),
(-79.7091111111111, 8.594625),
(-79.8726944444444, 8.68505555555556)
]
p1 = Polygon(vertices)
print(p1)
print(p1.area)
print(p1.bounds)
print(p1.centroid)
plt.plot( [v[0] for v in vertices], [v[1] for v in vertices] )
plt.show()

最新更新