earthdistance module provides
two different approaches to calculating great circle distances on
the surface of the Earth. The one described first depends on the
cube module (which must be installed before
earthdistance can be installed). The second one
is based on the built-in
point data type,
using longitude and latitude for the coordinates.
In this module, the Earth is assumed to be perfectly spherical. (If that's too inaccurate for you, you might want to look at the PostGIS project.)
Data is stored in cubes that are points (both corners are the
same) using 3 coordinates representing the x, y, and z distance
from the center of the Earth. A domain
provided, which includes constraint checks that the value meets
these restrictions and is reasonably close to the actual surface of
The radius of the Earth is obtained from the
earth() function. It is given in meters. But by
changing this one function you can change the module to use some
other units, or to use a different value of the radius that you
feel is more appropriate.
This package has applications to astronomical databases as well.
Astronomers will probably want to change
earth() to return a radius of
180/pi() so that distances are in degrees.
Functions are provided to support input in latitude and longitude (in degrees), to support output of latitude and longitude, to calculate the great circle distance between two points and to easily specify a bounding box usable for index searches.
The provided functions are shown in Table F.6.
Table F.6. Cube-based Earthdistance Functions
||Returns the assumed radius of the Earth.|
||Converts the normal straight line (secant) distance between two points on the surface of the Earth to the great circle distance between them.|
||Converts the great circle distance between two points on the surface of the Earth to the normal straight line (secant) distance between them.|
||Returns the location of a point on the surface of the Earth given its latitude (argument 1) and longitude (argument 2) in degrees.|
||Returns the latitude in degrees of a point on the surface of the Earth.|
||Returns the longitude in degrees of a point on the surface of the Earth.|
||Returns the great circle distance between two points on the surface of the Earth.|
||Returns a box suitable for an indexed search using the cube
The second part of the module relies on representing Earth
locations as values of type
which the first component is taken to represent longitude in
degrees, and the second component is taken to represent latitude in
degrees. Points are taken as (longitude, latitude) and not vice
versa because longitude is closer to the intuitive idea of x-axis
and latitude to y-axis.
A single operator is provided, shown in Table F.7.
Table F.7. Point-based Earthdistance Operators
||Gives the distance in statute miles between two points on the Earth's surface.|
Note that unlike the
of the module, units are hardwired here: changing the
earth() function will not affect the results of
One disadvantage of the longitude/latitude representation is
that you need to be careful about the edge conditions near the
poles and near +/- 180 degrees of longitude. The
cube-based representation avoids these
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.