Intuitiveness of spatial predicates

Sometimes the outcome of a predicate is not intuitive, so you should test special cases to make sure you are getting the results you want. For example, in order for a geometry to contain another geometry (a.ST_Contains(b)=1), or for a geometry to be within another geometry (b.ST_Within(a)=1), the interior of a and the interior of b must intersect, and no part of b can intersect the exterior of a. However, there are some cases where you would expect a geometry to be considered contained or within another geometry, but it is not.

For example, the following return 0 (a is red) for a.ST_Contains(b) and b.ST_Within(a):

Four graphics with red square for each. First one shows blue rectangle partially overlapping the red square. Second one shows blue dot well outside the red square. Third one shows a blue dot on the boundary of the red square. Fourth one shows a line that follows along a portion of the boundary of the red square.

Case one and two are obvious; the purple geometries are not completely within the red squares. Case three and four, however, are not as obvious. In both of these cases, the purple geometries are only on the boundary of the red squares. ST_Contains does not consider the purple geometries to be within the red squares, even though they appear to be within them.

If your predicate tests return a different result for cases than desired, consider using the ST_Relate method to specify the exact relationship you are testing for. See Test custom relationships using the ST_Relate method.