用Shapely覆盖多边形


15

我正在尝试使用Shapely(给定的多边形A,B和C)捕获下面指示的所有非重叠多边形。而且,我希望这样做而无需迭代,测试相交等。这个问题的公认答案表达了PostGIS方法,但似乎“工会”对不同的人而言意味着不同的事情。

多边形叠加

Answers:


21

您需要进行某种程度的迭代。(更新:我已编辑删除了所有“ for”循环,除了一个列表理解

# imports used throughout this example
from shapely.geometry import Point
from shapely.ops import cascaded_union
from itertools import combinations

# Here are your input shapes (circles A, B, C)
A = Point(3, 6).buffer(4)
B = Point(6, 2).buffer(4)
C = Point(1, 2).buffer(4)

# list the shapes so they are iterable
shapes = [A, B, C]

首先,需要使用每个形状的组合对,所有交叉点的并集(使用级联并集)。然后,从所有形状的并集中删除(通过)相交。difference

# All intersections
inter = cascaded_union([pair[0].intersection(pair[1]) for pair in combinations(shapes, 2)])
# Remove from union of all shapes
nonoverlap = cascaded_union(shapes).difference(inter)

如下nonoverlap所示(通过JTS Test Builder): 不重叠


1

几年后,似乎可以通过以下方法找到更好的解决方案shapely

# imports used throughout this example
from shapely.geometry import Point
from shapely.ops import polygonize, unary_union

# Here are your input shapes (circles A, B, C)
A = Point(3, 6).buffer(4)
B = Point(6, 2).buffer(4)
C = Point(1, 2).buffer(4)
...

# list the shapes so they are iterable
shapes = [A, B, C, ...]

# generate the overlay
list(polygonize(unary_union(list(x.exterior for x in shapes))))

它支持任何长度的几何图形,唯一的问题是计算时间,不支持带孔的多边形。


出于好奇,您为什么认为您的解决方案比@MikeT的解决方案好?我只能从计算时间上了解问题。
mgri
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.