This topic describes the ST_GeometryN function. This function returns the Nth object of the input geometry object.

Syntax

geometry  ST_GeometryN(geometry  geomA , integer  n);

Parameters

Parameter Description
geomA The geometry object that you want to specify.
n The index number of the object that you want to obtain.

Description

  • The index number starts from 1.
  • This function supports GeometryCollection objects, MultiPoint objects, MultiLineString objects, MultiCurve objects, MultiPolygon objects, and polyhedral surfaces. If you specify another type of geometry object, this function returns NULL.
  • If you want to extract all the geometries of the input geometry object, you can use the ST_Dump function, which is more efficient and supportsvarious geometry objects.
  • This function supports circular strings, curves, polyhedral surfaces, triangles, triangulated irregular network (TIN) surfaces, and 3D objects.

Examples

  • Execute the following statement to return a MULTI object:
    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)
                        
  • Execute the following statement to return a GeometryCollection objects:
    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)