<!-- doc/src/sgml/earthdistance.sgml -->
<sect1 id="earthdistance" xreflabel="earthdistance">
<title>earthdistance — calculate great-circle distances</title>
<indexterm zone="earthdistance">
<primary>earthdistance</primary>
</indexterm>
<para>
The <filename>earthdistance</filename> module provides two different approaches to
calculating great circle distances on the surface of the Earth. The one
described first depends on the <filename>cube</filename> module.
The second one is based on the built-in <type>point</type> data type,
using longitude and latitude for the coordinates.
</para>
<para>
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
<application><ulink url="https://postgis.net/">PostGIS</ulink></application>
project.)
</para>
<para>
The <filename>cube</filename> module must be installed
before <filename>earthdistance</filename> can be installed
(although you can use the <literal>CASCADE</literal> option
of <command>CREATE EXTENSION</command> to install both in one command).
</para>
<caution>
<para>
It is strongly recommended that <filename>earthdistance</filename>
and <filename>cube</filename> be installed in the same schema, and that
that schema be one for which CREATE privilege has not been and will not
be granted to any untrusted users.
Otherwise there are installation-time security hazards
if <filename>earthdistance</filename>'s schema contains objects defined
by a hostile user.
Furthermore, when using <filename>earthdistance</filename>'s functions
after installation, the entire search path should contain only trusted
schemas.
</para>
</caution>
<sect2 id="earthdistance-cube-based">
<title>Cube-Based Earth Distances</title>
<para>
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 <glossterm linkend="glossary-domain">domain</glossterm>
<type>earth</type> over type <type>cube</type> is provided, which
includes constraint checks that the value meets these restrictions and
is reasonably close to the actual surface of the Earth.
</para>
<para>
The radius of the Earth is obtained from the <function>earth()</function>
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.
</para>
<para>
This package has applications to astronomical databases as well.
Astronomers will probably want to change <function>earth()</function> to return a
radius of <literal>180/pi()</literal> so that distances are in degrees.
</para>
<para>
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.
</para>
<para>
The provided functions are shown
in <xref linkend="earthdistance-cube-functions"/>.
</para>
<table id="earthdistance-cube-functions">
<title>Cube-Based Earthdistance Functions</title>
<tgroup cols="1">
<thead>
<row>
<entry role="func_table_entry"><para role="func_signature">
Function
</para>
<para>
Description
</para></entry>
</row>
</thead>
<tbody>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm><primary>earth</primary></indexterm>
<function>earth</function> ()
<returnvalue>float8</returnvalue>
</para>
<para>
Returns the assumed radius of the Earth.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm><primary>sec_to_gc</primary></indexterm>