ST_PolygonN function

The ST_PolygonN function takes a multipolygon and an index as input parameters and returns the polygon that is identified by the index. The resulting polygon is represented in the spatial reference system of the specified multipolygon.

If the specified multipolygon is null or is empty, or if the index is smaller than 1 or larger than the number of polygons, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramST_PolygonN(multipolygon,index )

Parameters

multipolygon
A value of type ST_MultiPolygon that represents the multipolygon from which the polygon that is identified by index is returned.
index
A value of type INTEGER that identifies the nth polygon that is to be returned from multipolygon.

Return type

ST_Polygon

Example

In the following example, the lines of results have been reformatted for readability. The spacing in your results will vary according to your online display

This example illustrates the use of ST_PolygonN.

CREATE TABLE sample_mpolys (id INTEGER, geometry ST_MultiPolygon)

INSERT INTO sample_mpolys
  VALUES (1, ST_Polygon ('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))

SELECT id, CAST ( ST_AsText (ST_PolygonN (geometry, 2) ) 
    AS VARCHAR(120) ) SECOND_ELEMENT
FROM sample_mpolys
Results:

ID        SECOND_ELEMENT
--------- -----------------------------------------------------------
        1 POLYGON ((8.000000 24.000000, 9.000000 25.000000, 
                    1.000000 28.000000, 8.000000 24.000000))