How the ST_ENVELOPE() function works in MariaDB?

The ST_ENVELOPE() function in MariaDB is used to calculate the minimum bounding rectangle (MBR) or envelope of a spatial object.

Posted on

The ST_ENVELOPE() function in MariaDB is used to calculate the minimum bounding rectangle (MBR) or envelope of a spatial object. The envelope is a rectangular polygon that represents the smallest rectangle that can enclose the given geometry.

Syntax

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

ST_ENVELOPE(g)
  • g: A geometry value representing the spatial object.

The function returns a polygon geometry representing the minimum bounding rectangle that encloses the input geometry g. If the input geometry is empty or NULL, it returns NULL.

Examples

Example 1: Envelope of a Point

This example demonstrates the envelope of a point geometry.

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

SELECT ST_AsText(ST_ENVELOPE(@point));

Output:

POLYGON((1 2, 1 2, 1 2, 1 2, 1 2))

The output shows a polygon with the same coordinates as the input point, as a point is its own minimum bounding rectangle.

Example 2: Envelope of a LineString

This example calculates the envelope of a linestring geometry.

SET @line = ST_GeomFromText('LINESTRING(0 0, 2 2, 4 0)');

SELECT ST_AsText(ST_ENVELOPE(@line));

Output:

POLYGON((0 0, 4 0, 4 2, 0 2, 0 0))

The output represents the minimum bounding rectangle that encloses the linestring.

Example 3: Envelope of a Polygon

This example demonstrates the envelope of a polygon geometry.

SET @poly = ST_GeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))');

SELECT ST_AsText(ST_ENVELOPE(@poly));

Output:

POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))

The output shows that the envelope of the polygon is the same as the polygon itself, as it is already a rectangle.

Example 4: Envelope from a Table

This example retrieves the envelope of geometries stored in a table.

DROP TABLE IF EXISTS geometries;
CREATE TABLE geometries (id INT PRIMARY KEY, geom GEOMETRY);

INSERT INTO geometries (id, geom) VALUES
  (1, ST_GeomFromText('POINT(1 2)')),
  (2, ST_GeomFromText('LINESTRING(0 0, 2 2, 4 0)')),
  (3, ST_GeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))'));

SELECT id, ST_AsText(ST_ENVELOPE(geom)) AS envelope
FROM geometries;

Output:

1, POLYGON((1 2, 1 2, 1 2, 1 2, 1 2))
2, POLYGON((0 0, 4 0, 4 2, 0 2, 0 0))
3, POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))

The output shows the envelope of each geometry stored in the table.

Example 5: Envelope of a Multi-Geometry

This example demonstrates the envelope of a multi-geometry.

SET @multi = ST_GeomFromText('MULTIPOINT(1 1, 2 2, 3 3)');

SELECT ST_AsText(ST_ENVELOPE(@multi));

Output:

POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))

The output represents the minimum bounding rectangle that encloses all the points in the multi-point geometry.

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

  • MariaDB ST_BUFFER() function is used to create a buffer around a spatial object at a specified distance.
  • MariaDB ST_AREA() function is used to calculate the area of a spatial object.
  • MariaDB ST_CENTROID() function is used to calculate the centroid of a spatial object.
  • MariaDB ST_BOUNDINGCIRCLE() function is used to calculate the minimum bounding circle of a spatial object.

Conclusion

The ST_ENVELOPE() function in MariaDB is a valuable tool for working with spatial data. It allows you to calculate the minimum bounding rectangle of a spatial object, which can be useful in various applications, such as spatial indexing, spatial queries, and spatial analysis. By understanding the envelope of a spatial object, you can optimize spatial operations, perform spatial filtering, and gain insights into the extent and distribution of your spatial data.