pgSphere 1.1.5
Prev |  | Next

# 5. Operators

## 5.1. Casting

pgSphere provides some casting operators. So, you can transform an object to another data type. A cast is done using a `CAST(x AS typename)`, `x::typename` or `typename(x)` construct.

Table 2. Castings

casting argument | type target | returns
--- | --- | ---
spoint | scircle | circle with center position spoint and radius 0.0
spoint | sellipse | an ellipse at position spoint and radius 0.0
spoint | sline | a line with length 0.0 at position spoint
scircle | sellipse | the scircle as sellipse
sline | strans | the Euler transformation of sline
sellipse | scircle | the bounding circle of sellipse
sellipse | strans | the Euler transformation of sellipse

Example 26. Cast a spherical point as a circle

    sql> SELECT CAST ( spoint '(10d,20d)' AS scircle );
          scircle
    --------------------
     <(10d , 20d) , 0d>
    (1 row)

## 5.2. Equality

All data types of pgSphere have equality operators. The equality operator is as in `SQL` `=`. Furthermore, there are two valid negators to indicate that two objects are not equal: `!=` and `<>`.

Example 27. Equality of two spherical points

    sql> SELECT spoint '(10d,20d)' = spoint '(370d,20d)' ;
     test
    ------
     t
    (1 row)

## 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 3. 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.

Example 28. 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 29. Are the circles overlapping?

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

## 5.4. Crossing of lines

Another binary relationship is crossing. pgSphere supports only crossing of lines. The correlative operator is named `#`.

Example 30. Are the lines crossed?

    sql> SELECT sline '(0d,0d,0d),10d' # sline '(90d,5d,5d,XYZ),10d' AS test ;
     test
    ------
     t
    (1 row)

## 5.5. Distance

The binary distance operator `<->` is a non-boolean operator returning the distance between two objects in radians. Currently, pgSphere supports only distances between points, circles, and between point and circle. If the objects are overlapping, the distance operator returns zero (0.0).

Example 31. Distance between two circles

    sql> SELECT 180 * ( scircle '<(0d,20d),2d>' <-> scircle '<(0d,40d),2d>' )
            / pi() AS dist ;
     dist
    ------
     16
    (1 row)

## 5.6. Length and circumference

The length/circumference operator `@-@` is a non-boolean unary operator returning the circumference or length of an object. In the current implementation, pgSphere supports only circumferences of circles, polygons, and boxes. It supports lengths of lines and paths too. Instead of using the operator, you can use the functions `circum(object)` or `length(object)`.

Example 32. Circumference of a circle

    sql> SELECT 180 * ( @-@ scircle '<(0d,20d),30d>' )/ pi() AS circ ;
     circ
    ------
     180
    (1 row)

Example 33. Length of a line

    sql> SELECT 180 * ( @-@ sline '(0d,0d,0d),30d' )/ pi() AS length ;
     length
    --------
     30
    (1 row)

## 5.7. Center

The center operator `@@` is a non-boolean unary operator returning the center of an object. In the current implementation of pgSphere, only centers of circles and ellipses are supported. Instead of using the operator, you can use the function `center(object)`.

Example 34. Center of a circle

    sql> SELECT @@ scircle '<(0d,20d),30d>';

## 5.8. Change the direction

The unary operator `-` changes the direction of sline or spath objects. You can use it with an Euler transformation object in the figurative sense, too (Section 5.10).

Example 35. Swap begin and end of a sline

    sql> SELECT - sline (spoint '(0d,0d)', spoint '(10d,0d)');

## 5.9. Turn the path of a line

The unary operator `!` turns the path of sline objects, but preserves begin and end of the spherical line. The length of returned line will be 360° minus the line length of operator's argument.

The operator `!` returns `NULL`, if the length of sline argument is 0, because the path of returned sline is undefined.

Example 36. Return length and check if north pole on slines

    sql> SELECT set_sphere_output('DEG');
     set_sphere_output
    -------------------
     SET DEG
    (1 row)

    sql> SELECT length ( sline ( spoint '(0d,0d)', spoint '(0d,10d)' ) ) *
            180.0 / pi() AS length;
     length
    --------
         10
    (1 row)

    sql> SELECT spoint '(0d,90d)' @
            sline ( spoint '(0d,0d)', spoint '(0d,10d)' ) AS test;
     test
    ------
     f
    (1 row)

    sql> SELECT length ( ! sline ( spoint '(0d,0d)', spoint '(0d,10d)' ) ) *
            180.0 / pi() AS length;
     length
    --------
        350
    (1 row)

    sql> SELECT spoint '(0d,90d)' @
            ! sline ( spoint '(0d,0d)', spoint '(0d,10d)' ) AS test;
     test
    ------
     t
    (1 row)

## 5.10. Transformation

As in a plane, translations and rotations are needed to do object or coordinate transformations. With pgSphere, it is done using Euler transformations (strans). On a sphere, there aren't real translations. All movements on a sphere are rotations around axes.

The general syntax for a transformation is always:

    object operator euler

where operators are `+` for a usual transformation, `-` for an inverse transformation. You can transform any object having a pgSphere data type, except the data type sbox.

Example 37. Transformation of a point

Rotate a spherical point counterclockwise, first 90° around the `x`-axis, second 90° around the `z`-axis, and last 40.5° around the `x`-axis.

    sql> SELECT set_sphere_output('DEG');
     set_sphere_output
    -------------------
     SET DEG
    (1 row)

    sql> SELECT spoint '(30d,0d)' + strans '90d, 90d, 40.5d, XZX AS spoint';
       spoint
    ---------------
     (90d , 70.5d)
    (1 row)

You can use the `+` and `-` operator as unary operators for transformations, too. `+strans` just returns the transformation itself, `-strans` returns the inverse transformation.

Example 38. An inverse transformation

    sql> SELECT set_sphere_output('DEG');
     set_sphere_output
    -------------------
     SET DEG
    (1 row)

    sql> SELECT - strans '20d, 50d, 80d, XYZ' AS inverted;
          inverted
    -----------------------
     280d, 310d, 340d, ZYX
    (1 row)

## 5.11. smoc operators

A unique feature of MOCs compared to the other pgSphere objects is that their unions and intersections are again MOCs. Hence, smocs support two special operators which both yield smoc as output:

Union:

    smoc | smoc

Intersection:

    smoc & smoc

Prev | Home | Next
Constructors |  | Functions