How the ST_ExteriorRing() function works in MariaDB?

The ST_ExteriorRing() function in MariaDB is used to retrieve the exterior ring of a polygon geometry.

Posted on

The ST_ExteriorRing() function in MariaDB is used to retrieve the exterior ring of a polygon geometry. A polygon consists of an exterior ring and zero or more interior rings (holes). The exterior ring defines the outer boundary of the polygon.

Syntax

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

ST_ExteriorRing(poly)
  • poly: A polygon value representing the spatial object.

The function returns a linestring geometry representing the exterior ring of the input polygon poly. If the input is not a polygon or is an empty polygon, it returns NULL.

Examples

Example 1: Exterior Ring of a Polygon

This example demonstrates how to retrieve the exterior ring of a polygon.

SET @poly = ST_GeomFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))');

SELECT ST_AsText(ST_ExteriorRing(@poly));

Output:

LINESTRING(0 0, 4 0, 4 4, 0 4, 0 0)

The output shows the exterior ring of the polygon as a linestring geometry.

Example 2: Exterior Ring of a Polygon with Holes

This example retrieves the exterior ring of a polygon with interior rings (holes).

SET @poly = ST_GeomFromText('POLYGON((0 0, 8 0, 8 8, 0 8, 0 0), (2 2, 2 6, 6 6, 6 2, 2 2))');

SELECT ST_AsText(ST_ExteriorRing(@poly));

Output:

LINESTRING(0 0, 8 0, 8 8, 0 8, 0 0)

The output represents the exterior ring of the polygon, excluding the interior ring.

Example 3: Exterior Ring of a Non-Polygon

This example demonstrates the behavior of ST_ExteriorRing() when applied to a non-polygon geometry.

SET @point = ST_GeomFromText('POINT(1 1)');

SELECT ST_AsText(ST_ExteriorRing(@point));

Output:

NULL

The output is NULL because the input is a point, not a polygon.

Example 4: Exterior Ring from a Table

This example retrieves the exterior ring of polygons stored in a table.

DROP TABLE IF EXISTS polygons;
CREATE TABLE polygons (id INT PRIMARY KEY, poly POLYGON);

INSERT INTO polygons (id, poly) VALUES
  (1, ST_GeomFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))')),
  (2, ST_GeomFromText('POLYGON((0 0, 8 0, 8 8, 0 8, 0 0), (2 2, 2 6, 6 6, 6 2, 2 2))'));

SELECT id, ST_AsText(ST_ExteriorRing(poly)) AS exterior_ring
FROM polygons;

Output:

1, LINESTRING(0 0, 4 0, 4 4, 0 4, 0 0)
2, LINESTRING(0 0, 8 0, 8 8, 0 8, 0 0)

The output shows the exterior ring of each polygon stored in the table.

Example 5: Exterior Ring of an Empty Polygon

This example demonstrates the behavior of ST_ExteriorRing() when applied to an empty polygon.

SET @empty_poly = ST_GeomFromText('POLYGON EMPTY');

SELECT ST_AsText(ST_ExteriorRing(@empty_poly));

Output:

NULL

The output is NULL because the input is an empty polygon, which does not have an exterior ring.

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

  • MariaDB ST_InteriorRingN() function is used to retrieve a specific interior ring of a polygon geometry.
  • MariaDB ST_NumInteriorRings() function is used to retrieve the number of interior rings in a polygon geometry.
  • MariaDB ST_IsClosed() function is used to determine whether a linestring geometry is closed or not.
  • MariaDB ST_IsRing() function is used to determine whether a linestring geometry is a ring or not.

Conclusion

The ST_ExteriorRing() function in MariaDB is a useful tool for working with polygon geometries. It allows you to retrieve the exterior ring of a polygon, which defines the outer boundary of the spatial object. By understanding the exterior ring of a polygon, you can perform various spatial analyses and operations, such as calculating areas, identifying intersections, or performing spatial joins. This function is particularly valuable when working with complex polygon geometries that may include interior rings or holes.