如何合并几乎接触的多边形

问题描述 投票:0回答:1

如何使用 Python 合并几乎接触的多边形?例如,给定这些多边形和一些距离阈值 T:

算法会产生这样的东西:

所以基本上我需要删除两个多边形之间小于 T 的任何间隙。

不要对多边形的原始顶点进行不必要的修改。因此,例如简单地缓冲两个多边形然后将它们合并是不好的。

已经实现此功能的库也可以。

编辑: 作为另一个例子,ArcGIS 多边形聚合 似乎就是这样做的。然而,没有太多关于它是如何实现的提示,并且使用 ArcGIS 不是一个选项。

python geometry shapely
1个回答
0
投票

这是我尝试过的:

我已经用你的阈值

T
画了一个圆,圆心基于
polygon_1
中的点和
polygon_2
中的所有点之间最近的点。然后找到交点,并找到交点区域的凸包。然后找到三个多边形的并集,代码:

import numpy as np
import matplotlib.pyplot as plt
from shapely.ops import unary_union
from shapely.geometry import Polygon
from scipy.spatial import ConvexHull


polygon_1 = np.array([[1, 6, 7.8, 7.8, 1, 1], [4, 4, 6, 11, 11, 4]]).T
polygon_2 = np.array([[6, 14, 14, 11, 8.2, 9, 6, 6], [0.5, 0.5, 12, 12, 10, 4.2, 3.5, 0.5]]).T

poly_1 = Polygon(polygon_1)
poly_2 = Polygon(polygon_2)

# define threshold T
T = 2.5

#Initialize a variable to create a circle
theta = np.linspace(0, 2*np.pi, 100)



intersection_points  = []
coord_circles = np.zeros((0, 2))
for poly in polygon_1:
    # Calculate the distance between the points in polygon_1 ad all points in polygon_2
    norm = np.sqrt((poly[0] - polygon_2[:, 0])**2 + (poly[1] - polygon_2[:, 1])**2)
    
    # If the distance is smaaller than the threshold
    if np.min(norm) < T:
        # Find the nearest point in polygon_2
        point_near =polygon_2[ np.argmin(norm)]
        x = T*np.cos(theta) + point_near[0]
        y = T*np.sin(theta) + point_near[1]
        
        # Crear a polygon for the circle and find intersection points with polygon_1 and polygon_2
        poly_circle = Polygon(np.array([x, y]).T)
        intersec_1 = poly_1.boundary.intersection(poly_circle.boundary)
        intersection_points.append(intersec_1.geoms[0].coords._coords[0])
        intersection_points.append(intersec_1.geoms[1].coords._coords[0])
        intersec_2 = poly_2.boundary.intersection(poly_circle.boundary)
        intersection_points.append(intersec_2.geoms[0].coords._coords[0])
        intersection_points.append(intersec_2.geoms[1].coords._coords[0])
        
intersection_points = np.array(intersection_points)   

# Find the Convex hulls 
hull = ConvexHull(intersection_points)

# Creat a polygon for the convex hull of the intersections 
poly_3 = Polygon(np.c_[intersection_points[hull.vertices,0], intersection_points[hull.vertices,1]])


# Find the union of the these three polygon
a = unary_union([poly_1, poly_2, poly_3])

result_x, result_y = a.exterior.coords.xy
result_x = result_x.tolist()
result_y = result_y.tolist()

plt.subplot(121) 
plt.plot(polygon_1[:, 0], polygon_1[:, 1], 'b')
plt.plot(polygon_2[:, 0], polygon_2[:, 1], 'r')
plt.axis('equal')
plt.title("Before merging")
plt.subplot(122) 
plt.plot(result_x, result_y, 'k-o')
plt.axis('equal')
plt.title("After merging") 

你得到的数字:

© www.soinside.com 2019 - 2024. All rights reserved.