Comparison functions

You can use comparison functions to compare two geometries with one another.

Comparison functions return a value of 1 (one) if a comparison meets certain criteria, a value of 0 (zero) if a comparison fails to meet the criteria, and a null value if the comparison could not be performed. Comparisons cannot be performed if the comparison operation has not been defined for the input parameters, or if either of the parameters is null. Comparisons can be performed if geometries with different data types or dimensions are assigned to the parameters.

The Dimensionally Extended 9 Intersection Model (DE-9IM) is a mathematical approach that defines the pair-wise spatial relationship between geometries of different types and dimensions. This model expresses spatial relationships between all types of geometries as pair-wise intersections of their interiors, boundaries, and exteriors, with consideration for the dimension of the resulting intersections.

Given geometries a and b: I(a), B(a), and E(a) represent the interior, boundary, and exterior of a, respectively. And, I(b), B(b), and E(b) represent the interior, boundary, and exterior of b. The intersections of I(a), B(a), and E(a) with I(b), B(b), and E(b) produce a 3–by–3 matrix. Each intersection can result in geometries of different dimensions. For example, the intersection of the boundaries of two polygons consists of a point and a linestring, in which case the dim function returns the maximum dimension of 1.

The dim function returns a value of –1, 0, 1 or 2. The –1 corresponds to the null set or dim(null), which is returned when no intersection was found.

Results returned by comparison functions can be understood or verified by comparing the results returned by a comparison function with a pattern matrix that represents the acceptable values for the DE-9IM.

The pattern matrix contains the acceptable values for each of the intersection matrix cells. The possible pattern values are:
T
An intersection must exist; dim = 0, 1, or 2.
F
An intersection must not exist; dim = -1.
*
It does not matter if an intersection exists; dim = -1, 0, 1, or 2.
0
An intersection must exist and its exact dimension must be 0; dim = 0.
1
An intersection must exist and its maximum dimension must be 1; dim = 1.
2
An intersection must exist and its maximum dimension must be 2; dim = 2.
For example, the following pattern matrix for the ST_Within function includes the values T, F, and *.
Table 1. Matrix for ST_Within. The pattern matrix of the ST_Within function for geometry combinations.
 

Geometry b Interior

Geometry b Boundary

Geometry b Exterior

Geometry a Interior

T

*

F

Geometry a Boundary

*

*

F

Geometry a Exterior

*

*

*

The ST_Within function returns a value of 1 when the interiors of both geometries intersect and when the interior or boundary of a does not intersect the exterior of b. All other conditions do not matter.

Each function has at least one pattern matrix, but some require more than one to describe the relationships of various geometry type combinations.

The DE-9IM was developed by Clementini and Felice, who dimensionally extended the 9 Intersection Model of Egenhofer and Herring. The DE-9IM is a collaboration of four authors (Clementini, Eliseo, Di Felice, and van Osstrom) who published the model in "A Small Set of Formal Topological Relationships Suitable for End-User Interaction," D. Abel and B.C. Ooi (Ed.), Advances in Spatial Database—Third International Symposium. SSD '93. LNCS 692. Pp. 277-295. The 9 Intersection model by M. J. Egenhofer and J. Herring (Springer-Verlag Singapore [1993]) was published in "Categorizing binary topological relationships between regions, lines, and points in geographic databases," Tech. Report, Department of Surveying Engineering, University of Maine, Orono, ME 1991.

Spatial comparison functions

Spatial comparison functions compare two geometries with one another.

The comparison functions are:

  • EnvelopesIntersect
  • ST_Contains
  • ST_Crosses
  • ST_Disjoint
  • ST_Equals
  • ST_Intersects
  • ST_Overlaps
  • ST_Relate
  • ST_Touches
  • ST_Within

Functions that compare geographic features

You can use comparison functions to compare geographic features.

Certain spatial functions return information about ways in which geographic features relate to one another or compare with one another. Other spatial functions return information as to whether two definitions of coordinate systems or two spatial reference systems are the same. In all cases, the information returned is a result of a comparison between geometries, between definitions of coordinate systems, or between spatial reference systems.

Table 2. Comparison functions by purpose
Purpose Functions
Determines whether the interior of one geometry intersects the interior of another.
  • ST_Contains
  • ST_Within
Returns information about intersections of geometries.
  • ST_Crosses
  • ST_Disjoint
  • ST_Intersects
  • ST_Overlaps
  • ST_Touches
Determines whether the smallest rectangle that encloses one geometry intersects with the smallest rectangle that encloses another geometry.
  • EnvelopesIntersect
Determines whether two objects are identical.
  • ST_Equals
Determines the shortest distance between any point in the first geometry to any point in the second geometry.
  • ST_Distance
Determines whether the geometries that are being compared meet the conditions of the DE-9IM pattern matrix string.
  • ST_Relate