ST_Centroid
Name
ST_Centroid — Returns the geometric center of a geometry.
Synopsis
geometry
ST_Centroid
(
geometry
g1
)
;
geography
ST_Centroid
(
geography
g1
, boolean
use_spheroid = true
)
;
Description
Computes a point which is the geometric center of mass of a geometry.
For [
MULTI
]
POINT
s,
the centroid is the arithmetic mean of the input coordinates.
For [
MULTI
]
LINESTRING
s,
the centroid is computed using the weighted length of each line segment.
For [
MULTI
]
POLYGON
s,
the centroid is computed in terms of area.
If an empty geometry is supplied, an empty
GEOMETRYCOLLECTION
is returned.
If
NULL
is supplied,
NULL
is returned.
If
CIRCULARSTRING
or
COMPOUNDCURVE
are supplied, they are converted to linestring with CurveToLine first,
then same than for
LINESTRING
For mixed-dimension input, the result is equal to the centroid of the component Geometries of highest dimension (since the lower-dimension geometries contribute zero "weight" to the centroid).
Note that for polygonal geometries the centroid does not necessarily lie in the interior of the polygon. For example, see the diagram below of the centroid of a C-shaped polygon. To construct a point guaranteed to lie in the interior of a polygon use ST_PointOnSurface .
New in 2.3.0 : supports
CIRCULARSTRING
and
COMPOUNDCURVE
(using CurveToLine)
Availability: 2.4.0 support for geography was introduced.
This method implements the
OGC Simple Features
Implementation Specification for SQL 1.1.
This method implements the SQL/MM specification.
SQL-MM 3: 8.1.4, 9.5.5
Examples
Centroid of a MultiPoint.
SELECT ST_Centroid( 'MULTIPOINT (8 24,10 92,12 154,17 68,28 10,29 52,29 84,55 50,56 24,131 14,160 180,189 180)');
POINT(60.333 77.667)
Centroid of a LineString.
SELECT ST_Centroid( 'LINESTRING (190 160,10 190,40 90,20 70,10 10,30 40,30 10,110 40,70 10,110 10,140 40,140 10,160 30,180 10)');
POINT(76.191 79.876)
Centroid of a Polygon.
SELECT ST_Centroid( 'POLYGON ((190 190,10 190,10 10,190 10,190 20,160 30,60 30,60 130,190 140,190 190))');
POINT(80.251 113.405)
Centroid of the highest-dimension components of a GeometryCollection.
SELECT ST_Centroid(
'GEOMETRYCOLLECTION (POLYGON ((190 170,180 100,80 140,80 160,130 160,110 180,110 190,180 180,190 170)),
LINESTRING (80 120,120 20,140 70,150 30,180 50,190 10),
MULTIPOINT (19 150,22 49,30 13,32 101,45 35,67 88,75 16))');
POINT(143.361 148.263)
SELECT ST_Centroid(g)
FROM ST_GeomFromText('CIRCULARSTRING(0 2,-1 1,0 0,0.5 0,1 0,2 1,1 2,0.5 2,0 2)') AS g ;
st_astext ------------- POINT(0.5 1) (1 row)
SELECT ST_Centroid(g)
FROM ST_GeomFromText('COMPOUNDCURVE(CIRCULARSTRING(0 2,-1 1,0 0),(0 0,0.5 0,1 0), CIRCULARSTRING( 1 0,2 1,1 2),(1 2,0.5 2,0 2))' ) AS g;
st_astext ------------- POINT(0.5 1) (1 row)
Centroids of the parts of a MultiPolygon.
SELECT ST_Collect(ST_Centroid(geom) ORDER BY path)
FROM ST_Dump('MULTIPOLYGON (
((0 0,0 1,1 1,1 0,0 0)),
((2 2,2 3,3 3,3 2,2 2))
)'::geometry);
MULTIPOINT((0.5 0.5),(2.5 2.5))
See Also
ST_Collect , ST_Dump , ST_PointOnSurface , ST_GeometricMedian