名称

ST_Centroid — 返回几何体的几何中心。

大纲

geometry ST_Centroid(geometry g1);

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

描述

计算作为几何体的几何质心的点。 对于 [MULTI]POINT,质心是输入坐标的算术平均值。 对于 [MULTI]LINESTRING,质心是使用每条线段的加权长度计算的。 对于 [MULTI]POLYGON,质心是根据面积计算的。 如果提供了空几何图形,则返回空的GEOMETRYCOLLECTION。 如果提供 NULL,则返回 NULL。 如果提供了CIRCULARSTRINGCOMPOUNDCURVE,它们首先使用CurveToLine转换为线串,然后与LINESTRING相同

对于混合维度输入,结果等于最高维度的组件几何图形的质心(因为较低维度的几何图形对质心的“权重”贡献为零)。

请注意,对于多边形几何形状,质心不一定位于多边形的内部。 例如,请参见下图 C 形多边形的质心。 要构造保证位于多边形内部的点,请使用 ST_PointOnSurface

2.3.0 中的新增功能:支持 CIRCULARSTRINGCOMPOUNDCURVE(使用 CurveToLine)

可用性:2.4.0 引入了对地理的支持。

此方法实现了 SQL 1.1 的 OGC 简单功能规范。

该方法实现了SQL/MM规范。 SQL-MM 3: 8.1.4, 9.5.5

示例

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)');
栅格输出
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)');
栅格输出
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))');
栅格输出
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))');
栅格输出
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 ;
栅格输出
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;
栅格输出
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);
栅格输出
MULTIPOINT((0.5 0.5),(2.5 2.5))
Figure
Geometry figure for visual-st-centroid-07