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)
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)
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)
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)
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)
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)
def test_mask_multipoly(self): t = -np.linspace(0, 2 * np.pi, 200) coords = [] for dx, dy in [(60, 30), (45, 80), (25, 35)]: xp = (2 + np.cos(7 * t)) * np.cos(t + 0.3) * 6 + dx yp = (2 + np.cos(7 * t)) * np.sin(t + 0.2) * 6 + dy coords.append([list(zip(xp, yp))]) poly = karta.Multipolygon(coords, crs=karta.crs.Cartesian) grid = RegularGrid([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(int(np.nansum(masked_grid[:, :])), 73399874364)