2014-10-09 56 views
2

我有一个10×10网格,我想删除一个匀称多边形的外部的点:卸下匀称多边形的外numpy的meshgrid点

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×2阵列的各个网格点:

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

我可以看到,如果点位于多边形内轻松:

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

但我无法采取这一信息,并修改原始网格

回答

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并不认为趴在多边形的边缘点是“内”它虽然。