Ejemplo n.º 1
0
 def test_mask_poly_multiple(self):
     # mask by multiple polygons
     t = -np.linspace(0, 2 * np.pi, 200)
     xp = ((2 + np.cos(7 * t)) * np.cos(t + 0.3) + 4) * 4 + 15
     yp = ((2 + np.cos(7 * t)) * np.sin(t + 0.2) + 4) * 4 + 72
     poly = karta.Polygon(zip(xp, yp), crs=karta.crs.Cartesian)
     xp2 = ((2 + np.cos(7 * t)) * np.cos(t + 0.3) + 4) * 6 + 40
     yp2 = ((2 + np.cos(7 * t)) * np.sin(t + 0.2) + 4) * 6 + 30
     poly2 = karta.Polygon(zip(xp2, yp2), crs=karta.crs.Cartesian)
     grid = RegularGrid([0.0, 0.0, 0.1, 0.1, 0.0, 0.0],
                        values=np.arange(1e6).reshape(1000, 1000),
                        crs=karta.crs.Cartesian)
     masked_grid = grid.mask_by_poly([poly, poly2])
     self.assertEqual(int(np.nansum(masked_grid[:, :])), 47081206720)
Ejemplo n.º 2
0
 def test_mask_poly_partial2(self):
     # this case has the first transect begin off-grid, which has caused
     # problems in the past
     g = RegularGrid([0, 0, 1, 1, 0, 0], values=np.ones((7, 7)))
     p = karta.Polygon([(-2, 3), (8, -5), (8, -1), (-2, 7)])
     gc = g.mask_by_poly(p)
     self.assertEqual(gc.data_mask.sum(), 20)
Ejemplo n.º 3
0
 def test_mask_poly_partial(self):
     t = -np.linspace(0, 2 * np.pi, 200)
     xp = ((2 + np.cos(7 * t)) * np.cos(t + 0.3) + 2) * 12
     yp = ((2 + np.cos(7 * t)) * np.sin(t + 0.2) + 2) * 12
     poly = karta.Polygon(zip(xp, yp), crs=karta.crs.Cartesian)
     grid = RegularGrid([0.0, 0.0, 0.1, 0.1, 0.0, 0.0],
                        values=np.arange(1e6).reshape(1000, 1000),
                        crs=karta.crs.Cartesian)
     masked_grid = grid.mask_by_poly(poly)
     self.assertEqual(masked_grid.data_mask.sum(), 181424)
Ejemplo n.º 4
0
 def test_mask_poly_inplace(self):
     t = -np.linspace(0, 2 * np.pi, 200)
     xp = ((2 + np.cos(7 * t)) * np.cos(t + 0.3) + 4) * 12
     yp = ((2 + np.cos(7 * t)) * np.sin(t + 0.2) + 4) * 12
     poly = karta.Polygon(zip(xp, yp), crs=karta.crs.Cartesian)
     grid = RegularGrid([0.0, 0.0, 0.1, 0.1, 0.0, 0.0],
                        values=np.arange(1e6).reshape(1000, 1000),
                        crs=karta.crs.Cartesian)
     grid.mask_by_poly(poly, inplace=True)
     self.assertEqual(int(np.nansum(grid[:, :])), 97048730546)
Ejemplo n.º 5
0
 def test_mask_poly_inverted(self):
     # grids where transform dy is negative (common for geotiffs)
     t = -np.linspace(0, 2 * np.pi, 200)
     xp = ((2 + np.cos(7 * t)) * np.cos(t + 0.3) + 4) * 12
     yp = ((2 + np.cos(7 * t)) * np.sin(t + 0.2) + 4) * 12
     poly = karta.Polygon(zip(xp, yp), crs=karta.crs.Cartesian)
     grid = RegularGrid([0.0, 100, 0.1, -0.1, 0.0, 0.0],
                        values=np.arange(1e6).reshape(1000, 1000),
                        crs=karta.crs.Cartesian)
     masked_grid = grid.mask_by_poly(poly)
     self.assertEqual(int(np.nansum(masked_grid[:, :])), 97048730546)
Ejemplo n.º 6
0
 def test_mask_poly_partial(self):
     # test case where polygon is partly outside the grid extents
     # simply ensures that it doesn't crash for now
     t = -np.linspace(0, 2 * np.pi, 200)
     xp = ((2 + np.cos(7 * t)) * np.cos(t + 0.3) + 2) * 12
     yp = ((2 + np.cos(7 * t)) * np.sin(t + 0.2) + 2) * 12
     poly = karta.Polygon(zip(xp, yp), crs=karta.crs.Cartesian)
     grid = karta.RegularGrid([0.0, 0.0, 0.1, 0.1, 0.0, 0.0],
                              values=np.arange(1e6).reshape(1000, 1000),
                              crs=karta.crs.Cartesian)
     masked_grid = grid.mask_by_poly(poly)
     return
Ejemplo n.º 7
0
 def test_mask_poly_multiband(self):
     t = -np.linspace(0, 2 * np.pi, 200)
     xp = ((2 + np.cos(7 * t)) * np.cos(t + 0.3) + 4) * 12
     yp = ((2 + np.cos(7 * t)) * np.sin(t + 0.2) + 4) * 12
     poly = karta.Polygon(zip(xp, yp), crs=karta.crs.Cartesian)
     grid = RegularGrid([0.0, 0.0, 0.1, 0.1, 0.0, 0.0],
                        values=np.broadcast_to(
                            np.atleast_3d(
                                np.arange(1e6).reshape(1000, 1000)),
                            (1000, 1000, 3)),
                        crs=karta.crs.Cartesian)
     masked_grid = grid.mask_by_poly(poly)
     self.assertEqual(int(np.nansum(masked_grid[:, :])), 97048730546 * 3)
Ejemplo n.º 8
0
    def test_polygon_contains(self):
        """ Search for points contained within a polygon, using a quadtree. """
        th = np.linspace(-np.pi, np.pi, 18)
        xp = 5 * np.cos(th)
        yp = 5 * np.sin(th)
        poly = karta.Polygon(zip(xp, yp), crs=karta.crs.SphericalEarth)

        np.random.seed(42)
        x = (np.random.random(1000) - 0.5) * 180.0
        y = (np.random.random(1000) - 0.5) * 30.0
        mp = karta.Multipoint(zip(x, y), crs=karta.crs.SphericalEarth)

        contained = mp.within_polygon(poly)
        mp.build_quadtree()
        contained_qt = mp.within_polygon(poly)
        self.assertEqual(len(contained), len(contained_qt))
        for i in range(len(contained)):
            self.assertTrue(contained[i] in contained_qt)
        return