Skip to content

Commit

Permalink
use a copy of the profile
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed May 18, 2021
1 parent 088f974 commit 4573ee5
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions rio_cogeo/cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def cog_translate( # noqa: C901
Use GDAL COG driver if set to True. COG driver is available starting with GDAL 3.1.
"""
out_profile = dst_kwargs.copy()

if isinstance(indexes, int):
indexes = (indexes,)

Expand All @@ -156,7 +158,7 @@ def cog_translate( # noqa: C901

if not add_mask and (
(nodata is not None or alpha)
and dst_kwargs.get("compress", "").lower() == "jpeg"
and out_profile.get("compress", "").lower() == "jpeg"
):
warnings.warn(
"Nodata/Alpha band will be translated to an internal mask band.",
Expand All @@ -168,7 +170,9 @@ def cog_translate( # noqa: C901
else indexes
)

tilesize = min(int(dst_kwargs["blockxsize"]), int(dst_kwargs["blockysize"]))
tilesize = min(
int(out_profile["blockxsize"]), int(out_profile["blockysize"])
)

if src_dst.width < tilesize or src_dst.height < tilesize:
tilesize = 2 ** int(math.log(min(src_dst.width, src_dst.height), 2))
Expand All @@ -178,9 +182,9 @@ def cog_translate( # noqa: C901
" and overviews cannot be added.",
IncompatibleBlockRasterSize,
)
dst_kwargs.pop("blockxsize", None)
dst_kwargs.pop("blockysize", None)
dst_kwargs.pop("tiled")
out_profile.pop("blockxsize", None)
out_profile.pop("blockysize", None)
out_profile.pop("tiled")
overview_level = 0

else:
Expand All @@ -189,8 +193,8 @@ def cog_translate( # noqa: C901
"Setting blocksize to {}".format(tilesize),
IncompatibleBlockRasterSize,
)
dst_kwargs["blockxsize"] = tilesize
dst_kwargs["blockysize"] = tilesize
out_profile["blockxsize"] = tilesize
out_profile["blockysize"] = tilesize

vrt_params = {
"add_alpha": True,
Expand Down Expand Up @@ -226,16 +230,16 @@ def cog_translate( # noqa: C901
meta.pop("alpha", None)

if (
dst_kwargs.get("photometric", "").upper() == "YCBCR"
out_profile.get("photometric", "").upper() == "YCBCR"
and meta["count"] == 1
):
warnings.warn(
"PHOTOMETRIC=YCBCR not supported on a 1-band raster"
" and has been set to 'MINISBLACK'"
)
dst_kwargs["photometric"] = "MINISBLACK"
out_profile["photometric"] = "MINISBLACK"

meta.update(**dst_kwargs)
meta.update(**out_profile)
meta.pop("compress", None)
meta.pop("photometric", None)

Expand Down Expand Up @@ -325,7 +329,7 @@ def cog_translate( # noqa: C901
tags.update(**additional_cog_metadata)

if web_optimized and not use_cog_driver:
dst_kwargs.update(
out_profile.update(
{
"@TILING_SCHEME_NAME": "WebMercatorQuad",
"@TILING_SCHEME_ZOOM_LEVEL": tms.zoom_for_res(
Expand All @@ -337,7 +341,7 @@ def cog_translate( # noqa: C901
)

if aligned_levels:
dst_kwargs.update(
out_profile.update(
{"@TILING_SCHEME_ALIGNED_LEVELS": aligned_levels}
)

Expand All @@ -349,31 +353,31 @@ def cog_translate( # noqa: C901
click.echo("Writing output to: {}".format(dst_path), err=True)

if use_cog_driver:
dst_kwargs["driver"] = "COG"
out_profile["driver"] = "COG"
if web_optimized:
dst_kwargs["TILING_SCHEME"] = (
out_profile["TILING_SCHEME"] = (
"GoogleMapsCompatible"
if tms.identifier == "WebMercatorQuad"
else tms.identifier
)

dst_kwargs["zoom_level_strategy"] = zoom_level_strategy
dst_kwargs["overview_resampling"] = overview_resampling
dst_kwargs["warp_resampling"] = resampling
out_profile["zoom_level_strategy"] = zoom_level_strategy
out_profile["overview_resampling"] = overview_resampling
out_profile["warp_resampling"] = resampling
if aligned_levels is not None:
dst_kwargs["aligned_levels"] = aligned_levels
out_profile["aligned_levels"] = aligned_levels

dst_kwargs["blocksize"] = tilesize
dst_kwargs.pop("blockxsize", None)
dst_kwargs.pop("blockysize", None)
dst_kwargs.pop("tiled", None)
dst_kwargs.pop("interleave", None)
dst_kwargs.pop("photometric", None)
out_profile["blocksize"] = tilesize
out_profile.pop("blockxsize", None)
out_profile.pop("blockysize", None)
out_profile.pop("tiled", None)
out_profile.pop("interleave", None)
out_profile.pop("photometric", None)

copy(tmp_dst, dst_path, **dst_kwargs)
copy(tmp_dst, dst_path, **out_profile)

else:
copy(tmp_dst, dst_path, copy_src_overviews=True, **dst_kwargs)
copy(tmp_dst, dst_path, copy_src_overviews=True, **out_profile)


def cog_validate( # noqa: C901
Expand Down

0 comments on commit 4573ee5

Please sign in to comment.