meshflow.utility.geom.extract_centroid

meshflow.utility.geom.extract_centroid(gdf, obj_id, epsg=4326)[source]

Extract centroid latitude and longitude for each polygon in a GeoDataFrame.

Parameters:
  • gdf (geopandas.GeoDataFrame) – GeoDataFrame containing polygon geometries in the ‘geometry’ column.

  • obj_id (str) – Name of the column containing unique IDs for each polygon element.

  • epsg (int, optional) – EPSG code for the output coordinate reference system (default: 4326).

Returns:

DataFrame with columns:
  • obj_id: IDs for each polygon (copied from input)

  • lat: Centroid latitude in the specified CRS

  • lon: Centroid longitude in the specified CRS

Return type:

pandas.DataFrame

Notes

  • If the input GeoDataFrame has no CRS, EPSG:4326 is assumed.

  • Only polygon geometries are supported; other types may yield errors.

  • Centroids are calculated in an equal-area projection and reprojected to the requested CRS for accurate coordinates.

Examples

>>> import geopandas as gpd
>>> from shapely.geometry import Polygon
>>> gdf = gpd.GeoDataFrame({
...     'id': [1],
...     'geometry': [Polygon([(0,0), (1,0), (1,1), (0,1)])]
... }, crs='EPSG:4326')
>>> extract_centroid(gdf, obj_id='id')