#### 12.17.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 that distance functions return distance values.

 Functions in this section detect arguments in either Cartesian or geographic spatial reference systems (SRSs), and return results appropriate to the SRS.

 Unless otherwise specified, functions in this section handle their geometry arguments as follows:

* If any argument is `NULL` or any geometry argument is an empty geometry, the return value is `NULL`.

* If any geometry argument is not a syntactically well-formed geometry, an [`ER_GIS_INVALID_DATA`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.

* If any geometry argument is a syntactically well-formed geometry in an undefined spatial reference system (SRS), an [`ER_SRS_NOT_FOUND`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_srs_not_found) error occurs.

* For functions that take multiple geometry arguments, if those arguments are not in the same SRS, an [`ER_GIS_DIFFERENT_SRIDS`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_gis_different_srids) error occurs.

* If any geometry argument is geometrically invalid, either the result is true or false (it is undefined which), or an error occurs.

* For geographic SRS geometry arguments, if any argument has a longitude or latitude that is out of range, an error occurs:

  * If a longitude value is not in the range (−180, 180], an [`ER_GEOMETRY_PARAM_LONGITUDE_OUT_OF_RANGE`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_geometry_param_longitude_out_of_range) error occurs ([`ER_LONGITUDE_OUT_OF_RANGE`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_longitude_out_of_range) prior to MySQL 8.0.12).

  * If a latitude value is not in the range [−90, 90], an [`ER_GEOMETRY_PARAM_LATITUDE_OUT_OF_RANGE`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_geometry_param_latitude_out_of_range) error occurs ([`ER_LATITUDE_OUT_OF_RANGE`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_latitude_out_of_range) prior to MySQL 8.0.12).

   Ranges shown are in degrees. If an SRS uses another unit, the range uses the corresponding values in its unit. The exact range limits deviate slightly due to floating-point arithmetic.

* Otherwise, the return value is non-`NULL`.

 Some functions in this section permit a unit argument that specifies the length unit for the return value. Unless otherwise specified, functions handle their unit argument as follows:

* A unit is supported if it is found in the `INFORMATION_SCHEMA` [`ST_UNITS_OF_MEASURE`](information-schema-st-units-of-measure-table.html) table. See [Section 26.3.37, “The INFORMATION\_SCHEMA ST\_UNITS\_OF\_MEASURE Table”](information-schema-st-units-of-measure-table.html).

* If a unit is specified but not supported by MySQL, an [`ER_UNIT_NOT_FOUND`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_unit_not_found) error occurs.

* If a supported linear unit is specified and the SRID is 0, an [`ER_GEOMETRY_IN_UNKNOWN_LENGTH_UNIT`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_geometry_in_unknown_length_unit) error occurs.

* If a supported linear unit is specified and the SRID is not 0, the result is in that unit.

* If a unit is not specified, the result is in the unit of the SRS of the geometries, whether Cartesian or geographic. Currently, all MySQL SRSs are expressed in meters.

 These object-shape functions are available for testing geometry relationships:

* []()

  [`ST_Contains(*`g1`*, *`g2`*)`](spatial-relation-functions-object-shapes.html#function_st-contains)

  []()

   Returns 1 or 0 to indicate whether *`g1`* completely contains *`g2`*. This tests the opposite relationship as [`ST_Within()`](spatial-relation-functions-object-shapes.html#function_st-within).

  [`ST_Contains()`](spatial-relation-functions-object-shapes.html#function_st-contains) handles its arguments as described in the introduction to this section.

* []()

  [`ST_Crosses(*`g1`*, *`g2`*)`](spatial-relation-functions-object-shapes.html#function_st-crosses)

  []()

   Two geometries *spatially cross* if their spatial relation has the following properties:

  * Unless *`g1`* and *`g2`* are both of dimension 1: *`g1`* crosses *`g2`* if the interior of *`g2`* has points in common with the interior of *`g1`*, but *`g2`* does not cover the entire interior of *`g1`*.

  * If both *`g1`* and *`g2`* are of dimension 1: If the lines cross each other in a finite number of points (that is, no common line segments, only single points in common).

   This function returns 1 or 0 to indicate whether *`g1`* spatially crosses *`g2`*.

  [`ST_Crosses()`](spatial-relation-functions-object-shapes.html#function_st-crosses) handles its arguments as described in the introduction to this section except that the return value is `NULL` for these additional conditions:

  * *`g1`* is of dimension 2 (`Polygon` or `MultiPolygon`).

  * *`g2`* is of dimension 1 (`Point` or `MultiPoint`).

* []()

  [`ST_Disjoint(*`g1`*, *`g2`*)`](spatial-relation-functions-object-shapes.html#function_st-disjoint)

  []()

   Returns 1 or 0 to indicate whether *`g1`* is spatially disjoint from (does not intersect) *`g2`*.

  [`ST_Disjoint()`](spatial-relation-functions-object-shapes.html#function_st-disjoint) handles its arguments as described in the introduction to this section.

* []()

  [`ST_Distance(*`g1`*, *`g2`* [, *`unit`*])`](spatial-relation-functions-object-shapes.html#function_st-distance)

  []()

   Returns the distance between *`g1`* and *`g2`*, measured in the length unit of the spatial reference system (SRS) of the geometry arguments, or in the unit of the optional *`unit`* argument if that is specified.

   This function processes geometry collections by returning the shortest distance among all combinations of the components of the two geometry arguments.

  [`ST_Distance()`](spatial-relation-functions-object-shapes.html#function_st-distance) handles its geometry arguments as described in the introduction to this section, with these exceptions:

  * [`ST_Distance()`](spatial-relation-functions-object-shapes.html#function_st-distance) detects arguments in a geographic (ellipsoidal) spatial reference system and returns the geodetic distance on the ellipsoid. As of MySQL 8.0.18, [`ST_Distance()`](spatial-relation-functions-object-shapes.html#function_st-distance) supports distance calculations for geographic SRS arguments of all geometry types. Prior to MySQL 8.0.18, the only permitted geographic argument types are `Point` and `Point`, or `Point` and `MultiPoint` (in any argument order). If called with other geometry type argument combinations in a geographic SRS, an [`ER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_not_implemented_for_geographic_srs) error occurs.

  * If any argument is geometrically invalid, either the result is an undefined distance (that is, it can be any number), or an error occurs.

  * If an intermediate or final result produces `NaN` or a negative number, an [`ER_GIS_INVALID_DATA`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_gis_invalid_data) error occurs.

   As of MySQL 8.0.14, [`ST_Distance()`](spatial-relation-functions-object-shapes.html#function_st-distance) permits an optional *`unit`* argument that specifies the linear unit for the returned distance value. [`ST_Distance()`](spatial-relation-functions-object-shapes.html#function_st-distance) handles its *`unit`* argument as described in the introduction to this section.

  ```
  mysql> SET @g1 = ST_GeomFromText('POINT(1 1)');
  mysql> SET @g2 = ST_GeomFromText('POINT(2 2)');
  mysql> SELECT ST_Distance(@g1, @g2);
  +-----------------------+
  | ST_Distance(@g1, @g2) |
  +-----------------------+
  |    1.4142135623730951 |
  +-----------------------+

  mysql> SET @g1 = ST_GeomFromText('POINT(1 1)', 4326);
  mysql> SET @g2 = ST_GeomFromText('POINT(2 2)', 4326);
  mysql> SELECT ST_Distance(@g1, @g2);
  +-----------------------+
  | ST_Distance(@g1, @g2) |
  +-----------------------+
  |     156874.3859490455 |
  +-----------------------+
  mysql> SELECT ST_Distance(@g1, @g2, 'metre');
  +--------------------------------+
  | ST_Distance(@g1, @g2, 'metre') |
  +--------------------------------+
  |              156874.3859490455 |
  +--------------------------------+
  mysql> SELECT ST_Distance(@g1, @g2, 'foot');
  +-------------------------------+
  | ST_Distance(@g1, @g2, 'foot') |
  +-------------------------------+
  |             514679.7439273146 |
  +-------------------------------+
  ```

   For the special case of distance calculations on a sphere, see the [`ST_Distance_Sphere()`](spatial-convenience-functions.html#function_st-distance-sphere) function.

* []()

  [`ST_Equals(*`g1`*, *`g2`*)`](spatial-relation-functions-object-shapes.html#function_st-equals)

  []()

   Returns 1 or 0 to indicate whether *`g1`* is spatially equal to *`g2`*.

  [`ST_Equals()`](spatial-relation-functions-object-shapes.html#function_st-equals) handles its arguments as described in the introduction to this section, except that it does not return `NULL` for empty geometry arguments.

  ```
  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_FrechetDistance(*`g1`*, *`g2`* [, *`unit`*])`](spatial-relation-functions-object-shapes.html#function_st-frechetdistance)

  []()

   Returns the discrete Fréchet distance between two geometries, reflecting how similar the geometries are. The result is a double-precision number measured in the length unit of the spatial reference system (SRS) of the geometry arguments, or in the length unit of the *`unit`* argument if that argument is given.

   This function implements the discrete Fréchet distance, which means it is restricted to distances between the points of the geometries. For example, given two `LineString` arguments, only the points explicitly mentioned in the geometries are considered. Points on the line segments between these points are not considered.

  [`ST_FrechetDistance()`](spatial-relation-functions-object-shapes.html#function_st-frechetdistance) handles its geometry arguments as described in the introduction to this section, with these exceptions:

  * The geometries may have a Cartesian or geographic SRS, but only `LineString` values are supported. If the arguments are in the same Cartesian or geographic SRS, but either is not a `LineString`, an [`ER_NOT_IMPLEMENTED_FOR_CARTESIAN_SRS`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_not_implemented_for_cartesian_srs) or [`ER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_not_implemented_for_geographic_srs) error occurs, depending on the SRS type.

  [`ST_FrechetDistance()`](spatial-relation-functions-object-shapes.html#function_st-frechetdistance) handles its optional *`unit`* argument as described in the introduction to this section.

  ```
  mysql> SET @ls1 = ST_GeomFromText('LINESTRING(0 0,0 5,5 5)');
  mysql> SET @ls2 = ST_GeomFromText('LINESTRING(0 1,0 6,3 3,5 6)');
  mysql> SELECT ST_FrechetDistance(@ls1, @ls2);
  +--------------------------------+
  | ST_FrechetDistance(@ls1, @ls2) |
  +--------------------------------+
  |             2.8284271247461903 |
  +--------------------------------+

  mysql> SET @ls1 = ST_GeomFromText('LINESTRING(0 0,0 5,5 5)', 4326);
  mysql> SET @ls2 = ST_GeomFromText('LINESTRING(0 1,0 6,3 3,5 6)', 4326);
  mysql> SELECT ST_FrechetDistance(@ls1, @ls2);
  +--------------------------------+
  | ST_FrechetDistance(@ls1, @ls2) |
  +--------------------------------+
  |              313421.1999416798 |
  +--------------------------------+
  mysql> SELECT ST_FrechetDistance(@ls1, @ls2, 'foot');
  +----------------------------------------+
  | ST_FrechetDistance(@ls1, @ls2, 'foot') |
  +----------------------------------------+
  |                     1028284.7767115477 |
  +----------------------------------------+
  ```

   This function was added in MySQL 8.0.23.

* []()

  [`ST_HausdorffDistance(*`g1`*, *`g2`* [, *`unit`*])`](spatial-relation-functions-object-shapes.html#function_st-hausdorffdistance)

  []()

   Returns the discrete Hausdorff distance between two geometries, reflecting how similar the geometries are. The result is a double-precision number measured in the length unit of the spatial reference system (SRS) of the geometry arguments, or in the length unit of the *`unit`* argument if that argument is given.

   This function implements the discrete Hausdorff distance, which means it is restricted to distances between the points of the geometries. For example, given two `LineString` arguments, only the points explicitly mentioned in the geometries are considered. Points on the line segments between these points are not considered.

  [`ST_HausdorffDistance()`](spatial-relation-functions-object-shapes.html#function_st-hausdorffdistance) handles its geometry arguments as described in the introduction to this section, with these exceptions:

  * If the geometry arguments are in the same Cartesian or geographic SRS, but are not in a supported combination, an [`ER_NOT_IMPLEMENTED_FOR_CARTESIAN_SRS`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_not_implemented_for_cartesian_srs) or [`ER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS`](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_not_implemented_for_geographic_srs) error occurs, depending on the SRS type. These combinations are supported:

    * `LineString` and `LineString`

    * `Point` and `MultiPoint`

    * `LineString` and `MultiLineString`

    * `MultiPoint` and `MultiPoint`

    * `MultiLineString` and `MultiLineString`

  [`ST_HausdorffDistance()`](spatial-relation-functions-object-shapes.html#function_st-hausdorffdistance) handles its optional *`unit`* argument as described in the introduction to this section.

  ```
  mysql> SET @ls1 = ST_GeomFromText('LINESTRING(0 0,0 5,5 5)');
  mysql> SET @ls2 = ST_GeomFromText('LINESTRING(0 1,0 6,3 3,5 6)');
  mysql> SELECT ST_HausdorffDistance(@ls1, @ls2);
  +----------------------------------+
  | ST_HausdorffDistance(@ls1, @ls2) |
  +----------------------------------+
  |                                1 |
  +----------------------------------+

  mysql> SET @ls1 = ST_GeomFromText('LINESTRING(0 0,0 5,5 5)', 4326);
  mysql> SET @ls2 = ST_GeomFromText('LINESTRING(0 1,0 6,3 3,5 6)', 4326);
  mysql> SELECT ST_HausdorffDistance(@ls1, @ls2);
  +----------------------------------+
  | ST_HausdorffDistance(@ls1, @ls2) |
  +----------------------------------+
  |               111319.49079326246 |
  +----------------------------------+
  mysql> SELECT ST_HausdorffDistance(@ls1, @ls2, 'foot');
  +------------------------------------------+
  | ST_HausdorffDistance(@ls1, @ls2, 'foot') |
  +------------------------------------------+
  |                        365221.4264870815 |
  +------------------------------------------+
  ```

   This function was added in MySQL 8.0.23.

* []()

  [`ST_Intersects(*`g1`*, *`g2`*)`](spatial-relation-functions-object-shapes.html#function_st-intersects)

  []()

   Returns 1 or 0 to indicate whether *`g1`* spatially intersects *`g2`*.

  [`ST_Intersects()`](spatial-relation-functions-object-shapes.html#function_st-intersects) handles its arguments as described in the introduction to this section.

* []()

  [`ST_Overlaps(*`g1`*, *`g2`*)`](spatial-relation-functions-object-shapes.html#function_st-overlaps)

  []()

   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`*.

  [`ST_Overlaps()`](spatial-relation-functions-object-shapes.html#function_st-overlaps) handles its arguments as described in the introduction to this section except that the return value is `NULL` for the additional condition that the dimensions of the two geometries are not equal.

* []()

  [`ST_Touches(*`g1`*, *`g2`*)`](spatial-relation-functions-object-shapes.html#function_st-touches)

  []()

   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`*.

  [`ST_Touches()`](spatial-relation-functions-object-shapes.html#function_st-touches) handles its arguments as described in the introduction to this section except that the return value is `NULL` for the additional condition that both geometries are of dimension 0 (`Point` or `MultiPoint`).

* []()

  [`ST_Within(*`g1`*, *`g2`*)`](spatial-relation-functions-object-shapes.html#function_st-within)

  []()

   Returns 1 or 0 to indicate whether *`g1`* is spatially within *`g2`*. This tests the opposite relationship as [`ST_Contains()`](spatial-relation-functions-object-shapes.html#function_st-contains).

  [`ST_Within()`](spatial-relation-functions-object-shapes.html#function_st-within) handles its arguments as described in the introduction to this section.

