Skip to content

Commit

Permalink
feat(distance_computation): add max distance parameter to distance co…
Browse files Browse the repository at this point in the history
…mputation and distance to anomaly
  • Loading branch information
nmaarnio committed Apr 29, 2024
1 parent 4226672 commit 0a8a2fd
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions eis_toolkit/vector_processing/distance_computation.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
from numbers import Number

import geopandas as gpd
import numpy as np
from beartype import beartype
from beartype.typing import Union
from beartype.typing import Optional, Union
from rasterio import profiles, transform
from shapely.geometry.base import BaseGeometry, BaseMultipartGeometry

from eis_toolkit.exceptions import EmptyDataFrameException, NonMatchingCrsException
from eis_toolkit.exceptions import EmptyDataFrameException, NonMatchingCrsException, NumericValueSignException
from eis_toolkit.utilities.checks.raster import check_raster_profile
from eis_toolkit.utilities.miscellaneous import row_points


@beartype
def distance_computation(geodataframe: gpd.GeoDataFrame, raster_profile: Union[profiles.Profile, dict]) -> np.ndarray:
def distance_computation(
geodataframe: gpd.GeoDataFrame, raster_profile: Union[profiles.Profile, dict], max_distance: Optional[Number] = None
) -> np.ndarray:
"""Calculate distance from raster cell to nearest geometry.
Args:
geodataframe: The GeoDataFrame with geometries to determine distance to.
raster_profile: The raster profile of the raster in which the distances
to the nearest geometry are determined.
max_distance: The maximum distance in the output array.
Returns:
A 2D numpy array with the distances computed.
Expand All @@ -30,19 +35,25 @@ def distance_computation(geodataframe: gpd.GeoDataFrame, raster_profile: Union[p
raise NonMatchingCrsException("Expected coordinate systems to match between raster and GeoDataFrame.")
if geodataframe.shape[0] == 0:
raise EmptyDataFrameException("Expected GeoDataFrame to not be empty.")
if max_distance is not None and max_distance <= 0:
raise NumericValueSignException("Expected max distance to be a positive number.")

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(
distance_matrix = _distance_computation(
raster_width=raster_width,
raster_height=raster_height,
raster_transform=raster_transform,
geodataframe=geodataframe,
)
if max_distance is not None:
distance_matrix[distance_matrix > max_distance] = max_distance

return distance_matrix


def _calculate_row_distances(
Expand Down

0 comments on commit 0a8a2fd

Please sign in to comment.