def test_extract_from_tile(): """Extract subdata from bigger tile.""" in_tile = BufferedTilePyramid("geodetic", metatiling=4).tile(5, 5, 5) shape = (in_tile.shape[0] / 2, in_tile.shape[1]) in_tile.data = ma.masked_array( np.concatenate([np.ones(shape), np.ones(shape) * 2])) # intersecting at top out_tile = BufferedTilePyramid("geodetic").tile(5, 20, 20) out_array = raster.extract_from_tile(in_tile, out_tile) assert isinstance(out_array, np.ndarray) assert np.all(np.where(out_array == 1, True, False)) # intersecting at bottom out_tile = BufferedTilePyramid("geodetic").tile(5, 22, 20) out_array = raster.extract_from_tile(in_tile, out_tile) assert isinstance(out_array, np.ndarray) assert np.all(np.where(out_array == 2, True, False)) # not intersecting try: out_tile = BufferedTilePyramid("geodetic").tile(5, 15, 20) out_array = raster.extract_from_tile(in_tile, out_tile) raise Exception() except ValueError: pass
def test_write_raster_window(): """Basic output format writing.""" path = tempfile.NamedTemporaryFile(delete=False).name # standard tile tp = BufferedTilePyramid("geodetic") tile = tp.tile(5, 5, 5) tile.data = ma.masked_array(np.ones((2, ) + tile.shape)) for out_profile in [ dict(driver="GTiff", count=2, dtype="uint8", compress="lzw", nodata=0, height=tile.height, width=tile.width, affine=tile.affine), dict(driver="GTiff", count=2, dtype="uint8", compress="deflate", nodata=0, height=tile.height, width=tile.width, affine=tile.affine), dict(driver="PNG", count=2, dtype="uint8", nodata=0, height=tile.height, width=tile.width, compress=None, affine=tile.affine), ]: try: raster.write_raster_window(in_tile=tile, out_profile=out_profile, out_tile=tile, out_path=path) with rasterio.open(path, 'r') as src: assert src.read().any() assert src.meta["driver"] == out_profile["driver"] assert src.transform == tile.affine if out_profile["compress"]: assert src.compression == Compression( out_profile["compress"].upper()) finally: shutil.rmtree(path, ignore_errors=True) # with metatiling tile = BufferedTilePyramid("geodetic", metatiling=4).tile(5, 1, 1) tile.data = ma.masked_array(np.ones((2, ) + tile.shape)) out_tile = BufferedTilePyramid("geodetic").tile(5, 5, 5) out_profile = dict(driver="GTiff", count=2, dtype="uint8", compress="lzw", nodata=0, height=out_tile.height, width=out_tile.width, affine=out_tile.affine) try: raster.write_raster_window(in_tile=tile, out_profile=out_profile, out_tile=out_tile, out_path=path) with rasterio.open(path, 'r') as src: assert src.shape == out_tile.shape assert src.read().any() assert src.meta["driver"] == out_profile["driver"] assert src.transform == out_profile["affine"] finally: shutil.rmtree(path, ignore_errors=True)