5.3. Contain and overlap
Prev | Up | Chapter 5. Operators | Home | Next

## 5.3. Contain and overlap #

On the sphere, an equality relationship is rarely used. There are frequently questions like Is object `a` contained by object `b`? or Does object `a` overlap object `b`? pgSphere supports such queries using binary operators returning true or false:

Table 5.2. Contain and overlap operators

operator | operator returns true, if
--- | ---
<@ or @ (deprecated, not for smoc) | the left object is contained by the right object
@> or ˜ (deprecated, not for smoc) | the left object contains the right object
!<@ or !@ (deprecated, not for smoc) | the left object is not contained by the right object
!@> or !˜ (deprecated, not for smoc) | the left object does not contain the right object
&& | the objects overlap each other
!&& | the objects do not overlap each other

An overlap or contain operator does not exist for all combinations of data types. For instance, scircle <@ `spoint` is useless because a spherical point can never contain a spherical circle.

When one of the arguments of such an operator is a MOC and the other is an scircle or an spoly, the non-MOC argument is converted to a MOC of the order of the maximum order of the MOC. When comparing against a MOC-valued column, it is usually much faster to explicitly convert the geometry using the smoc constructor, as the conversion will then only happen once.

Example 5.3. Is the left circle contained by the right circle?

    sql> SELECT scircle '<(0d,20d),2d>' <@ scircle '<(355d,20d),10d>' AS test ;
    test
    ------
     t
    (1 row)

Example 5.4. Are the circles overlapping?

    sql> SELECT scircle '<(0d,20d),2d>' && scircle '<(199d,-10d),10d>' AS test ;
     test
    ------
     f
    (1 row)

Example 5.5. Overlaps between a circle and a moc

    sql> SELECT scircle '<(37d, 5d), 0.25d>' <@ smoc('4/1117') AS test ;
     test
    ------
     f
    (1 row)

Example 5.6. Overlaps between a circle and a moc with explicit order

    sql> SELECT scircle '<(37d, 5d), 0.25d>' <@ smoc('4/1117 5/') AS test ;
     test
    ------
     t
    (1 row)

Example 5.7. Overlaps between a circle and a moc with explicit cast (normally faster)

    sql> SELECT smoc(5, scircle '<(37d, 5d), 0.25d>') <@ smoc('4/1117 5/') AS test ;
     test
    ------
     t
    (1 row)

Prev | Up | Next
5.2. Equality | Home | 5.4. Crossing of lines