Skip to content

Instantly share code, notes, and snippets.

@vincentsarago
Created April 19, 2022 15:45
Show Gist options
  • Save vincentsarago/9ece7981c4cd2da265e2175e858a1531 to your computer and use it in GitHub Desktop.
Save vincentsarago/9ece7981c4cd2da265e2175e858a1531 to your computer and use it in GitHub Desktop.
SUM overview + cogger

1. Create input tif

gdal_translate file.tif input.tif -of GTiff -co TILED=TRUE -co COMPRESS=DEFLATE

2. Create Overview

python create_overview.py input.tif

3. Create COG

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment