【问题标题】:Removing numpy meshgrid points outside of a Shapely polygon删除 Shapely 多边形之外的 numpy meshgrid 点
【发布时间】:2014-10-09 16:10:57
【问题描述】:

我有一个 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

但我无法获取这些信息并修改原始网格

【问题讨论】:

    标签: numpy shapely


    【解决方案1】:

    你已经很接近了;您可以将点附加到列表中,而不是打印 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 似乎并未将位于多边形边缘的点视为“内部”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-26
      • 2018-05-01
      • 2019-12-05
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2010-12-29
      • 2013-12-26
      相关资源
      最近更新 更多