Skip to content

Commit

Permalink
adding decimation base var to cog_translate, including tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan McCarthy committed Mar 29, 2024
1 parent b1a7bb1 commit 10d99e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
16 changes: 15 additions & 1 deletion rio_cogeo/cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def cog_translate( # noqa: C901
colormap: Optional[Dict] = None,
additional_cog_metadata: Optional[Dict] = None,
use_cog_driver: bool = False,
decimation_base: int = 2,
):
"""
Create Cloud Optimized Geotiff.
Expand Down Expand Up @@ -171,6 +172,10 @@ def cog_translate( # noqa: C901
Additional dataset metadata to add to the COG.
use_cog_driver: bool, optional (default: False)
Use GDAL COG driver if set to True. COG driver is available starting with GDAL 3.1.
decimation_base: int, default: 2
How overviews are divided at each zoom level (default is 2). Must be greater than 1.
Also requires that `overview_level` is provided for `decimation_base` values greater
than 2.
.. deprecated:: 5.1.2
`web_optimized` is deprecated in favor of `tms`.
Expand All @@ -187,6 +192,15 @@ def cog_translate( # noqa: C901
)
tms = tms or morecantile.tms.get("WebMercatorQuad")

if decimation_base <= 1:
raise ValueError(
"Decimation base must be greater than 1 for building overviews."
)
elif decimation_base > 2 and overview_level is None:
raise ValueError(
"Decimation base values greater than 2 require that overview_level is defined."
)

dst_kwargs = dst_kwargs.copy()

if isinstance(indexes, int):
Expand Down Expand Up @@ -343,7 +357,7 @@ def cog_translate( # noqa: C901
if not quiet and overview_level:
click.echo("Adding overviews...", err=True)

overviews = [2**j for j in range(1, overview_level + 1)]
overviews = [decimation_base**j for j in range(1, overview_level + 1)]
tmp_dst.build_overviews(overviews, ResamplingEnums[overview_resampling])

if not quiet:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,23 @@ def test_cog_translate_forward_ns_metadata(runner):
with rasterio.open("cogeo.tif") as src:
assert src.tags(ns="IMD")
assert src.tags(ns="RPC")


def test_cog_translate_decimation_base(runner):
"""Should create proper overviews when using custom decimation base."""
with runner.isolated_filesystem():

base_level_pairs = [(3, 6), (4, 5), (5, 4)]

for decimation_base, overview_level in base_level_pairs:
cog_translate(
raster_path_rgb,
"cogeo.tif",
cog_profiles.get("deflate"),
decimation_base=decimation_base,
overview_level=overview_level,
quiet=True,
)

with rasterio.open("cogeo.tif") as src:
assert src.overviews(1)[0] == decimation_base

0 comments on commit 10d99e3

Please sign in to comment.