def mask_with_polygon(x, y, polyverts, inside=True): """ Mask x-y arrays inside or outside a polygon Parameters ---------- x, y : array-like NxM arrays of x- and y-coordinates. polyverts : sequence of a polygon's vertices A sequence of x-y pairs for each vertex of the polygon. inside : bool (default is True) Toggles masking the inside or outside the polygon Returns ------- mask : bool array The NxM mask that can be applied to ``x`` and ``y``. """ # validate input polyverts = validate.polygon(polyverts) points = validate.xy_array(x, y, as_pairs=True) # compute the mask mask = mpath.Path(polyverts).contains_points(points).reshape(x.shape) if inside: return mask else: return ~mask
def mask_with_polygon(x, y, polyverts, inside=True): """ Mask x-y arrays inside or outside a polygon Parameters ---------- x, y : array-like NxM arrays of x- and y-coordinates. polyverts : sequence of a polygon's vertices A sequence of x-y pairs for each vertex of the polygon. inside : bool (default = True) Toggles returning a mask *inside* or *outside* the polygon. Returns ------- mask : bool array The NxM mask that can be applied to ``x`` and ``y``. """ # validate input polyverts = validate.polygon(polyverts) points = validate.xy_array(x, y, as_pairs=True) # compute the mask mask = mpath.Path(polyverts).contains_points(points).reshape(x.shape) # invert if we're masking things outside the polygon if not inside: mask = ~mask return mask
def Test_polygon(polycoords, error): with utils.raises(error): poly = validate.polygon(polycoords) nptest.assert_array_equal(numpy.array(polycoords), poly)
def test_polygon(polycoords, error): with raises(error): poly = validate.polygon(polycoords) nptest.assert_array_equal( numpy.array([(2, 2), (5, 2), (5, 5), (2, 5)]), poly)
def test_wrong_dims(self): validate.polygon([self.poly_array, self.poly_array])
def test_too_short(self): validate.polygon(self.too_short)
def test_too_wide(self): validate.polygon(self.too_wide)
def test_array(self): poly = validate.polygon(self.poly_array) nt.assert_true(np.all(self.poly_array == poly))