Skip to content

Unique combinations in rasters

unique_combinations(raster_list)

Get combinations of raster values between rasters.

All bands in all rasters are used for analysis. The first band of the first raster is used for reference when making the output.

Parameters:

Name Type Description Default
raster_list Sequence[DatasetReader]

Rasters to be used for finding combinations.

required

Returns:

Type Description
ndarray

Combinations of rasters.

dict

The metadata of the first raster in raster_list.

Raises:

Type Description
InvalidParameterValueException

Input rasters don't have enough bands to perform the operation or input rasters are of different shape.

Source code in eis_toolkit/raster_processing/unique_combinations.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@beartype
def unique_combinations(  # type: ignore[no-any-unimported]
    raster_list: Sequence[rasterio.io.DatasetReader],
) -> Tuple[np.ndarray, dict]:
    """Get combinations of raster values between rasters.

    All bands in all rasters are used for analysis.
    The first band of the first raster is used for reference when making the output.

    Args:
        raster_list: Rasters to be used for finding combinations.

    Returns:
        Combinations of rasters.
        The metadata of the first raster in raster_list.

    Raises:
        InvalidParameterValueException: Input rasters don't have enough bands to perform
            the operation or input rasters are of different shape.
    """
    bands = []
    out_meta = raster_list[0].meta
    out_meta["count"] = 1

    raster_profiles = []
    for raster in raster_list:
        for band in range(1, raster.count + 1):
            bands.append(raster.read(band))
        raster_profiles.append(raster.profile)

    if len(bands) == 1:
        raise InvalidParameterValueException("Expected to have more bands than 1")

    if check_raster_grids(raster_profiles) is not True:
        raise NonMatchingRasterMetadataException("Expected raster grids to be have the same grid properties.")

    out_image = _unique_combinations(bands)
    return out_image, out_meta