ST_Within

更新时间:
复制 MD 格式

Returns true if geometry A is fully contained within geometry B.

Syntax

boolean ST_Within(geometry A, geometry B)

Parameters

ParameterDescription
AThe geometry to test for containment.
BThe geometry that acts as the container.

Usage notes

Same spatial reference identifier (SRID) required. Both geometries must use the same projection method and the same SRID.

Spatial equality. If both ST_Within(A, B) and ST_Within(B, A) return true, A and B are spatially equal.

Input validity. Pass only valid geometry objects. Invalid geometries return incorrect results.

GeometryCollection not supported. This function does not support GeometryCollection objects.

Index usage. ST_Within generates a bounding box and uses any available spatial indexes. To skip index usage, call _ST_Within instead.

Examples

Basic containment check

The following example checks whether a small polygon lies within a larger one.

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
);

Output:

 st_within
-----------
 t
(1 row)