ST_NumGeometries

ST_NumGeometries takes a geometry type as an input parameter and returns the number of geometries in the collection.

If the given geometry type is null or is empty, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramdb2gse.ST_NumGeometries(geometry)

Parameter

geometry
A value of type ST_MultiPoint, ST_MultiLineString, or ST_MultiPolygon that represents the geometry type for which the number of geometries is returned.

Return Type

INTEGER

Example

Two geometry types are stored in the SAMPLE_GEOMCOLL table. One is a multipolygon, and the other is a multipoint. The ST_NumGeometries function determines how many individual geometries are within each geometry type.

SET CURRENT FUNCTION PATH = CURRENT FUNCTION PATH, db2gse
CREATE TABLE sample_geomcoll (id INTEGER, geometry ST_Geometry)

INSERT INTO sample_geomcoll
  VALUES (1, ST_Geometry(ST_MultiPolygon 
                                ('multipolygon (( (3 3, 4 6, 5 3, 3 3),
                                (8 24, 9 25, 1 28, 8 24),
                                (13 33, 7 36, 1 40, 10 43, 13 33) ))', 1)) )

INSERT INTO sample_geomcoll
  VALUES (2, ST_Geometry(ST_MultiPoint 
                              ('multipoint (1 2, 4 3, 5 6, 7 6, 8 8)', 1)) )

SELECT id, ST_NumGeometries (geometry) NUM_GEOMS_IN_COLL
  FROM sample_geomcoll
Results:

ID          NUM_GEOMS_IN_COLL
----------- -----------------
          1                 3
          2                 5