ST_NumGeometries function
The ST_NumGeometries function takes a geometry collection as an input parameter and returns the number of geometries in the collection.
If the given geometry collection is null or is empty, then null is returned.
This function can also be called as a method.
Syntax
Parameter
- collection
- A value of type ST_GeomCollection or one of its subtypes that represents the geometry collection for which the number of geometries is returned.
Return Type
INTEGER
Example
Two geometry collections 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 collection.
SET CURRENT FUNCTION PATH = CURRENT FUNCTION PATH, db2gse
CREATE TABLE sample_geomcoll (id INTEGER, geometry ST_GeomCollection)
INSERT INTO sample_geomcoll
VALUES (1,
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_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