Skip to content

Distance computation

distance_computation(raster_profile, geometries)

Calculate distance from raster cell to nearest geometry.

Parameters:

Name Type Description Default
raster_profile Union[Profile, dict]

The raster profile of the raster in which the distances to the nearest geometry are determined.

required
geometries GeoDataFrame

The geometries to determine distance to.

required

Returns:

Type Description
ndarray

A 2D numpy array with the distances computed.

Source code in eis_toolkit/vector_processing/distance_computation.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@beartype
def distance_computation(raster_profile: Union[profiles.Profile, dict], geometries: gpd.GeoDataFrame) -> np.ndarray:
    """Calculate distance from raster cell to nearest geometry.

    Args:
        raster_profile: The raster profile of the raster in which the distances
            to the nearest geometry are determined.
        geometries: The geometries to determine distance to.

    Returns:
        A 2D numpy array with the distances computed.

    """
    if raster_profile.get("crs") != geometries.crs:
        raise NonMatchingCrsException("Expected coordinate systems to match between raster and geometries. ")
    if geometries.shape[0] == 0:
        raise EmptyDataFrameException("Expected GeoDataFrame to not be empty.")

    check_raster_profile(raster_profile=raster_profile)

    raster_width = raster_profile.get("width")
    raster_height = raster_profile.get("height")
    raster_transform = raster_profile.get("transform")

    return _distance_computation(
        raster_width=raster_width, raster_height=raster_height, raster_transform=raster_transform, geometries=geometries
    )