Source raster mask

A source raster mask flags pixels in the source image as invalid. Any output sample whose interpolation footprint overlaps an invalid source pixel is itself flagged invalid and set to nodata_out. Unlike the grid mask, which operates on output samples, the source raster mask operates on input pixels.

What you’ll learn

  • How to build and save a source raster mask

  • How to pass it through array_src_mask_ds, array_src_mask_validity_pair and array_src_mask_band

  • How source-mask invalidation propagates to the output mask

Setting things up

We reuse the source raster, grid file and grid mask from the previous notebook. The source raster mask itself is created below if absent.

Building the source raster mask

We mark a small square near the mandrill’s left eye as invalid.

array_src_mask_validity_valid, array_src_mask_validity_invalid = 1, 0

# Create the mask raster, initially all valid.
array_in_mask = np.full(mandrill[0].shape, array_src_mask_validity_valid, dtype=np.uint8)
array_in_mask[slice(50, 71), slice(150, 171)] = array_src_mask_validity_invalid

write_array(array_in_mask, dtype=np.uint8, fileout=raster_mask_in_path_u8)

The invalid region is shown below as a red square overlaid on the source image. The grid mask coloured nodes are also visible.

array_in_mask_input.png

Running the chain with the source mask

Three arguments are needed:

  • array_src_mask_ds – opened mask dataset,

  • array_src_mask_validity_pair(valid_value, invalid_value) tuple,

  • array_src_mask_band – band index to read.

with rasterio.open(GRID_IN_F64, "r") as grid_in_ds, \
        rasterio.open(RASTER_IN, "r") as array_src_ds, \
        rasterio.open(grid_mask_in_path, "r") as grid_mask_in_ds, \
        rasterio.open(raster_mask_in_path_u8, "r") as raster_mask_in_ds, \
        rasterio.open(output_raster_path, "w", **raster_out_open_args) as array_out_ds, \
        rasterio.open(output_mask_path, "w", **mask_out_open_args) as mask_out_ds:

    basic_grid_resampling_chain(
        grid_ds                        = grid_in_ds,
        grid_row_coords_band           = 1,
        grid_col_coords_band           = 2,
        grid_resolution                = grid_resolution,
        array_src_ds                   = array_src_ds,
        array_src_bands                = 1,
        array_out_ds                   = array_out_ds,
        interp                         = "cubic",
        nodata_out                     = 0,
        mask_out_ds                    = mask_out_ds,
        grid_mask_in_ds                = grid_mask_in_ds,
        grid_mask_in_unmasked_value    = grid_mask_in_valid_value,
        grid_mask_in_band              = 1,
        array_src_mask_ds              = raster_mask_in_ds,
        array_src_mask_band            = 1,
        array_src_mask_validity_pair   = (array_src_mask_validity_valid,
                                          array_src_mask_validity_invalid),
    )

array_in_mask_output.png

Both masks contribute to the output mask: any output sample is invalid if its grid node is masked or if its interpolation kernel touches an invalid source pixel.