Skip to content

Unifying

unify_raster_grids(base_raster, rasters_to_unify, resampling_method='nearest', same_extent=False)

Unifies (reprojects, resamples, aligns and optionally clips) given rasters relative to base raster.

Parameters:

Name Type Description Default
base_raster DatasetReader

The base raster to determine target raster grid properties.

required
rasters_to_unify Sequence[DatasetReader]

Rasters to be unified with the base raster.

required
resampling_method Literal[nearest, bilinear, cubic, average, gauss, max, min]

Resampling method. Most suitable method depends on the dataset and context. Nearest, bilinear and cubic are some common choices. This parameter defaults to nearest.

'nearest'
same_extent bool

If the unified rasters will be forced to have the same extent/bounds as the base raster. Expands smaller rasters with nodata cells. Defaults to False.

False

Returns:

Type Description
List[Tuple[ndarray, dict]]

List of unified rasters' data and metadata. First element is the base raster.

Raises:

Type Description
InvalidParameterValueException

Rasters to unify is empty.

Source code in eis_toolkit/raster_processing/unifying.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
@beartype
def unify_raster_grids(
    base_raster: rasterio.io.DatasetReader,
    rasters_to_unify: Sequence[rasterio.io.DatasetReader],
    resampling_method: Literal["nearest", "bilinear", "cubic", "average", "gauss", "max", "min"] = "nearest",
    same_extent: bool = False,
) -> List[Tuple[np.ndarray, dict]]:
    """Unifies (reprojects, resamples, aligns and optionally clips) given rasters relative to base raster.

    Args:
        base_raster: The base raster to determine target raster grid properties.
        rasters_to_unify: Rasters to be unified with the base raster.
        resampling_method: Resampling method. Most suitable
            method depends on the dataset and context. Nearest, bilinear and cubic are some
            common choices. This parameter defaults to nearest.
        same_extent: If the unified rasters will be forced to have the same extent/bounds
            as the base raster. Expands smaller rasters with nodata cells. Defaults to False.

    Returns:
        List of unified rasters' data and metadata. First element is the base raster.

    Raises:
        InvalidParameterValueException: Rasters to unify is empty.
    """
    if len(rasters_to_unify) == 0:
        raise InvalidParameterValueException("Rasters to unify is empty.")

    method = RESAMPLE_METHOD_MAP[resampling_method]
    out_rasters = _unify_raster_grids(base_raster, rasters_to_unify, method, same_extent)
    return out_rasters