How the CROSSES() function works in Mariadb?
The CROSSES()
function is a spatial function that returns 1 if two geometries cross each other, 0 if they do not, or NULL if either geometry is NULL or invalid.
The CROSSES()
function is a spatial function that returns 1 if two geometries cross each other, 0 if they do not, or NULL if either geometry is NULL or invalid. The function can be used to test the spatial relationship between two geometries, such as lines, polygons, or multipoints.
Syntax
The syntax of the CROSSES()
function is as follows:
CROSSES(g1, g2)
The function takes two arguments, g1
and g2
, which are the geometries to be compared. The arguments can be strings or expressions that evaluate to strings. The function returns an integer value that represents the result of the comparison.
Examples
Example 1: Testing if two lines cross each other
The following example uses the CROSSES()
function to test if two lines cross each other. The lines are defined by four points: (0, 0), (2, 2), (0, 2), and (2, 0).
SELECT CROSSES(ST_GeomFromText('LINESTRING(0 0,2 2)'), ST_GeomFromText('LINESTRING(0 2,2 0)'));
The output is 1
.
The output shows that the two lines cross each other, as expected.
Example 2: Testing if a line and a polygon cross each other
The following example uses the CROSSES()
function to test if a line and a polygon cross each other. The line is defined by two points: (0, 0) and (2, 2). The polygon is defined by four points: (0, 1), (1, 2), (2, 1), and (1, 0).
SELECT CROSSES(ST_GeomFromText('LINESTRING(0 0,2 2)'), ST_GeomFromText('POLYGON((0 1,1 2,2 1,1 0,0 1))'));
The output is 1
.
The output shows that the line and the polygon cross each other, as expected.
Example 3: Testing if two polygons cross each other
The following example uses the CROSSES()
function to test if two polygons cross each other. The polygons are defined by eight points: (0, 0), (0, 2), (1, 1), (2, 0), (2, 2), (3, 1), (4, 2), and (4, 0).
SELECT CROSSES(ST_GeomFromText('POLYGON((0 0,0 2,1 1,2 0,0 0))'), ST_GeomFromText('POLYGON((2 2,3 1,4 2,4 0,2 2))'));
The output is: 0
.
The output shows that the two polygons do not cross each other, as expected. They only touch at one point, which is not considered as crossing.
Example 4: Testing if two multipoints cross each other
The following example uses the CROSSES()
function to test if two multipoints cross each other. The multipoints are defined by five points: (0, 0), (0, 2), (1, 1), (2, 0), and (2, 2).
SELECT CROSSES(ST_GeomFromText('MULTIPOINT(0 0,0 2,1 1)'), ST_GeomFromText('MULTIPOINT(2 0,2 2)'));
The output is: 0
.
The output shows that the two multipoints do not cross each other, as expected. They only share one point, which is not considered as crossing.
Example 5: Testing if two invalid geometries cross each other
The following example uses the CROSSES()
function to test if two invalid geometries cross each other. The invalid geometries are defined by empty strings.
SELECT CROSSES(ST_GeomFromText(''), ST_GeomFromText(''));
The output is: NULL
.
The output shows that the function returns NULL, as expected. The function returns NULL if either geometry is NULL or invalid.
Related Functions
There are some other functions that are related to the CROSSES()
function in Mariadb. They are:
ST_Crosses()
: This function is an alias for theCROSSES()
function. It follows the OpenGIS specifications for theST_Crosses()
function.INTERSECTS()
: This function returns 1 if two geometries intersect each other, 0 if they do not, or NULL if either geometry is NULL or invalid. The function can be used to test the spatial relationship between two geometries, such as points, lines, polygons, or multipolygons.TOUCHES()
: This function returns 1 if two geometries touch each other, 0 if they do not, or NULL if either geometry is NULL or invalid. The function can be used to test the spatial relationship between two geometries, such as lines, polygons, or multipolygons.OVERLAPS()
: This function returns 1 if two geometries overlap each other, 0 if they do not, or NULL if either geometry is NULL or invalid. The function can be used to test the spatial relationship between two geometries, such as lines, polygons, or multipolygons.CONTAINS()
: This function returns 1 if the first geometry contains the second geometry, 0 if it does not, or NULL if either geometry is NULL or invalid. The function can be used to test the spatial relationship between two geometries, such as points, lines, polygons, or multipolygons.
Conclusion
The CROSSES()
function is a useful function to test if two geometries cross each other. It returns an integer value that represents the result of the comparison. The function can handle different types of geometries, such as lines, polygons, or multipoints. It is similar to the ST_Crosses()
function, but with a different name. It is also related to some other functions that provide spatial information or operations on geometries.