Pergunta

Eu vi online que buffer(0) deveria "consertar" gravatas-borboleta.Shapely encontra o ponto de intersecção da gravata borboleta, mas mantém apenas a parte superior direita.Procurando uma solução alternativa, tentei inverter a ordem dos meus pontos.Surpreendentemente (para mim), a mesma parte superior direita da gravata borboleta ainda era mantida.Eu não entendo.Qualquer ajuda será apreciada.

Eu gostaria de manter a gravata borboleta inteira como dois triângulos (ou um polígono de seis lados - qualquer um seria útil).Procurando uma solução alternativa para esse "problema".

#!/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)))

Depois de pensar sobre isso, posso entender por que uma gravata borboleta é tratada de maneira diferente de uma estrela, mas estou interessado em encontrar uma solução alternativa.

Foi útil?

Solução

Sua gravata borboleta não é válida e bem torneada Polígono.Leia essa documentação e a documentação para Anel Linear (logo acima do Polygon documentação).Em particular, observe os exemplos de válidos e inválidos LinearRingS.

Se você criar a gravata borboleta assim:

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

In [47]: poly = Polygon(bt)

então buffer(0) retorna um Multipolígono:

In [48]: poly.buffer(0)
Out[48]: <shapely.geometry.multipolygon.MultiPolygon at 0x4a40050>

Outras dicas

buffer(0) nem sempre produz um 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))'
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top