The ST_NumGeometries() function

The ST_NumGeometries() function takes a ST_GeomCollection (ST_MultiPoint, ST_MultiLineString, or ST_MultiPolygon) and returns the number of geometries in the collection.

Syntax

ST_NumGeometries(mpt1 ST_MultiPoint)
ST_NumGeometries(mln1 ST_MultiLineString)
ST_NumGeometries(mpl1 ST_MultiPolygon)

Return type

INTEGER

Example

The city engineer needs to know the number of distinct buildings associated with each building footprint.

The building footprints are stored in the buildingfootprints table that was created with the following CREATE TABLE statement:
CREATE TABLE buildingfootprints (building_id  integer,
                                 lot_id       integer,
                                 footprint    ST_MultiPolygon);
The query lists the building_id that uniquely identifies each building and the number of buildings in each footprint with the ST_NumGeometries() function:
SELECT building_id, ST_NumGeometries(footprint) no_of_buildings
   FROM buildingfootprints;

building_id no_of_buildings 

        506               1
        543               1
       1208               1
        178               1