ST_NumPolygons function

The ST_NumPolygons function takes a multipolygon as an input parameter and returns the number of polygons that it contains.

If the specified multipolygon is null or is empty, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramST_NumPolygons(multipolygon)

Parameter

multipolygon
A value of type ST_MultiPolygon that represents the multipolygon for which the number of polygons is returned.

Return type

INTEGER

Example

Multipolygons are stored in the SAMPLE_MPOLYS table. The ST_NumPolygons function determines how many individual geometries are within each multipolygon.

CREATE TABLE sample_mpolys (id INTEGER, geometry ST_MultiPolygon)

INSERT INTO sample_mpolys
  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_polys
  VALUES (2,
          ST_MultiPolygon ('multipolygon empty', 1) )

INSERT INTO sample_polys
  VALUES (3,
          ST_MultiPolygon ('multipolygon (( (3 3, 4 6, 5 3, 3 3),
                                 (13 33, 7 36, 1 40, 10 43, 13 33) ))', 1) )

SELECT id, ST_NumPolygons (geometry) NUM_WITHIN
  FROM sample_mpolys
Results:

ID          NUM_WITHIN
----------- ----------
          1          3
          2          0
          3          2