Skip to content

quadbin_grid

Fit a quadbin rectangular grid on top of a geopandas.GeoDataFrame.

Parameters:

Name Type Description Default
gdf GeoDataFrame

GeoDataFrame to fit grid to.

required
resolution int

Quadbin resolution, by default 17.

17
to_lonlat bool

Whether to convert to lonlat coordinates, by default True.

True

Returns:

Type Description
GeoDataFrame

gpd.GeoDataFrame: Fitted Quadbin quad grid.

Examples:

>>> from histolytics.spatial_ops.quadbin import quadbin_grid
>>> from histolytics.data import cervix_tissue
>>>
>>> # get the stromal tissue
>>> tis = cervix_tissue()
>>> stroma = tis[tis["class_name"] == "stroma"]
>>>
>>> # Fit a quadbin grid to the stromal tissue
>>> quad_grid = quadbin_grid(stroma, resolution=17)
>>> print(quad_grid.head(3))
                                                            geometry
5271089524171866111  POLYGON ((6581.37043 761.23896, 6581.36916 608...
5271089524172062719  POLYGON ((6734.64415 761.23754, 6734.64288 608...
5271089524171931647  POLYGON ((6734.64571 913.48504, 6734.64415 761...
>>> ax = tis.plot(column="class_name", figsize=(5, 5), aspect=1, alpha=0.5)
>>> quad_grid.plot(ax=ax, edgecolor="black", facecolor="none", lw=1)
>>> ax.set_axis_off()

out

Source code in src/histolytics/spatial_ops/quadbin.py
def quadbin_grid(
    gdf: gpd.GeoDataFrame, resolution: int = 17, to_lonlat: bool = True
) -> gpd.GeoDataFrame:
    """Fit a `quadbin` rectangular grid on top of a `geopandas.GeoDataFrame`.

    Parameters:
        gdf (gpd.GeoDataFrame):
            GeoDataFrame to fit grid to.
        resolution (int):
            Quadbin resolution, by default 17.
        to_lonlat (bool):
            Whether to convert to lonlat coordinates, by default True.

    Returns:
        gpd.GeoDataFrame:
            Fitted Quadbin quad grid.

    Examples:
        >>> from histolytics.spatial_ops.quadbin import quadbin_grid
        >>> from histolytics.data import cervix_tissue
        >>>
        >>> # get the stromal tissue
        >>> tis = cervix_tissue()
        >>> stroma = tis[tis["class_name"] == "stroma"]
        >>>
        >>> # Fit a quadbin grid to the stromal tissue
        >>> quad_grid = quadbin_grid(stroma, resolution=17)
        >>> print(quad_grid.head(3))
                                                                    geometry
        5271089524171866111  POLYGON ((6581.37043 761.23896, 6581.36916 608...
        5271089524172062719  POLYGON ((6734.64415 761.23754, 6734.64288 608...
        5271089524171931647  POLYGON ((6734.64571 913.48504, 6734.64415 761...
        >>> ax = tis.plot(column="class_name", figsize=(5, 5), aspect=1, alpha=0.5)
        >>> quad_grid.plot(ax=ax, edgecolor="black", facecolor="none", lw=1)
        >>> ax.set_axis_off()
    ![out](../../img/quadbin_grid.png)
    """
    if gdf.empty or gdf is None:
        return

    # drop invalid geometries if there are any after buffer
    gdf.loc[:, "geometry"] = gdf.make_valid()
    orig_crs = gdf.crs

    poly = shapely.force_2d(gdf.union_all())
    if isinstance(poly, Polygon):
        quads = _poly2hexgrid(poly, resolution=resolution, to_lonlat=to_lonlat)
    else:
        output = []
        for geom in poly.geoms:
            hexes = _poly2hexgrid(geom, resolution=resolution, to_lonlat=to_lonlat)
            output.append(hexes)
        quads = pd.concat(output)

    return quads.set_crs(orig_crs, allow_override=True).drop_duplicates("geometry")