geodata.mask.trim_raster

geodata.mask.trim_raster#

geodata.mask.trim_raster(raster: rasterio.DatasetReader) rasterio.DatasetReader#

Remove the all-zero columns and rows at the border of the raster and returns the trimmed raster. This method does not remove the all-zero cols or rows in the middle of the valid values.

For example: if the raster.read(1) (the array values) is the following:

np.array(
    [
        [0, 0, 9, 0, 0, 0, 0],
        [0, 0, 1, 2, 0, 0, 0],
        [0, 0, 2, 3, 4, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
    ]
)

The bounds of valid values will be (2, 4, 0, 4); The all zero columns and rows at the border of the array will be removed.

Parameters:

raster (ras.DatasetReader) – The source raster

Returns:

The trimmed raster.

Return type:

ras.DatasetReader