How the ST_CONVEXHULL() function works in MariaDB?

The ST_ConvexHull() function in MariaDB is used to calculate the convex hull of a given geometry value, allowing for efficient analysis and manipulation of spatial data.

Posted on

The ST_ConvexHull() function in MariaDB is used to calculate the convex hull of a given geometry value. The convex hull is the smallest convex polygon that encloses all points of the input geometry. In other words, it is the minimal convex set containing the geometry. This function is particularly useful in spatial analysis tasks, such as simplifying complex geometries, identifying the overall extent of a set of points, or calculating the minimum bounding polygon for a geometry.

Syntax

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

ST_ConvexHull(geom)
  • geom: The geometry value for which the convex hull needs to be calculated.

The function returns a Polygon geometry representing the convex hull of the input geometry.

Examples

Example 1: Calculate the convex hull of a Point geometry

This example demonstrates that for a single Point geometry, the ST_ConvexHull() function returns the same Point.

SET @point = ST_GeomFromText('POINT(1 1)');
SELECT ST_AsText(ST_ConvexHull(@point));

The following is the output:

POINT(1 1)

Since a single Point geometry is already convex, its convex hull is the same point.

Example 2: Calculate the convex hull of a LineString geometry

This example shows how to calculate the convex hull of a LineString geometry.

SET @line = ST_GeomFromText('LINESTRING(0 0, 1 1, 2 2, 3 0)');
SELECT ST_AsText(ST_ConvexHull(@line));

The following is the output:

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

The convex hull of the LineString geometry is a Polygon that encloses all points of the LineString in the smallest possible convex area.

Example 3: Calculate the convex hull of a Polygon geometry

This example demonstrates how to calculate the convex hull of a Polygon geometry.

SET @poly = ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 5 15, 0 10, 0 0))');
SELECT ST_AsText(ST_ConvexHull(@poly));

The following is the output:

POLYGON((0 0, 10 0, 10 10, 5 15, 0 0))

The convex hull of the input Polygon geometry is a new Polygon that represents the smallest convex area enclosing all points of the original Polygon.

Example 4: Calculate the convex hull of a MultiPoint geometry

This example shows how to calculate the convex hull of a MultiPoint geometry.

SET @multipoint = ST_GeomFromText('MULTIPOINT(1 1, 2 2, 3 3, 4 0, 5 1)');
SELECT ST_AsText(ST_ConvexHull(@multipoint));

The following is the output:

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

The convex hull of the MultiPoint geometry is a Polygon that represents the smallest convex area enclosing all points in the MultiPoint geometry.

Example 5: Calculate the convex hull of a geometry from a table

This example demonstrates how to calculate the convex hull of a geometry stored in a table.

DROP TABLE IF EXISTS polygons;
CREATE TABLE polygons (id INT PRIMARY KEY, poly GEOMETRY);
INSERT INTO polygons (id, poly) VALUES
    (1, ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 5 15, 0 10, 0 0))'));
SELECT id, ST_AsText(ST_ConvexHull(poly)) AS convex_hull FROM polygons;

The following is the output:

+----+------------------------------------------+
| id | convex_hull                              |
+----+------------------------------------------+
|  1 | POLYGON((0 0, 10 0, 10 10, 5 15, 0 0))  |
+----+------------------------------------------+

In this example, a table named polygons is created with an id column and a poly column of type GEOMETRY. A polygon geometry is inserted into the table, and then the ST_ConvexHull() function is used to calculate the convex hull of the stored geometry for each row in the table.

The following are some functions related to the MariaDB ST_ConvexHull() function:

  • The MariaDB ST_Envelope() function is used to calculate the bounding rectangle of a geometry.
  • The MariaDB ST_Buffer() function is used to create a buffer polygon around a geometry.
  • The MariaDB ST_SimplifyPreserveTopology() function is used to simplify a geometry while preserving its topology.
  • The MariaDB ST_Centroid() function is used to calculate the centroid (geometric center) of a geometry.

Conclusion

The ST_ConvexHull() function in MariaDB is a powerful spatial function that allows you to calculate the convex hull of a given geometry. By determining the smallest convex polygon that encloses all points of a geometry, you can simplify complex geometries, identify the overall extent of a set of points, or calculate the minimum bounding polygon for a geometry. This function finds numerous applications in spatial analysis, data visualization, and spatial data processing tasks. By understanding the behavior of the ST_ConvexHull() function for different geometry types, you can effectively work with spatial data and perform necessary operations to analyze and process spatial information in your applications.