How the MultiLineStringFromText() function works in Mariadb?

The MultiLineStringFromText() function is a spatial function that creates a MULTILINESTRING object from a text representation.

Posted on

The MultiLineStringFromText() function is a spatial function that creates a MULTILINESTRING object from a text representation. The text representation is a string that follows the Well-Known Text (WKT) format, which is a standard way of encoding geometric objects as a text string. The WKT format for a MULTILINESTRING object is as follows:

MULTILINESTRING((x1 y1, x2 y2, ...), (x3 y3, x4 y4, ...), ...)

The function takes one or two arguments:

  • wkt: A text string that represents a MULTILINESTRING object in WKT format. This argument is mandatory.
  • srid: An optional argument that specifies the spatial reference system identifier (SRID) of the MULTILINESTRING object. If not specified, the default value is 0, which means the object has no SRID.

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

Syntax

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

MultiLineStringFromText(wkt [, srid])

Examples

In this section, we will show some examples of how to use the MultiLineStringFromText() 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(MultiLineStringFromText('MULTILINESTRING((0 0, 0 10, 10 10, 10 0), (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 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 the second argument of the MultiLineStringFromText() function.

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(MultiLineStringFromText('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))', 4326));
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 MultiLineStringFromText() function. Here are some of them:

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

  • 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.

  • ST_AsText(): This function returns the text representation of a geometry object. It is the inverse of the ST_GeomFromText() function.

  • MultiLineStringFromWKB(): This function creates a MULTILINESTRING object from a WKB representation. It is similar to the MultiLineStringFromText() function, but it takes a binary string as input instead of a text string.

Conclusion

In this article, we have learned how to use the MultiLineStringFromText() function in MariaDB to create a MULTILINESTRING object from a text representation. 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.