gdal_translate file.tif input.tif -of GTiff -co TILED=TRUE -co COMPRESS=DEFLATE
python create_overview.py input.tif
bin/cogger -output cog.tif input.tif overview.ovr.2 overview.ovr.4 overview.ovr.8
| import click | |
| import numpy | |
| import rasterio | |
| from rasterio import transform | |
| from rasterio.rio import options | |
| @click.command() | |
| @options.file_in_arg | |
| def main(input): | |
| with rasterio.open(input) as src: | |
| profile = src.profile | |
| profile["blockxsize"] = 256 | |
| profile["blockysize"] = 256 | |
| profile["compress"] = "DEFLATE" | |
| profile["tiled"] = True | |
| arr = src.read(indexes=1) | |
| overview_factor = 1 | |
| while min(src.width // overview_factor, src.height // overview_factor) > 256: | |
| overview_factor *= 2 | |
| arr = arr.reshape(arr.shape[0]//2, 2, arr.shape[1]//2, 2).sum(axis=(1,-1)) | |
| profile["width"] = arr.shape[1] | |
| profile["height"] = arr.shape[0] | |
| profile["transform"] = transform.from_bounds(*src.bounds, arr.shape[0], arr.shape[1]) | |
| with rasterio.open(f"overview.ovr.{overview_factor}", "w", **profile) as dst: | |
| dst.write(arr, indexes=1) | |
| if __name__ == '__main__': | |
| main() |