Menu

MariaDB ST_CROSSES() Function: Syntax and Examples

Learn how MariaDB ST_CROSSES() evaluates spatial relations, including its 1, 0, and NULL results, with geometry examples.

Posted on By Updated on
On this page

MariaDB ST_CROSSES(g1, g2) checks whether two geometries cross. It returns 1 when they cross and 0 otherwise. It returns NULL when g1 is a POLYGON or MULTIPOLYGON, or when g2 is a POINT or MULTIPOINT. Unlike the older CROSSES() function, ST_CROSSES() evaluates the geometry shapes rather than their bounding rectangles. See the MariaDB ST_CROSSES reference.

Syntax

The syntax for the MariaDB ST_CROSSES() function is as follows:

ST_CROSSES(g1, g2)
  • g1: A geometry value representing the first spatial object.
  • g2: A geometry value representing the second spatial object.

The function returns:

  • 1 if the spatial objects cross each other.
  • 0 if the spatial objects do not cross each other.
  • NULL if either of the input values is NULL.

Examples

Example 1: Crossing Lines

This example demonstrates crossing line segments.

SET @line1 = ST_GeomFromText('LINESTRING(0 0, 2 2)');
SET @line2 = ST_GeomFromText('LINESTRING(1 2, 3 0)');

SELECT ST_CROSSES(@line1, @line2);

Output:

1

The output 1 indicates that the two line segments cross each other.

Example 2: Non-crossing Lines

This example shows two lines that do not cross each other.

SET @line1 = ST_GeomFromText('LINESTRING(0 0, 2 2)');
SET @line2 = ST_GeomFromText('LINESTRING(3 0, 5 2)');

SELECT ST_CROSSES(@line1, @line2);

Output:

0

The output 0 indicates that the two lines do not cross each other.

Example 3: Crossing Polygons

This example demonstrates crossing polygons.

DROP TABLE IF EXISTS polygons;
CREATE TABLE polygons (id INT PRIMARY KEY, poly GEOMETRY);

INSERT INTO polygons (id, poly) VALUES
  (1, ST_GeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))'));

INSERT INTO polygons (id, poly) VALUES
  (2, ST_GeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))'));

SELECT ST_CROSSES(poly, (SELECT poly FROM polygons WHERE id = 2)) AS cross
FROM polygons
WHERE id = 1;

Output:

1

The output 1 indicates that the two polygons cross each other.

Example 4: Non-crossing Polygons

This example shows two polygons that do not cross each other.

DROP TABLE IF EXISTS polygons;
CREATE TABLE polygons (id INT PRIMARY KEY, poly GEOMETRY);

INSERT INTO polygons (id, poly) VALUES
  (1, ST_GeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))'));

INSERT INTO polygons (id, poly) VALUES
  (2, ST_GeomFromText('POLYGON((3 3, 5 3, 5 5, 3 5, 3 3))'));

SELECT ST_CROSSES(poly, (SELECT poly FROM polygons WHERE id = 2)) AS cross
FROM polygons
WHERE id = 1;

Output:

0

The output 0 indicates that the two polygons do not cross each other.

Example 5: Crossing a Line and a Polygon

This example demonstrates crossing a line and a polygon.

DROP TABLE IF EXISTS polygons;
CREATE TABLE polygons (id INT PRIMARY KEY, poly GEOMETRY);

INSERT INTO polygons (id, poly) VALUES
  (1, ST_GeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))'));

SET @line = ST_GeomFromText('LINESTRING(1 1, 3 1)');

SELECT ST_CROSSES(poly, @line) AS cross
FROM polygons
WHERE id = 1;

Output:

1

The output 1 indicates that the line crosses the polygon.

The following are some functions related to MariaDB ST_CROSSES():

  • MariaDB ST_INTERSECTS() function is used to determine whether two spatial objects intersect each other.
  • MariaDB ST_DISJOINT() function is used to determine whether two spatial objects are disjoint (not intersecting).
  • MariaDB ST_TOUCHES() function is used to determine whether two spatial objects touch each other.
  • MariaDB ST_CROSSES() function is used to determine whether two spatial objects cross each other.

Conclusion

The ST_CROSSES() function in MariaDB is a useful tool for analyzing spatial data and determining whether two spatial objects cross each other. It can be used in conjunction with other spatial functions to perform various spatial analyses and operations.