删除形状多边形外部的numpy meshgrid点

删除形状多边形外部的numpy meshgrid点,numpy,shapely,Numpy,Shapely,我有一个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.fi

我有一个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()
以下是结果图:

我希望最终结果是什么样的:

我知道我可以将阵列重塑为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]
<> >代码>点。在中,不考虑位于多边形边缘的点是“内”。