Explore miscellaneous GIS functions in MariaDB Server. This section details various SQL functions that support geographic information system operations and spatial data analysis.
ST_Collect(g)
ST_Collect is an aggregate function that can also be used as a window function.
Given multiple geometries, returns the aggregation of the distinct geometry arguments. This function also supports the DISTINCT option. If DISTINCT is used, it returns the aggregation of the distinct geometry arguments.
The resulting value type is chosen using the following policy:
If all arguments are Point values, the result is a MultiPoint value.
If all arguments are LineString values, the result is a MultiLineString value.
If all arguments are Polygon values, the result is a MultiPolygon value.
Otherwise, the result is a GeometryCollection value.
If there are multiple geometry arguments and those arguments are in the same spatial reference system (SRS), the return value is in that SRS. If those arguments are not in the same SRS, an ER_GIS_DIFFERENT_SRIDS_AGGREGATION
error occurs.
Multiple Point geometries aggregated into a MultiPoint geometry:
CREATE OR REPLACE TABLE t1 ( running_number INTEGER NOT NULL
AUTO_INCREMENT, grouping_condition INTEGER, location GEOMETRY , PRIMARY KEY (
running_number));
INSERT INTO t1 ( grouping_condition, location ) VALUES
( 0,ST_GEOMFROMTEXT('POINT(0 0)',4326)),
( 1,ST_GEOMFROMTEXT('POINT(0 0)',4326)),
( 0,ST_GEOMFROMTEXT('POINT(1 0)',4326)),
( 1,ST_GEOMFROMTEXT('POINT(2 0)',4326)),
( 0,ST_GEOMFROMTEXT('POINT(3 0)',4326));
SELECT ST_EQUALS( (SELECT ST_COLLECT( location ) AS t FROM t1),
ST_GEOMFROMTEXT('MULTIPOINT(0 0,0 0,1 0,2 0,3 0) ',4326)) AS equals;
+--------+
| equals |
+--------+
| 1 |
+--------+
This page is licensed: CC BY-SA / Gnu FDL
ST_GeoHash(longitude, latitude, max_length)
ST_GeoHash(point, max_length)
Returns the geohash corresponding to the input values, or NULL if any argument is NULL. Geohashes encode latitude and longitude coordinates into a text string made up only of numeric and lowercase latin letter characters.
The longitude
parameter is a numeric value in the interval [180, -180]. latitude
is a numeric value in the interval [90, -90].
In the case of point
, the x coordinate is treated as the latitude and the y coordinate is treated as the latitude. The same constraints apply.
The max_length
parameter is the upper limit on the resulting string size and cannot exceed 100.
The ST_LatFromGeoHash function decodes a given geohash and returns the latitude.
SELECT ST_GeoHash(ST_GeomFromText('POINT(1 1)'),15), ST_GeoHash(0,30,15);
+----------------------------------------------+---------------------+
| ST_GeoHash(ST_GeomFromText('POINT(1 1)'),15) | ST_GeoHash(0,30,15) |
+----------------------------------------------+---------------------+
| s00twy01mtw037m | sj248j248j248j2 |
+----------------------------------------------+---------------------+
This page is licensed: CC BY-SA / Gnu FDL
ST_IsValid(g)
Given a geometry input, returns 1 if the argument is geometrically valid according to the OGC specifications, 0 if the argument is not geometrically valid.
Unlike ST_Validate, requires valid GIS data, or ERROR 3037 (22023): Invalid GIS data provided to function st_isvalid
is returned.
SELECT ST_IsValid(ST_GeomFromText('LINESTRING (0 0, 1 1)'));
+------------------------------------------------------+
| ST_IsValid(ST_GeomFromText('LINESTRING (0 0, 1 1)')) |
+------------------------------------------------------+
| 1 |
+------------------------------------------------------+
SELECT ST_IsValid(ST_GeomFromText('LINESTRING (0 0, 0 0)'));
+------------------------------------------------------+
| ST_IsValid(ST_GeomFromText('LINESTRING (0 0, 0 0)')) |
+------------------------------------------------------+
| 0 |
+------------------------------------------------------+
A POINT requires both x and y co-ordinates:
SELECT ST_IsValid(ST_GeomFromText('POINT (0)'));
ERROR 3037 (22023): Invalid GIS data provided to function st_isvalid.
This page is licensed: CC BY-SA / Gnu FDL
ST_LatFromGeoHash(geohash)
Decodes a given geohash
string and returns the latitude in the interval [90, -90].
If the argument is NULL, the return value is NULL. If the argument is invalid, an ER_INCORRECT_TYPE error is thrown.
The ST_GeoHash function can be used to generate geohashes.
SELECT ST_LatFromGeoHash('zzzzzzzzz'), ST_LatFromGeoHash('xvrfxvrfxvrfxvr');
+--------------------------------+--------------------------------------+
| ST_LatFromGeoHash('zzzzzzzzz') | ST_LatFromGeoHash('xvrfxvrfxvrfxvr') |
+--------------------------------+--------------------------------------+
| 90 | 30 |
+--------------------------------+--------------------------------------+
This page is licensed: CC BY-SA / Gnu FDL
ST_LongFromGeoHash(geohash)
Decodes a given geohash
string and returns the longitude in the interval [180, -180].
If the argument is NULL, the return value is NULL. If the argument is invalid, an ER_INCORRECT_TYPE error is thrown.
The ST_GeoHash function can be used to generate geohashes.
SELECT ST_LongFromGeoHash('zzzzzzzzz'), ST_LongFromGeoHash('sj248j248j248j2');
+---------------------------------+---------------------------------------+
| ST_LongFromGeoHash('zzzzzzzzz') | ST_LongFromGeoHash('sj248j248j248j2') |
+---------------------------------+---------------------------------------+
| 180 | 0 |
+---------------------------------+---------------------------------------+
This page is licensed: CC BY-SA / Gnu FDL
ST_PointFromGeoHash(geohash, srid)
Takes a given geohash
string and returns a point where the x is the longitude and the y is the latitude.
The latitude is returned as a numeric value in the interval [180, -180]. The longitude is returned as a numeric value in the interval [90, -90]. If the argument is NULL, the return value is NULL. If the argument is invalid, an ER_GIS_INVALID_DATA is thrown.
SELECT ST_ASTEXT(ST_POINTFROMGEOHASH("s00twy01mtw037m",0));
+-----------------------------------------------------+
| ST_ASTEXT(ST_POINTFROMGEOHASH("s00twy01mtw037m",0)) |
+-----------------------------------------------------+
| POINT(1 1) |
+-----------------------------------------------------+
SELECT ST_ASTEXT(ST_POINTFROMGEOHASH(ST_GEOHASH(180,90,20),0));
+---------------------------------------------------------+
| ST_ASTEXT(ST_POINTFROMGEOHASH(ST_GEOHASH(180,90,20),0)) |
+---------------------------------------------------------+
| POINT(180 90) |
+---------------------------------------------------------+
This page is licensed: CC BY-SA / Gnu FDL
ST_Simplify(g, max_distance)
Takes as input a geometry (g) and a double (max_distance). It applies the Ramer–Douglas–Peucker algorithm on g
and returns the resulting geometry.
The goal of the Douglas-Peucker algorithm is to provide generalized simplifications by returning a geometry that is similar to g
but uses only a subset of points. To perform the simplification, all the vertices that are shorter than max_distance
are removed.
The algorithm may produce self-intersections and therefore result in invalid geometries. ST_IsValid can be used to test validity of the result.
If the max_distance is not positive or is NULL
, an ER_WRONG_ARGUMENT
will occur.
SELECT ST_AsText(ST_Simplify(ST_GeomFromText('LINESTRING(0 0,0 2,2 2,2 4,4 4,4 6,6 6)'), 0.5));
+-----------------------------------------------------------------------------------------+
| ST_AsText(ST_Simplify(ST_GeomFromText('LINESTRING(0 0,0 2,2 2,2 4,4 4,4 6,6 6)'), 0.5)) |
+-----------------------------------------------------------------------------------------+
| LINESTRING(0 0,0 2,2 2,2 4,4 4,4 6,6 6) |
+-----------------------------------------------------------------------------------------+
SELECT ST_AsText(ST_Simplify(ST_GeomFromText('LINESTRING(0 0,0 2,2 2,2 4,4 4,4 6,6 6)'), 1));
+---------------------------------------------------------------------------------------+
| ST_AsText(ST_Simplify(ST_GeomFromText('LINESTRING(0 0,0 2,2 2,2 4,4 4,4 6,6 6)'), 1)) |
+---------------------------------------------------------------------------------------+
| LINESTRING(0 0,0 2,2 2,2 4,6 6) |
+---------------------------------------------------------------------------------------+
This page is licensed: CC BY-SA / Gnu FDL
ST_Validate(g)
The function checks that a given geometry is compliant with the Well-Known Binary (WKB) format and Spatial Reference System Identifier (SRID) syntax, and is geometrically valid.
It returns the geometry if it's valid, or NULL if not.
The function is useful to filter out invalid geometry data.
A POINT requires both x and y co-ordinates:
SELECT ST_ASTEXT(ST_VALIDATE(ST_GeomFromText('POINT(1 0)')));
+-------------------------------------------------------+
| ST_ASTEXT(ST_VALIDATE(ST_GeomFromText('POINT(1 0)'))) |
+-------------------------------------------------------+
| POINT(1 0) |
+-------------------------------------------------------+
SELECT ST_ASTEXT(ST_VALIDATE(ST_GeomFromText('POINT(1)')));
+-----------------------------------------------------+
| ST_ASTEXT(ST_VALIDATE(ST_GeomFromText('POINT(1)'))) |
+-----------------------------------------------------+
| NULL |
+-----------------------------------------------------+
This page is licensed: CC BY-SA / Gnu FDL