从 Shapely 多边形中移除 numpy meshgrid 点

4

我有一个 10 x 10 的网格,想要移除不在 Shapely 多边形之内的点:

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

这是最终的图形: original fig 这是我想要的最终效果: 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

但我在修改原始网格时遇到了麻烦

。关于此信息,我需要帮助。
1个回答

2

您已经很接近了,而不是打印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并不认为位于多边形边缘的点是“在内部”的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接