ST_Centroid function

The ST_Centroid function takes a geometry as an input parameter and returns the geometric center, which is the center of the minimum bounding rectangle of the specified geometry, as a point. The resulting point is represented in the spatial reference system of the specified geometry.

If the specified geometry is null or is empty, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramST_Centroid(geometry)

Parameter

geometry
A value of type ST_Geometry or one of its subtypes that represents the geometry to determine the geometric center.

Return type

ST_Point

Example

This example creates two geometries and finds the centroid of them.


CREATE TABLE sample_geoms (id INTEGER, geometry ST_Geometry)

INSERT INTO sample_geoms VALUES
    (1, ST_Polygon('polygon
    ((40 120, 90 120, 90 150, 40 150, 40 120),
    (50 130, 80 130, 80 140, 50 140, 50 130))',0))

INSERT INTO sample_geoms VALUES
    (2, ST_MultiPoint('multipoint(10 10, 50 10, 10 30)' ,0))


SELECT id, CAST(ST_AsText(ST_Centroid(geometry)) 
   as VARCHAR(40)) Centroid
FROM sample_geoms

Results:

ID          CENTROID
----------- ----------------------------------------
          1 POINT (65 135)
          2 POINT (30 20)