我在网上看到缓冲区(0)应该“修复”蝴蝶结。Shapely找到蝴蝶结的交点,但只保留右上角。为了找到解决办法,我试着颠倒我的观点顺序。令人惊讶的是(对我来说),领结的右上角仍然保留着。我不明白。感谢任何帮助。
我想把整个蝴蝶结保留为两个三角形(或者一个六边形——两者都有用)。寻找解决这个“问题”的方法
#!/usr/bin/env python3
from shapely.geometry.polygon import Polygon
bowtie_plot = [(1, 0), (0, 1), (0, -1), (-1, 0)]
bowties = [
Polygon(bowtie_plot),
Polygon(bowtie_plot[::-1])
]
cleaned = [
bowties[0].buffer(0),
bowties[1].buffer(0)
]
print('cleaned[0] exterior = {}'.format(list(cleaned[0].exterior.coords)))
# cleaned[0] exterior = [(0.0, 0.0), (-1.0, 1.0), (1.0, 1.0), (0.0, 0.0)]
print('cleaned[1] exterior = {}'.format(list(cleaned[1].exterior.coords)))
# cleaned[1] exterior = [(0.0, 0.0), (-1.0, 1.0), (1.0, 1.0), (0.0, 0.0)]
# ADDITIONAL INFORMATION BELOW
# here's what shapely *can* do with intersecting lines:
# a star shape made of five intersecting lines and five points
from math import sin, cos, pi
star = Polygon(
[(cos(x*pi*4/5), sin(x*pi*4/5)) for x in range(5)]
).buffer(0)
# after buffering, becomes a star shape made out of ten lines and ten points
# shapely found all intersections and corrected the polygon.
print('list exterior = {}'.format(list(star.exterior.coords)))
经过思考,我能理解为什么蝴蝶结被区别于星星,但我有兴趣找到一个解决办法。
最佳答案
您的领结不是有效的形状Polygon。请阅读该文档以及LinearRing的文档(就在Polygon
文档上方)。尤其要注意有效和无效的例子。
如果您创建这样的蝴蝶结:
In [46]: bt = [(1,0), (0,1), (0,0), (-1,0), (0, -1), (0,0)]
In [47]: poly = Polygon(bt)
然后
LinearRing
返回aMultiPolygon:In [48]: poly.buffer(0)
Out[48]: <shapely.geometry.multipolygon.MultiPolygon at 0x4a40050>