environmentaltools.spatial.centroid_region

environmentaltools.spatial.centroid_region(vertices)[source]

Calculate centroid of a polygon region.

Computes the geometric centroid (center of mass) of a polygon defined by ordered vertices. Uses the standard polygon centroid formula based on signed area.

Parameters:

vertices (np.ndarray) – Polygon vertices as (N, 2) array of [x, y] coordinates. First and last vertex should be identical (closed polygon).

Returns:

Centroid coordinates as (1, 2) array [[x_centroid, y_centroid]]

Return type:

np.ndarray

Notes

The formula computes the centroid using:

\[ \begin{align}\begin{aligned}C_x = \frac{1}{6A} \sum_{i=0}^{n-1} (x_i + x_{i+1})(x_i y_{i+1} - x_{i+1} y_i)\\C_y = \frac{1}{6A} \sum_{i=0}^{n-1} (y_i + y_{i+1})(x_i y_{i+1} - x_{i+1} y_i)\end{aligned}\end{align} \]

where A is the signed polygon area.

Examples

>>> import numpy as np
>>> # Square polygon
>>> vertices = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
>>> centroid = centroid_region(vertices)
>>> print(centroid)
[[0.5, 0.5]]