How the CONVEXHULL() function works in Mariadb?

The CONVEXHULL() function is a spatial function that returns the convex hull of a geometry. The convex hull of a geometry is the smallest convex polygon that contains the geometry.

Posted on

The CONVEXHULL() function is a spatial function that returns the convex hull of a geometry. The convex hull of a geometry is the smallest convex polygon that contains the geometry. The function returns a geometry that represents the convex hull of the input geometry.

Syntax

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

CONVEXHULL(g)

The function takes one argument, g, which is the geometry to be processed. The argument can be a string or an expression that evaluates to a string. The function returns NULL if the argument is NULL or not a valid geometry.

Examples

Example 1: Getting the convex hull of a point

The following example uses the CONVEXHULL() function to get the convex hull of a point. The point is defined by the coordinates (1, 1).

SELECT AsText(CONVEXHULL(ST_GeomFromText('POINT(1 1)')));

The output is:

+---------------------------------------------------+
| AsText(CONVEXHULL(ST_GeomFromText('POINT(1 1)'))) |
+---------------------------------------------------+
| POINT(1 1)                                        |
+---------------------------------------------------+

The output shows that the convex hull of a point is the point itself, as expected.

Example 2: Getting the convex hull of a line

The following example uses the CONVEXHULL() function to get the convex hull of a line. The line is defined by two points: (0, 0) and (2, 2).

SELECT AsText(CONVEXHULL(ST_GeomFromText('LINESTRING(0 0,2 2)')));

The output is:

+------------------------------------------------------------+
| AsText(CONVEXHULL(ST_GeomFromText('LINESTRING(0 0,2 2)'))) |
+------------------------------------------------------------+
| LINESTRING(0 0,2 2)                                        |
+------------------------------------------------------------+

The output shows that the convex hull of a line is the line itself, as expected.

Example 3: Getting the convex hull of a polygon

The following example uses the CONVEXHULL() function to get the convex hull of a polygon. The polygon is defined by four points: (0, 0), (0, 2), (1, 1), and (2, 0).

SELECT AsText(CONVEXHULL(ST_GeomFromText('POLYGON((0 0,0 2,1 1,2 0,0 0))')));

The output is:

+-----------------------------------------------------------------------+
| AsText(CONVEXHULL(ST_GeomFromText('POLYGON((0 0,0 2,1 1,2 0,0 0))'))) |
+-----------------------------------------------------------------------+
| POLYGON((0 0,0 2,2 0,0 0))                                            |
+-----------------------------------------------------------------------+

The output shows that the convex hull of the polygon is a triangle that contains the polygon, as expected.

Example 4: Getting the convex hull of a multipoint

The following example uses the CONVEXHULL() function to get the convex hull of a multipoint. The multipoint is defined by five points: (0, 0), (0, 2), (1, 1), (2, 0), and (2, 2).

SELECT AsText(CONVEXHULL(ST_GeomFromText('MULTIPOINT(0 0,0 2,1 1,2 0,2 2)')));

The output is:

+------------------------------------------------------------------------+
| AsText(CONVEXHULL(ST_GeomFromText('MULTIPOINT(0 0,0 2,1 1,2 0,2 2)'))) |
+------------------------------------------------------------------------+
| POLYGON((0 0,0 2,2 2,2 0,0 0))                                         |
+------------------------------------------------------------------------+

The output shows that the convex hull of the multipoint is a quadrilateral that contains the multipoint, as expected.

Example 5: Getting the convex hull of an invalid geometry

The following example uses the CONVEXHULL() function to get the convex hull of an invalid geometry. The invalid geometry is defined by an empty string.

SELECT CONVEXHULL(ST_GeomFromText(''));

The output is:

+---------------------------------+
| CONVEXHULL(ST_GeomFromText('')) |
+---------------------------------+
| NULL                            |
+---------------------------------+

The output shows that the function returns NULL, as expected. The function returns NULL if the argument is NULL or not a valid geometry.

There are some other functions that are related to the CONVEXHULL() function in Mariadb. They are:

  • ST_ConvexHull(): This function is an alias for the CONVEXHULL() function. It follows the OpenGIS specifications for the ST_ConvexHull() function.
  • ENVELOPE(): This function returns the minimum bounding rectangle of a geometry. The function returns a geometry that represents the smallest rectangle that contains the input geometry.
  • BUFFER(): This function returns a geometry that is the buffer of a geometry. The function returns a geometry that represents the area that is within a specified distance from the input geometry.
  • CENTROID(): This function returns the centroid of a geometry. The function returns a point that represents the geometric center of the input geometry.

Conclusion

The CONVEXHULL() function is a useful function to get the convex hull of a geometry. It can handle different types of geometries, such as points, lines, and polygons. It returns a geometry that represents the smallest convex polygon that contains the input geometry. It is similar to the ST_ConvexHull() function, but with a different name. It is also related to some other functions that provide spatial information or operations on geometries.