def test_read_unscale(): """Should or Shouldn't apply scale and offset to a data.""" with rasterio.open(COG_SCALE) as src_dst: arr, mask = reader.tile(src_dst, 218, 99, 8, tilesize=128) arrS, maskS = reader.tile(src_dst, 218, 99, 8, tilesize=128, unscale=True) assert arr.dtype == "int16" assert arrS.dtype == "float32" assert not numpy.array_equal(arr, arrS) numpy.testing.assert_array_equal(mask, maskS) meta = reader.metadata(src_dst) assert isinstance(meta["statistics"][1]["min"], int) meta = reader.metadata(src_dst, unscale=True) assert isinstance(meta["statistics"][1]["min"], float) p = reader.point(src_dst, [310000, 4100000], coord_crs=src_dst.crs) assert p == [8917] p = reader.point(src_dst, [310000, 4100000], coord_crs=src_dst.crs, unscale=True) assert round(p[0], 3) == 1000.892
def test_fullEarth(): """Should read tile for COG spanning the whole earth.""" with rasterio.open(COG_EARTH) as src_dst: tile, _ = reader.tile(src_dst, 1, 42, 7, tilesize=64) assert tile.shape == (1, 64, 64) tile, _ = reader.tile(src_dst, 127, 42, 7, tilesize=64) assert tile.shape == (1, 64, 64)
def test_dateline(): """Should return correct metadata.""" with rasterio.open(COG_DLINE) as src_dst: tile, _ = reader.tile(src_dst, 1, 42, 7, tilesize=64) assert tile.shape == (1, 64, 64) tile, _ = reader.tile(src_dst, 127, 42, 7, tilesize=64) assert tile.shape == (1, 64, 64)
def test_tile_read_mask(): """Read masked area.""" with rasterio.Env(GDAL_DISABLE_READDIR_ON_OPEN="EMPTY_DIR"): # non-boundless tile covering the masked part with rasterio.open(S3_MASK_PATH) as src_dst: arr, mask = reader.tile(src_dst, 876431, 1603669, 22, tilesize=16) assert arr.shape == (3, 16, 16) assert mask.shape == (16, 16) assert not mask.all() # boundless tile covering the masked part with rasterio.open(S3_MASK_PATH) as src_dst: arr, mask = reader.tile(src_dst, 876431, 1603668, 22, tilesize=256) assert arr.shape == (3, 256, 256) assert not mask.all()
def tiftile(): x = request.args.get('x') y = request.args.get('y') z = request.args.get('z') tifName = request.args.get('tname') tifPath1 = "d:\\data\\3010\\{}.tif".format(tifName) try: dataset = rasterio.open(tifPath1) tile, mask = reader.tile(dataset, int(x), int(y), int(z), tilesize=256) dataset.close() min = 0 max = 60 renderData = np.array([tile[0], tile[1]+tile[2]*0.3, tile[2]]) renderData = renderData.astype(np.uint8) mtdata = to_math_type(renderData) data = sigmoidal(mtdata, 10, 0.15)*255 buffer = render(data.astype(np.uint8), mask=mask) return send_file(io.BytesIO(buffer), mimetype="image/png", attachment_filename="{}_{}_{}.jpg".format(x, y, z)) except Exception as a: print(a) return abort(404) finally: pass
def test_tile_read_wrong_nodata(): """Return empty mask on wrong nodata.""" # non-boundless tile covering the nodata part with rasterio.open(S3_NODATA_PATH) as src_dst: arr, mask = reader.tile( src_dst, 438217, 801835, 21, tilesize=256, indexes=(1, 2, 3), nodata=1000 ) assert arr.shape == (3, 256, 256) assert mask.all() # Mask boundless values arr, mask = reader.tile( src_dst, 109554, 200458, 19, tilesize=256, indexes=(1, 2, 3), nodata=1000 ) assert arr.shape == (3, 256, 256) assert not mask.all()
def tile( address: str, tile_x: int, tile_y: int, tile_z: int, tilesize: int = 256, **kwargs: Any, ) -> Tuple[numpy.ndarray, numpy.ndarray]: """ Create mercator tile from any images. Attributes ---------- address : str file url. tile_x : int Mercator tile X index. tile_y : int Mercator tile Y index. tile_z : int Mercator tile ZOOM level. tilesize : int, optional (default: 256) Output image size. kwargs: dict, optional These will be passed to the 'rio_tiler.reader.tile' function. Returns ------- data : numpy ndarray mask: numpy array """ with rasterio.open(address) as src_dst: return reader.tile(src_dst, tile_x, tile_y, tile_z, tilesize, **kwargs)
def test_that_tiling_ignores_padding_if_web_friendly_internal_tiles_exist(): """Ignore Padding when COG is aligned.""" with rasterio.open(COG_WEB_TILED) as src_dst: arr, _ = reader.tile( src_dst, 147, 182, 9, tilesize=256, padding=0, resampling_method="bilinear" ) arr2, _ = reader.tile( src_dst, 147, 182, 9, tilesize=256, padding=100, resampling_method="bilinear", ) assert numpy.array_equal(arr, arr2)
def test_tile_read_alpha(): """Read masked area.""" # non-boundless tile covering the alpha masked part with rasterio.open(S3_ALPHA_PATH) as src_dst: arr, mask = reader.tile( src_dst, 876432, 1603670, 22, tilesize=256, indexes=(1, 2, 3) ) assert arr.shape == (3, 256, 256) assert not mask.all() with pytest.warns(AlphaBandWarning): with rasterio.open(S3_ALPHA_PATH) as src_dst: nb = src_dst.count arr, mask = reader.tile(src_dst, 876432, 1603670, 22, tilesize=256) assert not nb == arr.shape[0] assert arr.shape == (3, 256, 256) assert not mask.all()
def test_tile_read_internal_nodata(): """Read masked area.""" # non-boundless tile covering the nodata part with rasterio.open(S3_NODATA_PATH) as src_dst: arr, mask = reader.tile( src_dst, 876431, 1603670, 22, tilesize=256, indexes=(1, 2, 3) ) assert arr.shape == (3, 256, 256) assert not mask.all()
def worker(band: str): asset = "{}/{}-{}.tiff".format(sentinel_prefix, scene_params["beam"].lower(), band) with rasterio.open(asset) as src_dst: with WarpedVRT( src_dst, src_crs=src_dst.gcps[1], src_transform=transform.from_gcps(src_dst.gcps[0]), src_nodata=0, ) as vrt_dst: return reader.tile(vrt_dst, tile_x, tile_y, tile_z, tilesize=tilesize, **kwargs)
def worker(band: str): asset = f"{landsat_prefix}_B{band}.TIF" if band == "QA": nodata = 1 resamp = "nearest" else: nodata = 0 resamp = "bilinear" with rasterio.open(asset) as src_dst: tile, mask = reader.tile( src_dst, tile_x, tile_y, tile_z, tilesize=tilesize, nodata=nodata, resampling_method=resamp, ) return tile, mask
def tile( address: str, tile_x: int, tile_y: int, tile_z: int, tilesize: int = 256, map_bounds: List[float] = None, parse_asset_as_json: bool = True, **kwargs: Any, ) -> Tuple[np.ndarray, np.array]: """ Create mercator tile from any images. Attributes ---------- address : str file url. tile_x : int Mercator tile X index. tile_y : int Mercator tile Y index. tile_z : int Mercator tile ZOOM level. tilesize : int, optional (default: 256) Output image size. map_bounds : List[float], optional (default: inferred) Bounds of map excluding border in WGS84 Normal order: (minx, miny, maxx, maxy) parse_asset_as_json : bool, optional (default: True) Whether to attempt to parse address as a JSON object with "url" and "map_bounds keys" kwargs: dict, optional These will be passed to the 'rio_tiler.reader.tile' function. Returns ------- data : np ndarray mask : np array """ # Custom hack that encodes url and map_bounds into address string if parse_asset_as_json: try: asset_dict = json.loads(address) address = asset_dict['url'] map_bounds = asset_dict['map_bounds'] except json.JSONDecodeError: pass with rasterio.open(address) as src_dst: # Convert image bounds to wgs84 image_wgs_bounds = transform_bounds(src_dst.crs, CRS.from_epsg(4326), *src_dst.bounds) # Get extent and cutline if not map_bounds: map_bounds = estimate_extent(image_wgs_bounds, address) cutline = get_cutline(src_dst, map_bounds) return reader.tile(src_dst, tile_x, tile_y, tile_z, tilesize, warp_vrt_option={'cutline': cutline}, **kwargs)
from rio_tiler.utils import render from rio_color.operations import gamma, sigmoidal from rio_color.utils import to_math_type tifPath1 = "d:\\data\\3010\\1_1.tif" dataset = rasterio.open(tifPath1) band = rasterio.band(dataset, 1) print(band) tile, mask = reader.tile(dataset, 852, 418, 10, tilesize=1024) min = 0 max = 60 tile1 = (tile[0]-min)/max*255 tile2 = (tile[1]-min)/max*255 tile3 = (tile[2]-min)/max*255 tileList = np.array([tile[0], tile[1], tile[2]]) renderData = np.where(tileList > 255, 255, tileList) renderData = np.where(renderData < 0, 0, renderData) renderData = renderData.astype(np.uint8)
def test_cog_translate_Internal(): """ Test Web-Optimized COG. - Test COG size is a multiple of 256 (mercator tile size) - Test COG bounds are aligned with mercator grid at max zoom - Test high resolution internal tiles are equal to mercator tile using cogdumper and rio-tiler - Test overview internal tiles are equal to mercator tile using cogdumper and rio-tiler """ from cogdumper.cog_tiles import COGTiff from cogdumper.filedumper import Reader as FileReader runner = CliRunner() with runner.isolated_filesystem(): web_profile = cog_profiles.get("raw") web_profile.update({"blockxsize": 256, "blockysize": 256}) config = dict(GDAL_TIFF_OVR_BLOCKSIZE="128") cog_translate( raster_path_web, "cogeo.tif", web_profile, quiet=True, web_optimized=True, config=config, ) with rasterio.open(raster_path_web) as src_dst: with rasterio.open("cogeo.tif") as out_dst: blocks = list(set(out_dst.block_shapes)) assert len(blocks) == 1 ts = blocks[0][0] assert not out_dst.width % ts assert not out_dst.height % ts _, max_zoom = get_zooms(out_dst) bounds = list( transform_bounds(src_dst.crs, "epsg:4326", *src_dst.bounds, densify_pts=21)) minimumTile = mercantile.tile(bounds[0], bounds[3], max_zoom) maximumTile = mercantile.tile(bounds[2], bounds[1], max_zoom) with open("cogeo.tif", "rb") as out_body: reader = FileReader(out_body) cog = COGTiff(reader.read) # High resolution # Top Left tile _, tile = cog.get_tile(0, 0, 0) tile_length = 256 * 256 * 3 t = struct.unpack_from("{}b".format(tile_length), tile) arr = numpy.array(t).reshape(256, 256, 3).astype(numpy.uint8) arr = numpy.transpose(arr, [2, 0, 1]) with rasterio.open("cogeo.tif") as src_dst: data, _ = COGreader.tile(src_dst, *minimumTile, resampling_method="nearest") numpy.testing.assert_array_equal(data, arr) # Bottom right tile _, tile = cog.get_tile(4, 3, 0) tile_length = 256 * 256 * 3 t = struct.unpack_from("{}b".format(tile_length), tile) arr = numpy.array(t).reshape(256, 256, 3).astype(numpy.uint8) arr = numpy.transpose(arr, [2, 0, 1]) with rasterio.open("cogeo.tif") as src_dst: data, _ = COGreader.tile(src_dst, *maximumTile, resampling_method="nearest") numpy.testing.assert_array_equal(data, arr) # Low resolution (overview 1) # Top Left tile # NOTE: overview internal tile size is 128px # We need to stack two internal tiles to compare with # the 256px mercator tile fetched by rio-tiler # ref: https://github.com/cogeotiff/rio-cogeo/issues/60 _, tile = cog.get_tile(1, 0, 1) tile_length = 128 * 128 * 3 t = struct.unpack_from("{}b".format(tile_length), tile) arr1 = numpy.array(t).reshape(128, 128, 3).astype(numpy.uint8) arr1 = numpy.transpose(arr1, [2, 0, 1]) _, tile = cog.get_tile(2, 0, 1) tile_length = 128 * 128 * 3 t = struct.unpack_from("{}b".format(tile_length), tile) arr2 = numpy.array(t).reshape(128, 128, 3).astype(numpy.uint8) arr2 = numpy.transpose(arr2, [2, 0, 1]) arr = numpy.dstack((arr1, arr2)) with rasterio.open("cogeo.tif") as src_dst: data, _ = COGreader.tile(src_dst, 118594, 60034, 17, resampling_method="nearest") data = data[:, 128:, :] numpy.testing.assert_array_equal(data, arr)