加入没有覆盖的多边形(和绘图)

join polygons without overlay (and plot)

我正在尝试连接一组多边形以创建一个多边形。这些多边形(它们不是闭合的,因为最后一个点等于第一个),它们的边缘在某些点上完全相等:

poly1 = [(0,0), (1,0), (1,0.25), (1, 0.5), (1,0.75), (1,1), (0,1)]
poly2 = [(2,0), (2,0.25), (1,0.25), (1,0.5), (1,0.75), (2,1)]

可见多边形 "connect" 位于:(1,0.25), (1, 0.5), (1,0.75)

如何将这些多边形连接成一个多边形?

我当前的代码:

from __future__ import division
import pandas as pd
from shapely.geometry import Polygon,MultiPolygon
import os
import glob
from shapely.ops import cascaded_union
import matplotlib.pyplot as plt
from descartes import PolygonPatch

basePath = os.path.dirname(os.path.realpath(__file__)) # defines the directory where the current file resides

files = glob.glob(os.path.join(basePath, '*.txt'))

polygons = []

for f in files:
    data = pd.read_csv(f, sep=';') # file containing a list of x and y points

    points = []
    for index, point in data.iterrows():
        points.append((point['x'], point['y']))
    polygons.append(Polygon(points))

u = cascaded_union(polygons)
fig2 = plt.figure(2, figsize=(10,10), dpi=90)
ax2 = fig2.add_subplot(111)
patch2b = PolygonPatch(u, fc=BLUE, ec=BLUE, alpha=1, zorder=2)
ax2.add_patch(patch2b)

当我 运行 上面的代码不起作用时。当我尝试从 u 获取 x、y 坐标时我不能(u 是多面体)

我认为问题在于您的第二个多边形具有自连接。

poly1 = Polygon([(0,0), (1,0), (1,0.25), (1, 0.5), (1,0.75), (1,1), (0,1)])
poly2 = Polygon([(2,0), (2,0.25), (1,0.25), (1,0.5), (1,0.75), (2,1)])
poly2_corrected = Polygon([(2,0), (1,0.25), (1,0.5), (1,0.75), (2,1)])

那么,我们有:

poly1

poly2

poly2_corrected

尝试时出现错误:

u = cascaded_union([poly1, poly2])

但不适用于:

u_corrected = cascaded_union([poly1, poly2_corrected])
u_corrected

print u

多边形 ((1 0.25, 1 0, 0 0, 0 1, 1 1, 1 0.75, 2 1, 2 0, 1 0.25))