Python 在矩形的并集中找到孔?

Python 在矩形的并集中找到孔?,python,matplotlib,geos,shapely,Python,Matplotlib,Geos,Shapely,我在单位正方形(红色)内和周围有许多随机矩形(黑色),需要提取单位正方形内未被任何矩形覆盖的所有多边形区域 看起来Shapely可以做到这一点,我得到了矩形的并集(绿色),但我不确定如何从单位正方形中减去它,并检索多边形列表 以下是生成测试数据的代码: import pylab import random from matplotlib import pyplot from shapely.geometry import Point, Polygon from shapely.ops impo

我在单位正方形(红色)内和周围有许多随机矩形(黑色),需要提取单位正方形内未被任何矩形覆盖的所有多边形区域

看起来Shapely可以做到这一点,我得到了矩形的并集(绿色),但我不确定如何从单位正方形中减去它,并检索多边形列表

以下是生成测试数据的代码:

import pylab
import random
from matplotlib import pyplot
from shapely.geometry import Point, Polygon
from shapely.ops import cascaded_union
from descartes import PolygonPatch

def make_square(x, y, size1, size2):
    dx = [size1, -size1, -size1, size1, size1]
    dy = [size2, size2, -size2, -size2, size2]
    return [(x+sx, y+sy) for sx, sy in zip(dx, dy)]


pylab.figure()

square = make_square(0.5, 0.5, 1.0, 1.0)
a, b = zip(*square)
pylab.plot(a, b, 'r-')
polygons = []

for i in xrange(10):
    x = random.random()
    y = random.random()
    s1 = random.random()
    s2 = random.random()

    square = make_square(x, y, s1, s2)
    polygons.append(Polygon(square))
    a, b = zip(*square)
    pylab.plot(a, b, 'k-')

u = cascaded_union(polygons)
patch2b = PolygonPatch(u, fc='#00ff00', ec='#00ff00', alpha=0.5, zorder=2)
pylab.gca().add_patch(patch2b)


pylab.show()

基本上,您希望将“联合”多边形与大正方形的差值进行多边形化,然后对结果进行多边形化,以获得单独的多边形。例如:

#-- Get the region not covered by individual squares.
uncovered_region = Polygon(bigsquare).difference(union)

# In some cases, the result will be a single polygon...
if not isinstance(uncovered_region, MultiPolygon):
    uncovered_region = [uncovered_region]

for poly in polygonize(uncovered_region):
    patch = PolygonPatch(poly, fc='purple', alpha=0.5, zorder=2)
    ax.add_patch(patch)
作为一个完整的例子,基于您的:

import matplotlib.pyplot as plt
import random
from shapely.geometry import Polygon, MultiPolygon
from shapely.ops import cascaded_union, polygonize
from descartes import PolygonPatch

def make_square(x, y, size1, size2):
    dx = [size1, -size1, -size1, size1, size1]
    dy = [size2, size2, -size2, -size2, size2]
    return [(x+sx, y+sy) for sx, sy in zip(dx, dy)]


fig, ax = plt.subplots()

bigsquare = make_square(0.5, 0.5, 1.0, 1.0)
a, b = zip(*bigsquare)
ax.plot(a, b, 'r-')
polygons = []

for i in xrange(10):
    x = random.random()
    y = random.random()
    s1 = random.random()
    s2 = random.random()

    square = make_square(x, y, s1, s2)
    polygons.append(Polygon(square))
    a, b = zip(*square)
    ax.plot(a, b, 'k-')

union = cascaded_union(polygons)
patch2b = PolygonPatch(union, fc='#00ff00', ec='#00ff00', alpha=0.5, zorder=2)
ax.add_patch(patch2b)

#-- Get the region not covered by individual squares.
uncovered_region = Polygon(bigsquare).difference(union)

# In some cases, the result will be a single polygon...
if not isinstance(uncovered_region, MultiPolygon):
    uncovered_region = [uncovered_region]

for poly in polygonize(uncovered_region):
    print poly
    patch = PolygonPatch(poly, fc='purple', alpha=0.5, zorder=2)
    ax.add_patch(patch)

plt.margins(0.05)
plt.show()

代码返回的空多边形区域是否可以只是一个彼此相邻的矩形列表?。或者您想要一个多边形列表,该列表由组成该区域的点的数量定义。例如,图表中的三个开放空间区域可以用七个矩形精确描述。也许更多,也许更少。将其描述为具有任意数量点的三个不同多边形变得更加困难。