How the MultiPolygonFromText() function works in Mariadb?

The MultiPolygonFromText() function is a spatial function that converts a well-known text (WKT) representation of a multipolygon geometry into a multipolygon value.

Posted on

The MultiPolygonFromText() function is a spatial function that converts a well-known text (WKT) representation of a multipolygon geometry into a multipolygon value. A multipolygon is a geometry that consists of one or more polygons, each of which may have one or more holes. This function is useful for importing or exporting spatial data from or to other applications that use the WKT format.

Syntax

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

MultiPolygonFromText(wkt [, srid])

The function takes two arguments:

  • wkt: A string that represents a multipolygon geometry in WKT format. This argument is mandatory.
  • srid: An integer that represents the spatial reference system identifier (SRID) of the multipolygon value. This argument is optional. If omitted, the default SRID of 0 is used.

The function returns a multipolygon value that corresponds to the WKT representation. If the input is not a valid WKT representation of a multipolygon geometry, the function returns NULL.

Examples

Example 1: Creating a multipolygon value from a WKT representation

In this example, we use the MultiPolygonFromText() function to create a multipolygon value from a WKT representation. We use the ST_AsText() function to display the result in a human-readable format.

SELECT ST_AsText(MultiPolygonFromText('MULTIPOLYGON(((0 0,0 3,3 3,3 0,0 0)),((4 4,4 7,7 7,7 4,4 4)))'));

The output is:

MULTIPOLYGON(((0 0,0 3,3 3,3 0,0 0)),((4 4,4 7,7 7,7 4,4 4)))

The WKT representation consists of the keyword MULTIPOLYGON, followed by a set of parentheses that enclose one or more polygon geometries, each of which is enclosed by another set of parentheses. Each polygon geometry consists of one or more linear rings, each of which is enclosed by another set of parentheses. Each linear ring consists of a list of points, each of which is written as X Y or (X, Y), separated by commas. The first and the last point of each linear ring must be the same.

Example 2: Creating a multipolygon value from a WKT representation with a specified SRID

In this example, we use the MultiPolygonFromText() function to create a multipolygon value from a WKT representation with a specified SRID. We use the ST_SRID() function to display the SRID of the result.

SELECT ST_SRID(MultiPolygonFromText('MULTIPOLYGON(((0 0,0 3,3 3,3 0,0 0)),((4 4,4 7,7 7,7 4,4 4)))', 4326));

The output is:

4326

The WKT representation is similar to the previous example, except that it has a SRID of 4326 as the second argument of the function. The SRID indicates the coordinate system of the multipolygon value, which in this case is the World Geodetic System 1984 (WGS 84).

Example 3: Creating a multipolygon value from an invalid WKT representation

In this example, we use the MultiPolygonFromText() function to create a multipolygon value from an invalid WKT representation. We use the ISNULL() function to check if the result is NULL.

SELECT ISNULL(MultiPolygonFromText('MULTIPOLYGON(((0 0,0 3,3 3,0 0)))'));

The output is:

1

The WKT representation is invalid because it has only one polygon geometry, which has only three points, which is not enough to form a closed and simple linear ring. The function returns NULL, which is indicated by 1 in the output.

There are some other functions that are related to the MultiPolygonFromText() function, such as:

  • MULTIPOLYGON(): This function creates a multipolygon value from a set of polygon values. For example:

    SELECT ST_AsText(MULTIPOLYGON(POLYGON((0 0, 0 3, 3 3, 3 0, 0 0)), POLYGON((4 4, 4 7, 7 7, 7 4, 4 4))));
    

    The output is:

    MULTIPOLYGON(((0 0,0 3,3 3,3 0,0 0)),((4 4,4 7,7 7,7 4,4 4)))
  • ST_AsText(): This function converts a geometry value into a WKT representation. It is the inverse of the MultiPolygonFromText() function. For example:

    SELECT ST_AsText(MULTIPOLYGON(POLYGON((0 0, 0 3, 3 3, 3 0, 0 0)), POLYGON((4 4, 4 7, 7 7, 7 4, 4 4))));
    

    The output is:

    MULTIPOLYGON(((0 0,0 3,3 3,3 0,0 0)),((4 4,4 7,7 7,7 4,4 4)))

Conclusion

The MultiPolygonFromText() function is a useful function for working with multipolygon geometries in Mariadb. It allows us to convert a WKT representation of a multipolygon geometry into a multipolygon value. We can also specify the SRID of the multipolygon value, or use the default SRID of 0. The function returns NULL if the input is not a valid WKT representation of a multipolygon geometry. There are some other functions that are related to the MultiPolygonFromText() function, such as MULTIPOLYGON(), ST_MultiPolygonFromWKB(), and ST_AsText(). These functions can help us to import, export, or manipulate spatial data in Mariadb.