Menu:
spatial-convenience-functions:: Spatial Convenience Functions
MySQL provides functions to perform various operations on spatial data. These functions can be grouped into several major categories according to the type of operation they perform:
Functions that create geometries in various formats (WKT, WKB, internal)
Functions that convert geometries between formats
Functions that access qualitative or quantitative properties of a geometry
Functions that describe relations between two geometries
Functions that create new geometries from existing ones
For general background about MySQL support for using spatial data, see *note spatial-types::.
File: manual.info.tmp, Node: spatial-function-reference, Next: spatial-function-argument-handling, Prev: spatial-analysis-functions, Up: spatial-analysis-functions
The following table lists each spatial function and provides a short description of each one.
Spatial Functions
Name Description Deprecated
'Area()'
Return Polygon Yes or MultiPolygon
area
'AsBinary()', 'AsWKB()'
Convert from Yes internal
geometry format to WKB
'AsText()', 'AsWKT()'
Convert from Yes internal
geometry format to WKT
'Buffer()'
Return geometry Yes of points within
given distance from geometry
'Centroid()'
Return centroid Yes as a point
'Contains()'
Whether MBR of Yes one geometry
contains MBR of another
'ConvexHull()'
Return convex Yes hull of geometry
'Crosses()'
Whether one Yes geometry crosses
another
'Dimension()'
Dimension of Yes geometry
'Disjoint()'
Whether MBRs of Yes two geometries
are disjoint
'EndPoint()'
End Point of Yes LineString
'Envelope()'
Return MBR of Yes geometry
'Equals()'
Whether MBRs of Yes two geometries
are equal
'ExteriorRing()'
Return exterior Yes ring of Polygon
'GeomCollFromText()', 'GeometryCollectionFromText()'
Return geometry Yes collection from
WKT
'GeomCollFromWKB()', 'GeometryCollectionFromWKB()'
Return geometry Yes collection from
WKB
'GeometryCollection()'
Construct geometry collection from geometries
'GeometryN()'
Return N-th Yes geometry from
geometry collection
'GeometryType()'
Return name of Yes geometry type
'GeomFromText()', 'GeometryFromText()'
Return geometry Yes from WKT
'GeomFromWKB()', 'GeometryFromWKB()'
Return geometry Yes from WKB
'GLength()'
Return length of Yes LineString
'InteriorRingN()'
Return N-th Yes interior ring of
Polygon
'Intersects()'
Whether MBRs of Yes two geometries
intersect
'IsClosed()'
Whether a Yes geometry is
closed and simple
'IsEmpty()'
Whether a Yes geometry is
empty
'IsSimple()'
Whether a Yes geometry is
simple
'LineFromText()', 'LineStringFromText()'
Construct Yes LineString from
WKT
'LineFromWKB()', 'LineStringFromWKB()'
Construct Yes LineString from
WKB
'LineString()'
Construct LineString from Point values
'MBRContains()'
Whether MBR of one geometry contains MBR of another
'MBRCoveredBy()'
Whether one MBR is covered by another
'MBRCovers()'
Whether one MBR covers another
'MBRDisjoint()'
Whether MBRs of two geometries are disjoint
'MBREqual()'
Whether MBRs of Yes two geometries
are equal
'MBREquals()'
Whether MBRs of two geometries are equal
'MBRIntersects()'
Whether MBRs of two geometries intersect
'MBROverlaps()'
Whether MBRs of two geometries overlap
'MBRTouches()'
Whether MBRs of two geometries touch
'MBRWithin()'
Whether MBR of one geometry is within MBR of another
'MLineFromText()', 'MultiLineStringFromText()'
Construct Yes MultiLineString
from WKT
'MLineFromWKB()', 'MultiLineStringFromWKB()'
Construct Yes MultiLineString
from WKB
'MPointFromText()', 'MultiPointFromText()'
Construct Yes MultiPoint from
WKT
'MPointFromWKB()', 'MultiPointFromWKB()'
Construct Yes MultiPoint from
WKB
'MPolyFromText()', 'MultiPolygonFromText()'
Construct Yes MultiPolygon
from WKT
'MPolyFromWKB()', 'MultiPolygonFromWKB()'
Construct Yes MultiPolygon
from WKB
'MultiLineString()'
Contruct MultiLineString from LineString values
'MultiPoint()'
Construct MultiPoint from Point values
'MultiPolygon()'
Construct MultiPolygon from Polygon values
'NumGeometries()'
Return number of Yes geometries in
geometry collection
'NumInteriorRings()'
Return number of Yes interior rings
in Polygon
'NumPoints()'
Return number of Yes points in
LineString
'Overlaps()'
Whether MBRs of Yes two geometries
overlap
'Point()'
Construct Point from coordinates
'PointFromText()'
Construct Point Yes from WKT
'PointFromWKB()'
Construct Point Yes from WKB
'PointN()'
Return N-th Yes point from
LineString
'PolyFromText()', 'PolygonFromText()'
Construct Yes Polygon from WKT
'PolyFromWKB()', 'PolygonFromWKB()'
Construct Yes Polygon from WKB
'Polygon()'
Construct Polygon from LineString arguments
'Distance()'
The distance of Yes one geometry
from another
'SRID()'
Return spatial Yes reference system
ID for geometry
'ST_Area()'
Return Polygon or MultiPolygon area
'ST_AsBinary()', 'ST_AsWKB()'
Convert from internal geometry format to WKB
'ST_AsGeoJSON()'
Generate GeoJSON object from geometry
'ST_AsText()', 'ST_AsWKT()'
Convert from internal geometry format to WKT
'ST_Buffer()'
Return geometry of points within given distance from geometry
'ST_Buffer_Strategy()'
Produce strategy option for ST_Buffer()
'ST_Centroid()'
Return centroid as a point
'ST_Contains()'
Whether one geometry contains another
'ST_ConvexHull()'
Return convex hull of geometry
'ST_Crosses()'
Whether one geometry crosses another
'ST_Difference()'
Return point set difference of two geometries
'ST_Dimension()'
Dimension of geometry
'ST_Disjoint()'
Whether one geometry is disjoint from another
'ST_Distance()'
The distance of one geometry from another
'ST_Distance_Sphere()'
Minimum distance on earth between two geometries
'ST_EndPoint()'
End Point of LineString
'ST_Envelope()'
Return MBR of geometry
'ST_Equals()'
Whether one geometry is equal to another
'ST_ExteriorRing()'
Return exterior ring of Polygon
'ST_GeoHash()'
Produce a geohash value
'ST_GeomCollFromText()', 'ST_GeometryCollectionFromText()', 'ST_GeomCollFromTxt()'
Return geometry collection from WKT
'ST_GeomCollFromWKB()', 'ST_GeometryCollectionFromWKB()'
Return geometry collection from WKB
'ST_GeometryN()'
Return N-th geometry from geometry collection
'ST_GeometryType()'
Return name of geometry type
'ST_GeomFromGeoJSON()'
Generate geometry from GeoJSON object
'ST_GeomFromText()', 'ST_GeometryFromText()'
Return geometry from WKT
'ST_GeomFromWKB()', 'ST_GeometryFromWKB()'
Return geometry from WKB
'ST_InteriorRingN()'
Return N-th interior ring of Polygon
'ST_Intersection()'
Return point set intersection of two geometries
'ST_Intersects()'
Whether one geometry intersects another
'ST_IsClosed()'
Whether a geometry is closed and simple
'ST_IsEmpty()'
Whether a geometry is empty
'ST_IsSimple()'
Whether a geometry is simple
'ST_IsValid()'
Whether a geometry is valid
'ST_LatFromGeoHash()'
Return latitude from geohash value
'ST_Length()'
Return length of LineString
'ST_LineFromText()', 'ST_LineStringFromText()'
Construct LineString from WKT
'ST_LineFromWKB()', 'ST_LineStringFromWKB()'
Construct LineString from WKB
'ST_LongFromGeoHash()'
Return longitude from geohash value
'ST_MakeEnvelope()'
Rectangle around two points
'ST_MLineFromText()', 'ST_MultiLineStringFromText()'
Construct MultiLineString from WKT
'ST_MLineFromWKB()', 'ST_MultiLineStringFromWKB()'
Construct MultiLineString from WKB
'ST_MPointFromText()', 'ST_MultiPointFromText()'
Construct MultiPoint from WKT
'ST_MPointFromWKB()', 'ST_MultiPointFromWKB()'
Construct MultiPoint from WKB
'ST_MPolyFromText()', 'ST_MultiPolygonFromText()'
Construct MultiPolygon from WKT
'ST_MPolyFromWKB()', 'ST_MultiPolygonFromWKB()'
Construct MultiPolygon from WKB
'ST_NumGeometries()'
Return number of geometries in geometry collection
'ST_NumInteriorRing()', 'ST_NumInteriorRings()'
Return number of interior rings in Polygon
'ST_NumPoints()'
Return number of points in LineString
'ST_Overlaps()'
Whether one geometry overlaps another
'ST_PointFromGeoHash()'
Convert geohash value to POINT value
'ST_PointFromText()'
Construct Point from WKT
'ST_PointFromWKB()'
Construct Point from WKB
'ST_PointN()'
Return N-th point from LineString
'ST_PolyFromText()', 'ST_PolygonFromText()'
Construct Polygon from WKT
'ST_PolyFromWKB()', 'ST_PolygonFromWKB()'
Construct Polygon from WKB
'ST_Simplify()'
Return simplified geometry
'ST_SRID()'
Return spatial reference system ID for geometry
'ST_StartPoint()'
Start Point of LineString
'ST_SymDifference()'
Return point set symmetric difference of two geometries
'ST_Touches()'
Whether one geometry touches another
'ST_Union()'
Return point set union of two geometries
'ST_Validate()'
Return validated geometry
'ST_Within()'
Whether one geometry is within another
'ST_X()'
Return X coordinate of Point
'ST_Y()'
Return Y coordinate of Point
'StartPoint()'
Start Point of Yes LineString
'Touches()'
Whether one Yes geometry touches
another
'Within()'
Whether MBR of Yes one geometry is
within MBR of another
'X()'
Return X Yes coordinate of
Point
'Y()'
Return Y Yes coordinate of Point
File: manual.info.tmp, Node: spatial-function-argument-handling, Next: gis-wkt-functions, Prev: spatial-function-reference, Up: spatial-analysis-functions
Spatial values, or geometries, have the properties described in *note gis-class-geometry::. The following discussion lists general spatial function argument-handling characteristics. Specific functions or groups of functions may have additional or different argument-handling characteristics, as discussed in the sections where those function descriptions occur. Where that is true, those descriptions take precedence over the general discussion here.
Spatial functions are defined only for valid geometry values. See *note geometry-well-formedness-validity::.
The spatial reference identifier (SRID) of a geometry identifies the coordinate space in which the geometry is defined. In MySQL, the SRID value is an integer associated with the geometry value. The maximum usable SRID value is 2^32−1. If a larger value is given, only the lower 32 bits are used.
In MySQL, all computations are done assuming SRID 0, regardless of the actual SRID value. SRID 0 represents an infinite flat Cartesian plane with no units assigned to its axes. In the future, computations may use the specified SRID values. To ensure SRID 0 behavior, create geometry values using SRID 0. SRID 0 is the default for new geometry values if no SRID is specified.
Geometry values produced by any spatial function inherit the SRID of the geometry arguments.
The Open Geospatial Consortium (http://www.opengeospatial.org) guidelines require that input polygons already be closed, so unclosed polygons are rejected as invalid rather than being closed.
Empty geometry-collection handling is as follows: An empty WKT input geometry collection may be specified as ''GEOMETRYCOLLECTION()''. This is also the output WKT resulting from a spatial operation that produces an empty geometry collection.
During parsing of a nested geometry collection, the collection is flattened and its basic components are used in various GIS operations to compute results. This provides additional flexibility to users because it is unnecessary to be concerned about the uniqueness of geometry data. Nested geometry collections may be produced from nested GIS function calls without having to be explicitly flattened first.
File: manual.info.tmp, Node: gis-wkt-functions, Next: gis-wkb-functions, Prev: spatial-function-argument-handling, Up: spatial-analysis-functions
These functions take as arguments a Well-Known Text (WKT) representation and, optionally, a spatial reference system identifier (SRID). They return the corresponding geometry.
'ST_GeomFromText()' accepts a WKT value of any geometry type as its first argument. Other functions provide type-specific construction functions for construction of geometry values of each geometry type.
For a description of WKT format, see *note gis-wkt-format::.
'GeomCollFromText(WKT [, SRID])', 'GeometryCollectionFromText(WKT [, SRID])'
'ST_GeomCollFromText()', 'ST_GeometryCollectionFromText()', 'ST_GeomCollFromTxt()', 'GeomCollFromText()', and 'GeometryCollectionFromText()' are synonyms. For more information, see the description of 'ST_GeomCollFromText()'.
'GeomCollFromText()' and 'GeometryCollectionFromText()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_GeomCollFromText()' and 'ST_GeometryCollectionFromText()' instead.
'GeomFromText(WKT [, SRID])', 'GeometryFromText(WKT [, SRID])'
'ST_GeomFromText()', 'ST_GeometryFromText()', 'GeomFromText()', and 'GeometryFromText()' are synonyms. For more information, see the description of 'ST_GeomFromText()'.
'GeomFromText()' and 'GeometryFromText()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_GeomFromText()' and 'ST_GeometryFromText()' instead.
'LineFromText(WKT [, SRID])', 'LineStringFromText(WKT [, SRID])'
'ST_LineFromText()', 'ST_LineStringFromText()', 'LineFromText()', and 'LineStringFromText()' are synonyms. For more information, see the description of 'ST_LineFromText()'.
'LineFromText()' and 'LineStringFromText()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_LineFromText()' and 'ST_LineStringFromText()' instead.
'MLineFromText(WKT [, SRID])', 'MultiLineStringFromText(WKT [, SRID])'
'ST_MLineFromText()', 'ST_MultiLineStringFromText()', 'MLineFromText()', and 'MultiLineStringFromText()' are synonyms. For more information, see the description of 'ST_MLineFromText()'.
'MLineFromText()' and 'MultiLineStringFromText()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_MLineFromText()' and 'ST_MultiLineStringFromText()' instead.
'MPointFromText(WKT [, SRID])', 'MultiPointFromText(WKT [, SRID])'
'ST_MPointFromText()', 'ST_MultiPointFromText()', 'MPointFromText()', and 'MultiPointFromText()' are synonyms. For more information, see the description of 'ST_MPointFromText()'.
'MPointFromText()' and 'MultiPointFromText()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_MPointFromText()' and 'ST_MultiPointFromText()' instead.
'MPolyFromText(WKT [, SRID])', 'MultiPolygonFromText(WKT [, SRID])'
'ST_MPolyFromText()', 'ST_MultiPolygonFromText()', 'MPolyFromText()', and 'MultiPolygonFromText()' are synonyms. For more information, see the description of 'ST_MPolyFromText()'.
'MPolyFromText()' and 'MultiPolygonFromText()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_MPolyFromText()' and 'ST_MultiPolygonFromText()' instead.
'PointFromText(WKT [, SRID])'
'ST_PointFromText()' and 'PointFromText()' are synonyms. For more information, see the description of 'ST_PointFromText()'.
'PointFromText()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_PointFromText()' instead.
'PolyFromText(WKT [, SRID])', 'PolygonFromText(WKT [, SRID])'
'ST_PolyFromText()', 'ST_PolygonFromText()', 'PolyFromText()', and 'PolygonFromText()' are synonyms. For more information, see the description of 'ST_PolyFromText()'.
'PolyFromText()' and 'PolygonFromText()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_PolyFromText()' and 'ST_PolygonFromText()' instead.
'ST_GeomCollFromText(WKT [, SRID])', 'ST_GeometryCollectionFromText(WKT [, SRID])', 'ST_GeomCollFromTxt(WKT [, SRID])'
Constructs a 'GeometryCollection' value using its WKT representation and SRID.
If the geometry argument is 'NULL' or not a syntactically well-formed geometry, or if the SRID argument is 'NULL', the return value is 'NULL'.
mysql> SET @g = "MULTILINESTRING((10 10, 11 11), (9 9, 10 10))";
mysql> SELECT ST_AsText(ST_GeomCollFromText(@g));
+--------------------------------------------+
| ST_AsText(ST_GeomCollFromText(@g)) |
+--------------------------------------------+
| MULTILINESTRING((10 10,11 11),(9 9,10 10)) |
+--------------------------------------------+
'ST_GeomCollFromText()', 'ST_GeometryCollectionFromText()', 'ST_GeomCollFromTxt()', 'GeomCollFromText()', and 'GeometryCollectionFromText()' are synonyms.
'ST_GeomFromText(WKT [, SRID])', 'ST_GeometryFromText(WKT [, SRID])'
Constructs a geometry value of any type using its WKT representation and SRID.
If the geometry argument is 'NULL' or not a syntactically well-formed geometry, or if the SRID argument is 'NULL', the return value is 'NULL'.
'ST_GeomFromText()', 'ST_GeometryFromText()', 'GeomFromText()', and 'GeometryFromText()' are synonyms.
'ST_LineFromText(WKT [, SRID])', 'ST_LineStringFromText(WKT [, SRID])'
Constructs a 'LineString' value using its WKT representation and SRID.
If the geometry argument is 'NULL' or not a syntactically well-formed geometry, or if the SRID argument is 'NULL', the return value is 'NULL'.
'ST_LineFromText()', 'ST_LineStringFromText()', 'LineFromText()', and 'LineStringFromText()' are synonyms.
'ST_MLineFromText(WKT [, SRID])', 'ST_MultiLineStringFromText(WKT [, SRID])'
Constructs a 'MultiLineString' value using its WKT representation and SRID.
If the geometry argument is 'NULL' or not a syntactically well-formed geometry, or if the SRID argument is 'NULL', the return value is 'NULL'.
'ST_MLineFromText()', 'ST_MultiLineStringFromText()', 'MLineFromText()', and 'MultiLineStringFromText()' are synonyms.
'ST_MPointFromText(WKT [, SRID])', 'ST_MultiPointFromText(WKT [, SRID])'
Constructs a 'MultiPoint' value using its WKT representation and SRID.
If the geometry argument is 'NULL' or not a syntactically well-formed geometry, or if the SRID argument is 'NULL', the return value is 'NULL'.
Functions such as 'ST_MPointFromText()' and 'ST_GeomFromText()' that accept WKT-format representations of 'MultiPoint' values permit individual points within values to be surrounded by parentheses. For example, both of the following function calls are valid:
ST_MPointFromText('MULTIPOINT (1 1, 2 2, 3 3)')
ST_MPointFromText('MULTIPOINT ((1 1), (2 2), (3 3))')
'ST_MPointFromText()', 'ST_MultiPointFromText()', 'MPointFromText()', and 'MultiPointFromText()' are synonyms.
'ST_MPolyFromText(WKT [, SRID])', 'ST_MultiPolygonFromText(WKT [, SRID])'
Constructs a 'MultiPolygon' value using its WKT representation and SRID.
If the geometry argument is 'NULL' or not a syntactically well-formed geometry, or if the SRID argument is 'NULL', the return value is 'NULL'.
'ST_MPolyFromText()', 'ST_MultiPolygonFromText()', 'MPolyFromText()', and 'MultiPolygonFromText()' are synonyms.
'ST_PointFromText(WKT [, SRID])'
Constructs a 'Point' value using its WKT representation and SRID.
If the geometry argument is 'NULL' or not a syntactically well-formed geometry, or if the SRID argument is 'NULL', the return value is 'NULL'.
'ST_PointFromText()' and 'PointFromText()' are synonyms.
'ST_PolyFromText(WKT [, SRID])', 'ST_PolygonFromText(WKT [, SRID])'
Constructs a 'Polygon' value using its WKT representation and SRID.
If the geometry argument is 'NULL' or not a syntactically well-formed geometry, or if the SRID argument is 'NULL', the return value is 'NULL'.
'ST_PolyFromText()', 'ST_PolygonFromText()', 'PolyFromText()', and 'PolygonFromText()' are synonyms.
File: manual.info.tmp, Node: gis-wkb-functions, Next: gis-mysql-specific-functions, Prev: gis-wkt-functions, Up: spatial-analysis-functions
These functions take as arguments a *note 'BLOB': blob. containing a Well-Known Binary (WKB) representation and, optionally, a spatial reference system identifier (SRID). They return the corresponding geometry.
'ST_GeomFromWKB()' accepts a WKB value of any geometry type as its first argument. Other functions provide type-specific construction functions for construction of geometry values of each geometry type.
These functions also accept geometry objects as returned by the functions in *note gis-mysql-specific-functions::. Thus, those functions may be used to provide the first argument to the functions in this section. However, as of MySQL 5.7.19, use of geometry arguments is deprecated and generates a warning. Geometry arguments are not accepted in MySQL 8.0. To migrate calls from using geometry arguments to using WKB arguments, follow these guidelines:
For a description of WKB format, see *note gis-wkb-format::.
Rewrite constructs such as 'ST_GeomFromWKB(Point(0, 0))' as 'Point(0, 0)'.
Rewrite constructs such as 'ST_GeomFromWKB(Point(0, 0), 4326)' as 'ST_GeomFromWKB(ST_AsWKB(Point(0, 0)), 4326)'. (Alternatively, in MySQL 8.0, you can use 'ST_SRID(Point(0, 0), 4326)'.)
'GeomCollFromWKB(WKB [, SRID])', 'GeometryCollectionFromWKB(WKB [, SRID])'
'ST_GeomCollFromWKB()', 'ST_GeometryCollectionFromWKB()', 'GeomCollFromWKB()', and 'GeometryCollectionFromWKB()' are synonyms. For more information, see the description of 'ST_GeomCollFromWKB()'.
'GeomCollFromWKB()' and 'GeometryCollectionFromWKB()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_GeomCollFromWKB()' and 'ST_GeometryCollectionFromWKB()' instead.
'GeomFromWKB(WKB [, SRID])', 'GeometryFromWKB(WKB [, SRID])'
'ST_GeomFromWKB()', 'ST_GeometryFromWKB()', 'GeomFromWKB()', and 'GeometryFromWKB()' are synonyms. For more information, see the description of 'ST_GeomFromWKB()'.
'GeomFromWKB()' and 'GeometryFromWKB()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_GeomFromWKB()' and 'ST_GeometryFromWKB()' instead.
'LineFromWKB(WKB [, SRID])', 'LineStringFromWKB(WKB [, SRID])'
'ST_LineFromWKB()', 'ST_LineStringFromWKB()', 'LineFromWKB()', and 'LineStringFromWKB()' are synonyms. For more information, see the description of 'ST_LineFromWKB()'.
'LineFromWKB()' and 'LineStringFromWKB()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_LineFromWKB()' and 'ST_LineStringFromWKB()' instead.
'MLineFromWKB(WKB [, SRID])', 'MultiLineStringFromWKB(WKB [, SRID])'
'ST_MLineFromWKB()', 'ST_MultiLineStringFromWKB()', 'MLineFromWKB()', and 'MultiLineStringFromWKB()' are synonyms. For more information, see the description of 'ST_MLineFromWKB()'.
'MLineFromWKB()' and 'MultiLineStringFromWKB()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_MLineFromWKB()' and 'ST_MultiLineStringFromWKB()' instead.
'MPointFromWKB(WKB [, SRID])', 'MultiPointFromWKB(WKB [, SRID])'
'ST_MPointFromWKB()', 'ST_MultiPointFromWKB()', 'MPointFromWKB()', and 'MultiPointFromWKB()' are synonyms. For more information, see the description of 'ST_MPointFromWKB()'.
'MPointFromWKB()' and 'MultiPointFromWKB()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_MPointFromWKB()' and 'ST_MultiPointFromWKB()' instead.
'MPolyFromWKB(WKB [, SRID])', 'MultiPolygonFromWKB(WKB [, SRID])'
'ST_MPolyFromWKB()', 'ST_MultiPolygonFromWKB()', 'MPolyFromWKB()', and 'MultiPolygonFromWKB()' are synonyms. For more information, see the description of 'ST_MPolyFromWKB()'.
'MPolyFromWKB()' and 'MultiPolygonFromWKB()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_MPolyFromWKB()' and 'ST_MultiPolygonFromWKB()' instead.
'PointFromWKB(WKB [, SRID])'
'ST_PointFromWKB()' and 'PointFromWKB()' are synonyms. For more information, see the description of 'ST_PointFromWKB()'.
'PointFromWKB()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_PointFromWKB()' instead.
'PolyFromWKB(WKB [, SRID])', 'PolygonFromWKB(WKB [, SRID])'
'ST_PolyFromWKB()', 'ST_PolygonFromWKB()', 'PolyFromWKB()', and 'PolygonFromWKB()' are synonyms. For more information, see the description of 'ST_PolyFromWKB()'.
'PolyFromWKB()' and 'PolygonFromWKB()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_PolyFromWKB()' and 'ST_PolygonFromWKB()' instead.
'ST_GeomCollFromWKB(WKB [, SRID])', 'ST_GeometryCollectionFromWKB(WKB [, SRID])'
Constructs a 'GeometryCollection' value using its WKB representation and SRID.
The result is 'NULL' if the WKB or SRID argument is 'NULL'.
'ST_GeomCollFromWKB()', 'ST_GeometryCollectionFromWKB()', 'GeomCollFromWKB()', and 'GeometryCollectionFromWKB()' are synonyms.
'ST_GeomFromWKB(WKB [, SRID])', 'ST_GeometryFromWKB(WKB [, SRID])'
Constructs a geometry value of any type using its WKB representation and SRID.
The result is 'NULL' if the WKB or SRID argument is 'NULL'.
'ST_GeomFromWKB()', 'ST_GeometryFromWKB()', 'GeomFromWKB()', and 'GeometryFromWKB()' are synonyms.
'ST_LineFromWKB(WKB [, SRID])', 'ST_LineStringFromWKB(WKB [, SRID])'
Constructs a 'LineString' value using its WKB representation and SRID.
The result is 'NULL' if the WKB or SRID argument is 'NULL'.
'ST_LineFromWKB()', 'ST_LineStringFromWKB()', 'LineFromWKB()', and 'LineStringFromWKB()' are synonyms.
'ST_MLineFromWKB(WKB [, SRID])', 'ST_MultiLineStringFromWKB(WKB [, SRID])'
Constructs a 'MultiLineString' value using its WKB representation and SRID.
The result is 'NULL' if the WKB or SRID argument is 'NULL'.
'ST_MLineFromWKB()', 'ST_MultiLineStringFromWKB()', 'MLineFromWKB()', and 'MultiLineStringFromWKB()' are synonyms.
'ST_MPointFromWKB(WKB [, SRID])', 'ST_MultiPointFromWKB(WKB [, SRID])'
Constructs a 'MultiPoint' value using its WKB representation and SRID.
The result is 'NULL' if the WKB or SRID argument is 'NULL'.
'ST_MPointFromWKB()', 'ST_MultiPointFromWKB()', 'MPointFromWKB()', and 'MultiPointFromWKB()' are synonyms.
'ST_MPolyFromWKB(WKB [, SRID])', 'ST_MultiPolygonFromWKB(WKB [, SRID])'
Constructs a 'MultiPolygon' value using its WKB representation and SRID.
The result is 'NULL' if the WKB or SRID argument is 'NULL'.
'ST_MPolyFromWKB()', 'ST_MultiPolygonFromWKB()', 'MPolyFromWKB()', and 'MultiPolygonFromWKB()' are synonyms.
'ST_PointFromWKB(WKB [, SRID])'
Constructs a 'Point' value using its WKB representation and SRID.
The result is 'NULL' if the WKB or SRID argument is 'NULL'.
'ST_PointFromWKB()' and 'PointFromWKB()' are synonyms.
'ST_PolyFromWKB(WKB [, SRID])', 'ST_PolygonFromWKB(WKB [, SRID])'
Constructs a 'Polygon' value using its WKB representation and SRID.
The result is 'NULL' if the WKB or SRID argument is 'NULL'.
'ST_PolyFromWKB()', 'ST_PolygonFromWKB()', 'PolyFromWKB()', and 'PolygonFromWKB()' are synonyms.
File: manual.info.tmp, Node: gis-mysql-specific-functions, Next: gis-format-conversion-functions, Prev: gis-wkb-functions, Up: spatial-analysis-functions
MySQL provides a set of useful nonstandard functions for creating geometry values. The functions described in this section are MySQL extensions to the OpenGIS specification.
These functions produce geometry objects from either WKB values or geometry objects as arguments. If any argument is not a proper WKB or geometry representation of the proper object type, the return value is 'NULL'.
For example, you can insert the geometry return value from 'Point()' directly into a 'POINT' column:
INSERT INTO t1 (pt_col) VALUES(Point(1,2));
'GeometryCollection(G [, G] ...)'
Constructs a 'GeometryCollection' value from the geometry arguments.
'GeometryCollection()' returns all the proper geometries contained in the arguments even if a nonsupported geometry is present.
'GeometryCollection()' with no arguments is permitted as a way to create an empty geometry.
'LineString(PT [, PT] ...)'
Constructs a 'LineString' value from a number of 'Point' or WKB 'Point' arguments. If the number of arguments is less than two, the return value is 'NULL'.
'MultiLineString(LS [, LS] ...)'
Constructs a 'MultiLineString' value using 'LineString' or WKB 'LineString' arguments.
'MultiPoint(PT [, PT2] ...)'
Constructs a 'MultiPoint' value using 'Point' or WKB 'Point' arguments.
'MultiPolygon(POLY [, POLY] ...)'
Constructs a 'MultiPolygon' value from a set of 'Polygon' or WKB 'Polygon' arguments.
'Point(X, Y)'
Constructs a 'Point' using its coordinates.
'Polygon(LS [, LS] ...)'
Constructs a 'Polygon' value from a number of 'LineString' or WKB 'LineString' arguments. If any argument does not represent a 'LinearRing' (that is, not a closed and simple 'LineString'), the return value is 'NULL'.
File: manual.info.tmp, Node: gis-format-conversion-functions, Next: gis-property-functions, Prev: gis-mysql-specific-functions, Up: spatial-analysis-functions
MySQL supports the functions listed in this section for converting geometry values from internal geometry format to WKT or WKB format.
There are also functions to convert a string from WKT or WKB format to internal geometry format. See note gis-wkt-functions::, and note gis-wkb-functions::.
'AsBinary(G)', 'AsWKB(G)'
'ST_AsBinary()', 'ST_AsWKB()', 'AsBinary()', and 'AsWKB()' are synonyms. For more information, see the description of 'ST_AsBinary()'.
'AsBinary()' and 'AsWKB()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_AsBinary()' and 'ST_AsWKB()' instead.
'AsText(G)', 'AsWKT(G)'
'ST_AsText()', 'ST_AsWKT()', 'AsText()', and 'AsWKT()' are synonyms. For more information, see the description of 'ST_AsText()'.
'AsText()' and 'AsWKT()' are deprecated; expect them to be removed in a future MySQL release. Use 'ST_AsText()' and 'ST_AsWKT()' instead.
'ST_AsBinary(G)', 'ST_AsWKB(G)'
Converts a value in internal geometry format to its WKB representation and returns the binary result.
If the argument is 'NULL', the return value is 'NULL'. If the argument is not a syntactically well-formed geometry, an 'ER_GIS_INVALID_DATA' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.
SELECT ST_AsBinary(g) FROM geom;
'ST_AsBinary()', 'ST_AsWKB()', 'AsBinary()', and 'AsWKB()' are synonyms.
'ST_AsText(G)', 'ST_AsWKT(G)'
Converts a value in internal geometry format to its WKT representation and returns the string result.
If the argument is 'NULL', the return value is 'NULL'. If the argument is not a syntactically well-formed geometry, an 'ER_GIS_INVALID_DATA' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.
mysql> SET @g = 'LineString(1 1,2 2,3 3)';
mysql> SELECT ST_AsText(ST_GeomFromText(@g));
+--------------------------------+
| ST_AsText(ST_GeomFromText(@g)) |
+--------------------------------+
| LINESTRING(1 1,2 2,3 3) |
+--------------------------------+
'ST_AsText()', 'ST_AsWKT()', 'AsText()', and 'AsWKT()' are synonyms.
Output for 'MultiPoint' values includes parentheses around each point. For example:
mysql> SET @mp = 'MULTIPOINT(1 1, 2 2, 3 3)';
mysql> SELECT ST_AsText(ST_GeomFromText(@mp));
+---------------------------------+
| ST_AsText(ST_GeomFromText(@mp)) |
+---------------------------------+
| MULTIPOINT((1 1),(2 2),(3 3)) |
+---------------------------------+
File: manual.info.tmp, Node: gis-property-functions, Next: spatial-operator-functions, Prev: gis-format-conversion-functions, Up: spatial-analysis-functions
Menu:
gis-geometrycollection-property-functions:: GeometryCollection Property Functions
Each function that belongs to this group takes a geometry value as its argument and returns some quantitative or qualitative property of the geometry. Some functions restrict their argument type. Such functions return 'NULL' if the argument is of an incorrect geometry type. For example, the 'ST_Area()' polygon function returns 'NULL' if the object type is neither 'Polygon' nor 'MultiPolygon'.
File: manual.info.tmp, Node: gis-general-property-functions, Next: gis-point-property-functions, Prev: gis-property-functions, Up: gis-property-functions
12.16.7.1 General Geometry Property Functions .............................................
The functions listed in this section do not restrict their argument and accept a geometry value of any type.
'Dimension(G)'
'ST_Dimension()' and 'Dimension()' are synonyms. For more information, see the description of 'ST_Dimension()'.
'Dimension()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Dimension()' instead.
'Envelope(G)'
'ST_Envelope()' and 'Envelope()' are synonyms. For more information, see the description of 'ST_Envelope()'.
'Envelope()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Envelope()' instead.
'GeometryType(G)'
'ST_GeometryType()' and 'GeometryType()' are synonyms. For more information, see the description of 'ST_GeometryType()'.
'GeometryType()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_GeometryType()' instead.
'IsEmpty(G)'
'ST_IsEmpty()' and 'IsEmpty()' are synonyms. For more information, see the description of 'ST_IsEmpty()'.
'IsEmpty()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_IsEmpty()' instead.
'IsSimple(G)'
'ST_IsSimple()' and 'IsSimple()' are synonyms. For more information, see the description of 'ST_IsSimple()'.
'IsSimple()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_IsSimple()' instead.
'SRID(G)'
'ST_SRID()' and 'SRID()' are synonyms. For more information, see the description of 'ST_SRID()'.
'SRID()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_SRID()' instead.
'ST_Dimension(G)'
Returns the inherent dimension of the geometry value G, or 'NULL' if the argument is 'NULL'. The dimension can be −1, 0, 1, or 2. The meaning of these values is given in *note gis-class-geometry::.
mysql> SELECT ST_Dimension(ST_GeomFromText('LineString(1 1,2 2)'));
+------------------------------------------------------+
| ST_Dimension(ST_GeomFromText('LineString(1 1,2 2)')) |
+------------------------------------------------------+
| 1 |
+------------------------------------------------------+
'ST_Dimension()' and 'Dimension()' are synonyms.
'ST_Envelope(G)'
Returns the minimum bounding rectangle (MBR) for the geometry value G, or 'NULL' if the argument is 'NULL'. The result is returned as a 'Polygon' value that is defined by the corner points of the bounding box:
POLYGON((MINX MINY, MAXX MINY, MAXX MAXY, MINX MAXY, MINX MINY))
mysql> SELECT ST_AsText(ST_Envelope(ST_GeomFromText('LineString(1 1,2 2)')));
+----------------------------------------------------------------+
| ST_AsText(ST_Envelope(ST_GeomFromText('LineString(1 1,2 2)'))) |
+----------------------------------------------------------------+
| POLYGON((1 1,2 1,2 2,1 2,1 1)) |
+----------------------------------------------------------------+
If the argument is a point or a vertical or horizontal line segment, 'ST_Envelope()' returns the point or the line segment as its MBR rather than returning an invalid polygon:
mysql> SELECT ST_AsText(ST_Envelope(ST_GeomFromText('LineString(1 1,1 2)')));
+----------------------------------------------------------------+
| ST_AsText(ST_Envelope(ST_GeomFromText('LineString(1 1,1 2)'))) |
+----------------------------------------------------------------+
| LINESTRING(1 1,1 2) |
+----------------------------------------------------------------+
'ST_Envelope()' and 'Envelope()' are synonyms.
'ST_GeometryType(G)'
Returns a binary string indicating the name of the geometry type of which the geometry instance G is a member, or 'NULL' if the argument is 'NULL'. The name corresponds to one of the instantiable 'Geometry' subclasses.
mysql> SELECT ST_GeometryType(ST_GeomFromText('POINT(1 1)'));
+------------------------------------------------+
| ST_GeometryType(ST_GeomFromText('POINT(1 1)')) |
+------------------------------------------------+
| POINT |
+------------------------------------------------+
'ST_GeometryType()' and 'GeometryType()' are synonyms.
'ST_IsEmpty(G)'
This function is a placeholder that returns 0 for any valid geometry value, 1 for any invalid geometry value, or 'NULL' if the argument is 'NULL'.
MySQL does not support GIS 'EMPTY' values such as 'POINT EMPTY'.
'ST_IsEmpty()' and 'IsEmpty()' are synonyms.
'ST_IsSimple(G)'
Returns 1 if the geometry value G has no anomalous geometric points, such as self-intersection or self-tangency. 'ST_IsSimple()' returns 0 if the argument is not simple, and 'NULL' if the argument is 'NULL'.
The descriptions of the instantiable geometric classes given under *note opengis-geometry-model:: includes the specific conditions that cause class instances to be classified as not simple.
'ST_IsSimple()' and 'IsSimple()' are synonyms.
'ST_SRID(G)'
Returns an integer indicating the spatial reference system ID associated with the geometry value G, or 'NULL' if the argument is 'NULL'.
mysql> SELECT ST_SRID(ST_GeomFromText('LineString(1 1,2 2)',101));
+-----------------------------------------------------+
| ST_SRID(ST_GeomFromText('LineString(1 1,2 2)',101)) |
+-----------------------------------------------------+
| 101 |
+-----------------------------------------------------+
'ST_SRID()' and 'SRID()' are synonyms.
File: manual.info.tmp, Node: gis-point-property-functions, Next: gis-linestring-property-functions, Prev: gis-general-property-functions, Up: gis-property-functions
12.16.7.2 Point Property Functions ..................................
A 'Point' consists of X and Y coordinates, which may be obtained using the following functions:
'ST_X(P)'
Returns the X-coordinate value for the 'Point' object P as a double-precision number.
mysql> SELECT ST_X(Point(56.7, 53.34));
+--------------------------+
| ST_X(Point(56.7, 53.34)) |
+--------------------------+
| 56.7 |
+--------------------------+
'ST_X()' and 'X()' are synonyms.
'ST_Y(P)'
Returns the Y-coordinate value for the 'Point' object P as a double-precision number.
mysql> SELECT ST_Y(Point(56.7, 53.34));
+--------------------------+
| ST_Y(Point(56.7, 53.34)) |
+--------------------------+
| 53.34 |
+--------------------------+
'ST_Y()' and 'Y()' are synonyms.
'X(P)'
'ST_X()' and 'X()' are synonyms. For more information, see the description of 'ST_X()'.
'X()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_X()' instead.
'Y(P)'
'ST_Y()' and 'Y()' are synonyms. For more information, see the description of 'ST_Y()'.
'Y()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Y()' instead.
File: manual.info.tmp, Node: gis-linestring-property-functions, Next: gis-polygon-property-functions, Prev: gis-point-property-functions, Up: gis-property-functions
12.16.7.3 LineString and MultiLineString Property Functions ...........................................................
A 'LineString' consists of 'Point' values. You can extract particular points of a 'LineString', count the number of points that it contains, or obtain its length.
Some functions in this section also work for 'MultiLineString' values.
'EndPoint(LS)'
'ST_EndPoint()' and 'EndPoint()' are synonyms. For more information, see the description of 'ST_EndPoint()'.
'EndPoint()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_EndPoint()' instead.
'GLength(LS)'
'GLength()' is a nonstandard name. It corresponds to the OpenGIS 'ST_Length()' function. (There is an existing SQL function 'Length()' that calculates the length of string values.)
'GLength()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Length()' instead.
'IsClosed(LS)'
'ST_IsClosed()' and 'IsClosed()' are synonyms. For more information, see the description of 'ST_IsClosed()'.
'IsClosed()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_IsClosed()' instead.
'NumPoints(LS)'
'ST_NumPoints()' and 'NumPoints()' are synonyms. For more information, see the description of 'ST_NumPoints()'.
'NumPoints()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_NumPoints()' instead.
'PointN(LS, N)'
'ST_PointN()' and 'PointN()' are synonyms. For more information, see the description of 'ST_PointN()'.
'PointN()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_PointN()' instead.
'ST_EndPoint(LS)'
Returns the 'Point' that is the endpoint of the 'LineString' value LS. If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
mysql> SET @ls = 'LineString(1 1,2 2,3 3)';
mysql> SELECT ST_AsText(ST_EndPoint(ST_GeomFromText(@ls)));
+----------------------------------------------+
| ST_AsText(ST_EndPoint(ST_GeomFromText(@ls))) |
+----------------------------------------------+
| POINT(3 3) |
+----------------------------------------------+
'ST_EndPoint()' and 'EndPoint()' are synonyms.
'ST_IsClosed(LS)'
For a 'LineString' value LS, 'ST_IsClosed()' returns 1 if LS is closed (that is, its 'ST_StartPoint()' and 'ST_EndPoint()' values are the same). If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
For a 'MultiLineString' value LS, 'ST_IsClosed()' returns 1 if LS is closed (that is, the 'ST_StartPoint()' and 'ST_EndPoint()' values are the same for each 'LineString' in LS).
'ST_IsClosed()' returns 0 if LS is not closed.
mysql> SET @ls1 = 'LineString(1 1,2 2,3 3,2 2)';
mysql> SET @ls2 = 'LineString(1 1,2 2,3 3,1 1)';
mysql> SELECT ST_IsClosed(ST_GeomFromText(@ls1));
+------------------------------------+
| ST_IsClosed(ST_GeomFromText(@ls1)) |
+------------------------------------+
| 0 |
+------------------------------------+
mysql> SELECT ST_IsClosed(ST_GeomFromText(@ls2));
+------------------------------------+
| ST_IsClosed(ST_GeomFromText(@ls2)) |
+------------------------------------+
| 1 |
+------------------------------------+
mysql> SET @ls3 = 'MultiLineString((1 1,2 2,3 3),(4 4,5 5))';
mysql> SELECT ST_IsClosed(ST_GeomFromText(@ls3));
+------------------------------------+
| ST_IsClosed(ST_GeomFromText(@ls3)) |
+------------------------------------+
| 0 |
+------------------------------------+
'ST_IsClosed()' and 'IsClosed()' are synonyms.
'ST_Length(LS)'
Returns a double-precision number indicating the length of the 'LineString' or 'MultiLineString' value LS in its associated spatial reference system. The length of a 'MultiLineString' value is equal to the sum of the lengths of its elements. If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
mysql> SET @ls = 'LineString(1 1,2 2,3 3)';
mysql> SELECT ST_Length(ST_GeomFromText(@ls));
+---------------------------------+
| ST_Length(ST_GeomFromText(@ls)) |
+---------------------------------+
| 2.8284271247461903 |
+---------------------------------+
mysql> SET @mls = 'MultiLineString((1 1,2 2,3 3),(4 4,5 5))';
mysql> SELECT ST_Length(ST_GeomFromText(@mls));
+----------------------------------+
| ST_Length(ST_GeomFromText(@mls)) |
+----------------------------------+
| 4.242640687119286 |
+----------------------------------+
'ST_Length()' should be used in preference to 'GLength()', which has a nonstandard name.
'ST_NumPoints(LS)'
Returns the number of 'Point' objects in the 'LineString' value LS. If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
mysql> SET @ls = 'LineString(1 1,2 2,3 3)';
mysql> SELECT ST_NumPoints(ST_GeomFromText(@ls));
+------------------------------------+
| ST_NumPoints(ST_GeomFromText(@ls)) |
+------------------------------------+
| 3 |
+------------------------------------+
'ST_NumPoints()' and 'NumPoints()' are synonyms.
'ST_PointN(LS, N)'
Returns the N-th 'Point' in the 'Linestring' value LS. Points are numbered beginning with 1. If any argument is 'NULL' or the geometry argument is an empty geometry, the return value is 'NULL'.
mysql> SET @ls = 'LineString(1 1,2 2,3 3)';
mysql> SELECT ST_AsText(ST_PointN(ST_GeomFromText(@ls),2));
+----------------------------------------------+
| ST_AsText(ST_PointN(ST_GeomFromText(@ls),2)) |
+----------------------------------------------+
| POINT(2 2) |
+----------------------------------------------+
'ST_PointN()' and 'PointN()' are synonyms.
'ST_StartPoint(LS)'
Returns the 'Point' that is the start point of the 'LineString' value LS. If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
mysql> SET @ls = 'LineString(1 1,2 2,3 3)';
mysql> SELECT ST_AsText(ST_StartPoint(ST_GeomFromText(@ls)));
+------------------------------------------------+
| ST_AsText(ST_StartPoint(ST_GeomFromText(@ls))) |
+------------------------------------------------+
| POINT(1 1) |
+------------------------------------------------+
'ST_StartPoint()' and 'StartPoint()' are synonyms.
'StartPoint(LS)'
'ST_StartPoint()' and 'StartPoint()' are synonyms. For more information, see the description of 'ST_StartPoint()'.
'StartPoint()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_StartPoint()' instead.
File: manual.info.tmp, Node: gis-polygon-property-functions, Next: gis-geometrycollection-property-functions, Prev: gis-linestring-property-functions, Up: gis-property-functions
12.16.7.4 Polygon and MultiPolygon Property Functions .....................................................
Functions in this section return properties of 'Polygon' or 'MultiPolygon' values.
'Area({POLY|MPOLY})'
'ST_Area()' and 'Area()' are synonyms. For more information, see the description of 'ST_Area()'.
'Area()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Area()' instead.
'Centroid({POLY|MPOLY})'
'ST_Centroid()' and 'Centroid()' are synonyms. For more information, see the description of 'ST_Centroid()'.
'Centroid()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Centroid()' instead.
'ExteriorRing(POLY)'
'ST_ExteriorRing()' and 'ExteriorRing()' are synonyms. For more information, see the description of 'ST_ExteriorRing()'.
'ExteriorRing()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_ExteriorRing()' instead.
'InteriorRingN(POLY, N)'
'ST_InteriorRingN()' and 'InteriorRingN()' are synonyms. For more information, see the description of 'ST_InteriorRingN()'.
'InteriorRingN()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_InteriorRingN()' instead.
'NumInteriorRings(POLY)'
'ST_NumInteriorRings()' and 'NumInteriorRings()' are synonyms. For more information, see the description of 'ST_NumInteriorRings()'.
'NumInteriorRings()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_NumInteriorRings()' instead.
'ST_Area({POLY|MPOLY})'
Returns a double-precision number indicating the area of the 'Polygon' or 'MultiPolygon' argument, as measured in its spatial reference system. For arguments of dimension 0 or 1, the result isThe result is the sum of the area values of all components for a geometry collection. If a geometry collection is empty, its area is returned as 0.
mysql> SET @poly =
'Polygon((0 0,0 3,3 0,0 0),(1 1,1 2,2 1,1 1))';
mysql> SELECT ST_Area(ST_GeomFromText(@poly));
+---------------------------------+
| ST_Area(ST_GeomFromText(@poly)) |
+---------------------------------+
| 4 |
+---------------------------------+
mysql> SET @mpoly =
'MultiPolygon(((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1)))';
mysql> SELECT ST_Area(ST_GeomFromText(@mpoly));
+----------------------------------+
| ST_Area(ST_GeomFromText(@mpoly)) |
+----------------------------------+
| 8 |
+----------------------------------+
'ST_Area()' and 'Area()' are synonyms.
'ST_Centroid({POLY|MPOLY})'
Returns the mathematical centroid for the 'Polygon' or 'MultiPolygon' argument as a 'Point'. The result is not guaranteed to be on the 'MultiPolygon'. If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
This function processes geometry collections by computing the centroid point for components of highest dimension in the collection. Such components are extracted and made into a single 'MultiPolygon', 'MultiLineString', or 'MultiPoint' for centroid computation. If the argument is an empty geometry collection, the return value is 'NULL'.
mysql> SET @poly =
ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))');
mysql> SELECT ST_GeometryType(@poly),ST_AsText(ST_Centroid(@poly));
+------------------------+--------------------------------------------+
| ST_GeometryType(@poly) | ST_AsText(ST_Centroid(@poly)) |
+------------------------+--------------------------------------------+
| POLYGON | POINT(4.958333333333333 4.958333333333333) |
+------------------------+--------------------------------------------+
'ST_Centroid()' and 'Centroid()' are synonyms.
'ST_ExteriorRing(POLY)'
Returns the exterior ring of the 'Polygon' value POLY as a 'LineString'. If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
mysql> SET @poly =
'Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))';
mysql> SELECT ST_AsText(ST_ExteriorRing(ST_GeomFromText(@poly)));
+----------------------------------------------------+
| ST_AsText(ST_ExteriorRing(ST_GeomFromText(@poly))) |
+----------------------------------------------------+
| LINESTRING(0 0,0 3,3 3,3 0,0 0) |
+----------------------------------------------------+
'ST_ExteriorRing()' and 'ExteriorRing()' are synonyms.
'ST_InteriorRingN(POLY, N)'
Returns the N-th interior ring for the 'Polygon' value POLY as a 'LineString'. Rings are numbered beginning with 1. If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
mysql> SET @poly =
'Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))';
mysql> SELECT ST_AsText(ST_InteriorRingN(ST_GeomFromText(@poly),1));
+-------------------------------------------------------+
| ST_AsText(ST_InteriorRingN(ST_GeomFromText(@poly),1)) |
+-------------------------------------------------------+
| LINESTRING(1 1,1 2,2 2,2 1,1 1) |
+-------------------------------------------------------+
'ST_InteriorRingN()' and 'InteriorRingN()' are synonyms.
'ST_NumInteriorRing(POLY)', 'ST_NumInteriorRings(POLY)'
Returns the number of interior rings in the 'Polygon' value POLY. If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
mysql> SET @poly =
'Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))';
mysql> SELECT ST_NumInteriorRings(ST_GeomFromText(@poly));
+---------------------------------------------+
| ST_NumInteriorRings(ST_GeomFromText(@poly)) |
+---------------------------------------------+
| 1 |
+---------------------------------------------+
'ST_NumInteriorRing()', 'ST_NumInteriorRings()', and 'NumInteriorRings()' are synonyms.
File: manual.info.tmp, Node: gis-geometrycollection-property-functions, Prev: gis-polygon-property-functions, Up: gis-property-functions
12.16.7.5 GeometryCollection Property Functions ...............................................
These functions return properties of 'GeometryCollection' values.
'GeometryN(GC, N)'
'ST_GeometryN()' and 'GeometryN()' are synonyms. For more information, see the description of 'ST_GeometryN()'.
'GeometryN()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_GeometryN()' instead.
'NumGeometries(GC)'
'ST_NumGeometries()' and 'NumGeometries()' are synonyms. For more information, see the description of 'ST_NumGeometries()'.
'NumGeometries()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_NumGeometries()' instead.
'ST_GeometryN(GC, N)'
Returns the N-th geometry in the 'GeometryCollection' value GC. Geometries are numbered beginning with 1. If any argument is 'NULL' or the geometry argument is an empty geometry, the return value is 'NULL'.
mysql> SET @gc = 'GeometryCollection(Point(1 1),LineString(2 2, 3 3))';
mysql> SELECT ST_AsText(ST_GeometryN(ST_GeomFromText(@gc),1));
+-------------------------------------------------+
| ST_AsText(ST_GeometryN(ST_GeomFromText(@gc),1)) |
+-------------------------------------------------+
| POINT(1 1) |
+-------------------------------------------------+
'ST_GeometryN()' and 'GeometryN()' are synonyms.
'ST_NumGeometries(GC)'
Returns the number of geometries in the 'GeometryCollection' value GC. If the argument is 'NULL' or an empty geometry, the return value is 'NULL'.
mysql> SET @gc = 'GeometryCollection(Point(1 1),LineString(2 2, 3 3))';
mysql> SELECT ST_NumGeometries(ST_GeomFromText(@gc));
+----------------------------------------+
| ST_NumGeometries(ST_GeomFromText(@gc)) |
+----------------------------------------+
| 2 |
+----------------------------------------+
'ST_NumGeometries()' and 'NumGeometries()' are synonyms.
File: manual.info.tmp, Node: spatial-operator-functions, Next: spatial-relation-functions, Prev: gis-property-functions, Up: spatial-analysis-functions
OpenGIS proposes a number of functions that can produce geometries. They are designed to implement spatial operators.
These functions support all argument type combinations except those that are inapplicable according to the Open Geospatial Consortium (http://www.opengeospatial.org) specification.
In addition, *note gis-property-functions::, discusses several functions that construct new geometries from existing ones. See that section for descriptions of these functions:
'ST_Envelope(G)'
'ST_StartPoint(LS)'
'ST_EndPoint(LS)'
'ST_PointN(LS, N)'
'ST_ExteriorRing(POLY)'
'ST_InteriorRingN(POLY, N)'
'ST_GeometryN(GC, N)'
These spatial operator functions are available:
'Buffer(G, D [, STRATEGY1 [, STRATEGY2 [, STRATEGY3]]])'
'ST_Buffer()' and 'Buffer()' are synonyms. For more information, see the description of 'ST_Buffer()'.
'Buffer()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Buffer()' instead.
'ConvexHull(G)'
'ST_ConvexHull()' and 'ConvexHull()' are synonyms. For more information, see the description of 'ST_ConvexHull()'.
'ConvexHull()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_ConvexHull()' instead.
'ST_Buffer(G, D [, STRATEGY1 [, STRATEGY2 [, STRATEGY3]]])'
Returns a geometry that represents all points whose distance from the geometry value G is less than or equal to a distance of D, or 'NULL' if any argument is 'NULL'. The SRID of the geometry argument must be 0 because 'ST_Buffer()' supports only the Cartesian coordinate system. If any geometry argument is not a syntactically well-formed geometry, an 'ER_GIS_INVALID_DATA' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.
If the geometry argument is empty, 'ST_Buffer()' returns an empty geometry.
If the distance is 0, 'ST_Buffer()' returns the geometry argument unchanged:
mysql> SET @pt = ST_GeomFromText('POINT(0 0)');
mysql> SELECT ST_AsText(ST_Buffer(@pt, 0));
+------------------------------+
| ST_AsText(ST_Buffer(@pt, 0)) |
+------------------------------+
| POINT(0 0) |
+------------------------------+
'ST_Buffer()' supports negative distances for 'Polygon' and 'MultiPolygon' values, and for geometry collections containing 'Polygon' or 'MultiPolygon' values. The result may be an empty geometry. An 'ER_WRONG_ARGUMENTS' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_arguments) error occurs for 'ST_Buffer()' with a negative distance for 'Point', 'MultiPoint', 'LineString', and 'MultiLineString' values, and for geometry collections not containing any 'Polygon' or 'MultiPolygon' values.
'ST_Buffer()' permits up to three optional strategy arguments following the distance argument. Strategies influence buffer computation. These arguments are byte string values produced by the 'ST_Buffer_Strategy()' function, to be used for point, join, and end strategies:
* Point strategies apply to 'Point' and 'MultiPoint' geometries.
If no point strategy is specified, the default is
'ST_Buffer_Strategy('point_circle', 32)'.
* Join strategies apply to 'LineString', 'MultiLineString',
'Polygon', and 'MultiPolygon' geometries. If no join strategy
is specified, the default is 'ST_Buffer_Strategy('join_round',
32)'.
* End strategies apply to 'LineString' and 'MultiLineString'
geometries. If no end strategy is specified, the default is
'ST_Buffer_Strategy('end_round', 32)'.
Up to one strategy of each type may be specified, and they may be given in any order. If multiple strategies of a given type are specified, an 'ER_WRONG_ARGUMENTS' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_arguments) error occurs.
mysql> SET @pt = ST_GeomFromText('POINT(0 0)');
mysql> SET @pt_strategy = ST_Buffer_Strategy('point_square');
mysql> SELECT ST_AsText(ST_Buffer(@pt, 2, @pt_strategy));
+--------------------------------------------+
| ST_AsText(ST_Buffer(@pt, 2, @pt_strategy)) |
+--------------------------------------------+
| POLYGON((-2 -2,2 -2,2 2,-2 2,-2 -2)) |
+--------------------------------------------+
mysql> SET @ls = ST_GeomFromText('LINESTRING(0 0,0 5,5 5)');
mysql> SET @end_strategy = ST_Buffer_Strategy('end_flat');
mysql> SET @join_strategy = ST_Buffer_Strategy('join_round', 10);
mysql> SELECT ST_AsText(ST_Buffer(@ls, 5, @end_strategy, @join_strategy))
+---------------------------------------------------------------+
| ST_AsText(ST_Buffer(@ls, 5, @end_strategy, @join_strategy)) |
+---------------------------------------------------------------+
| POLYGON((5 5,5 10,0 10,-3.5355339059327373 8.535533905932738, |
| -5 5,-5 0,0 0,5 0,5 5)) |
+---------------------------------------------------------------+
'ST_Buffer()' and 'Buffer()' are synonyms.
'ST_Buffer_Strategy(STRATEGY [, POINTS_PER_CIRCLE])'
This function returns a strategy byte string for use with 'ST_Buffer()' to influence buffer computation. If any argument is 'NULL', the return value is 'NULL'. If any argument is invalid, an 'ER_WRONG_ARGUMENTS' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_arguments) error occurs.
Information about strategies is available at Boost.org (http://www.boost.org).
The first argument must be a string indicating a strategy option:
* For point strategies, permitted values are ''point_circle''
and ''point_square''.
* For join strategies, permitted values are ''join_round'' and
''join_miter''.
* For end strategies, permitted values are ''end_round'' and
''end_flat''.
If the first argument is ''point_circle'', ''join_round'', ''join_miter'', or ''end_round'', the POINTS_PER_CIRCLE argument must be given as a positive numeric value. The maximum POINTS_PER_CIRCLE value is the value of the 'max_points_in_geometry' system variable. If the first argument is ''point_square'' or ''end_flat'', the POINTS_PER_CIRCLE argument must not be given or an 'ER_WRONG_ARGUMENTS' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_arguments) error occurs.
For examples, see the description of 'ST_Buffer()'.
'ST_ConvexHull(G)'
Returns a geometry that represents the convex hull of the geometry value G. If the argument is 'NULL', the return value is 'NULL'.
This function computes a geometry's convex hull by first checking whether its vertex points are colinear. The function returns a linear hull if so, a polygon hull otherwise. This function processes geometry collections by extracting all vertex points of all components of the collection, creating a 'MultiPoint' value from them, and computing its convex hull. If the argument is an empty geometry collection, the return value is 'NULL'.
mysql> SET @g = 'MULTIPOINT(5 0,25 0,15 10,15 25)';
mysql> SELECT ST_AsText(ST_ConvexHull(ST_GeomFromText(@g)));
+-----------------------------------------------+
| ST_AsText(ST_ConvexHull(ST_GeomFromText(@g))) |
+-----------------------------------------------+
| POLYGON((5 0,25 0,15 25,5 0)) |
+-----------------------------------------------+
'ST_ConvexHull()' and 'ConvexHull()' are synonyms.
'ST_Difference(G1, G2)'
Returns a geometry that represents the point set difference of the geometry values G1 and G2. If any argument is 'NULL', the return value is 'NULL'.
mysql> SET @g1 = Point(1,1), @g2 = Point(2,2);
mysql> SELECT ST_AsText(ST_Difference(@g1, @g2));
+------------------------------------+
| ST_AsText(ST_Difference(@g1, @g2)) |
+------------------------------------+
| POINT(1 1) |
+------------------------------------+
'ST_Intersection(G1, G2)'
Returns a geometry that represents the point set intersection of the geometry values G1 and G2. If any argument is 'NULL', the return value is 'NULL'.
mysql> SET @g1 = ST_GeomFromText('LineString(1 1, 3 3)');
mysql> SET @g2 = ST_GeomFromText('LineString(1 3, 3 1)');
mysql> SELECT ST_AsText(ST_Intersection(@g1, @g2));
+--------------------------------------+
| ST_AsText(ST_Intersection(@g1, @g2)) |
+--------------------------------------+
| POINT(2 2) |
+--------------------------------------+
'ST_SymDifference(G1, G2)'
Returns a geometry that represents the point set symmetric difference of the geometry values G1 and G2, which is defined as:
G1 symdifference G2 := (G1 union G2) difference (G1 intersection G2)
Or, in function call notation:
ST_SymDifference(G1, G2) = ST_Difference(ST_Union(G1, G2), ST_Intersection(G1, G2))
If any argument is 'NULL', the return value is 'NULL'.
mysql> SET @g1 = ST_GeomFromText('MULTIPOINT(5 0,15 10,15 25)');
mysql> SET @g2 = ST_GeomFromText('MULTIPOINT(1 1,15 10,15 25)');
mysql> SELECT ST_AsText(ST_SymDifference(@g1, @g2));
+---------------------------------------+
| ST_AsText(ST_SymDifference(@g1, @g2)) |
+---------------------------------------+
| MULTIPOINT((1 1),(5 0)) |
+---------------------------------------+
'ST_Union(G1, G2)'
Returns a geometry that represents the point set union of the geometry values G1 and G2. If any argument is 'NULL', the return value is 'NULL'.
mysql> SET @g1 = ST_GeomFromText('LineString(1 1, 3 3)');
mysql> SET @g2 = ST_GeomFromText('LineString(1 3, 3 1)');
mysql> SELECT ST_AsText(ST_Union(@g1, @g2));
+--------------------------------------+
| ST_AsText(ST_Union(@g1, @g2)) |
+--------------------------------------+
| MULTILINESTRING((1 1,3 3),(1 3,3 1)) |
+--------------------------------------+
File: manual.info.tmp, Node: spatial-relation-functions, Next: spatial-geohash-functions, Prev: spatial-operator-functions, Up: spatial-analysis-functions
Menu:
spatial-relation-functions-mbr:: Spatial Relation Functions That Use Minimum Bounding Rectangles
The functions described in this section take two geometries as arguments and return a qualitative or quantitative relation between them.
MySQL implements two sets of functions using function names defined by the OpenGIS specification. One set tests the relationship between two geometry values using precise object shapes, the other set uses object minimum bounding rectangles (MBRs).
There is also a MySQL-specific set of MBR-based functions available to test the relationship between two geometry values.
File: manual.info.tmp, Node: spatial-relation-functions-object-shapes, Next: spatial-relation-functions-mbr, Prev: spatial-relation-functions, Up: spatial-relation-functions
12.16.9.1 Spatial Relation Functions That Use Object Shapes ...........................................................
The OpenGIS specification defines the following functions to test the relationship between two geometry values G1 and G2, using precise object shapes. The return values 1 and 0 indicate true and false, respectively, except for 'ST_Distance()' and 'Distance()', which return distance values.
These functions support all argument type combinations except those that are inapplicable according to the Open Geospatial Consortium specification.
'Crosses(G1, G2)'
'ST_Crosses()' and 'Crosses()' are synonyms. For more information, see the description of 'ST_Crosses()'.
'Crosses()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Crosses()' instead.
'Distance(G1, G2)'
'ST_Distance()' and 'Distance()' are synonyms. For more information, see the description of 'ST_Distance()'.
'Distance()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Distance()' instead.
'ST_Contains(G1, G2)'
Returns 1 or 0 to indicate whether G1 completely contains G2. This tests the opposite relationship as 'ST_Within()'.
'ST_Crosses(G1, G2)'
The term spatially crosses denotes a spatial relation between two given geometries that has the following properties:
* The two geometries intersect.
* Their intersection results in a geometry that has a dimension
that is one less than the maximum dimension of the two given
geometries.
* Their intersection is not equal to either of the two given
geometries.
This function returns 1 or 0 to indicate whether G1 spatially crosses G2. If G1 is a 'Polygon' or a 'MultiPolygon', or if G2 is a 'Point' or a 'MultiPoint', the return value is 'NULL'.
This function returns 0 if called with an inapplicable geometry argument type combination. For example, it returns 0 if the first argument is a 'Polygon' or 'MultiPolygon' and/or the second argument is a 'Point' or 'MultiPoint'.
Returns 1 if G1 spatially crosses G2. Returns 'NULL' if G1 is a 'Polygon' or a 'MultiPolygon', or if G2 is a 'Point' or a 'MultiPoint'. Otherwise, returns 0.
This function returns 0 if called with an inapplicable geometry argument type combination. For example, it returns 0 if the first argument is a 'Polygon' or 'MultiPolygon' and/or the second argument is a 'Point' or 'MultiPoint'.
'ST_Crosses()' and 'Crosses()' are synonyms.
'ST_Disjoint(G1, G2)'
Returns 1 or 0 to indicate whether G1 is spatially disjoint from (does not intersect) G2.
'ST_Distance(G1, G2)'
Returns the distance between G1 and G2. If either argument is 'NULL' or an empty geometry, the return value is 'NULL'.
This function processes geometry collections by returning the shortest distance among all combinations of the components of the two geometry arguments.
If an intermediate or final result produces NaN or a negative number, an 'ER_GIS_INVALID_DATA' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.
mysql> SET @g1 = Point(1,1);
mysql> SET @g2 = Point(2,2);
mysql> SELECT ST_Distance(@g1, @g2);
+-----------------------+
| ST_Distance(@g1, @g2) |
+-----------------------+
| 1.4142135623730951 |
+-----------------------+
'ST_Distance()' and 'Distance()' are synonyms.
'ST_Equals(G1, G2)'
Returns 1 or 0 to indicate whether G1 is spatially equal to G2.
mysql> SET @g1 = Point(1,1), @g2 = Point(2,2);
mysql> SELECT ST_Equals(@g1, @g1), ST_Equals(@g1, @g2);
+---------------------+---------------------+
| ST_Equals(@g1, @g1) | ST_Equals(@g1, @g2) |
+---------------------+---------------------+
| 1 | 0 |
+---------------------+---------------------+
'ST_Intersects(G1, G2)'
Returns 1 or 0 to indicate whether G1 spatially intersects G2.
'ST_Overlaps(G1, G2)'
Two geometries spatially overlap if they intersect and their intersection results in a geometry of the same dimension but not equal to either of the given geometries.
This function returns 1 or 0 to indicate whether G1 spatially overlaps G2.
This function returns 0 if called with an inapplicable geometry argument type combination. For example, it returns 0 if called with geometries of different dimensions or any argument is a 'Point'.
'ST_Touches(G1, G2)'
Two geometries spatially touch if their interiors do not intersect, but the boundary of one of the geometries intersects either the boundary or the interior of the other.
This function returns 1 or 0 to indicate whether G1 spatially touches G2.
This function returns 0 if called with an inapplicable geometry argument type combination. For example, it returns 0 if either of the arguments is a 'Point' or 'MultiPoint'.
'ST_Touches()' and 'Touches()' are synonyms.
'ST_Within(G1, G2)'
Returns 1 or 0 to indicate whether G1 is spatially within G2. This tests the opposite relationship as 'ST_Contains()'.
'Touches(G1, G2)'
'ST_Touches()' and 'Touches()' are synonyms. For more information, see the description of 'ST_Touches()'.
'Touches()' is deprecated; expect it to be removed in a future MySQL release. Use 'ST_Touches()' instead.
File: manual.info.tmp, Node: spatial-relation-functions-mbr, Prev: spatial-relation-functions-object-shapes, Up: spatial-relation-functions
12.16.9.2 Spatial Relation Functions That Use Minimum Bounding Rectangles .........................................................................
MySQL provides several MySQL-specific functions that test the relationship between minimum bounding rectangles (MBRs) of two geometries G1 and G2. The return values 1 and 0 indicate true and false, respectively.
A corresponding set of MBR functions defined according to the OpenGIS specification is described later in this section.
'MBRContains(G1, G2)'
Returns 1 or 0 to indicate whether the minimum bounding rectangle of G1 contains the minimum bounding rectangle of G2. This tests the opposite relationship as 'MBRWithin()'.
mysql> SET @g1 = ST_GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');
mysql> SET @g2 = ST_GeomFromText('Point(1 1)');
mysql> SELECT MBRContains(@g1,@g2), MBRWithin(@g2,@g1);
+----------------------+--------------------+
| MBRContains(@g1,@g2) | MBRWithin(@g2,@g1) |
+----------------------+--------------------+
| 1 | 1 |
+----------------------+--------------------+
'MBRContains()' and 'Contains()' are synonyms.
'MBRCoveredBy(G1, G2)'
Returns 1 or 0 to indicate whether the minimum bounding rectangle of G1 is covered by the minimum bounding rectangle of G2. This tests the opposite relationship as 'MBRCovers()'.
'MBRCoveredBy()' handles its arguments as follows:
* If either argument is 'NULL' or an empty geometry, the return
value is 'NULL'.
* If either argument is not a syntactically well-formed geometry
byte string, an 'ER_GIS_INVALID_DATA'
(https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_invalid_data)
error occurs.
* Otherwise, the return value is non-'NULL'.
mysql> SET @g1 = ST_GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');
mysql> SET @g2 = ST_GeomFromText('Point(1 1)');
mysql> SELECT MBRCovers(@g1,@g2), MBRCoveredby(@g1,@g2);
+--------------------+-----------------------+
| MBRCovers(@g1,@g2) | MBRCoveredby(@g1,@g2) |
+--------------------+-----------------------+
| 1 | 0 |
+--------------------+-----------------------+
mysql> SELECT MBRCovers(@g2,@g1), MBRCoveredby(@g2,@g1);
+--------------------+-----------------------+
| MBRCovers(@g2,@g1) | MBRCoveredby(@g2,@g1) |
+--------------------+-----------------------+
| 0 | 1 |
+--------------------+-----------------------+
'MBRCovers(G1, G2)'
Returns 1 or 0 to indicate whether the minimum bounding rectangle of G1 covers the minimum bounding rectangle of G2. This tests the opposite relationship as 'MBRCoveredBy()'. See the description of 'MBRCoveredBy()' for examples.
'MBRCovers()' handles its arguments as follows:
* If either argument is 'NULL' or an empty geometry, the return
value is 'NULL'.
* If either argument is not a syntactically well-formed geometry
byte string, an 'ER_GIS_INVALID_DATA'
(https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_invalid_data)
error occurs.
* Otherwise, the return value is non-'NULL'.
'MBRDisjoint(G1, G2)'
Returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries G1 and G2 are disjoint (do not intersect).
'MBRDisjoint()' and 'Disjoint()' are synonyms.
'MBREqual(G1, G2)'
Returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries G1 and G2 are the same.
'MBREqual()' is deprecated; expect it to be removed in a future MySQL release. Use 'MBREquals()' instead.
'MBREquals(G1, G2)'
Returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries G1 and G2 are the same.
'MBREquals()', 'MBREqual()', and 'Equals()' are synonyms.
'MBRIntersects(G1, G2)'
Returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries G1 and G2 intersect.
'MBRIntersects()' and 'Intersects()' are synonyms.
'MBROverlaps(G1, G2)'
Two geometries spatially overlap if they intersect and their intersection results in a geometry of the same dimension but not equal to either of the given geometries.
This function returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries G1 and G2 overlap.
'MBROverlaps()' and 'Overlaps()' are synonyms.
'MBRTouches(G1, G2)'
Two geometries spatially touch if their interiors do not intersect, but the boundary of one of the geometries intersects either the boundary or the interior of the other.
This function returns 1 or 0 to indicate whether the minimum bounding rectangles of the two geometries G1 and G2 touch.
'MBRWithin(G1, G2)'
Returns 1 or 0 to indicate whether the minimum bounding rectangle of G1 is within the minimum bounding rectangle of G2. This tests the opposite relationship as 'MBRContains()'.
mysql> SET @g1 = ST_GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');
mysql> SET @g2 = ST_GeomFromText('Polygon((0 0,0 5,5 5,5 0,0 0))');
mysql> SELECT MBRWithin(@g1,@g2), MBRWithin(@g2,@g1);
+--------------------+--------------------+
| MBRWithin(@g1,@g2) | MBRWithin(@g2,@g1) |
+--------------------+--------------------+
| 1 | 0 |
+--------------------+--------------------+
'MBRWithin()' and 'Within()' are synonyms.
The OpenGIS specification defines the following functions that test the relationship between two geometry values G1 and G2. The MySQL implementation uses minimum bounding rectangles, so these functions return the same result as the corresponding MBR-based functions described earlier in this section. The return values 1 and 0 indicate true and false, respectively.
These functions support all argument type combinations except those that are inapplicable according to the Open Geospatial Consortium specification.
'Contains(G1, G2)'
'MBRContains()' and 'Contains()' are synonyms. For more information, see the description of 'MBRContains()'.
'Contains()' is deprecated; expect it to be removed in a future MySQL release. Use 'MBRContains()' instead.
'Disjoint(G1, G2)'
'MBRDisjoint()' and 'Disjoint()' are synonyms. For more information, see the description of 'MBRDisjoint()'.
'Disjoint()' is deprecated; expect it to be removed in a future MySQL release. Use 'MBRDisjoint()' instead.
'Equals(G1, G2)'
'MBREquals()' and 'Equals()' are synonyms. For more information, see the description of 'MBREquals()'.
'Equals()' is deprecated; expect it to be removed in a future MySQL release. Use 'MBREquals()' instead.
'Intersects(G1, G2)'
'MBRIntersects()' and 'Intersects()' are synonyms. For more information, see the description of 'MBRIntersects()'.
'Intersects()' is deprecated; expect it to be removed in a future MySQL release. Use 'MBRIntersects()' instead.
'Overlaps(G1, G2)'
'MBROverlaps()' and 'Overlaps()' are synonyms. For more information, see the description of 'MBROverlaps()'.
'Overlaps()' is deprecated; expect it to be removed in a future MySQL release. Use 'MBROverlaps()' instead.
'Within(G1, G2)'
'MBRWithin()' and 'Within()' are synonyms. For more information, see the description of 'MBRWithin()'.
'Within()' is deprecated; expect it to be removed in a future MySQL release. Use 'MBRWithin()' instead.
File: manual.info.tmp, Node: spatial-geohash-functions, Next: spatial-geojson-functions, Prev: spatial-relation-functions, Up: spatial-analysis-functions
Geohash is a system for encoding latitude and longitude coordinates of arbitrary precision into a text string. Geohash values are strings that contain only characters chosen from '"0123456789bcdefghjkmnpqrstuvwxyz"'.
The functions in this section enable manipulation of geohash values, which provides applications the capabilities of importing and exporting geohash data, and of indexing and searching geohash values.
'ST_GeoHash(LONGITUDE, LATITUDE, MAX_LENGTH)', 'ST_GeoHash(POINT, MAX_LENGTH)'
Returns a geohash string in the connection character set and collation.
If any argument is 'NULL', the return value is 'NULL'. If any argument is invalid, an error occurs.
For the first syntax, the LONGITUDE must be a number in the range [−180, 180], and the LATITUDE must be a number in the range [−90, 90]. For the second syntax, a 'POINT' value is required, where the X and Y coordinates are in the valid ranges for longitude and latitude, respectively.
The resulting string is no longer than MAX_LENGTH characters, which has an upper limit of 100. The string might be shorter than MAX_LENGTH characters because the algorithm that creates the geohash value continues until it has created a string that is either an exact representation of the location or MAX_LENGTH characters, whichever comes first.
mysql> SELECT ST_GeoHash(180,0,10), ST_GeoHash(-180,-90,15);
+----------------------+-------------------------+
| ST_GeoHash(180,0,10) | ST_GeoHash(-180,-90,15) |
+----------------------+-------------------------+
| xbpbpbpbpb | 000000000000000 |
+----------------------+-------------------------+
'ST_LatFromGeoHash(GEOHASH_STR)'
Returns the latitude from a geohash string value, as a *note 'DOUBLE': floating-point-types. value in the range [−90, 90].
If the argument is 'NULL', the return value is 'NULL'. If the argument is invalid, an error occurs.
The 'ST_LatFromGeoHash()' decoding function reads no more than 433 characters from the GEOHASH_STR argument. That represents the upper limit on information in the internal representation of coordinate values. Characters past the 433rd are ignored, even if they are otherwise illegal and produce an error.
mysql> SELECT ST_LatFromGeoHash(ST_GeoHash(45,-20,10));
+------------------------------------------+
| ST_LatFromGeoHash(ST_GeoHash(45,-20,10)) |
+------------------------------------------+
| -20 |
+------------------------------------------+
'ST_LongFromGeoHash(GEOHASH_STR)'
Returns the longitude from a geohash string value, as a *note 'DOUBLE': floating-point-types. value in the range [−180, 180].
If the argument is 'NULL', the return value is 'NULL'. If the argument is invalid, an error occurs.
The remarks in the description of 'ST_LatFromGeoHash()' regarding the maximum number of characters processed from the GEOHASH_STR argument also apply to 'ST_LongFromGeoHash()'.
mysql> SELECT ST_LongFromGeoHash(ST_GeoHash(45,-20,10));
+-------------------------------------------+
| ST_LongFromGeoHash(ST_GeoHash(45,-20,10)) |
+-------------------------------------------+
| 45 |
+-------------------------------------------+
'ST_PointFromGeoHash(GEOHASH_STR, SRID)'
Returns a 'POINT' value containing the decoded geohash value, given a geohash string value.
The X and Y coordinates of the point are the longitude in the range [−180, 180] and the latitude in the range [−90, 90], respectively.
If any argument is 'NULL', the return value is 'NULL'. If any argument is invalid, an error occurs.
The SRID argument is an unsigned 32-bit integer.
The remarks in the description of 'ST_LatFromGeoHash()' regarding the maximum number of characters processed from the GEOHASH_STR argument also apply to 'ST_PointFromGeoHash()'.
mysql> SET @gh = ST_GeoHash(45,-20,10);
mysql> SELECT ST_AsText(ST_PointFromGeoHash(@gh,0));
+---------------------------------------+
| ST_AsText(ST_PointFromGeoHash(@gh,0)) |
+---------------------------------------+
| POINT(45 -20) |
+---------------------------------------+
File: manual.info.tmp, Node: spatial-geojson-functions, Next: spatial-convenience-functions, Prev: spatial-geohash-functions, Up: spatial-analysis-functions
This section describes functions for converting between GeoJSON documents and spatial values. GeoJSON is an open standard for encoding geometric/geographical features. For more information, see http://geojson.org. The functions discussed here follow GeoJSON specification revision 1.0.
GeoJSON supports the same geometric/geographic data types that MySQL supports. Feature and FeatureCollection objects are not supported, except that geometry objects are extracted from them. CRS support is limited to values that identify an SRID.
MySQL also supports a native note 'JSON': json. data type and a set of SQL functions to enable operations on JSON values. For more information, see note json::, and *note json-functions::.
'ST_AsGeoJSON(G [, MAX_DEC_DIGITS [, OPTIONS]])'
Generates a GeoJSON object from the geometry G. The object string has the connection character set and collation.
If any argument is 'NULL', the return value is 'NULL'. If any non-'NULL' argument is invalid, an error occurs.
MAX_DEC_DIGITS, if specified, limits the number of decimal digits for coordinates and causes rounding of output. If not specified, this argument defaults to its maximum value of 2^32 − 1. The minimum is 0.
OPTIONS, if specified, is a bitmask. The following table shows the permitted flag values. If the geometry argument has an SRID of 0, no CRS object is produced even for those flag values that request one.
Flag Meaning Value
0 No options. This is the default if OPTIONS is not specified.
1 Add a bounding box to the output.
2 Add a short-format CRS URN to the output. The default format is a short format ('EPSG:SRID').
4 Add a long-format CRS URN ('urn:ogc:def:crs:EPSG::SRID'). This flag overrides flag 2. For example, option values of 5 and 7 mean the same (add a bounding box and a long-format CRS URN).
mysql> SELECT ST_AsGeoJSON(ST_GeomFromText('POINT(11.11111 12.22222)'),2);
+-------------------------------------------------------------+
| ST_AsGeoJSON(ST_GeomFromText('POINT(11.11111 12.22222)'),2) |
+-------------------------------------------------------------+
| {"type": "Point", "coordinates": [11.11, 12.22]} |
+-------------------------------------------------------------+
'ST_GeomFromGeoJSON(STR [, OPTIONS [, SRID]])'
Parses a string STR representing a GeoJSON object and returns a geometry.
If any argument is 'NULL', the return value is 'NULL'. If any non-'NULL' argument is invalid, an error occurs.
OPTIONS, if given, describes how to handle GeoJSON documents that contain geometries with coordinate dimensions higher than 2. The following table shows the permitted OPTIONS values.
Option Meaning Value
1 Reject the document and produce an error. This is the default if OPTIONS is not specified.
2, 3, Accept the document and strip off the coordinates for higher 4 coordinate dimensions.
OPTIONS values of 2, 3, and 4 currently produce the same effect. If geometries with coordinate dimensions higher than 2 are supported in the future, these values can be expected to produce different effects.
The SRID argument, if given, must be a 32-bit unsigned integer. If not given, the geometry return value has an SRID of 4326.
GeoJSON geometry, feature, and feature collection objects may have a 'crs' property. The parsing function parses named CRS URNs in the 'urn:ogc:def:crs:EPSG::SRID' and 'EPSG:SRID' namespaces, but not CRSs given as link objects. Also, 'urn:ogc:def:crs:OGC:1.3:CRS84' is recognized as SRID 4326. If an object has a CRS that is not understood, an error occurs, with the exception that if the optional SRID argument is given, any CRS is ignored even if it is invalid.
As specified in the GeoJSON specification, parsing is case-sensitive for the 'type' member of the GeoJSON input ('Point', 'LineString', and so forth). The specification is silent regarding case sensitivity for other parsing, which in MySQL is not case-sensitive.
This example shows the parsing result for a simple GeoJSON object:
mysql> SET @json = '{ "type": "Point", "coordinates": [102.0, 0.0]}';
mysql> SELECT ST_AsText(ST_GeomFromGeoJSON(@json));
+--------------------------------------+
| ST_AsText(ST_GeomFromGeoJSON(@json)) |
+--------------------------------------+
| POINT(102 0) |
+--------------------------------------+
File: manual.info.tmp, Node: spatial-convenience-functions, Prev: spatial-geojson-functions, Up: spatial-analysis-functions
The functions in this section provide convenience operations on geometry values.
'ST_Distance_Sphere(G1, G2 [, RADIUS])'
Returns the mimimum spherical distance between two points and/or multipoints on a sphere, in meters, or 'NULL' if any geometry argument is 'NULL' or empty.
Calculations use a spherical earth and a configurable radius. The optional RADIUS argument should be given in meters. If omitted, the default radius is 6,370,986 meters. An 'ER_WRONG_ARGUMENTS' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_arguments) error occurs if the RADIUS argument is present but not positive.
The geometry arguments should consist of points that specify (longitude, latitude) coordinate values:
* Longitude and latitude are the first and second coordinates of
the point, respectively.
* Both coordinates are in degrees.
* Longitude values must be in the range (-180, 180]. Positive
values are east of the prime meridian.
* Latitude values must be in the range [-90, 90]. Positive
values are north of the equator.
Supported argument combinations are ('Point', 'Point'), ('Point', 'MultiPoint'), and ('MultiPoint', 'Point'). An 'ER_GIS_UNSUPPORTED_ARGUMENT' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_unsupported_argument) error occurs for other combinations.
If any geometry argument is not a syntactically well-formed geometry byte string, an 'ER_GIS_INVALID_DATA' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.
mysql> SET @pt1 = ST_GeomFromText('POINT(0 0)');
mysql> SET @pt2 = ST_GeomFromText('POINT(180 0)');
mysql> SELECT ST_Distance_Sphere(@pt1, @pt2);
+--------------------------------+
| ST_Distance_Sphere(@pt1, @pt2) |
+--------------------------------+
| 20015042.813723423 |
+--------------------------------+
'ST_IsValid(G)'
Returns 1 if the argument is syntactically well-formed and is geometrically valid, 0 if the argument is not syntactically well-formed or is not geometrically valid. If the argument is 'NULL', the return value is 'NULL'. Geometry validity is defined by the OGC specification.
The only valid empty geometry is represented in the form of an empty geometry collection value. 'ST_IsValid()' returns 1 in this case.
'ST_IsValid()' works only for the Cartesian coordinate system and requires a geometry argument with an SRID of 0. An 'ER_WRONG_ARGUMENTS' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_arguments) error occurs otherwise.
mysql> SET @ls1 = ST_GeomFromText('LINESTRING(0 0,-0.00 0,0.0 0)');
mysql> SET @ls2 = ST_GeomFromText('LINESTRING(0 0, 1 1)');
mysql> SELECT ST_IsValid(@ls1);
+------------------+
| ST_IsValid(@ls1) |
+------------------+
| 0 |
+------------------+
mysql> SELECT ST_IsValid(@ls2);
+------------------+
| ST_IsValid(@ls2) |
+------------------+
| 1 |
+------------------+
'ST_MakeEnvelope(PT1, PT2)'
Returns the rectangle that forms the envelope around two points, as a 'Point', 'LineString', or 'Polygon'. If any argument is 'NULL', the return value is 'NULL'.
Calculations are done using the Cartesian coordinate system rather than on a sphere, spheroid, or on earth.
Given two points PT1 and PT2, 'ST_MakeEnvelope()' creates the result geometry on an abstract plane like this:
* If PT1 and PT2 are equal, the result is the point PT1.
* Otherwise, if '(PT1, PT2)' is a vertical or horizontal line
segment, the result is the line segment '(PT1, PT2)'.
* Otherwise, the result is a polygon using PT1 and PT2 as
diagonal points.
The result geometry has an SRID of 0.
'ST_MakeEnvelope()' requires 'Point' geometry arguments with an SRID of 0. An 'ER_WRONG_ARGUMENTS' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_arguments) error occurs otherwise.
If any argument is not a syntactically well-formed geometry byte string, or if any coordinate value of the two points is infinite or 'NaN', an 'ER_GIS_INVALID_DATA' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.
mysql> SET @pt1 = ST_GeomFromText('POINT(0 0)');
mysql> SET @pt2 = ST_GeomFromText('POINT(1 1)');
mysql> SELECT ST_AsText(ST_MakeEnvelope(@pt1, @pt2));
+----------------------------------------+
| ST_AsText(ST_MakeEnvelope(@pt1, @pt2)) |
+----------------------------------------+
| POLYGON((0 0,1 0,1 1,0 1,0 0)) |
+----------------------------------------+
'ST_Simplify(G, MAX_DISTANCE)'
Simplifies a geometry using the Douglas-Peucker algorithm and returns a simplified value of the same type. If any argument is 'NULL', the return value is 'NULL'.
The geometry may be any geometry type, although the Douglas-Peucker algorithm may not actually process every type. A geometry collection is processed by giving its components one by one to the simplification algorithm, and the returned geometries are put into a geometry collection as result.
The MAX_DISTANCE argument is the distance (in units of the input coordinates) of a vertex to other segments to be removed. Vertices within this distance of the simplified linestring are removed. If the MAX_DISTANCE argument is not positive, or is 'NaN', an 'ER_WRONG_ARGUMENTS' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_arguments) error occurs.
According to Boost.Geometry, geometries might become invalid as a result of the simplification process, and the process might create self-intersections. To check the validity of the result, pass it to 'ST_IsValid()'.
If the geometry argument is not a syntactically well-formed geometry byte string, an 'ER_GIS_INVALID_DATA' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.
mysql> SET @g = ST_GeomFromText('LINESTRING(0 0,0 1,1 1,1 2,2 2,2 3,3 3)');
mysql> SELECT ST_AsText(ST_Simplify(@g, 0.5));
+---------------------------------+
| ST_AsText(ST_Simplify(@g, 0.5)) |
+---------------------------------+
| LINESTRING(0 0,0 1,1 1,2 3,3 3) |
+---------------------------------+
mysql> SELECT ST_AsText(ST_Simplify(@g, 1.0));
+---------------------------------+
| ST_AsText(ST_Simplify(@g, 1.0)) |
+---------------------------------+
| LINESTRING(0 0,3 3) |
+---------------------------------+
'ST_Validate(G)'
Validates a geometry according to the OGC specification. A geometry can be syntactically well-formed (WKB value plus SRID) but geometrically invalid. For example, this polygon is geometrically invalid: 'POLYGON((0 0, 0 0, 0 0, 0 0, 0 0))'
'ST_Validate()' returns the geometry if it is syntactically well-formed and is geometrically valid, 'NULL' if the argument is not syntactically well-formed or is not geometrically valid or is 'NULL'.
'ST_Validate()' can be used to filter out invalid geometry data, although at a cost. For applications that require more precise results not tainted by invalid data, this penalty may be worthwhile.
If the geometry argument is valid, it is returned as is, except that if an input 'Polygon' or 'MultiPolygon' has clockwise rings, those rings are reversed before checking for validity. If the geometry is valid, the value with the reversed rings is returned.
The only valid empty geometry is represented in the form of an empty geometry collection value. 'ST_Validate()' returns it directly without further checks in this case.
'ST_Validate()' works only for the Cartesian coordinate system and requires a geometry argument with an SRID of 0. An 'ER_WRONG_ARGUMENTS' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_arguments) error occurs otherwise.
mysql> SET @ls1 = ST_GeomFromText('LINESTRING(0 0)');
mysql> SET @ls2 = ST_GeomFromText('LINESTRING(0 0, 1 1)');
mysql> SELECT ST_AsText(ST_Validate(@ls1));
+------------------------------+
| ST_AsText(ST_Validate(@ls1)) |
+------------------------------+
| NULL |
+------------------------------+
mysql> SELECT ST_AsText(ST_Validate(@ls2));
+------------------------------+
| ST_AsText(ST_Validate(@ls2)) |
+------------------------------+
| LINESTRING(0 0,1 1) |
+------------------------------+
File: manual.info.tmp, Node: json-functions, Next: gtid-functions, Prev: spatial-analysis-functions, Up: functions