The ST_Equals() function
The ST_Equals() function compares two
geometries and returns t
(TRUE) if the geometries
are spatially equal; otherwise, it returns f
(FALSE).
Syntax
ST_Equals(g1 ST_Geometry, g2 ST_Geometry)
Return type
BOOLEAN
Usage
Using the ST_Equals() function is functionally equivalent to using ST_IsEmpty(ST_SymDifference(a,b)).
Since the ST_Equals() function is computationally intensive, consider whether you can use the Equals() function instead, which does a byte-by-byte comparison of two objects. The Equals() function is a system function. It is called in SQL statements when you use the = operator, as shown in the second SELECT statement in the following example.
CREATE TABLE equal_test (id integer,
line ST_LineString);
INSERT INTO equal_test VALUES
(1, ST_LineFromText('linestring(10 10, 20 20)', 1000));
INSERT INTO equal_test VALUES
(2, ST_LineFromText('linestring(20 20, 10 10)', 1000));
SELECT id FROM equal_test
WHERE ST_Equals (line, ST_LineFromText('linestring(10 10, 20 20)', 1000));
id
1
2
SELECT id FROM equal_test
WHERE line = ST_LineFromText('linestring(10 10, 20 20)', 1000);
id
1
The results of the spatial relationship of the ST_Equals() function can be understood or verified by comparing the results with a pattern matrix that represents the acceptable values for the DE-9IM. The DE-9IM pattern matrix for the ST_Equals() function ensures that the interiors intersect and that no interior or boundary of either geometry intersects the exterior of the other.
b | ||||
---|---|---|---|---|
Interior | Boundary | Exterior | ||
Interior | T | * | F | |
a | Boundary | * | * | F |
Exterior | F | F | * |
Example
The city GIS technician suspects that some of the data in the buildingfootprints table was somehow duplicated. To alleviate concern, the technician queries the table to determine whether any of the footprints multipolygons are equal.
CREATE TABLE buildingfootprints (building_id integer,
lot_id integer,
footprint ST_MultiPolygon);
1
whenever
it finds two multipolygons that are equal. The bf1.building_id <>
bf2.building_id condition eliminates the comparison of a geometry
to itself: SELECT bf1.building_id, bf2.building_id
FROM buildingfootprints bf1, buildingfootprints bf2
WHERE ST_Equals(bf1.footprint,bf2.footprint)
AND bf1.building_id <> bf2.building_id;