1
<!-- doc/src/sgml/earthdistance.sgml -->
3
<sect1 id="earthdistance">
4
<title>earthdistance</title>
6
<indexterm zone="earthdistance">
7
<primary>earthdistance</primary>
11
The <filename>earthdistance</> module provides two different approaches to
12
calculating great circle distances on the surface of the Earth. The one
13
described first depends on the <filename>cube</> module (which
14
<emphasis>must</> be installed before <filename>earthdistance</> can be
15
installed). The second one is based on the built-in <type>point</> data type,
16
using longitude and latitude for the coordinates.
20
In this module, the Earth is assumed to be perfectly spherical.
21
(If that's too inaccurate for you, you might want to look at the
22
<application><ulink url="http://www.postgis.org/">PostGIS</ulink></>
27
<title>Cube-based Earth Distances</title>
30
Data is stored in cubes that are points (both corners are the same) using 3
31
coordinates representing the x, y, and z distance from the center of the
32
Earth. A domain <type>earth</> over <type>cube</> is provided, which
33
includes constraint checks that the value meets these restrictions and
34
is reasonably close to the actual surface of the Earth.
38
The radius of the Earth is obtained from the <function>earth()</>
39
function. It is given in meters. But by changing this one function you can
40
change the module to use some other units, or to use a different value of
41
the radius that you feel is more appropriate.
45
This package has applications to astronomical databases as well.
46
Astronomers will probably want to change <function>earth()</> to return a
47
radius of <literal>180/pi()</> so that distances are in degrees.
51
Functions are provided to support input in latitude and longitude (in
52
degrees), to support output of latitude and longitude, to calculate
53
the great circle distance between two points and to easily specify a
54
bounding box usable for index searches.
58
The following functions are provided:
61
<table id="earthdistance-cube-functions">
62
<title>Cube-based Earthdistance Functions</title>
66
<entry>Function</entry>
67
<entry>Returns</entry>
68
<entry>Description</entry>
73
<entry><function>earth()</function></entry>
74
<entry><type>float8</type></entry>
75
<entry>Returns the assumed radius of the Earth.</entry>
78
<entry><function>sec_to_gc(float8)</function></entry>
79
<entry><type>float8</type></entry>
80
<entry>Converts the normal straight line
81
(secant) distance between between two points on the surface of the Earth
82
to the great circle distance between them.
86
<entry><function>gc_to_sec(float8)</function></entry>
87
<entry><type>float8</type></entry>
88
<entry>Converts the great circle distance between two points on the
89
surface of the Earth to the normal straight line (secant) distance
94
<entry><function>ll_to_earth(float8, float8)</function></entry>
95
<entry><type>earth</type></entry>
96
<entry>Returns the location of a point on the surface of the Earth given
97
its latitude (argument 1) and longitude (argument 2) in degrees.
101
<entry><function>latitude(earth)</function></entry>
102
<entry><type>float8</type></entry>
103
<entry>Returns the latitude in degrees of a point on the surface of the
108
<entry><function>longitude(earth)</function></entry>
109
<entry><type>float8</type></entry>
110
<entry>Returns the longitude in degrees of a point on the surface of the
115
<entry><function>earth_distance(earth, earth)</function></entry>
116
<entry><type>float8</type></entry>
117
<entry>Returns the great circle distance between two points on the
118
surface of the Earth.
122
<entry><function>earth_box(earth, float8)</function></entry>
123
<entry><type>cube</type></entry>
124
<entry>Returns a box suitable for an indexed search using the cube
126
operator for points within a given great circle distance of a location.
127
Some points in this box are further than the specified great circle
128
distance from the location, so a second check using
129
<function>earth_distance</> should be included in the query.
139
<title>Point-based Earth Distances</title>
142
The second part of the module relies on representing Earth locations as
143
values of type <type>point</>, in which the first component is taken to
144
represent longitude in degrees, and the second component is taken to
145
represent latitude in degrees. Points are taken as (longitude, latitude)
146
and not vice versa because longitude is closer to the intuitive idea of
147
x-axis and latitude to y-axis.
151
A single operator is provided:
154
<table id="earthdistance-point-operators">
155
<title>Point-based Earthdistance Operators</title>
159
<entry>Operator</entry>
160
<entry>Returns</entry>
161
<entry>Description</entry>
166
<entry><type>point</> <literal><@></literal> <type>point</></entry>
167
<entry><type>float8</type></entry>
168
<entry>Gives the distance in statute miles between
169
two points on the Earth's surface.
177
Note that unlike the <type>cube</>-based part of the module, units
178
are hardwired here: changing the <function>earth()</> function will
179
not affect the results of this operator.
183
One disadvantage of the longitude/latitude representation is that
184
you need to be careful about the edge conditions near the poles
185
and near +/- 180 degrees of longitude. The <type>cube</>-based
186
representation avoids these discontinuities.