我使用shapely和python将多边形划分为更小的部分:梯形,平行四边形,边平行于x轴的三角形和边平行于x轴的三角形。初始数据来自.gds文件,并以元组x,y坐标列表的形式呈现。出于我的目的,我使用了像所描述的here这样的差分方法。但是当多边形有holes时,我会得到堆栈,例如:
from shapely.geometry import Polygon
points = [(0.0, -1.0), (0.0, 2.0), (3.0, 2.0), (3.0, 1.0), (1.0, 1.0), (1.0, 0.0), (2.0, 0.0), (2.0, 1.0), (3.0, 1.0), (3.0,-1.0)]
poly = Polygon(points)
print(poly.is_valid)
#Self-intersection at or near point 2 1
#False所以问题是什么是最简单的方法,如何将这个点列表转换为外壳和洞,以便正确地创建多边形?
谢谢!
发布于 2017-02-04 04:06:43
这方面的标准技巧是使用poly.buffer(0) (请参阅the shapely manual)。
polyb = poly.buffer(0)
print(shapely.geometry.mapping(polyb))
# {'type': 'Polygon', 'coordinates': (((0.0, -1.0), (0.0, 2.0), (3.0, 2.0), (3.0, 1.0), (3.0, -1.0), (0.0, -1.0)), ((2.0, 1.0), (1.0, 1.0), (1.0, 0.0), (2.0, 0.0), (2.0, 1.0)))}
print(polyb.is_valid)
# Truehttps://stackoverflow.com/questions/42025349
复制相似问题