dtoolkit.geoaccessor.geoseries.geocentroid#

dtoolkit.geoaccessor.geoseries.geocentroid(s: GeoSeries, /, weights: Series = None, max_iter: int = 300, tol: float = 1e-05) Point[source]#

Return the centroid of all points via the center of gravity method.

\[\begin{split}\left\{\begin{matrix} d_i &=& D(P(\bar{x}_n, \bar{y}_n), P(x_i, y_i)) \\ \bar{x}_0 &=& \frac{\sum w_i x_i}{\sum w_i} \\ \bar{y}_0 &=& \frac{\sum w_i y_i}{\sum w_i} \\ \bar{x}_{n+1} &=& \frac{\sum w_i x_i / d_i}{\sum w_i / d_i} \\ \bar{y}_{n+1} &=& \frac{\sum w_i y_i / d_i}{\sum w_i / d_i} \\ \end{matrix}\right.\end{split}\]
Parameters:
weightsHashable or 1d array-like, optional
  • None : All weights will be set to 1.

  • Hashable : Only for DataFrame, the column name.

  • 1d array-like : The weights of each point.

max_iterint, default 300

Maximum number of iterations to perform.

tolfloat, default 1e-5

Tolerance for convergence.

Returns:
Point

Examples

>>> import dtoolkit.geoaccessor
>>> import geopandas as gpd
>>> from shapely import Point
>>> df = gpd.GeoDataFrame(
...     {
...         "weights": [1, 2, 3],
...         "geometry": [Point(100, 32), Point(120, 50), Point(122, 55)],
...     },
...     crs=4326,
... )
>>> df
   weights                    geometry
0        1  POINT (100.00000 32.00000)
1        2  POINT (120.00000 50.00000)
2        3  POINT (122.00000 55.00000)
>>> df.geocentroid()
<POINT (120 50)>

Set weights for each point.

>>> df.geocentroid("weights")
<POINT (121.999 54.999)>
>>> df.geocentroid([1, 2, 3])
<POINT (121.999 54.999)>