How the MULTILINESTRING() function works in Mariadb?

The MULTILINESTRING() function is a spatial function that creates a MULTILINESTRING object from a list of line strings.

Posted on

The MULTILINESTRING() function is a spatial function that creates a MULTILINESTRING object from a list of line strings. A line string is a one-dimensional geometric object that consists of a sequence of points. A MULTILINESTRING object is a collection of one or more line strings that may or may not be connected to each other.

Syntax

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

MULTILINESTRING(linestring1, linestring2, ...)

The function takes one or more arguments, each of which is a line string. The line strings can be specified either as LINESTRING objects or as text literals in Well-Known Text (WKT) format. The WKT format is a standard way of encoding geometric objects as a text string. For example, a line string with three points can be written as LINESTRING(0 0, 10 10, 20 0).

The function returns a MULTILINESTRING object if the input is valid, or NULL if the input is invalid or empty.

Examples

In this section, we will show some examples of how to use the MULTILINESTRING() function in MariaDB.

Example 1: Creating a simple multilinestring object

In this example, we will create a simple multilinestring object that consists of two line strings. The first line string has four points: (0, 0), (0, 10), (10, 10), and (10, 0). The second line string has three points: (20, 20), (20, 30), and (30, 30). We will use the ST_AsText() function to display the result in WKT format, which is a human-readable representation of geometric objects.

SELECT ST_AsText(MULTILINESTRING(LINESTRING(0 0, 0 10, 10 10, 10 0), LINESTRING(20 20, 20 30, 30 30)));
MULTILINESTRING((0 0,0 10,10 10,10 0),(20 20,20 30,30 30))

Example 2: Creating a multilinestring object from text literals

In this example, we will create a multilinestring object from text literals in WKT format. We will use the same line strings as in the previous example, but we will write them as text literals instead of LINESTRING objects. Note that we need to enclose the text literals in single quotes.

SELECT ST_AsText(MULTILINESTRING('LINESTRING(0 0, 0 10, 10 10, 10 0)', 'LINESTRING(20 20, 20 30, 30 30)'));
MULTILINESTRING((0 0,0 10,10 10,10 0),(20 20,20 30,30 30))

Example 3: Creating a multilinestring object with a specified SRID

In this example, we will create a multilinestring object with a specified SRID. The SRID is a numeric identifier that defines the coordinate system and projection of the spatial object. For example, the SRID 4326 represents the World Geodetic System 1984 (WGS 84), which is a common coordinate system for geographic data. The SRID can be specified as a prefix to the WKT representation, using the syntax SRID=value;WKT. For example, SRID=4326;POINT(1 1) indicates that the point is in the WGS 84 coordinate system.

We will create a multilinestring object that consists of two line strings. The first line string has four points: (-122.4194, 37.7749), (-118.2437, 34.0522), (-73.9352, 40.7306), and (-77.0369, 38.9072). The second line string has three points: (139.6917, 35.6895), (151.2093, -33.8688), and (174.7633, -36.8485). These coordinates are in degrees of longitude and latitude, and we will use the SRID 4326 to indicate that they are in the WGS 84 coordinate system.

SELECT ST_AsText(MULTILINESTRING(SRID=4326;'LINESTRING(-122.4194 37.7749, -118.2437 34.0522, -73.9352 40.7306, -77.0369 38.9072)', 'LINESTRING(139.6917 35.6895, 151.2093 -33.8688, 174.7633 -36.8485)'));
MULTILINESTRING((-122.4194 37.7749,-118.2437 34.0522,-73.9352 40.7306,-77.0369 38.9072),(139.6917 35.6895,151.2093 -33.8688,174.7633 -36.8485))

There are some other functions that are related to the MULTILINESTRING() function. Here are some of them:

  • ST_GeomFromText(): This function creates a geometry object from a WKT representation. It can handle any type of geometry, not just multilinestrings. For example:

    SELECT ST_AsText(ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'));
    
    POLYGON((0 0,0 10,10 10,10 0,0 0))
  • ST_GeomFromWKB(): This function creates a geometry object from a Well-Known Binary (WKB) representation. The WKB format is a standard way of encoding geometric objects as a sequence of bytes. It can also handle any type of geometry. For example:

    SELECT ST_AsText(ST_GeomFromWKB(0x0101000000000000000000F03F000000000000F03F));
    
    POINT(1 1)
  • ST_AsText(): This function returns the WKT representation of a geometry object. It is the inverse of the ST_GeomFromText() function. For example:

    SELECT ST_AsText(ST_GeomFromWKB(0x010300000001000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000));
    
    POLYGON((0 0,0 10,10 10,10 0,0 0))
  • LINESTRING(): This function creates a LINESTRING object from a list of points. It is similar to the MULTILINESTRING() function, but it only takes one line string as input. For example:

    SELECT ST_AsText(LINESTRING(0 0, 10 10, 20 0));
    
    LINESTRING(0 0,10 10,20 0)

Conclusion

In this article, we have learned how to use the MULTILINESTRING() function in MariaDB to create a MULTILINESTRING object from a list of line strings. We have also seen some examples of how to use the function with different inputs and outputs. We have also learned about some related functions that can be used to create or manipulate spatial objects in MariaDB. Spatial functions are useful for working with geographic data and performing spatial analysis.