删除 Shapely 多边形外部的 numpy 网格点

2024-03-12

我有一个 10 x 10 网格,我想删除形状多边形之外的点:

import numpy as np
from shapely.geometry import Polygon, Point
from descartes import PolygonPatch

gridX, gridY = np.mgrid[0.0:10.0, 0.0:10.0]
poly = Polygon([[1,1],[1,7],[7,7],[7,1]])

#plot original figure
fig = plt.figure()
ax = fig.add_subplot(111)
polyp = PolygonPatch(poly)
ax.add_patch(polyp)
ax.scatter(gridX,gridY)
plt.show()

Here is the resulting figure: original fig

And what I want the end result to look like: end

我知道我可以将数组重塑为 100 x 2 网格点数组:

stacked = np.dstack([gridX,gridY])
reshaped = stacked.reshape(100,2)

我可以轻松查看该点是否位于多边形内:

for i in reshaped:
    if Point(i).within(poly):
         print True

但我在获取这些信息并修改原始网格时遇到困难


你已经很接近了;您可以将点附加到列表中,而不是打印 True。

output = []
for i in reshaped:
    if Point(i).within(poly):
        output.append(i)

output = np.array(output)
x, y = output[:, 0], output[:, 1]

看起来Point.within但并不认为位于多边形边缘的点位于多边形“内部”。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

删除 Shapely 多边形外部的 numpy 网格点 的相关文章

随机推荐