Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
def Test_polygon(polycoords, error):
    with utils.raises(error):
        poly = validate.polygon(polycoords)
        nptest.assert_array_equal(numpy.array(polycoords), poly)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
 def test_wrong_dims(self):
     validate.polygon([self.poly_array, self.poly_array])
Exemplo n.º 6
0
 def test_too_short(self):
     validate.polygon(self.too_short)
Exemplo n.º 7
0
 def test_too_wide(self):
     validate.polygon(self.too_wide)
Exemplo n.º 8
0
 def test_array(self):
     poly = validate.polygon(self.poly_array)
     nt.assert_true(np.all(self.poly_array == poly))