Name

ST_Within — Tests if a Geometry is spatially within another Geometry.

Synopsis

boolean ST_Within(bytea GeomA, bytea GeomB);

Alias

Within

Description

Tests if a Geometry is spatially within another Geometry. The within predicate has the following equivalent definitions:

  • Every point of Geometry A is a point of Geometry B, and the interiors of the two geometries have at least one point in common.

  • In mathematical terms: a.Within(b) «-» (a ∩ b = a) Ʌ (I(a) ∩I(b) ≠ ᴓ)

  • The DE-9IM Intersection Matrix for the two geometries is T*F**F*** *

  • ST_Within is the inverse of ST_Contains

[Note]

GeometryCollections are unsupported

Coordinate Dimensions
2D3DM
Spatial Standards Support
OGC SFS for SQL. 1.1 (1999)OGC SFS for SQL. 1.1.0 (2005)OGC SFS for SQL. 1.2.0 (2006)SQL-MM Part 3
2.1.1.2 , 2.1.13.37.2.19.17.2.8.15.1.30

Examples

SELECT ST_Within(PA,PB) as AwithinB, ST_Contains(PB,PA) as BcontainsA, ST_Within(PB,PA) as BwithinA 
from (SELECT ST_GeomFromText('POLYGON ((3 5, 3 8, 6 8, 6 5, 3 5))') as PA, 
        ST_GeomFromText('POLYGON ((1 1, 1 9, 8 9, 8 1, 1 1))') as PB) as foo;
--Result
 awithinb | bcontainsa | bwithina
----------+------------+----------
 t        | t          | f
ST_Within(A,B)=TRUE ST_Within(B,A)=FALSE

Related functions

ST_Contains, ST_Relate