How the TOUCHES() function works in Mariadb?
The TOUCHES()
function in MariaDB is a spatial function used to test whether two geometries touch each other.
The TOUCHES()
function in MariaDB is a spatial function used to test whether two geometries touch each other. It returns 1 (true) if the two geometries have at least one point in common but do not overlap or intersect, and 0 (false) otherwise.
Syntax
The syntax of the MariaDB TOUCHES()
function is as follows:
TOUCHES(g1, g2)
- g1: This is the first geometry value to be tested.
- g2: This is the second geometry value to be tested.
The function returns 1 (true) if the two geometries touch each other, and 0 (false) otherwise.
Examples
Example 1: Testing if two linestrings touch
This example demonstrates how to use the TOUCHES()
function to test if two linestrings touch each other.
SELECT TOUCHES(
LineString(Point(0, 0), Point(2, 2)),
LineString(Point(2, 2), Point(4, 4))
);
The following is the output:
1
In this example, the two linestrings share a single point (2, 2), which means they touch each other, so the TOUCHES()
function returns 1 (true).
Example 2: Testing if a linestring touches a polygon
This example shows how to use the TOUCHES()
function to test if a linestring touches a polygon.
SELECT TOUCHES(
LineString(Point(0, 0), Point(2, 2)),
Polygon(LineString(Point(2, 2), Point(4, 2), Point(4, 4), Point(2, 4), Point(2, 2)))
);
The following is the output:
1
In this example, the linestring shares a single point (2, 2) with the polygon, which means they touch each other, so the TOUCHES()
function returns 1 (true).
Example 3: Testing if two polygons touch
This example demonstrates how to use the TOUCHES()
function to test if two polygons touch each other.
SELECT TOUCHES(
Polygon(LineString(Point(0, 0), Point(2, 0), Point(2, 2), Point(0, 2), Point(0, 0))),
Polygon(LineString(Point(2, 0), Point(4, 0), Point(4, 2), Point(2, 2), Point(2, 0)))
);
The following is the output:
1
In this example, the two polygons share a single line segment (2, 0) to (2, 2), which means they touch each other, so the TOUCHES()
function returns 1 (true).
Example 4: Testing if geometries do not touch
This example shows how the TOUCHES()
function returns 0 (false) when the geometries do not touch each other.
SELECT TOUCHES(
Point(0, 0),
Point(2, 2)
);
The following is the output:
0
In this example, the two points are separate and do not touch each other, so the TOUCHES()
function returns 0 (false).
Example 5: Using TOUCHES()
with a table
This example demonstrates how to use the TOUCHES()
function in combination with a table.
DROP TABLE IF EXISTS geometries;
CREATE TABLE geometries (
id INT PRIMARY KEY,
geom GEOMETRY
);
INSERT INTO geometries VALUES
(1, LineString(Point(0, 0), Point(2, 2))),
(2, LineString(Point(2, 2), Point(4, 4))),
(3, Polygon(LineString(Point(2, 0), Point(4, 0), Point(4, 2), Point(2, 2), Point(2, 0))));
SELECT g1.id AS id1, g2.id AS id2, TOUCHES(g1.geom, g2.geom) AS touches
FROM geometries g1, geometries g2
WHERE g1.id <> g2.id;
The following is the output:
+-----+-----+---------+
| id1 | id2 | touches |
+-----+-----+---------+
| 2 | 1 | 1 |
| 3 | 1 | 1 |
| 1 | 2 | 1 |
| 3 | 2 | 1 |
| 1 | 3 | 1 |
| 2 | 3 | 1 |
+-----+-----+---------+
In this example, the TOUCHES()
function is used to test if the geometries in the geometries
table touch each other. The result shows that the linestrings with IDs 1 and 2 touch each other, the linestring with ID 1 touches the polygon with ID 3, and the linestring with ID 2 touches the polygon with ID 3.
Related Functions
The following are some functions related to the MariaDB TOUCHES()
function:
- MariaDB
INTERSECTS()
function is used to test if two geometries intersect. - MariaDB
CROSSES()
function is used to test if two geometries cross each other. - MariaDB
WITHIN()
function is used to test if a geometry is within another geometry. - MariaDB
CONTAINS()
function is used to test if a geometry contains another geometry.
Conclusion
The TOUCHES()
function in MariaDB is a useful spatial function for testing if two geometries touch each other. It can be used in various scenarios, such as spatial analysis, geographic information systems (GIS), and location-based services. By understanding the usage and capabilities of this function, along with related spatial functions, developers can effectively work with and analyze spatial data in their MariaDB applications.