Categories:

Geospatial Functions

ST_CENTROID

Returns the Point representing the geometric center of a geospatial object.

Syntax

ST_CENTROID( <geography_expression> )

Arguments

geography_expression

The argument must be an expression of type GEOGRAPHY.

Returns

Returns a GEOGRAPHY object for the Point that represents geometric center of the input object.

Usage Notes

  • Returns NULL if the input is NULL.

  • If the input object is a GeometryCollection that contains different types of objects (Polygons, LineStrings, and Points), ST_CENTROID uses the type with the highest dimension to determine the geometric center. For example:

    • If the GeometryCollection contains Polygons, LineStrings, and Points, ST_CENTROID uses the Polygons and ignores the LineStrings and Points in the collection.

    • If the GeometryCollection contains LineStrings and Points, ST_CENTROID uses the LineStrings and ignores the Points in the collection.

Examples

The following example returns the Point that represents the geometric center of a LineString.

SELECT ST_CENTROID(
    TO_GEOGRAPHY(
        'LINESTRING(0 0, 0 -2)'
    )
) as center_of_linestring;
+----------------------+
| CENTER_OF_LINESTRING |
|----------------------|
| POINT(0 -1)          |
+----------------------+

The following example returns the Point that represents the geometric center of a Polygon.

SELECT ST_CENTROID(
    TO_GEOGRAPHY(
        'POLYGON((10 10, 10 20, 20 20, 20 10, 10 10))'
    )
) as center_of_polygon;
+------------------------+
| CENTER_OF_POLYGON      |
|------------------------|
| POINT(15 15.014819855) |
+------------------------+

The following example returns the Point that represents the geometric center of a GeometryCollection. This collection contains a Polygon, LineString, and Point. ST_CENTROID only uses the Polygon (and ignores the LineString and Point) when determining the geometric center.

SELECT ST_CENTROID(
    TO_GEOGRAPHY(
        'GEOMETRYCOLLECTION(POLYGON((10 10, 10 20, 20 20, 20 10, 10 10)), LINESTRING(0 0, 0 -2), POINT(50 -50))'
    )
) as center_of_collection_with_polygons;
+------------------------------------+
| CENTER_OF_COLLECTION_WITH_POLYGONS |
|------------------------------------|
| POINT(15 15.014819855)             |
+------------------------------------+