How the ENVELOPE() function works in Mariadb?

The ENVELOPE() function is a geometric function that returns the minimum bounding rectangle (MBR) of a geometry.

Posted on

The ENVELOPE() function is a geometric function that returns the minimum bounding rectangle (MBR) of a geometry. It is a synonym for the ST_ENVELOPE() function¹.

Syntax

The syntax of the ENVELOPE() function is as follows:

ENVELOPE(geometry)

The function takes one argument, where:

  • geometry is a geometry value. It can be any valid expression that returns a geometry value.

The function returns a Polygon geometry that represents the MBR of the input geometry. If the argument is NULL, the function returns NULL.

Examples

Example 1: Returning the envelope of a point

In this example, we use the ENVELOPE() function to return the envelope of a point. We also use the ST_AsText() function to display the result in a human-readable format.

SELECT ST_AsText(ENVELOPE(ST_GeomFromText('Point(1 1)'))) AS envelope;

The output is:

+--------------------------------+
| envelope                       |
+--------------------------------+
| POLYGON((1 1,1 1,1 1,1 1,1 1)) |
+--------------------------------+

Example 2: Returning the envelope of a line string

In this example, we use the ENVELOPE() function to return the envelope of a line string. We also use the ST_AsText() function to display the result in a human-readable format.

SELECT ST_AsText(ENVELOPE(ST_GeomFromText('LineString(1 2, 2 3, 3 4)'))) AS envelope;

The output is:

+--------------------------------+
| envelope                       |
+--------------------------------+
| POLYGON((1 2,3 2,3 4,1 4,1 2)) |
+--------------------------------+

Example 3: Returning the envelope of a polygon

In this example, we use the ENVELOPE() function to return the envelope of a polygon. We also use the ST_AsText() function to display the result in a human-readable format.

SELECT ST_AsText(ENVELOPE(ST_GeomFromText('Polygon((1 1, 2 2, 3 1, 1 1))'))) AS envelope;

The output is:

+--------------------------------+
| envelope                       |
+--------------------------------+
| POLYGON((1 1,3 1,3 2,1 2,1 1)) |
+--------------------------------+

Example 4: Returning the envelope of a geometry column

In this example, we use the ENVELOPE() function to return the envelope of a geometry column from a table. We assume that the table has a column named shape that stores the geometry values of different objects.

DROP TABLE IF EXISTS obejcts;
CREATE TABLE objects (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  shape Geometry
);

INSERT INTO objects VALUES
(1, 'Point A', ST_GeomFromText('Point(1 1)')),
(2, 'Line B', ST_GeomFromText('LineString(2 2, 3 3)')),
(3, 'Polygon C', ST_GeomFromText('Polygon((4 4, 5 5, 6 4, 4 4))'));

SELECT id, name, ST_AsText(ENVELOPE(shape)) AS envelope FROM objects;

The output is:

+----+-----------+--------------------------------+
| id | name      | envelope                       |
+----+-----------+--------------------------------+
|  1 | Point A   | POLYGON((1 1,1 1,1 1,1 1,1 1)) |
|  2 | Line B    | POLYGON((2 2,3 2,3 3,2 3,2 2)) |
|  3 | Polygon C | POLYGON((4 4,6 4,6 5,4 5,4 4)) |
+----+-----------+--------------------------------+

Example 5: Returning the envelope of a null argument

In this example, we use the ENVELOPE() function to return the envelope of a null argument. We expect the function to return NULL.

SELECT ST_AsText(ENVELOPE(NULL)) AS envelope;

The output is:

+----------+
| envelope |
+----------+
| NULL     |
+----------+

Some of the functions that are related to the ENVELOPE() function are:

  • ST_ENVELOPE():This function is equivalent to the ENVELOPE() function. It returns the minimum bounding rectangle of a geometry. For example, ST_ENVELOPE(ST_GeomFromText('LineString(1 2, 2 3, 3 4)')) returns POLYGON((1 2,3 2,3 4,1 4,1 2)).
  • MBRContains():This function returns 1 if the MBR of the first geometry contains the MBR of the second geometry, 0 otherwise. For example, MBRContains(ENVELOPE(ST_GeomFromText('LineString(1 2, 2 3, 3 4)')), ST_GeomFromText('Point(2 3)')) returns 1.
  • MBRIntersects():This function returns 1 if the MBRs of the two geometries intersect, 0 otherwise. For example, MBRIntersects(ENVELOPE(ST_GeomFromText('LineString(1 2, 2 3, 3 4)')), ENVELOPE(ST_GeomFromText('LineString(2 2, 3 3, 4 2)'))) returns 1.
  • MBRWithin():This function returns 1 if the MBR of the first geometry is within the MBR of the second geometry, 0 otherwise. For example, MBRWithin(ST_GeomFromText('Point(2 3)'), ENVELOPE(ST_GeomFromText('LineString(1 2, 2 3, 3 4)'))) returns 1.

Conclusion

The ENVELOPE() function is a useful geometric function that can return the minimum bounding rectangle of a geometry. It can be used to get the spatial extent of a geometry, the spatial index of a geometry column, or the spatial relation between two geometries. It takes a geometry value as an argument, and returns a polygon geometry that represents the MBR. If the argument is NULL, the function returns NULL. The ENVELOPE() function can be combined with other functions, such as ST_AsText(), ST_GeomFromText(), or MBRContains(), to perform various geometric operations.