How the ST_NUMPOINTS() function works in Mariadb?

The ST_NUMPOINTS() function in MariaDB is used to retrieve the number of points in a geometry object.

Posted on

The ST_NUMPOINTS() function in MariaDB is used to retrieve the number of points in a geometry object. This function can be applied to various geometry types, including linestrings, polygons, and multi-geometries.

Syntax

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

ST_NUMPOINTS(geom)
  • geom: The input geometry for which you want to count the number of points.

The function returns an integer value representing the number of points in the input geometry. If the input geometry is NULL or an empty geometry, the function will return NULL.

Examples

Example 1: Counting points in a linestring

This example demonstrates how ST_NUMPOINTS() works with a linestring geometry.

SELECT ST_NUMPOINTS(ST_GeomFromText('LINESTRING(0 0, 1 1, 2 2)'));

The following is the output:

3

The linestring has three points, so the function returns 3.

Example 2: Counting points in a polygon

This example shows how ST_NUMPOINTS() handles a polygon geometry.

SELECT ST_NUMPOINTS(ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))'));

The following is the output:

5

The polygon has five points, one for each vertex, so the function returns 5.

Example 3: Counting points in a multi-geometry

This example demonstrates how ST_NUMPOINTS() works with a multi-geometry.

DROP TABLE IF EXISTS geometries;
CREATE TABLE geometries (id INT PRIMARY KEY, geom GEOMETRY);
INSERT INTO geometries (id, geom) VALUES
  (1, ST_GeomFromText('MULTILINESTRING((0 0, 1 1), (2 2, 3 3, 4 4))'));

SELECT ST_NUMPOINTS(geom) FROM geometries;

The following is the output:

5

The multi-linestring geometry consists of two linestrings with a total of five points, so the function returns 5.

Example 4: Handling NULL geometries

This example shows how ST_NUMPOINTS() handles NULL geometries.

SELECT ST_NUMPOINTS(NULL);

The following is the output:

NULL

Since the input geometry is NULL, the function returns NULL.

Example 5: Counting points in an empty geometry

This example demonstrates how ST_NUMPOINTS() handles empty geometries.

SELECT ST_NUMPOINTS(ST_GeomFromText('LINESTRING EMPTY'));

The following is the output:

NULL

The input geometry is an empty linestring, so the function returns NULL.

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

  • MariaDB ST_NumInteriorRings() function is used to determine the number of interior rings in a polygon geometry.
  • MariaDB ST_NumGeometries() function is used to get the number of geometries in a multi-geometry.
  • MariaDB ST_GeometryType() function is used to retrieve the type of a geometry.
  • MariaDB ST_PointN() function is used to retrieve a specific point from a geometry.

Conclusion

The ST_NUMPOINTS() function in MariaDB is a handy tool for working with geometry objects. It allows you to quickly determine the number of points in a linestring, polygon, or multi-geometry, which can be valuable information for various spatial analysis tasks. By understanding the syntax and providing appropriate examples, this article aimed to provide a comprehensive guide on how to use this function effectively.