12.16 Spatial Analysis 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:

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

12.16.1 Spatial Function Reference

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

12.16.2 Argument Handling by Spatial 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

12.16.3 Functions That Create Geometry Values from WKT Values

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

 File: manual.info.tmp, Node: gis-wkb-functions, Next: gis-mysql-specific-functions, Prev: gis-wkt-functions, Up: spatial-analysis-functions

12.16.4 Functions That Create Geometry Values from WKB Values

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

 File: manual.info.tmp, Node: gis-mysql-specific-functions, Next: gis-format-conversion-functions, Prev: gis-wkb-functions, Up: spatial-analysis-functions

12.16.5 MySQL-Specific Functions That Create Geometry Values

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));

 File: manual.info.tmp, Node: gis-format-conversion-functions, Next: gis-property-functions, Prev: gis-mysql-specific-functions, Up: spatial-analysis-functions

12.16.6 Geometry Format Conversion 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::.

 File: manual.info.tmp, Node: gis-property-functions, Next: spatial-operator-functions, Prev: gis-format-conversion-functions, Up: spatial-analysis-functions

12.16.7 Geometry 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.

 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:

 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.

 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.

 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.

 File: manual.info.tmp, Node: spatial-operator-functions, Next: spatial-relation-functions, Prev: gis-property-functions, Up: spatial-analysis-functions

12.16.8 Spatial Operator 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:

These spatial operator functions are available:

 File: manual.info.tmp, Node: spatial-relation-functions, Next: spatial-geohash-functions, Prev: spatial-operator-functions, Up: spatial-analysis-functions

12.16.9 Functions That Test Spatial Relations Between Geometry Objects

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.

 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.

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.

 File: manual.info.tmp, Node: spatial-geohash-functions, Next: spatial-geojson-functions, Prev: spatial-relation-functions, Up: spatial-analysis-functions

12.16.10 Spatial Geohash 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.

 File: manual.info.tmp, Node: spatial-geojson-functions, Next: spatial-convenience-functions, Prev: spatial-geohash-functions, Up: spatial-analysis-functions

12.16.11 Spatial GeoJSON 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::.

 File: manual.info.tmp, Node: spatial-convenience-functions, Prev: spatial-geojson-functions, Up: spatial-analysis-functions

12.16.12 Spatial Convenience Functions

The functions in this section provide convenience operations on geometry values.

 File: manual.info.tmp, Node: json-functions, Next: gtid-functions, Prev: spatial-analysis-functions, Up: functions