2016-08-25 19 views
0

ポリゴンの集合を結合して1つのポリゴンを作成しようとしています。オーバーレイなしでポリゴンに結合する(そしてプロット)

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

ポリゴンに「接続」と表示されている:これらのポリゴンが(それらが最初に等しい最後の点でのように閉じていない)、それらのエッジはいくつかの点で正確に同一である(1,0.25 )、(1,0)、(1,0.75)

これらのポリゴンを1つのポリゴンに結合するにはどうすればよいですか?

私の現在のコード:

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座標を取得しようとすると、私はできません(uはマルチポリゴンです)。

答えて

0

私の問題は、2番目のポリゴンに自己結合があるということです。その後

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

関連する問題