Skip to main content
Tests whether geography A is fully within geography B using S2 spherical boolean operations. Returns true when every point of A’s interior lies in B’s interior. By OGC convention, ST_Within(A, B) is equivalent to ST_Contains(B, A), and shares the same boundary semantics. Boundary semantics on the sphere are inherited from S2’s boolean operations and depend on each ring’s vertex orientation: along an edge that is “owned” by B’s boundary the test returns true, and along the opposite edge it returns false. Do not rely on a specific result for points that lie exactly on B’s boundary; for predictable behavior, use a strict interior point or expand B slightly with ST_Buffer before testing. ST_Within returning true ST_Within returning false

Signatures

ST_Within (A: Geography, B: Geography)

Parameters

A
Geography
required
The geography tested for being within B.
B
Geography
required
The containing geography.

Return type

true if A is fully within B.

Example

SELECT ST_Within(
  ST_GeogFromWKT('POINT (0.5 0.5)', 4326),
  ST_GeogFromWKT('POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))', 4326)
);
true