How the ST_EQUALS() function works in MariaDB?
The ST_EQUALS()
function in MariaDB is used to determine whether two spatial objects are geometrically equal or not.
The ST_EQUALS()
function in MariaDB is used to determine whether two spatial objects are geometrically equal or not. It performs a precise geometric comparison between the two objects, considering their coordinates, shapes, and dimensions.
Syntax
The syntax for the MariaDB ST_EQUALS()
function is as follows:
ST_EQUALS(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 two spatial objects are geometrically equal.
- 0 if the two spatial objects are not geometrically equal.
NULL
if either of the input values isNULL
.
Examples
Example 1: Equal Polygons
This example demonstrates two equal polygons.
SET @poly1 = ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))');
SET @poly2 = ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))');
SELECT ST_EQUALS(@poly1, @poly2);
Output:
1
The output 1
indicates that the two polygons are geometrically equal.
Example 2: Non-equal Polygons
This example shows two non-equal polygons.
SET @poly1 = ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))');
SET @poly2 = ST_GeomFromText('POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))');
SELECT ST_EQUALS(@poly1, @poly2);
Output:
0
The output 0
indicates that the two polygons are not geometrically equal.
Example 3: Equal Points
This example demonstrates two equal points.
SET @point1 = ST_GeomFromText('POINT(1 2)');
SET @point2 = ST_GeomFromText('POINT(1 2)');
SELECT ST_EQUALS(@point1, @point2);
Output:
1
The output 1
indicates that the two points are geometrically equal.
Example 4: Non-equal Points
This example shows two non-equal points.
SET @point1 = ST_GeomFromText('POINT(1 2)');
SET @point2 = ST_GeomFromText('POINT(2 3)');
SELECT ST_EQUALS(@point1, @point2);
Output:
0
The output 0
indicates that the two points are not geometrically equal.
Example 5: Equal Geometries from a Table
This example demonstrates equal geometries stored in a table.
DROP TABLE IF EXISTS geometries;
CREATE TABLE geometries (id INT PRIMARY KEY, geom GEOMETRY);
INSERT INTO geometries (id, geom) VALUES
(1, ST_GeomFromText('POINT(1 2)')),
(2, ST_GeomFromText('POINT(1 2)')),
(3, ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'));
SELECT id, ST_EQUALS(geom, (SELECT geom FROM geometries WHERE id = 1)) AS equals
FROM geometries;
Output:
1, 1
2, 1
3, 0
The output shows that the geometries with ids 1 and 2 are equal, while the geometry with id 3 is not equal to the geometry with id 1.
Related Functions
The following are some functions related to MariaDB ST_EQUALS()
:
- 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_EQUALS()
function in MariaDB is an essential tool for performing precise geometric comparisons between spatial objects. It allows you to determine whether two objects are geometrically equal, considering their coordinates, shapes, and dimensions. This function is particularly useful in applications that require accurate spatial analysis, such as geographic information systems (GIS), spatial databases, and location-based services.