ST_Centroid — Returns a point, which is the centroid (geometric center) of a geometry, which may lie outside the geometry
geometry ST_Centroid(
bytea
Geometry)
;
Returns the centroid (geometric center) of a geometry, which may lie outside the geometry.
The centroid is equal to the centroid of the set of component Geometries of highest dimension (since the lower-dimension geometries contribute zero "weight" to the centroid)
The centroid will have the same SRID as the incoming geometry. |
2D | 3D | M |
---|---|---|
- | - |
OGC SFS for SQL. 1.1 (1999) | OGC SFS for SQL. 1.1.0 (2005) | OGC SFS for SQL. 1.2.0 (2006) | SQL-MM Part 3 |
---|---|---|---|
2.1.9.1 | 7.2.14.1 | 7.2.12.2 | 8.1.4 - 9.5.5 |
SELECT ST_AsEWKT(ST_Centroid(ST_GeomFromText('MULTIPOINT (0 0, 8 0, 5 10, 10 10, 15 20, 10 20)',25830))); --Result SRID=25830;POINT (8 10) SELECT ST_AsEWKT(ST_Centroid(ST_GeomFromText('POLYGON((0 0 2, 0 10 4, 10 10 5, 10 0 5, 0 0 2))'))); --Result POINT (5 5)
MULTIPOINT CENTROID | LINESTRING CENTROID |
POLYGON CENTROID | GEOMETRY COLLECTION CENTROID |
This example extracts the Centroid of a set of Parcels. The parcels are stored in the parcels table created with a CREATE TABLE statement. The INSERT INTO statements that follow insert each one of the parcels geometries. CREATE TABLE "parcels"(id serial PRIMARY KEY); SELECT AddGeometryColumn ('parcels','geom',-1,'POLYGON',2); INSERT INTO "parcels"("geom") SELECT (ST_GeomFromText('POLYGON((6 14, 7.5 15, 10 11, 8 10, 6 14))')); INSERT INTO "parcels"("geom") SELECT (ST_GeomFromText('POLYGON((7 12, 4 10, 3 12, 6 14, 7 12))')); INSERT INTO "parcels"("geom") SELECT (ST_GeomFromText('POLYGON((5 8, 4 10, 7 12, 8 10, 5 8))')); SELECT ST_AsText(ST_Centroid("geom")) from (SELECT "geom" from "parcels") as foo; SELECT DropGeometryTable('parcels'); --Result st_astext ----------------------------------------------------- MULTIPOINT ((7.923423423423424 12.432432432432432)) MULTIPOINT ((5 12)) MULTIPOINT ((6 10))
Parcels | ST_Multicentroid |