修复Shapely中的无效多边形 [英] Fix invalid polygon in Shapely
问题描述
Shapely将多边形的任何线段(包括共线的线段)相交时定义为无效.许多软件包都会创建带有切口"的区域或区域,如下所示,该区域具有共线段:
Shapely defines a Polygon as invalid if any of its segments intersect, including segments that are colinear. Many software packages will create a region or area with a "cutout" as shown here which has colinear segments:
>>> pp = Polygon([(0,0), (0,3), (3,3), (3,0), (2,0),
(2,2), (1,2), (1,1), (2,1), (2,0), (0,0)])
>>> pp.is_valid
WARNING:shapely.geos:Self-intersection at or near point 2 0
False
自然地,可以在Shapely中本地实现切口,或者可以将相同的几何体实现为两个有效的多边形,但是如果我仅具有上面显示的点列表,是否可以轻松地修复"此问题(创建有效的点列表中的几何形状)?
Naturally, the cutout can be implemented natively in Shapely, or this same geometry can be implemented as two valid polygons, but if I only have the list of points shown above, is there an easy to "fix" this (create valid geometry from this list of points)?
推荐答案
我找到了适用于特定情况的解决方案:
I found a solution that works for the specific case given:
>>> pp2 = pp.buffer(0)
>>> pp2.is_valid
True
>>> pp2.exterior.coords[:]
[(0.0, 0.0), (0.0, 3.0), (3.0, 3.0), (3.0, 0.0), (2.0, 0.0), (0.0, 0.0)]
>>> pp2.interiors[0].coords[:]
[(2.0, 1.0), (2.0, 2.0), (1.0, 2.0), (1.0, 1.0), (2.0, 1.0)]
这篇关于修复Shapely中的无效多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!