Skip to content

Commit a854688

Browse files
Merge pull request #305 from cogeotiff/patch/nodata-alpha-warning
raise warning with both nodata and alpha
2 parents 7c9c579 + 6746610 commit a854688

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release Notes
22

3+
4+
## 5.4.2 (2025-06-27)
5+
6+
* add user warning when input dataset has both Nodata and internal Alpha/Mask band
7+
38
## 5.4.1 (2024-12-16)
49

510
* fix reading raster compression value with `rasterio>=1.4.3` (author @glostis, https://github.com/cogeotiff/rio-cogeo/pull/300)

rio_cogeo/cogeo.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from rasterio.vrt import WarpedVRT
2222

2323
from rio_cogeo import models, utils
24-
from rio_cogeo.errors import IncompatibleOptions
24+
from rio_cogeo.errors import IncompatibleOptions, NodataAlphaMaskWarning
2525

2626
IN_MEMORY_THRESHOLD = int(os.environ.get("IN_MEMORY_THRESHOLD", 10980 * 10980))
2727

@@ -222,6 +222,12 @@ def cog_translate( # noqa: C901
222222
alpha = utils.has_alpha_band(src_dst)
223223
mask = utils.has_mask_band(src_dst)
224224

225+
if nodata is not None and (alpha or mask):
226+
warnings.warn(
227+
"Input dataset has both a nodata value and internal alpha/mask band. Nodata value will be prioritized.",
228+
NodataAlphaMaskWarning,
229+
)
230+
225231
if colormap and len(indexes) > 1:
226232
raise IncompatibleOptions(
227233
"Cannot add a colormap for multiple bands data."

rio_cogeo/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class IncompatibleBlockRasterSize(UserWarning):
99
"""Rio-cogeo module incompatible raster block/size warning."""
1010

1111

12+
class NodataAlphaMaskWarning(UserWarning):
13+
"""Dataset with nodata and alpha/mask warning."""
14+
15+
1216
class RioCogeoError(Exception):
1317
"""Base exception class."""
1418

704 KB
Binary file not shown.

tests/test_cogeo.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "fixtures")
2323
raster_path_rgba = os.path.join(FIXTURES_DIR, "image_rgba.tif")
24+
raster_path_rgba_nodata = os.path.join(FIXTURES_DIR, "image_rgba_nodata.tif")
2425
raster_path_rgb = os.path.join(FIXTURES_DIR, "image_rgb.tif")
2526
raster_path_nan = os.path.join(FIXTURES_DIR, "image_nan.tif")
2627
raster_path_nodata = os.path.join(FIXTURES_DIR, "image_nodata.tif")
@@ -920,3 +921,20 @@ def test_cog_values(src_path, runner):
920921
assert cog.colorinterp == source.colorinterp
921922
assert has_mask_band(cog)
922923
assert not has_alpha_band(cog)
924+
925+
926+
def test_nodata_alpha_incompatible(runner):
927+
"""test nodata/alpha warning."""
928+
with runner.isolated_filesystem():
929+
with pytest.warns(UserWarning):
930+
cog_translate(
931+
raster_path_rgba_nodata,
932+
"cogeo.tif",
933+
cog_profiles.get("deflate"),
934+
quiet=True,
935+
)
936+
assert cog_validate("cogeo.tif")
937+
938+
with rasterio.open("cogeo.tif") as cog:
939+
assert cog.nodata is not None
940+
assert has_alpha_band(cog)

0 commit comments

Comments
 (0)