2016-08-25 1 views
0

Ich versuche, eine Reihe von Polygonen zu verbinden, um ein einzelnes Polygon zu erstellen. Diese Polygone (sie sind nicht wie bei dem letzten Punkt geschlossen gleich sind zu dem ersten), sind ihre Kanten genau gleich an einem gewissen Punkt:Join Polygone ohne Überlagerung (und 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)] 

Es ist ersichtlich, dass die Polygone „connect“ auf: (1,0.25), (1, 0.5), (1.0.75)

Wie verbinde ich diese Polygone zu einem einzigen Polygon?

Mein aktueller Code:

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) 

Wenn ich den obigen Code ausführen funktioniert es nicht. Wenn ich versuche, die x, y-Koordinaten von u ich kann nicht (u ist ein Multipolygon)

Antwort

0

Ich denke, das Problem ist, dass Ihr zweites Polygon eine Self-Join hat.

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

Dann haben wir:

poly1 

enter image description here

poly2 

enter image description here

poly2_corrected 

enter image description here

Wir bekommen einen Fehler, wenn wir versuchen:

u = cascaded_union([poly1, poly2]) 

Aber nicht für:

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, 1 1, 1 0,75, 2 1, 2 0, 1 0,25))