diff --git a/rasterio/_warp.pyx b/rasterio/_warp.pyx index a7393116..ced626b6 100644 --- a/rasterio/_warp.pyx +++ b/rasterio/_warp.pyx @@ -649,7 +649,7 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase): src_nodata=None, dst_nodata=None, nodata=None, dst_width=None, width=None, dst_height=None, height=None, src_transform=None, dst_transform=None, transform=None, - init_dest_nodata=True, add_alpha=False, **warp_extras): + init_dest_nodata=True, add_alpha=None, **warp_extras): """Make a virtual warped dataset Parameters @@ -699,8 +699,9 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase): ----- When the source dataset has an internal bitmask or sidecar .msk file and no nodata value and no nodata value is specified for - the warped VRT, the `add_alpha` parameter will be set internally - to True so that a mask can be computed for the VRT. + the warped VRT, *and* `add_alpha` is None, the `add_alpha` + parameter will be set internally to True so that a mask can be + computed for the VRT. Returns ------- @@ -857,7 +858,7 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase): # If the source dataset has a per-dataset mask and no dst_nodata # value is specified, we need to specify that an alpha band is # to be added to the VRT. - if MaskFlags.per_dataset in self.src_dataset.mask_flag_enums[0] and self.dst_nodata is None: + if MaskFlags.per_dataset in self.src_dataset.mask_flag_enums[0] and self.dst_nodata is None and self.dst_alpha is None: self.dst_alpha = True psWOptions = create_warp_options( diff --git a/tests/test_warpedvrt.py b/tests/test_warpedvrt.py index d00ebea6..6f446d95 100644 --- a/tests/test_warpedvrt.py +++ b/tests/test_warpedvrt.py @@ -253,3 +253,16 @@ def test_add_alpha_read(path_rgb_byte_tif): assert vrt.count == 4 assert vrt.colorinterp[3] == ColorInterp.alpha data = vrt.read(boundless=True, window=Window(-200, -200, 1000, 1000), out_shape=((3, 600, 600))) + + +def test_default_add_alpha_read(path_rgb_msk_byte_tif): + """An alpha band is added if add_alpha is unspecified and source has .msk""" + with rasterio.open(path_rgb_msk_byte_tif) as src, WarpedVRT(src) as vrt: + assert vrt.count == 4 + assert vrt.colorinterp[3] == ColorInterp.alpha + + +def test_no_add_alpha_read(path_rgb_msk_byte_tif): + """An alpha band is not added if add_alpha=False""" + with rasterio.open(path_rgb_msk_byte_tif) as src, WarpedVRT(src, add_alpha=False) as vrt: + assert vrt.count == 3