How the CENTROID() function works in Mariadb?

The CENTROID() function is a spatial function that returns the geometric center of a geometry value. The centroid of a geometry is the point that balances the geometry if it is placed on a plane.

Posted on

The CENTROID() function is a spatial function that returns the geometric center of a geometry value. The centroid of a geometry is the point that balances the geometry if it is placed on a plane. For example, the centroid of a polygon is the average of the coordinates of its vertices, and the centroid of a point is the point itself.

Syntax

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

CENTROID(g)

Where g is a geometry value.

The return type of the function is a point value of the same spatial reference system as the input.

Examples

Example 1: Centroid of a point

In this example, we use the CENTROID() function to get the centroid of a point. We use the ST_AsText() function to display the result as a well-known text (WKT) string.

SELECT ST_AsText(CENTROID(ST_GeomFromText('POINT(1 2)'))) AS centroid;

The output is:

+-----------+
| centroid  |
+-----------+
| POINT(1 2)|
+-----------+

Example 2: Centroid of a line string

In this example, we use the CENTROID() function to get the centroid of a line string. We use the ST_AsText() function to display the result as a WKT string.

SELECT ST_AsText(CENTROID(ST_GeomFromText('LINESTRING(1 1, 2 2, 3 3)'))) AS centroid;

The output is:

+-----------------+
| centroid        |
+-----------------+
| POINT(2 2)      |
+-----------------+

Example 3: Centroid of a polygon

In this example, we use the CENTROID() function to get the centroid of a polygon. We use the ST_AsText() function to display the result as a WKT string.

SELECT ST_AsText(CENTROID(ST_GeomFromText('POLYGON((0 0, 0 3, 3 3, 3 0, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1))'))) AS centroid;

The output is:

+-----------------+
| centroid        |
+-----------------+
| POINT(1.5 1.5)  |
+-----------------+

Some other spatial functions that are related to the CENTROID() function are:

  • ST_Centroid(): Returns the centroid of a geometry value as a point value. This function is equivalent to the CENTROID() function. The syntax is ST_Centroid(g).
  • ST_PointOnSurface(): Returns a point value that is guaranteed to lie on the surface of a geometry value. The syntax is ST_PointOnSurface(g).
  • ST_Area(): Returns the area of a geometry value. The syntax is ST_Area(g).
  • ST_Length(): Returns the length of a geometry value. The syntax is ST_Length(g).

For example, you can use the ST_Centroid() function to achieve the same result as the first example of the CENTROID() function, but with a different syntax:

SELECT ST_AsText(ST_Centroid(ST_GeomFromText('POINT(1 2)'))) AS centroid;

The output is:

+-----------+
| centroid  |
+-----------+
| POINT(1 2)|
+-----------+

Conclusion

The CENTROID() function is a useful function to get the geometric center of a geometry value. The centroid of a geometry is the point that balances the geometry if it is placed on a plane. The CENTROID() function takes a geometry value as an argument, and returns a point value of the same spatial reference system as the input. The CENTROID() function can be applied to different types of geometries, such as points, line strings, and polygons. The CENTROID() function can be combined with other spatial functions to perform various spatial operations and analyses.