Documentation Home
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 33.8Mb
PDF (A4) - 33.8Mb
PDF (RPM) - 31.8Mb
HTML Download (TGZ) - 8.1Mb
HTML Download (Zip) - 8.2Mb
HTML Download (RPM) - 7.0Mb
Man Pages (TGZ) - 145.9Kb
Man Pages (Zip) - 206.8Kb
Info (Gzip) - 3.1Mb
Info (Zip) - 3.1Mb


Pre-General Availability Draft: 2017-11-22

12.15.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. Except for ST_Distance(), the return values 1 and 0 indicate true and false, respectively. ST_Distance() returns 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 arguments as follows:

  • If any argument is NULL or a geometry argument is an empty geometry, the return value is NULL.

  • If a geometry argument is not a syntactically well-formed geometry, an ER_GIS_INVALID_DATA error occurs.

  • For functions that take multiple geometry arguments, if those arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

  • If a geometry argument does not refer to a known SRS (has an unknown SRID value), an ER_SRS_NOT_FOUND error occurs.

  • If a 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:

    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.

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

  • ST_Contains(g1, g2)

    Returns 1 or 0 to indicate whether g1 completely contains g2. This tests the opposite relationship as ST_Within().

    ST_Contains() handles its arguments as described in the introduction of this section.

  • ST_Crosses(g1, g2)

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

    • Unless g1 and 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() handles its arguments as described in the introduction of 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)

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

    ST_Disjoint() handles its arguments as described in the introduction of this section.

  • ST_Distance(g1, g2)

    Returns the distance between g1 and g2, measured in the length unit of the spatial reference system (SRS).

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

    ST_Distance() handles its arguments as described in the introduction of this section, with these exceptions:

    • ST_Distance() detects arguments in a geographic (ellipsoidal) spatial reference system and returns the geodetic distance on the ellipsoid. 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 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 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 |
    +-----------------------+
    
    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 |
    +-----------------------+

    For the special case of distance calculations on a sphere, see the ST_Distance_Sphere() function.

  • ST_Equals(g1, g2)

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

    ST_Equals() handles its arguments as described in the introduction of 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_Intersects(g1, g2)

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

    ST_Intersects() handles its arguments as described in the introduction of this section.

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

    ST_Overlaps() handles its arguments as described in the introduction of 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)

    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() handles its arguments as described in the introduction of 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)

    Returns 1 or 0 to indicate whether g1 is spatially within g2. This tests the opposite relationship as ST_Contains().

    ST_Within() handles its arguments as described in the introduction of this section.


User Comments
Sign Up Login You must be logged in to post a comment.