ST_GeometricMedian
Returns the geometric median of a geometry object.
Syntax
geometry ST_GeometricMedian(geometry g, float8 tolerance, int maxIter, boolean failIfNotConverged);Parameters
Parameter | Description |
| The geometry object. |
| The convergence threshold. The function stops iterating when the distance change between successive iterations falls below this value. If not specified, the default tolerance is calculated from the extent of |
| The maximum number of iterations. |
| Whether to return an error if the function does not converge within |
Usage notes
Convergence behavior
The function iterates until the distance change between successive iterations is less than tolerance. If the distance change remains greater than or equal to tolerance after maxIter iterations, the function exits with an error — unless failIfNotConverged is set to false.
Default tolerance
If tolerance is not specified, the function calculates a default tolerance based on the extent of g.
M coordinates as weights
If the points in g have M coordinates, those values are interpreted as the relative weights of the points.
Examples
The following example compares the output of ST_GeometricMedian and ST_Centroid on the same four-point MultiPoint. The geometric median (approximately (0.67, 0.67)) shifts toward the cluster of three nearby points, while the centroid ((0.75, 0.5)) is pulled by the more isolated point at (2 0).
SELECT ST_AsText(ST_GeometricMedian(geom)) AS GeometricMedian,
ST_AsText(ST_Centroid(geom)) AS Centroid
FROM (
SELECT 'MULTIPOINT((0 0), (0 1), (1 1), (2 0))'::geometry AS geom
) AS test;Expected output:
geometricmedian | centroid
--------------------------------------------+-----------------
POINT(0.665913838138866 0.666097415551148) | POINT(0.75 0.5)
(1 row)
