Skip to content

clip_gdf

cellseg_gsontools.clip_gdf(gdf, bbox)

Clip a gdf to a bounding box.

Parameters:

Name Type Description Default
gdf GeoDataFrame

Input geodataframe.

required
bbox Tuple[int, int, int, int]

Bounding box to clip to. Format: (xmin, ymin, xmax, ymax).

required

Returns:

Type Description
GeoDataFrame

gpd.GeoDataFrame: The Clipped gdf.

Examples:

>>> from cellseg_gsontools import clip_gdf
>>> gdf = clip_gdf(gdf, bbox=(0, 0, 100, 100))
Source code in cellseg_gsontools/utils.py
def clip_gdf(
    gdf: gpd.GeoDataFrame, bbox: Tuple[int, int, int, int]
) -> gpd.GeoDataFrame:
    """Clip a gdf to a bounding box.

    Parameters:
        gdf (gpd.GeoDataFrame):
            Input geodataframe.
        bbox (Tuple[int, int, int, int]):
            Bounding box to clip to. Format: (xmin, ymin, xmax, ymax).

    Returns:
        gpd.GeoDataFrame:
            The Clipped gdf.

    Examples:
        >>> from cellseg_gsontools import clip_gdf
        >>> gdf = clip_gdf(gdf, bbox=(0, 0, 100, 100))
    """
    xmin = bbox[0]
    ymin = bbox[1]
    xmax = bbox[2]
    ymax = bbox[3]

    crop = Polygon(
        [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)]
    )

    return gdf.clip(crop)