ST_GeometryN

更新时间:
复制 MD 格式

Returns the Nth sub-geometry from a geometry collection.

Syntax

geometry ST_GeometryN(geometry geomA, integer n)

Parameters

ParameterDescription
geomAThe geometry object.
nThe 1-based index of the sub-geometry to return.

Description

  • The index starts at 1, not 0.

  • Supported geometry types: GeometryCollection, MultiPoint, MultiLineString, MultiCurve, MultiPolygon, and polyhedral surfaces. Returns NULL for all other geometry types.

  • Also supports circular strings, curves, triangles, triangulated irregular network (TIN) surfaces, and 3D objects.

  • To extract all sub-geometries from a geometry object, use ST_Dump instead. ST_Dump is more efficient and supports a broader range of geometry types.

Examples

Return the second polygon from a MULTIPOLYGON:

SELECT ST_AsText(ST_GeometryN('MULTIPOLYGON(((1 0,3 0,0 3,1 0)),((1 0,2 0, 0 2,1 0)))'::geometry,2));
         st_astext
----------------------------
 POLYGON((1 0,2 0,0 2,1 0))
(1 row)

Return the first sub-geometry from a GeometryCollection:

SELECT ST_AsText(ST_GeometryN('GeometryCollection(POINT(1 0),POLYGON((1 0,2 0, 0 2,1 0)))'::geometry,1));
 st_astext
------------
 POINT(1 0)
(1 row)