From b1ce0fa9e1db3978f6a7f21677869164bc25bbdb Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Thu, 13 Jun 2024 15:48:47 +0200 Subject: [PATCH] fix alpha band for GCPS and make sure mask are forwarded --- CHANGES.md | 5 ++ rio_cogeo/cogeo.py | 21 +++++- tests/fixtures/image_rgb_mask.tif | Bin 790050 -> 790240 bytes tests/test_cogeo.py | 110 +++++++++++++++++++++++++++++- 4 files changed, 131 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6c10f370..043f7959 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,10 @@ # Release Notes +## 5.3.2 (2024-06-13) + +* do not set output size in the intermediate VRT +* add Alpha band for GCPS wrapped dataset + ## 5.3.1 (2024-06-12) * fix issue when creating COG from file with internal GCPS diff --git a/rio_cogeo/cogeo.py b/rio_cogeo/cogeo.py index 85314ae0..84e6b551 100644 --- a/rio_cogeo/cogeo.py +++ b/rio_cogeo/cogeo.py @@ -227,6 +227,13 @@ def cog_translate( # noqa: C901 "Cannot add a colormap for multiple bands data." ) + # we need to remove the `alpha band` index from the output data + # when we translate the band to an internal mask + if alpha and add_mask: + indexes = tuple( + x for x in indexes if x in utils.non_alpha_indexes(src_dst) + ) + if not add_mask and ( (nodata is not None or alpha) and dst_kwargs.get("compress", "").lower() == "jpeg" @@ -241,10 +248,11 @@ def cog_translate( # noqa: C901 else indexes ) + src_indexes = indexes + tilesize = min(int(dst_kwargs["blockxsize"]), int(dst_kwargs["blockysize"])) vrt_params = { - "add_alpha": True, "dtype": dtype, "width": src_dst.width, "height": src_dst.height, @@ -253,6 +261,7 @@ def cog_translate( # noqa: C901 if src_dst.gcps[1]: vrt_params.update( { + "add_alpha": True, "src_crs": src_dst.gcps[1], "src_transform": transform_from_gcps(src_dst.gcps[0]), } @@ -263,9 +272,12 @@ def cog_translate( # noqa: C901 {"nodata": nodata, "add_alpha": False, "src_nodata": nodata} ) - if alpha: + elif alpha: vrt_params.update({"add_alpha": False}) + elif mask: + vrt_params.update({"add_alpha": True}) + if tms: wo_params = utils.get_web_optimized_params( src_dst, @@ -276,6 +288,9 @@ def cog_translate( # noqa: C901 ) vrt_params.update(**wo_params) + if vrt_params.get("add_alpha", False) and not (add_mask or mask): + indexes = tuple(indexes) + (src_dst.count + 1,) + with WarpedVRT(src_dst, **vrt_params) as vrt_dst: meta = vrt_dst.meta meta["count"] = len(indexes) @@ -371,7 +386,7 @@ def cog_translate( # noqa: C901 if not quiet: click.echo("Updating dataset tags...", err=True) - for i, b in enumerate(indexes): + for i, b in enumerate(src_indexes): tmp_dst.set_band_description(i + 1, src_dst.descriptions[b - 1]) if forward_band_tags: tmp_dst.update_tags(bidx=i + 1, **src_dst.tags(b)) diff --git a/tests/fixtures/image_rgb_mask.tif b/tests/fixtures/image_rgb_mask.tif index 686a3496d3518f5d988e19c869f0156d82d89702..0f5440e68c21fe93251b90174f938f5ec9c86805 100644 GIT binary patch delta 340 zcmZ29$Kb(SgAE&4RfQQC7zpCc8(E7PwRnI^mH2oVOo7+~h@*ix4~QFqcnT0N2jX2o ze3q}dh`qgtoe_wcfS4JGS%8=oh}nP`B*y{7oIuP4#N6A9*m*Xv)mO|(PFTS5;s0`v U-v$@>8CJe$b*HD0J%v^R0LmnI&Hw-a delta 114 zcmaDb*I>~cgAE&4H$P!bVVwMd)kKSjfq~%#2M@znAZFs^Vc-X186egKVlyCi2I2r9 sj^%7FVs9^EX9Qv