Documentation Home
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 32.9Mb
PDF (A4) - 32.9Mb
PDF (RPM) - 30.9Mb
HTML Download (TGZ) - 7.8Mb
HTML Download (Zip) - 7.9Mb
HTML Download (RPM) - 6.7Mb
Man Pages (TGZ) - 143.5Kb
Man Pages (Zip) - 203.6Kb
Info (Gzip) - 3.0Mb
Info (Zip) - 3.0Mb


Pre-General Availability Draft: 2017-07-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. The return values 1 and 0 indicate true and false, respectively, except for ST_Distance(), which returns distance values.

In general, these functions support all argument type combinations except those that are inapplicable according to the Open Geospatial Consortium specification.

Exception: ST_Distance() detects arguments in a geographic (ellipsoidal) spatial reference system (SRS). For arguments in a geographic SRS, the only permitted argument types are Point and Point, or Point and MultiPoint (in any argument order).

  • 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 follows:

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

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

    • If the arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

    • If the arguments are in an undefined SRS, an ER_SRS_NOT_FOUND error occurs.

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

    • Otherwise, the return value is non-NULL.

  • ST_Crosses(g1, g2)

    Two geometries spatially cross if their spatial relation 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 of dimension 2 (Polygon or MultiPolygon), or g2 is of dimension 1 (Point or MultiPoint), the return value is NULL.

    ST_Crosses() handles its arguments as follows:

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

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

    • If the arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

    • If the arguments are in an undefined SRS, an ER_SRS_NOT_FOUND error occurs.

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

    • Otherwise, the return value is non-NULL.

  • 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 follows:

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

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

    • If the arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

    • If the arguments are in an undefined SRS, an ER_SRS_NOT_FOUND error occurs.

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

    • Otherwise, the return value is non-NULL.

  • 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() detects arguments in a geographic (ellipsoidal) spatial reference system and returns the geodetic distance on the ellipsoid in meters. 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. Calculations for projected SRSs and SRID 0 remain the same.

    ST_Distance() handles its arguments as follows:

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

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

    • If the arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

    • If the arguments are in an undefined SRS, an ER_SRS_NOT_FOUND 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.

    • Otherwise, the return value is non-NULL.

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

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

    • If any argument is NULL, the return value is NULL.

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

    • If the arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

    • If the arguments are in an undefined SRS, an ER_SRS_NOT_FOUND error occurs.

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

    • Otherwise, the return value is non-NULL.

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

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

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

    • If the arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

    • If the arguments are in an undefined SRS, an ER_SRS_NOT_FOUND error occurs.

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

    • Otherwise, the return value is non-NULL.

  • 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. If the dimensions of the two geometries are not equal, the return value is NULL.

    ST_Overlaps() handles its arguments as follows:

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

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

    • If the arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

    • If the arguments are in an undefined SRS, an ER_SRS_NOT_FOUND error occurs.

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

    • Otherwise, the return value is non-NULL.

  • 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. If both geometries are of dimension 0 (Point or MultiPoint), the return value is NULL.

    ST_Touches() handles its arguments as follows:

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

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

    • If the arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

    • If the arguments are in an undefined SRS, an ER_SRS_NOT_FOUND error occurs.

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

    • Otherwise, the return value is non-NULL.

  • 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 follows:

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

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

    • If the arguments do not have the same SRID, an ER_GIS_DIFFERENT_SRIDS error occurs.

    • If the arguments are in an undefined SRS, an ER_SRS_NOT_FOUND error occurs.

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

    • Otherwise, the return value is non-NULL.


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