Name

ST_GeometryN — Returns the Nth geometry object in a Collection.

Synopsis

geometry ST_GeometryN(varchar WKT, integer SRID);

Alias

GeometryN

Description

Returns the Nth geometry object in a Collection. The incoming geometry shall be a collection type like MULTIPOINT, MULTILINESTRING, MULTIPOLYGON or GEOMETRYCOLLECTION. Otherwise, returns NULL.

N index starts at 1. If N is less than one or greater than the cardinality of the Interior Rings attribute, then the function returns Null.

Coordinate Dimensions
2D3DM
Spatial Standards Support
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.2.17.2.16.17.2.159.1.5

Examples

--Return the 1st geometry
SELECT ST_AsEWKT(
 GeometryN(
   ST_GeomFromText('GEOMETRYCOLLECTION(POINT (5 5 10),LINESTRING (0 0 10, 0 50 10))'),1
  ));
--Result
POINT (5 5 10)

----Return the 3rd point
SELECT ST_AsEWKT(
 GeometryN(
   ST_GeomFromText('MULTIPOINT ((2 1), (1 2), (4 4), (2 4), (5 1))'),3
  ));
--Result
POINT (4 4)

Related functions

ST_InteriorRingN