How the InteriorRingN() function works in Mariadb?

The InteriorRingN() function is a spatial function that returns the N-th interior ring of a polygon.

Posted on

The MariaDB InteriorRingN() function is used to return the N-th interior ring of a Polygon geometry. This function is particularly useful in geographical databases where the internal areas of complex polygons need to be analyzed or manipulated.

Syntax

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

InteriorRingN(polygon, N)
  • polygon is a Polygon geometry from which the interior ring is to be extracted.
  • N is the 1-based index of the interior ring to return.

The function returns the N-th interior ring of the polygon as a LineString. If the polygon does not have an N-th interior ring, the function returns NULL.

Examples

Example 1: Extracting the First Interior Ring

This example demonstrates how to extract the first interior ring from a polygon.

SELECT AsText(InteriorRingN(PolygonFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1))'), 1)) result;

The output for this statement is:

+---------------------------------+
| result                          |
+---------------------------------+
| LINESTRING(1 1,2 1,2 2,1 2,1 1) |
+---------------------------------+

This result is the first interior ring of the specified polygon.

Example 2: Non-Existent Interior Ring

Showing the result when requesting an interior ring that does not exist.

SELECT AsText(InteriorRingN(PolygonFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))'), 1)) result;

The output for this statement is:

+--------+
| result |
+--------+
| NULL   |
+--------+

Since the polygon does not have any interior rings, the function returns NULL.

Example 3: Invalid Polygon

Attempting to extract an interior ring from an invalid polygon.

SELECT InteriorRingN(PolygonFromText('POLYGON((0 0, 4 0, 4 4))'), 1);

The output for this statement is:

NULL

The provided polygon is invalid, so the function returns NULL.

Example 4: Using InteriorRingN() with a Table

Extracting interior rings from polygons stored in a table.

DROP TABLE IF EXISTS polygons;
CREATE TABLE polygons (id INT, shape POLYGON);
INSERT INTO polygons (id, shape) VALUES (1, PolygonFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1))'));

SELECT id, AsText(InteriorRingN(shape, 1)) AS inner_ring FROM polygons;

The output for this statement is:

+------+---------------------------------+
| id   | inner_ring                      |
+------+---------------------------------+
|    1 | LINESTRING(1 1,2 1,2 2,1 2,1 1) |
+------+---------------------------------+

This table shows the ID of the polygon and the first interior ring extracted from it.

Example 5: Multiple Interior Rings

Handling a polygon with multiple interior rings.

SELECT AsText(InteriorRingN(PolygonFromText('POLYGON((0 0, 5 0, 5 5, 0 5, 0 0),(1 1, 2 1, 2 2, 1 2, 1 1),(3 3, 4 3, 4 4, 3 4, 3 3))'), 2)) result;

The output for this statement is:

+---------------------------------+
| result                          |
+---------------------------------+
| LINESTRING(3 3,4 3,4 4,3 4,3 3) |
+---------------------------------+

The result is the second interior ring of the specified polygon.

Below are a few functions related to the MariaDB InteriorRingN() function:

  • MariaDB ExteriorRing() function is used to return the exterior ring of a Polygon geometry.
  • MariaDB NumInteriorRings() function is used to return the number of interior rings in a Polygon geometry.
  • MariaDB GeometryN() function is used to return the N-th geometry of a GeometryCollection.

Conclusion

The InteriorRingN() function in MariaDB is a powerful tool for working with spatial data, specifically for extracting and analyzing the interior rings of polygons. It is essential for applications that require detailed spatial analysis and manipulation of geographical data. Understanding how to use this function, along with its related functions, can significantly enhance the capabilities of a geographical database system.