The ST_Contains() function
The ST_Contains() function takes two
geometry objects and returns t
(TRUE) if the first
object completely contains the second; otherwise, it returns f
(FALSE).
Syntax
ST_Contains(g1 geometry, g2 geometry)
Usage
The results of the spatial relationship of the ST_Contains() function can be understood or verified by comparing the results with a pattern matrix that represents the acceptable values for the DE-9IM. The pattern matrix of the ST_Contains() function states that the interiors of both geometries must intersect. It also states that the interior and boundary of the secondary (geometry b) must not intersect the exterior of the primary (geometry a).
b | ||||
---|---|---|---|---|
Interior | Boundary | Exterior | ||
Interior | T | * | * | |
a | Boundary | * | * | * |
Exterior | F | F | * |
Return type
BOOLEAN
Example
In the example below, two tables are created: buildingfootprints contains a city's building footprints, and lots contains its lots. The city engineer wants to ensure that all building footprints are completely inside their lots.
CREATE TABLE buildingfootprints (building_id integer,
lot_id integer,
footprint ST_MultiPolygon);
CREATE TABLE lots (lot_id integer,
lot ST_MultiPolygon);
SELECT building_id
FROM buildingfootprints, lots
WHERE NOT ST_Contains(lot,footprint);
SELECT bf.building_id, bf.lot_id, lots.lot_id
FROM buildingfootprints bf, lots
WHERE NOT ST_Contains(lot,footprint)
AND lots.lot_id <> bf.lot_id;