3

我在网上看到 buffer(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)))

经过思考,我可以理解为什么领结与明星的待遇不同,但我有兴趣找到解决方法。

4

2 回答 2

2

您的领结不是有效的形状Polygon。阅读该文档和LinearRing的文档(就在Polygon文档上方)。特别要注意 valid 和 invalidLinearRing的例子。

如果您像这样创建领结:

In [46]: bt = [(1,0), (0,1), (0,0), (-1,0), (0, -1), (0,0)]

In [47]: poly = Polygon(bt)

然后buffer(0)返回一个MultiPolygon

In [48]: poly.buffer(0)
Out[48]: <shapely.geometry.multipolygon.MultiPolygon at 0x4a40050>
于 2014-01-11T07:16:48.417 回答
0

buffer(0)并不总是产生 MultiPolygon:

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))'
于 2014-04-29T04:00:41.643 回答