Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional overview resampling #29

Merged
merged 1 commit into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions rio_cogeo/cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def cog_translate(
nodata=None,
alpha=None,
overview_level=6,
overview_resampling="nearest",
config=None,
):
"""
Expand Down Expand Up @@ -85,9 +86,11 @@ def cog_translate(
mem.write_mask(mask_value, window=w)

overviews = [2 ** j for j in range(1, overview_level + 1)]
mem.build_overviews(overviews, Resampling.nearest)

mem.build_overviews(overviews, Resampling[overview_resampling])
mem.update_tags(
ns="rio_overview", resampling=Resampling.nearest.value
ns="rio_overview",
resampling=Resampling[overview_resampling].value,
)

copy(mem, dst_path, copy_src_overviews=True, **dst_kwargs)
18 changes: 17 additions & 1 deletion rio_cogeo/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click

from rasterio.rio import options
from rasterio.enums import Resampling

from rio_cogeo.cogeo import cog_translate
from rio_cogeo.profiles import cog_profiles
Expand Down Expand Up @@ -54,6 +55,12 @@ def convert(self, value, param, ctx):
@click.option(
"--overview-level", type=int, default=6, help="Overview level (default: 6)"
)
@click.option(
"--overview-resampling",
help="Resampling algorithm.",
type=click.Choice([it.name for it in Resampling if it.value in [0, 2, 5, 6, 7]]),
default="nearest",
)
@click.option("--threads", type=int, default=8)
@options.creation_options
def cogeo(
Expand All @@ -64,6 +71,7 @@ def cogeo(
nodata,
alpha,
overview_level,
overview_resampling,
threads,
creation_options,
):
Expand All @@ -85,5 +93,13 @@ def cogeo(
)

cog_translate(
input, output, output_profile, bidx, nodata, alpha, overview_level, config
input,
output,
output_profile,
bidx,
nodata,
alpha,
overview_level,
overview_resampling,
config,
)
30 changes: 30 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,33 @@ def test_cogeo_validGdalOptions():
assert (
not src.is_tiled
) # Because blocksize is 512 and the file is 512, the output is not tiled


def test_cogeo_validOvrOption():
"""Should work as expected."""
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
raster_path_rgb,
"output.tif",
"--overview-level",
2,
"--overview-resampling",
"cubic",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert src.height == 512
assert src.width == 512
assert src.meta["dtype"] == "uint8"
assert (
not src.is_tiled
) # Because blocksize is 512 and the file is 512, the output is not tiled
assert src.compression.value == "JPEG"
assert src.photometric.value == "YCbCr"
assert src.interleaving.value == "PIXEL"
assert src.overviews(1) == [2, 4]