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

The following figure shows sets of various geometric objects that are spatially equivalent.
Figure 1: Equal geometries

This graphic shows two sets of various geometric objects that are the same.

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.

To illustrate the difference between ST_Equals() and Equals(), consider 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));
The following query returns both rows because ST_Equals() determines that both linestrings are spatially equivalent:
SELECT id FROM equal_test 
   WHERE ST_Equals (line, ST_LineFromText('linestring(10 10, 20 20)', 1000));

         id 

          1
          2
The following query returns only the first row because Equals() performs only a memory comparison of the linestrings:
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.

Table 1. The DE-9IM pattern matrix for the ST_Equals() function

The table is a matrix. The top row and first column identify the geometries.

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.

The buildingfootprints table is created with the following statement. The building_id column uniquely identifies the buildings. The lot_id identifies the building's lot, and the footprint multipolygon stores the building's geometry:
CREATE TABLE buildingfootprints (building_id  integer,
                                 lot_id       integer,
                                 footprint    ST_MultiPolygon);
The buildingfootprints table is spatially joined to itself by the ST_Equals() function, which returns 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;