6.2.  spoint functions #

6.2.1. Longitude and latitude
6.2.2. Cartesian coordinates
6.2.3. Point-within-distance function

6.2.1.  Longitude and latitude #

The functions

long(p); 
spoint p;
 
lat(p); 
spoint p;
 

returns the longitude or latitude value of a spherical position p in radians.

Example 6.3. Get the longitude and latitude of a spherical point in degrees

sql> SELECT long ( spoint '(10d,20d)' ) * 180.0 / pi() AS longitude;
 longitude
------------
 10
(1 row)

sql> SELECT lat ( spoint '(10d,20d)' ) * 180.0 / pi() AS latitude;
 latitude
----------
 20
(1 row)
              

6.2.2.  Cartesian coordinates #

The functions

x(p); 
spoint p;
 
y(p); 
spoint p;
 
z(p); 
spoint p;
 

return the Cartesian x, y or z value of a spherical position p. The returned values are always between -1.0 and +1.0.

Example 6.4.  Get the Cartesian z-value of a spherical point

sql> SELECT z ( spoint '(10d,-90d)' ) AS z;
 z
----
 -1
(1 row)
              

You can get a float8 array of Cartesian values using the function

xyz(p); 
spoint p;
 

Example 6.5.  Get the Cartesian values of a spherical point

sql> SELECT xyz ( spoint '(0d,0d)' ) AS cart;
  cart
---------
 {1,0,0}
(1 row)
              

6.2.3.  Point-within-distance function #

The function

spoint_dwithin(p1,  
 p2,  
 radius); 
spoint p1;
spoint p2;
float8 radius;
 

returns a boolean value that signifies whether the points p1 and p2 lie within distance radius (in radians) of each other, i.e. it computes the boolean expression p1 <-> p2 <= radius. On PostgreSQL 12 and later, the function has GiST support and the PostgreSQL optimizer will transform it to either p1 <@ scircle(p2, radius) or p2 <@ scircle(p1, radius) where appropriate.

Example 6.6.  Efficiently join two tables of points with some fuzziness permitted

sql> SELECT * FROM stars1 JOIN stars2 WHERE spoint_dwithin(stars1.s, stars2.s, 1e-5);