Fork me on GitHub

ST_InteriorRingN

Signature

LINESTRING ST_InteriorRing(GEOMETRY geom, INT n);

Description

Returns the nth interior ring of geom if geom is a POLYGON. Returns NULL otherwise.

Do I start counting from 0 or 1?

The index runs from 1 to N.

Implements the OpenGIS Simple Features Implementation Specification for SQL version 1.2.1.

Example

SELECT ST_InteriorRingN('POLYGON((0 0, 10 0, 10 6, 0 6, 0 0),
                                 (1 1, 2 1, 2 5, 1 5, 1 1),
                                 (8 5, 8 4, 9 4, 9 5, 8 5))', 1);
-- Answer: LINEARRING(1 1, 2 1, 2 5, 1 5, 1 1)

SELECT ST_InteriorRingN('POLYGON((0 0, 10 0, 10 6, 0 6, 0 0),
                                 (1 1, 2 1, 2 5, 1 5, 1 1),
                                 (8 5, 8 4, 9 4, 9 5, 8 5))', 2);
-- Answer: LINEARRING(8 5, 8 4, 9 4, 9 5, 8 5)

SELECT ST_InteriorRingN('POLYGON((0 0, 10 0, 10 6, 0 6, 0 0),
                                 (1 1, 2 1, 2 5, 1 5, 1 1),
                                 (8 5, 8 4, 9 4, 9 5, 8 5))', 3);
-- Answer: Interior ring index out of range. Must be between 1 and
-- ST_NumInteriorRings.

SELECT ST_InteriorRingN('POINT(1 2)', 1);
-- Answer: NULL
Comparison with ST_ExteriorRing
SELECT ST_ExteriorRing('POLYGON((0 0, 10 0, 10 6, 0 6, 0 0),
                                (1 1, 2 1, 2 5, 1 5, 1 1),
                                (8 5, 8 4, 9 4, 9 5, 8 5))') ER,
       ST_InteriorRingN('POLYGON((0 0, 10 0, 10 6, 0 6, 0 0),
                                (1 1, 2 1, 2 5, 1 5, 1 1),
                                (8 5, 8 4, 9 4, 9 5, 8 5))', 2) IRN;

-- Answer:
-- |             ER              |            IRN               |
-- |-----------------------------|------------------------------|
-- | LINEARRING(0 0, 10 0, 10 6, |   LINEARRING(8 5, 8 4, 9 4,  |
-- |            0 6, 0 0)        |              9 5, 8 5)       |

See also