How the ST_CENTROID() function works in MariaDB?

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

Posted on

The ST_Centroid() function in MariaDB is used to calculate the centroid of a given geometry value. The centroid is the geometric center of a geometry, and it is a point that lies within the geometry’s interior or on its boundary. This function is often used in spatial analysis and data processing tasks, such as determining the center of a region or calculating the average location of a set of points.

Syntax

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

ST_Centroid(geom)
  • geom: The geometry value for which the centroid needs to be calculated.

The function returns a Point geometry representing the centroid of the input geometry. If the input geometry is empty or has no centroid (e.g., a LineString), the function returns an empty geometry.

Examples

Example 1: Calculate the centroid of a Point geometry

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

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

The following is the output:

POINT(1 1)

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

Example 2: Calculate the centroid of a LineString geometry

This example shows that the ST_Centroid() function returns an empty geometry for a LineString.

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

The following is the output:

GEOMETRYCOLLECTION EMPTY

LineString geometries do not have a well-defined centroid, so the function returns an empty geometry collection.

Example 3: Calculate the centroid of a Polygon geometry

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

SET @poly = ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))');
SELECT ST_AsText(ST_Centroid(@poly));

The following is the output:

POINT(5 5)

The centroid of a square polygon is the intersection point of its diagonals, which is (5, 5) in this case.

Example 4: Calculate the centroid of a MultiPolygon geometry

This example shows how to calculate the centroid of a MultiPolygon geometry.

SET @multipoly = ST_GeomFromText('MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)), ((20 20, 30 20, 30 30, 20 30, 20 20)))');
SELECT ST_AsText(ST_Centroid(@multipoly));

The following is the output:

POINT(15 15)

The centroid of a MultiPolygon geometry is a weighted average of the centroids of its constituent polygons.

Example 5: Calculate the centroid of a geometry from a table

This example demonstrates how to calculate the centroid 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, 0 10, 0 0))'));
SELECT id, ST_AsText(ST_Centroid(poly)) AS centroid FROM polygons;

The following is the output:

+----+-------------+
| id | centroid    |
+----+-------------+
|  1 | POINT(5 5) |
+----+-------------+

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_Centroid() function is used to calculate the centroid of the stored geometry for each row in the table.

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

  • The MariaDB ST_Area() function is used to calculate the area of a geometry.
  • The MariaDB ST_Length() function is used to calculate the length of a LineString geometry.
  • The MariaDB ST_Distance() function is used to calculate the distance between two geometries.
  • The MariaDB ST_PointOnSurface() function is used to return a Point geometry that is guaranteed to be on the surface of the input geometry.

Conclusion

The ST_Centroid() function in MariaDB is a useful spatial function for calculating the geometric center of a given geometry. By determining the centroid of a geometry, you can perform various spatial analysis tasks, such as finding the average location of a set of points, determining the center of a region, or calculating the center of mass for a distribution of geometries. This function is particularly valuable when working with complex geometries like polygons and multipolygons, where the centroid may not be immediately obvious. By understanding the behavior of the ST_Centroid() function for different geometry types, you can effectively process and analyze spatial data in your applications.