Skip to content

get_centroid_numpy

Get the centroid coordinates of a GeoDataFrame as a numpy array.

Parameters:

Name Type Description Default
gdf GeoDataFrame

Input GeoDataFrame.

required

Returns:

Type Description
ndarray

np.ndarray: A numpy array of shape (n, 2) containing the centroid coordinates of each geometry in the GeoDataFrame.

Examples:

>>> from histolytics.utils.gdf import get_centroid_numpy
>>> from histolytics.data import hgsc_cancer_nuclei
>>> gdf = hgsc_cancer_nuclei()
>>> centroids = get_centroid_numpy(gdf)
>>> print(centroids)
    [[1400.03798043    1.69248393]
    [1386.45857876    9.58076168]
    [1378.29668867  170.69547823]
    ...
    [ 847.54653982  425.80712554]
    [ 954.08683652  520.35605096]
    [ 784.46362434  483.4973545 ]]
Source code in src/histolytics/utils/gdf.py
def get_centroid_numpy(gdf: gpd.GeoDataFrame) -> np.ndarray:
    """Get the centroid coordinates of a GeoDataFrame as a numpy array.

    Parameters:
        gdf (gpd.GeoDataFrame):
            Input GeoDataFrame.

    Returns:
        np.ndarray:
            A numpy array of shape (n, 2) containing the centroid coordinates
            of each geometry in the GeoDataFrame.

    Examples:
        >>> from histolytics.utils.gdf import get_centroid_numpy
        >>> from histolytics.data import hgsc_cancer_nuclei
        >>> gdf = hgsc_cancer_nuclei()
        >>> centroids = get_centroid_numpy(gdf)
        >>> print(centroids)
            [[1400.03798043    1.69248393]
            [1386.45857876    9.58076168]
            [1378.29668867  170.69547823]
            ...
            [ 847.54653982  425.80712554]
            [ 954.08683652  520.35605096]
            [ 784.46362434  483.4973545 ]]
    """
    return shapely.get_coordinates(gdf.centroid)