ST_NRings

更新时间:
复制 MD 格式

Returns the total number of rings in a polygon or multipolygon geometry, including both the outer ring and any interior rings.

Syntax

integer ST_NRings(geometry geomA);

Parameters

ParameterDescription
geomAThe polygon or MultiPolygon geometry to evaluate.

Description

ST_NRings counts all rings in a polygonal geometry: the outer ring (the boundary of the polygon) and any interior rings (holes inside the polygon). Unlike ST_NumInteriorRings, which counts only interior rings, ST_NRings includes the outer ring in its count.

Additional behavior:

  • Supports 3D objects and preserves Z coordinates.

  • Supports circular strings and curves.

Examples

Compare ST_NRings and ST_NumInteriorRings

This example shows the key difference between the two functions. The polygon has one outer ring and no interior rings, so ST_NRings returns 1 while ST_NumInteriorRings returns 0.

SELECT
    ST_NRings(geom)            AS nrings,
    ST_NumInteriorRings(geom)  AS ninterrings
FROM (
    SELECT 'POLYGON((1 0,0 3,3 0,1 0))'::geometry AS geom
) AS t;
 nrings | ninterrings
--------+-------------
      1 |           0
(1 row)

Polygon with interior rings

The polygon below has one outer ring and one interior ring (a hole), so ST_NRings returns 2.

SELECT ST_NRings('POLYGON((1 0,0 3,3 0,1 0),(1 0,2 0,0 2,1 0))'::geometry);
 st_nrings
-----------
         2
(1 row)