Skip to content

Commit

Permalink
do not add alpha band if it exists (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
dionhaefner committed Jun 23, 2020
1 parent 4171aa5 commit 45e4ff0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions terracotta/drivers/raster_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,11 @@ def _get_resampling_enum(method: str) -> Any:

@staticmethod
def _has_alpha_band(src: 'DatasetReader') -> bool:
from rasterio.enums import MaskFlags
return any([MaskFlags.per_dataset in flags for flags in src.mask_flag_enums])
from rasterio.enums import MaskFlags, ColorInterp
return (
any([MaskFlags.alpha in flags for flags in src.mask_flag_enums])
or ColorInterp.alpha in src.colorinterp
)

@classmethod
@trace('get_raster_tile')
Expand Down Expand Up @@ -478,8 +481,9 @@ def _get_raster_tile(cls, path: str, *,
# construct VRT
vrt = es.enter_context(
WarpedVRT(
src, crs=cls._TARGET_CRS, resampling=reproject_enum, add_alpha=True,
transform=vrt_transform, width=vrt_width, height=vrt_height
src, crs=cls._TARGET_CRS, resampling=reproject_enum,
transform=vrt_transform, width=vrt_width, height=vrt_height,
add_alpha=not cls._has_alpha_band(src)
)
)

Expand All @@ -489,9 +493,11 @@ def _get_raster_tile(cls, path: str, *,
tile_data = vrt.read(
1, resampling=resampling_enum, window=out_window, out_shape=tile_size
)
# read alpha mask
mask_idx = src.count + 1

# assemble alpha mask
mask_idx = vrt.count
mask = vrt.read(mask_idx, window=out_window, out_shape=tile_size) == 0

if src.nodata is not None:
mask |= tile_data == src.nodata

Expand Down

0 comments on commit 45e4ff0

Please sign in to comment.