Functions that check whether one geometry contains another
ST_Contains and ST_Within both take two geometries as input and determine whether the interior of one intersects the interior of the other.
In colloquial terms, ST_Contains determines whether the first geometry given to it encloses the second geometry (whether the first contains the second). ST_Within determines whether the first geometry is completely inside the second (whether the first is within the second).
ST_Contains
Use ST_Contains to determine whether one geometry is completely contained by another geometry.
ST_Contains returns a value of 1 (one) if the second geometry is completely contained by the first geometry. The ST_Contains function returns the exact opposite result of the ST_Within function.
- A multipoint geometry contains a point or multipoint geometries when all of the points are within the first geometry.
- A polygon geometry contains a multipoint geometry when all of the points are either on the boundary of the polygon or in the interior of the polygon.
- A linestring geometry contains a point, multipoint, or linestring geometries when all of the points are within the first geometry.
- A polygon geometry contains a point, linestring or polygon geometries when the second geometry is in the interior of the polygon.
| Geometry b Interior |
Geometry b Boundary |
Geometry b Exterior |
|
|---|---|---|---|
| Geometry a Interior |
T |
* |
* |
| Geometry a Boundary |
* |
* |
* |
| Geometry a Exterior |
F |
F |
* |
ST_Within
Use ST_Within to determine whether one geometry is completely within another geometry.
| Geometry b Interior |
Geometry b Boundary |
Geometry b Exterior |
|
|---|---|---|---|
| Geometry a Interior |
T |
* |
F |
| Geometry a Boundary |
* |
* |
F |
| Geometry a Exterior |
* |
* |
* |
- A point geometry is within a multipoint geometry when its interior intersects one of the points in the second geometry.
- A multipoint geometry is within a multipoint geometry when the interiors of all points intersect the second geometry.
- A multipoint geometry is within a polygon geometry when all of the points are either on the boundary of the polygon or in the interior of the polygon.
- A point geometry is within a linestring geometry when all of the points are within the second geometry. In Figure 2, the point is not within the linestring because its interior does not intersect the linestring; however, the multipoint geometry is within the linestring because all of its points intersect the interior of the linestring.
- A linestring geometry is within another linestring geometries when all of its points intersect the second geometry.
- A point geometry is not within a polygon geometry because its interior does not intersect the boundary or interior of the polygon.
- A linestring geometry is within a polygon geometry when all of its points intersect either the boundary or interior of the polygon.
- A polygon geometry is within a polygon geometry when all of its points intersect either the boundary or interior of the polygon.