Skip to content

Commit

Permalink
Create dataset mask if appropriate, like gdalbuildvrt
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean C. Gillies committed Apr 6, 2018
1 parent 4ebdaa0 commit 22e6582
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
24 changes: 23 additions & 1 deletion rasterio/_warp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from rasterio._err import (
CPLE_AppDefinedError, CPLE_OpenFailedError)
from rasterio import dtypes
from rasterio.control import GroundControlPoint
from rasterio.enums import Resampling
from rasterio.enums import Resampling, MaskFlags
from rasterio.errors import DriverRegistrationError, CRSError, RasterioIOError
from rasterio.transform import Affine, from_bounds, tastes_like_gdal
from rasterio.vfs import parse_path, vsi_path
Expand Down Expand Up @@ -671,6 +671,10 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase):
cdef double src_gt[6]
cdef double dst_gt[6]
cdef void *hTransformArg = NULL
cdef GDALRasterBandH hband = NULL
cdef GDALRasterBandH hmask = NULL
cdef int mask_block_xsize = 0
cdef int mask_block_ysize = 0

hds = (<DatasetReaderBase?>self.src_dataset).handle()
hds = exc_wrap_pointer(hds)
Expand Down Expand Up @@ -708,6 +712,18 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase):

log.debug("Exported CRS to WKT.")

# Flag if the source dataset has a dataset mask, and
# get the block size. This code is adapted from GDAL's
# VRT builder.
if MaskFlags.per_dataset in src_dataset.mask_flag_enums:
has_dataset_mask = True
else:
has_dataset_mask = False

band = GDALGetRasterBand(hds, 1)
hmask = GDALGetMaskBand(hband)
GDALGetBlockSize(hmask, &mask_block_xsize, &mask_block_ysize)

log.debug("Warp_extras: %r", self.warp_extras)

for key, val in self.warp_extras.items():
Expand Down Expand Up @@ -760,6 +776,12 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase):
hds, NULL, dst_crs_wkt, c_resampling,
c_tolerance, psWOptions)
self._hds = exc_wrap_pointer(hds_warped)

# Add the mask band if appropriate.
if has_dataset_mask:
GDALCreateDatasetMaskBand(hds_warped, MaskFlags.per_dataset.value)
hmask = GDALGetMaskBand(GDALGetRasterBand(hds_warped, 1))

except CPLE_OpenFailedError as err:
raise RasterioIOError(err.errmsg)
finally:
Expand Down
1 change: 1 addition & 0 deletions rasterio/gdal.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ cdef extern from "gdal.h" nogil:
int GDALGetRasterColorInterpretation(GDALRasterBandH band)
int GDALSetRasterColorInterpretation(GDALRasterBandH band, int)
int GDALGetMaskFlags(GDALRasterBandH band)
int GDALCreateDatasetMaskBand(GDALDatasetH hds, int flags)
void *GDALGetMaskBand(GDALRasterBandH band)
int GDALCreateMaskBand(GDALDatasetH hds, int flags)
int GDALGetOverviewCount(GDALRasterBandH band)
Expand Down

0 comments on commit 22e6582

Please sign in to comment.