def test_cog_overviews(): with TemporaryDirectory() as dir_name: source = os.path.join(dir_name, 'source.tif') dest = os.path.join(dir_name, 'dest.tif') image = sample_raster_image(height=800, width=900) tl.GeoRaster2(deepcopy(image), crs=WEB_MERCATOR_CRS, affine=base_affine).save(source) convert_to_cog(source, dest) assert (os.path.exists(dest)) assert not os.path.exists("%s.msk" % dest) with rasterio.open(dest) as raster: data = raster.read() assert (image.data == data).all() mask = raster.read_masks() expected_mask = _mask_from_masked_array(image) assert (expected_mask == mask).all() # to confirtm that creating a cog remove namespace tags assert raster.tags(ns='rio_overview').get('resampling', 'empty') == 'empty' for i in raster.indexes: assert raster.overviews(i) == _calc_overviews_factors(raster) assert _has_internal_perdataset_mask(raster)
def make_test_raster(value=0, band_names=[], height=3, width=4, dtype=np.uint16, crs=WEB_MERCATOR_CRS, affine=None): if affine is None: affine = Affine.translation(10, 12) * Affine.scale(1, -1) shape = [len(band_names), height, width] array = np.full(shape, value, dtype=dtype) mask = np.full(shape, False, dtype=np.bool) image = np.ma.array(data=array, mask=mask) raster = tl.GeoRaster2(image=image, affine=affine, crs=crs, band_names=band_names) return raster
def test_cog_simple_file(): with TemporaryDirectory() as dir_name: file_name = os.path.join(dir_name, 'temp.tif') image = sample_raster_image() tl.GeoRaster2(deepcopy(image), crs=WEB_MERCATOR_CRS, affine=base_affine).save(file_name) with rasterio.open(file_name) as raster: data = raster.read() assert (image.data == data).all() mask = raster.read_masks() expected_mask = _mask_from_masked_array(image) assert (expected_mask == mask).all() assert _has_internal_perdataset_mask(raster)
def test_cog_move_telluric_tags_to_general_tags_space(): with TemporaryDirectory() as dir_name: source = os.path.join(dir_name, 'source.tif') dest = os.path.join(dir_name, 'dest.tif') image = sample_raster_image(height=800, width=900) band_names = ['red', 'green', 'blue'] tl.GeoRaster2(deepcopy(image), crs=WEB_MERCATOR_CRS, affine=base_affine, band_names=band_names).save(source) convert_to_cog(source, dest) tags = tl.GeoRaster2.tags(dest) assert (json.loads(tags['telluric_band_names']) == band_names) raster = tl.GeoRaster2.open(dest) assert raster.band_names == band_names
def test_colorize_jet_with_range(): raster = tl.GeoRaster2(image=np.array([i / 256 for i in range(256)], dtype=np.float16).reshape( (1, 16, 16)), band_names=['red'], crs=WGS84_CRS, affine=affine.Affine(2, 0, 0, 0, 1, 0)) heatmap = raster.colorize('jet', vmin=-1, vmax=1) assert (np.array_equal(heatmap.band_names, ['red', 'green', 'blue'])) assert (np.array_equal(heatmap.image.data[:, 0, 0], [0, 0, 0])) # nodata remains nodata assert (np.array_equal(heatmap.image.mask[:, 0, 0], [True, True, True])) # nodata remains nodata mask = heatmap.image.mask assert (len(mask[mask]) == 3)
def test_colorize_jet(): raster = tl.GeoRaster2(image=np.array([i / 256 for i in range(256)], dtype=np.float16).reshape( (1, 16, 16)), band_names=['red'], crs=WGS84_CRS, affine=affine.Affine(2, 0, 0, 0, 1, 0)) heatmap = raster.colorize('jet') assert (np.array_equal(heatmap.band_names, ['red', 'green', 'blue'])) assert (np.array_equal(heatmap.image.data[:, 0, 0], [0, 0, 0])) # nodata remains nodata assert (np.array_equal(heatmap.image.mask[:, 0, 0], [True, True, True])) # nodata remains nodata assert (np.array_equal(heatmap.image.data[:, 0, 1], [0, 0, 127])) # blue assert (np.array_equal(heatmap.image.mask[:, 0, 1], [False, False, False])) # blue assert (np.array_equal( heatmap.image.data[:, heatmap.height - 1, heatmap.width - 1], [127, 0, 0])) # red assert (np.array_equal( heatmap.image.mask[:, heatmap.height - 1, heatmap.width - 1], [False, False, False])) # red
def test_cog_calc_overviews_factors(height, factors): image = sample_raster_image(height=height, width=900) raster = tl.GeoRaster2(image, crs=WEB_MERCATOR_CRS, affine=base_affine) assert (_calc_overviews_factors(raster) == factors)
def test_raster_with_no_url(): raster = tl.GeoRaster2(np.random.uniform(0, 256, (3, 391, 370)), affine=Affine(1.0000252884112817, 0.0, 2653750.345511198, 0.0, -1.0000599330133702, 4594461.485763356), crs=CRS({'init': 'epsg:3857'})) return raster