Skip to content

Reprojecting

reproject_raster(raster, target_crs, resampling_method='nearest')

Reprojects raster to match given coordinate reference system (EPSG).

Parameters:

Name Type Description Default
raster DatasetReader

The raster to be reprojected.

required
target_crs int

Target CRS as EPSG code.

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'

Returns:

Type Description
ndarray

The reprojected raster data.

dict

The updated metadata.

Raises:

Type Description
NonMatchinCrsException

Raster is already in the target CRS.

Source code in eis_toolkit/raster_processing/reprojecting.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@beartype
def reproject_raster(
    raster: rasterio.io.DatasetReader,
    target_crs: int,
    resampling_method: Literal["nearest", "bilinear", "cubic", "average", "gauss", "max", "min"] = "nearest",
) -> Tuple[np.ndarray, dict]:
    """Reprojects raster to match given coordinate reference system (EPSG).

    Args:
        raster: The raster to be reprojected.
        target_crs: Target CRS as EPSG code.
        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.

    Returns:
        The reprojected raster data.
        The updated metadata.

    Raises:
        NonMatchinCrsException: Raster is already in the target CRS.
    """
    if target_crs == int(raster.crs.to_string()[5:]):
        raise MatchingCrsException("Raster is already in the target CRS.")

    method = RESAMPLE_METHOD_MAP[resampling_method]
    out_image, out_meta = _reproject_raster(raster, target_crs, method)

    return out_image, out_meta