How the ExteriorRing() function works in Mariadb?
The ExteriorRing()
function is a spatial function that returns the exterior ring of a polygon.
The ExteriorRing()
function is a spatial function that returns the exterior ring of a polygon. A polygon is a closed shape that consists of one or more rings. A ring is a simple, closed, and non-self-intersecting curve. The exterior ring is the outermost ring of a polygon, and it defines the boundary of the polygon. The interior rings, if any, are the holes inside the polygon.
Syntax
The syntax of the ExteriorRing()
function is as follows:
ExteriorRing(polygon)
The function takes one argument, which is a polygon value. The function returns a linestring value that represents the exterior ring of the polygon. If the argument is not a valid polygon, the function returns NULL.
Examples
Example 1: Simple polygon
In this example, we create a simple polygon that has only one ring, which is the exterior ring. We use the ST_GeomFromText()
function to create the polygon from a well-known text (WKT) representation. Then we use the ExteriorRing()
function to get the exterior ring of the polygon.
SELECT AsText(ExteriorRing(ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'))) AS ExteriorRing;
The output is:
+-------------------------------------+
| ExteriorRing |
+-------------------------------------+
| LINESTRING(0 0,0 10,10 10,10 0,0 0) |
+-------------------------------------+
The exterior ring is a linestring that connects the four vertices of the polygon.
Example 2: Polygon with a hole
In this example, we create a polygon that has two rings: one exterior ring and one interior ring. The interior ring creates a hole inside the polygon. We use the ST_GeomFromText()
function to create the polygon from a WKT representation. Then we use the ExteriorRing()
function to get the exterior ring of the polygon.
SELECT AsText(ExteriorRing(ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0),(2 2, 2 8, 8 8, 8 2, 2 2))'))) AS ExteriorRing;
The output is:
+-------------------------------------+
| ExteriorRing |
+-------------------------------------+
| LINESTRING(0 0,0 10,10 10,10 0,0 0) |
+-------------------------------------+
The exterior ring is the same as the previous example, and it does not include the interior ring.
Example 3: Multipolygon
In this example, we create a multipolygon that consists of two polygons. A multipolygon is a collection of polygons that may or may not touch each other. We use the ST_GeomFromText()
function to create the multipolygon from a WKT representation. Then we use the ExteriorRing()
function to get the exterior ring of the multipolygon.
SELECT AsText(ExteriorRing(ST_GeomFromText('MULTIPOLYGON(((0 0, 0 10, 10 10, 10 0, 0 0)),((20 20, 20 30, 30 30, 30 20, 20 20)))'))) AS ExteriorRing;
The output is:
+--------------+
| ExteriorRing |
+--------------+
| NULL |
+--------------+
The ExteriorRing()
function returns NULL, because the argument is not a valid polygon. The function only works on single polygons, not multipolygons.
Example 4: Empty polygon
In this example, we create an empty polygon that has no rings. We use the ST_GeomFromText()
function to create the polygon from a WKT representation. Then we use the ExteriorRing()
function to get the exterior ring of the polygon.
SELECT AsText(ExteriorRing(ST_GeomFromText('POLYGON EMPTY'))) AS ExteriorRing;
The output is:
+--------------+
| ExteriorRing |
+--------------+
| NULL |
+--------------+
The ExteriorRing()
function returns NULL, because the argument is an empty polygon. The function only works on non-empty polygons.
Related Functions
There are some other spatial functions that are related to the ExteriorRing()
function. Here are some of them:
-
InteriorRingN(polygon, n)
: This function returns the n-th interior ring of a polygon. The interior rings are the holes inside the polygon. The function returns NULL if the polygon is empty or the n-th interior ring does not exist. For example:SELECT AsText(InteriorRingN(ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0),(2 2, 2 8, 8 8, 8 2, 2 2))'), 1)) AS InteriorRing;
The output is:
+---------------------------------+ | InteriorRing | +---------------------------------+ | LINESTRING(2 2,2 8,8 8,8 2,2 2) | +---------------------------------+
The function returns the first interior ring of the polygon, which is a linestring that creates a hole inside the polygon.
-
NumInteriorRings(polygon)
: This function returns the number of interior rings in a polygon. The interior rings are the holes inside the polygon. The function returns NULL if the polygon is empty. For example:SELECT NumInteriorRings(ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0),(2 2, 2 8, 8 8, 8 2, 2 2))')) AS NumInteriorRings;
The output is:
+------------------+ | NumInteriorRings | +------------------+ | 1 | +------------------+
The function returns the number of interior rings in the polygon, which is 1.
-
IsClosed(linestring)
: This function returns 1 if the linestring is closed, or 0 otherwise. A linestring is closed if its start point and end point are the same. The function returns NULL if the linestring is empty. For example:SELECT IsClosed(ST_GeomFromText('LINESTRING(0 0, 0 10, 10 10, 10 0, 0 0)')) AS IsClosed;
The output is:
+----------+ | IsClosed | +----------+ | 1 | +----------+
The function returns 1, because the linestring is closed.
Conclusion
The ExteriorRing()
function is a useful spatial function that can extract the exterior ring of a polygon. The exterior ring is the outermost ring of a polygon, and it defines the boundary of the polygon. The function only works on single, non-empty, and valid polygons. The function returns a linestring value that represents the exterior ring of the polygon, or NULL if the argument is not a valid polygon. There are some other spatial functions that are related to the ExteriorRing()
function, such as InteriorRingN()
, NumInteriorRings()
, and IsClosed()
. These functions can help us manipulate and analyze the rings of polygons.