How the GEOMETRYCOLLECTION() function works in Mariadb?

The MariaDB GEOMETRYCOLLECTION() function is used to create a geometry collection object from a set of individual geometry objects.

Posted on

The MariaDB GEOMETRYCOLLECTION() function is used to create a geometry collection object from a set of individual geometry objects. It is particularly useful for working with spatial data that consists of multiple geometries, such as points, lines, and polygons, within a single object.

Syntax

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

GEOMETRYCOLLECTION(g1, g2, ..., gn)
  • g1, g2, …, gn: These are the individual geometry objects that will be combined into a single geometry collection. Each geometry object can be of any valid geometry type supported by MariaDB, such as POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, or MULTIPOLYGON.

The function returns a GEOMETRYCOLLECTION object containing the specified geometry objects. If any of the input geometry objects are NULL, the function returns NULL.

Examples

Example 1: Creating a simple geometry collection

This example demonstrates how to create a simple geometry collection using the GEOMETRYCOLLECTION() function.

SELECT AsText(GEOMETRYCOLLECTION(POINT(1, 1), LINESTRING(POINT(2, 2), POINT(3, 3)))) AS goemcoll;

The output of this statement is:

+----------------------------------------------------+
| goemcoll                                           |
+----------------------------------------------------+
| GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(2 2,3 3)) |
+----------------------------------------------------+

In this example, the GEOMETRYCOLLECTION() function combines a POINT and a LINESTRING geometry into a single geometry collection object.

Example 2: Creating a geometry collection with multiple types

This example shows how to create a geometry collection containing different types of geometries.

SET @pt = POINT(1, 1);
SET @ls = LINESTRING(POINT(2, 2), POINT(3, 3));
SET @poly = PolygonFromText('POLYGON((4 4,4 5,5 5,5 4,4 4))');
SELECT AsText(GEOMETRYCOLLECTION(@pt, @ls, @poly)) AS goemcoll;

The output of this statement is:

+-----------------------------------------------------------------------------------+
| goemcoll                                                                          |
+-----------------------------------------------------------------------------------+
| GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(2 2,3 3),POLYGON((4 4,4 5,5 5,5 4,4 4))) |
+-----------------------------------------------------------------------------------+

In this example, the GEOMETRYCOLLECTION() function combines a POINT, a LINESTRING, and a POLYGON geometry into a single geometry collection object.

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

  • MariaDB POINT() function is used to create a single point geometry object.
  • MariaDB LINESTRING() function is used to create a single linestring geometry object.
  • MariaDB POLYGON() function is used to create a single polygon geometry object.
  • MariaDB MULTIPOINT() function is used to create a multi-point geometry object.
  • MariaDB MULTILINESTRING() function is used to create a multi-linestring geometry object.
  • MariaDB MULTIPOLYGON() function is used to create a multi-polygon geometry object.

Conclusion

The MariaDB GEOMETRYCOLLECTION() function is a powerful tool for working with spatial data that consists of multiple geometries. It allows you to combine different types of geometries, such as points, lines, and polygons, into a single geometry collection object. By understanding its syntax, usage, and related functions, developers can effectively manage and manipulate complex spatial data within their MariaDB applications. The ability to perform spatial operations on geometry collections further enhances the functionality and versatility of this function in spatial data analysis and processing tasks.