Returns true if geometry A is completely inside geometry B: no point of A lies outside B, and at least one interior point of A lies in the interior of B.
Syntax
boolean ST_Within(geometry A, geometry B)Parameters
| Parameter | Description |
|---|---|
| A | The geometry to test. |
| B | The geometry to test against. |
Description
ST_Within(A, B) returns true when both of the following conditions are met:
No point of A lies outside B.
At least one interior point of A lies in the interior of B.
Relationship to other functions:
If both
ST_Within(A, B)andST_Within(B, A)returntrue, the two geometry objects are spatially equal.
Constraints:
Both geometry objects must use the same projection and have the same spatial reference identifier (SRID).
GeometryCollectionobjects are not supported.Invalid geometry objects produce an incorrect result.
Index behavior:
ST_Within generates a bounding box and uses any available spatial indexes on the geometry objects. To run the containment test without index acceleration, use _ST_Within.
Examples
Example 1: Polygon inside polygon
The smaller polygon (1,1)–(2,2) lies entirely inside the larger polygon (0,0)–(3,3), so ST_Within returns true.
SELECT ST_Within(
'POLYGON((1 1, 1 2, 2 2, 2 1, 1 1))'::geometry,
'POLYGON((0 0, 0 3, 3 3, 3 0, 0 0))'::geometry
);
st_within
-----------
t
(1 row)