14.16.9.1 Spatial Relation Functions That Use Object Shapes (original) (raw)

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

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:

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

mysql> SET @g1 = ST_GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))'),  
    ->     @p1 = ST_GeomFromText('Point(1 1)'),  
    ->     @p2 = ST_GeomFromText('Point(3 3)'),  
    ->     @p3 = ST_GeomFromText('Point(5 5)');  
Query OK, 0 rows affected (0.00 sec)  
mysql> SELECT  
    ->   ST_Contains(@g1, @p1), ST_Within(@p1, @g1),  
    ->   ST_Disjoint(@g1, @p1), ST_Intersects(@g1, @p1)\G  
*************************** 1. row ***************************  
  ST_Contains(@g1, @p1): 1  
    ST_Within(@p1, @g1): 1  
  ST_Disjoint(@g1, @p1): 0  
ST_Intersects(@g1, @p1): 1  
1 row in set (0.00 sec)  
mysql> SELECT  
    ->   ST_Contains(@g1, @p2), ST_Within(@p2, @g1),  
    ->   ST_Disjoint(@g1, @p2), ST_Intersects(@g1, @p2)\G  
*************************** 1. row ***************************  
  ST_Contains(@g1, @p2): 0  
    ST_Within(@p2, @g1): 0  
  ST_Disjoint(@g1, @p2): 0  
ST_Intersects(@g1, @p2): 1  
1 row in set (0.00 sec)  
mysql>  
    -> SELECT  
    ->   ST_Contains(@g1, @p3), ST_Within(@p3, @g1),  
    ->   ST_Disjoint(@g1, @p3), ST_Intersects(@g1, @p3)\G  
*************************** 1. row ***************************  
  ST_Contains(@g1, @p3): 0  
    ST_Within(@p3, @g1): 0  
  ST_Disjoint(@g1, @p3): 1  
ST_Intersects(@g1, @p3): 0  
1 row in set (0.00 sec)  
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() function.

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 |  
+---------------------+---------------------+  
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() 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.