【问题标题】:How to input an invalid polygon that crosses itself in Shapely如何在Shapely中输入一个与自身相交的无效多边形
【发布时间】:2018-08-18 03:40:10
【问题描述】:

我正在使用 shapely 并尝试输入如下所示的多边形,

但是,这显然是无效的,因为它跨越了自身。我想保持原样,并将其输入到我的程序中。有没有办法我可以做到这一点?我不应该使用匀称吗?

【问题讨论】:

标签: python shapely


【解决方案1】:

Shapely 可以“处理”无效多边形,您可以通过is_valid 属性检查其有效性。在某些情况下,为了获得不相交的外边界,可以采用一种“技巧”,使用大小为零的缓冲区,如下所示:

import math

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

import numpy as np
from shapely.geometry import Polygon

fig = plt.figure(figsize=(3, 6))

pnts = [ (math.cos(math.pi/2 - 2*math.pi*i/5), math.sin(math.pi/2 - 2*math.pi*i/5)) for i in range(5) ]
pnts = [ pnts[0], pnts[2], pnts[4], pnts[1], pnts[3] ]
P = Polygon(pnts)

print(P.is_valid) #False

Q = P.buffer(0)

ax = plt.subplot(211, aspect = 'equal')
ax.plot(*P.exterior.xy)

ax = plt.subplot(212, aspect = 'equal')
ax.plot(*Q.exterior.xy)

fig.savefig('fig.png')

这给出了:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2012-10-15
    相关资源
    最近更新 更多