将多边形裁剪到边界框而不丢失框边缘

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

我正在尝试将多边形裁剪到一个盒子中 到目前为止我得到的功能是:


import numpy as np

def crop_polygon(x, y, xmin, xmax, ymin, ymax):
    # Create a mask to identify points inside the bounding box
    mask = (x >= xmin) & (x <= xmax) & (y >= ymin) & (y <= ymax)

    # Find the indices of points that are inside the bounding box
    inside_indices = np.where(mask)[0]

    # Extract the coordinates of the points inside the bounding box
    cropped_x = x[inside_indices]
    cropped_y = y[inside_indices]

    return cropped_x, cropped_y


x=np.array([20,50,100,200,400,500,800,850,750,600,300,150,0])
y=np.array([100,120,80,140,130,110,100,99,20,10,30,40,10])
xmin=200
xmax=700
ymin=0
ymax=200
cropped_x, cropped_y = crop_polygon(x, y, xmin, xmax, ymin, ymax)
print("Cropped x:", cropped_x)
print("Cropped y:", cropped_y)


结果:

Cropped x: [200 400 500 600 300]
Cropped y: [140 130 110  10  30]

这里的目的是沿着x轴切割多边形,从xmin和xmax可以看出。

这里的问题是,我丢失了多边形与 x 处边界相交的“边”。 理想情况下,我会在 (xmin,y) 和 (xmax,y) 或多个点处插入一个点,因为这需要适用于所有类型的多边形。

有人对此有好的解决方案或库吗?

python polygon bounding-box intersect
1个回答
0
投票

我使用这个找到了一个很好的解决方案: 无法从 Shapely Polygons 制作 GeoDataFrame:NotImplementedError:多边形本身不提供数组接口。它的戒指可以

感谢您的阅读和回复!

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