Skip to content

cluster_area

Compute the area of a hull of a GeoDataFrame.

Parameters:

Name Type Description Default
gdf GeoDataFrame

The GeoDataFrame containing the cluster data.

required
hull_type str

The type of hull to compute. One of: "alpha_shape", "convex_hull", "ellipse".

'alpha_shape'
**kwargs

Additional keyword arguments for the hull computation (e.g., step for alpha shape).

{}

Returns:

Name Type Description
float float

The area of the computed hull.

Source code in src/histolytics/spatial_clust/clust_metrics.py
def cluster_area(
    gdf: gpd.GeoDataFrame, hull_type: str = "alpha_shape", **kwargs
) -> float:
    """Compute the area of a hull of a GeoDataFrame.

    Parameters:
        gdf (gpd.GeoDataFrame):
            The GeoDataFrame containing the cluster data.
        hull_type (str):
            The type of hull to compute. One of: "alpha_shape", "convex_hull", "ellipse".
        **kwargs:
            Additional keyword arguments for the hull computation
            (e.g., `step` for alpha shape).

    Returns:
        float: The area of the computed hull.
    """
    allowed_hulls = ["alpha_shape", "convex_hull", "ellipse"]
    if hull_type not in allowed_hulls:
        raise ValueError(f"Invalid hull type. Allowed values are: {allowed_hulls}")

    xy = get_centroid_numpy(gdf)
    hull_poly = hull(xy, hull_type=hull_type, **kwargs)

    return hull_poly.area