0

我有一个带有几个连续多边形的 shapefile,我想减少它们的节点数,以保持相邻多边形的拓扑一致。我正在考虑根据节点两侧的 2 个线段产生的角度删除节点;特别是删除形成角度 <180º 和 >175º 的节点。

我看过一个评论指的是同样的想法,但我对编码有非常基本的了解。这如何在 Python 中实现?

https://stackoverflow.com/a/2624475/8435715

4

1 回答 1

0

这是一个基于两个标准的示例 - 顶点之间的距离和您上面描述的角度:

import numpy as np
def reduce_polygon(polygon, angle_th=0, distance_th=0):

    angle_th_rad = np.deg2rad(angle_th)
    points_removed = [0]
    while len(points_removed):
        points_removed = list()
        for i in range(0, len(polygon)-2, 2):
            v01 = polygon[i-1] - polygon[i]
            v12 = polygon[i] - polygon[i+1]
            d01 = np.linalg.norm(v01)
            d12 = np.linalg.norm(v12)
            if d01 < distance_th and d12 < distance_th:
                points_removed.append(i)
                continue
            angle = np.arccos(np.sum(v01*v12) / (d01 * d12))
                if angle < angle_th_rad:
                    points_removed.append(i)
        polygon = np.delete(polygon, points_removed, axis=0)
    return polygon

例子:

from matplotlib import pyplot as plt
from time import time
tic = time()
reduced_polygon = reduce_polygon(original_polygon, angle_th=5, distance_th=4)
toc = time()

plt.figure()
plt.scatter(original_polygon[:, 0], original_polygon[:, 1], c='r', marker='o', s=2)
plt.scatter(reduced_polygon[:, 0], reduced_polygon[:, 1], c='b', marker='x', s=20)
plt.plot(reduced_polygon[:, 0], reduced_polygon[:, 1], c='black', linewidth=1)
plt.show()

print(f'original_polygon length: {len(original_polygon)}\n', 
      f'reduced_polygon length: {len(reduced_polygon)}\n'
      f'running time: {round(toc - tic, 4)} secends')

产生以下结果: 在此处输入图像描述

于 2021-09-02T07:42:59.840 回答