Nome

ST_Centroid — Restituisce il centro geometrico di una geometria.

Sinossi

geometry ST_Centroid(geometry g1);

geography ST_Centroid(geography g1, boolean use_spheroid = true);

Descrizione

Calcola un punto che è il centro di massa geometrico di una geometria. Per [MULTI]POINTs, il centroide è la media aritmetica delle coordinate immesse. Per [MULTI]LINESTRINGs, il centroide viene calcolato utilizzando la lunghezza ponderata di ciascun segmento di linea. Per [MULTI]POLYGONs, il centroide viene calcolato in termini di area. Se viene fornita una geometria vuota, viene restituita una GEOMETRYCOLLECTION vuota. Se viene fornito NULL viene restituito NULL. Se vengono fornite CIRCULARSTRING o COMPOUNDCURVE, queste vengono convertite in linestring con CurveToLine e poi come per LINESTRING

Per gli input a dimensione mista, il risultato è uguale al centroide delle geometrie componenti di dimensione più elevata (poiché le geometrie di dimensione inferiore contribuiscono al centroide con un "peso" nullo).

Si noti che per le geometrie poligonali il centroide non si trova necessariamente all'interno del poligono. Ad esempio, si veda la figura seguente del centroide di un poligono a forma di C. Per costruire un punto con garanzia di giacenza nell'interno di un poligono si usa ST_PointOnSurface.

New in 2.3.0 : supports CIRCULARSTRING and COMPOUNDCURVE (using CurveToLine)

Availability: 2.4.0 support for geography was introduced.

Questo metodo implementa le OGC Simple Features Implementation Specification for SQL 1.1.

Questo metodo implementa la specifica SQL/MM. SQL-MM 3: 8.1.4, 9.5.5

Esempi

Centroid of a MultiPoint.

Code
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)');
Output
POINT(60.333 77.667)
Figure
Geometry figure for st-centroid-multipoint

Centroid of a LineString.

Code
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)');
Output
POINT(76.191 79.876)
Figure
Geometry figure for visual-st-centroid-02

Centroid of a Polygon.

Code
SELECT ST_Centroid(
  'POLYGON ((190 190,10 190,10 10,190 10,190 20,160 30,60 30,60 130,190 140,190 190))');
Output
POINT(80.251 113.405)
Figure
Geometry figure for visual-st-centroid-03

Centroid of the highest-dimension components of a GeometryCollection.

Code
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))');
Output
POINT(143.361 148.263)
Figure
Geometry figure for visual-st-centroid-04
Code
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 ;
Output
st_astext
-------------
 POINT(0.5 1)
(1 row)
Figure
Geometry figure for visual-st-centroid-05
Code
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;
Output
st_astext
-------------
 POINT(0.5 1)
(1 row)
Figure
Geometry figure for visual-st-centroid-06

Centroids of the parts of a MultiPolygon.

Code
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);
Output
MULTIPOINT((0.5 0.5),(2.5 2.5))
Figure
Geometry figure for visual-st-centroid-07