How the ST_NumInteriorRings() function works in Mariadb?

The ST_NumInteriorRings() function in MariaDB is used to determine the number of interior rings (holes) in a polygon geometry.

Posted on

The ST_NumInteriorRings() function in MariaDB is used to determine the number of interior rings (holes) in a polygon geometry. A polygon with no interior rings is often referred to as a simple polygon, while a polygon with one or more interior rings is called a complex polygon.

Syntax

The syntax for the MariaDB ST_NumInteriorRings() function is as follows:

ST_NumInteriorRings(geom)
  • geom: The input geometry, which must be a polygon or multi-polygon. If the input is not a polygon or multi-polygon, the function will return NULL.

The function returns an integer value representing the number of interior rings in the input geometry. For a simple polygon, the return value will be 0. For a complex polygon, the return value will be the number of interior rings it contains.

Examples

Example 1: Simple polygon with no interior rings

This example demonstrates how ST_NumInteriorRings() handles a simple polygon with no interior rings.

SELECT ST_NumInteriorRings(ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))'));

The following is the output:

0

As expected, the function returns 0 since the input polygon has no interior rings.

Example 2: Complex polygon with one interior ring

This example shows how ST_NumInteriorRings() handles a complex polygon with one interior ring.

SELECT ST_NumInteriorRings(ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 2 8, 8 8, 8 2, 2 2))'));

The following is the output:

1

The function correctly identifies the single interior ring and returns 1.

Example 3: Complex polygon with multiple interior rings

This example demonstrates how ST_NumInteriorRings() handles a complex polygon with multiple interior rings.

DROP TABLE IF EXISTS polygons;
CREATE TABLE polygons (id INT PRIMARY KEY, geom GEOMETRY);
INSERT INTO polygons (id, geom) VALUES
  (1, ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 2 8, 8 8, 8 2, 2 2), (3 3, 3 7, 7 7, 7 3, 3 3))'));

SELECT ST_NumInteriorRings(geom) FROM polygons;

The following is the output:

2

The function correctly identifies the two interior rings in the complex polygon and returns 2.

Example 4: Multi-polygon with varying interior rings

This example shows how ST_NumInteriorRings() handles a multi-polygon with varying numbers of interior rings.

DROP TABLE IF EXISTS multi_polygons;
CREATE TABLE multi_polygons (id INT PRIMARY KEY, geom GEOMETRY);
INSERT INTO multi_polygons (id, geom) VALUES
  (1, ST_GeomFromText('MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)), ((15 15, 25 15, 25 25, 15 25, 15 15), (18 18, 18 22, 22 22, 22 18, 18 18)))'));

SELECT ST_NumInteriorRings(geom) FROM multi_polygons;

The following is the output:

1

The function returns 1, which is the total number of interior rings across all polygons in the multi-polygon geometry.

Example 5: Non-polygon geometry input

This example demonstrates how ST_NumInteriorRings() handles non-polygon geometries.

SELECT ST_NumInteriorRings(ST_GeomFromText('LINESTRING(0 0, 10 10)'));

The following is the output:

NULL

Since the input geometry is a linestring and not a polygon or multi-polygon, the function returns NULL.

The following are some functions related to MariaDB ST_NumInteriorRings():

  • MariaDB ST_NumGeometries() function is used to determine the number of geometries in a multi-geometry.
  • MariaDB ST_GeometryType() function is used to retrieve the type of a geometry.
  • MariaDB ST_IsSimple() function is used to check if a geometry is simple (without self-intersections).
  • MariaDB ST_InteriorRingN() function is used to retrieve a specific interior ring from a polygon.

Conclusion

The ST_NumInteriorRings() function in MariaDB is a useful tool for working with polygon geometries. It allows you to determine the number of interior rings (holes) in a polygon or multi-polygon geometry, which can be valuable information for various spatial analysis tasks. By understanding the syntax and providing appropriate examples, this article aimed to provide a comprehensive guide on how to use this function effectively.