Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As a slightly more direct alternative to @Anil's answer, matplotlib has <a href="http://matplotlib.sourceforge.net/faq/howto_faq.html#test-whether-a-point-is-inside-a-polygon" rel="noreferrer"><code>matplotlib.nxutils.points_inside_poly</code></a> that can be used to quickly rasterize an arbitrary polygon. E.g.</p> <pre><code>import numpy as np from matplotlib.nxutils import points_inside_poly nx, ny = 10, 10 poly_verts = [(1,1), (5,1), (5,9),(3,2),(1,1)] # Create vertex coordinates for each grid cell... # (&lt;0,0&gt; is at the top left of the grid in this system) x, y = np.meshgrid(np.arange(nx), np.arange(ny)) x, y = x.flatten(), y.flatten() points = np.vstack((x,y)).T grid = points_inside_poly(points, poly_verts) grid = grid.reshape((ny,nx)) print grid </code></pre> <p>Which yields (a boolean numpy array):</p> <pre><code>[[False False False False False False False False False False] [False True True True True False False False False False] [False False False True True False False False False False] [False False False False True False False False False False] [False False False False True False False False False False] [False False False False True False False False False False] [False False False False False False False False False False] [False False False False False False False False False False] [False False False False False False False False False False] [False False False False False False False False False False]] </code></pre> <p>You should be able to pass <code>grid</code> to any of the scipy.ndimage.morphology functions quite nicely.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload