Skip to content

Snapping

snap_with_raster(raster, snap_raster)

Snaps/aligns raster to given snap raster.

Raster is snapped from its left-bottom corner to nearest snap raster grid corner in left-bottom direction. If rasters are aligned, simply returns input raster data and metadata.

Parameters:

Name Type Description Default
raster DatasetReader

The raster to be clipped.

required
snap_raster DatasetReader

The snap raster i.e. reference grid raster.

required

Returns:

Type Description
ndarray

The snapped raster data.

dict

The updated metadata.

Raises:

Type Description
NonMatchingCrsException

Raster and and snap raster are not in the same CRS.

MatchingRasterGridException

Raster grids are already aligned.

Source code in eis_toolkit/raster_processing/snapping.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
@beartype
def snap_with_raster(raster: rasterio.DatasetReader, snap_raster: rasterio.DatasetReader) -> Tuple[np.ndarray, dict]:
    """Snaps/aligns raster to given snap raster.

    Raster is snapped from its left-bottom corner to nearest snap raster grid corner in left-bottom direction.
    If rasters are aligned, simply returns input raster data and metadata.

    Args:
        raster: The raster to be clipped.
        snap_raster: The snap raster i.e. reference grid raster.

    Returns:
        The snapped raster data.
        The updated metadata.

    Raises:
        NonMatchingCrsException: Raster and and snap raster are not in the same CRS.
        MatchingRasterGridException: Raster grids are already aligned.
    """

    if not check_matching_crs(
        objects=[raster, snap_raster],
    ):
        raise NonMatchingCrsException("Raster and and snap raster have different CRS.")

    if snap_raster.bounds.bottom == raster.bounds.bottom and snap_raster.bounds.left == raster.bounds.left:
        raise MatchingRasterGridException("Raster grids are already aligned.")

    out_image, out_meta = _snap(raster, snap_raster)
    return out_image, out_meta