匀称polygon.buffer(0)“失去”一半我的领结
-
22-12-2019 - |
题
我在网上看到了缓冲区(0)应该“修复”bowties。匀称地找到蝴蝶结的交叉点,但只能保持右上部分。寻找一个解决方法,我试图扭转我的点的顺序。令人惊讶的是(对我来说),Bowtie的相同右上部分仍然是一体的。我不明白。任何帮助感谢。
我想将整个蝴蝶结保留为两个三角形(矿石一个六面多边形 - 无论是有用的)。寻找这个“问题”的解决方法。
#!/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)))
.
思考后,我可以理解为什么一个蝴蝶结比星星不同,但我很兴趣找到解决方法。
解决方案
你的bowtie不是一个有效的匀称多边形。读取该文档,以及 linearring (就在Polygon
文档之上)。特别地,请注意有效和无效的生成的例子。
如果您创建像这样的蝴蝶结:
In [46]: bt = [(1,0), (0,1), (0,0), (-1,0), (0, -1), (0,0)]
In [47]: poly = Polygon(bt)
.
然后LinearRing
返回a multipolygon :
In [48]: poly.buffer(0)
Out[48]: <shapely.geometry.multipolygon.MultiPolygon at 0x4a40050>
. 其他提示
buffer(0)
并不总是产生多焊剂:
from shapely.wkt import loads
bt = loads('POLYGON ((0 0, 2 2, 2 0, 0 2, 0 0))')<br>
bt.buffer(0).wkt
produces 'POLYGON ((1 1, 2 2, 2 0, 1 1))'
. 不隶属于 StackOverflow