连接多边形而不覆盖(和绘图)

时间:2016-08-25 14:37:32

标签: python shapely

我试图加入一组多边形来创建一个多边形。这些多边形(它们没有像最后一个点那样关闭,等于第一个),它们的边缘在某些点上完全相等:

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)]

可见多边形"连接" at:(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)

当我运行上面的代码时,它不起作用。当我试图从你那里得到x,y coords时我不能(你是多边形)

1 个答案:

答案 0 :(得分:0)

我认为问题是你的第二个Polygon有一个自我加入。

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

enter image description here

poly2

enter image description here

poly2_corrected

enter image description here

我们尝试时收到错误:

u = cascaded_union([poly1, poly2])

但不是为了:

u_corrected = cascaded_union([poly1, poly2_corrected])
u_corrected

enter image description here

print u

POLYGON((1 0.25,1 0,0 0,0 1,11,0 0.75,2 1 1,0 0,1 0.25))