get_base_map_center

class pyhelpers.viz.get_base_map_center(gdf, center_method='bounds')[source]

Calculates geographical center and bounding box for a GeoDataFrame.

This function prepares data for web mapping by re-projecting to EPSG:4326 and calculating coordinates based on the chosen geometric method. It is primarily used to determine the initial central point for creating a base map using libraries such as Folium or Leaflet.

Parameters:
  • gdf (geopandas.GeoDataFrame) – Input GeoDataFrame containing geographical features.

  • center_method (str) –

    Method to calculate the center:

    • 'bounds': Calculates the center based on the arithmetic mean of the bounding box corners (quickest method).

    • 'centroid': Calculates the true geometric centroid of the merged (union) geometry (most accurate, requires projection).

    • 'convex_hull': Calculates the centroid of the convex hull of all geometries (quicker than ‘centroid’ for complex data).

Returns:

A tuple containing the re-projected GeoDataFrame and the center coordinates. The center coordinates are returned as a list [latitude, longitude].

Return type:

tuple[list[float], list[float], list[float], geopandas.GeoDataFrame]

Examples:

>>> from pyhelpers.viz import get_base_map_center
>>> import geopandas as gpd
>>> from shapely.geometry import Point
>>> gdf = gpd.GeoDataFrame({'col': [1, 2]}, geometry=[Point(0, 0), Point(2, 2)], crs=4326)
>>> center_lat_lon, ll_lat_lon, ur_lat_lon, gdf_proj = get_base_map_center(gdf)
>>> center_lat_lon
[1.0, 1.0]
>>> center_lat_lon, ll_lat_lon, ur_lat_lon, gdf_proj = get_base_map_center(
...     gdf, center_method='centroid')
>>> center_lat_lon
[1.0001523435165862, 0.9999999999999998]