]> granicus.if.org Git - postgresql/blob - doc/src/sgml/earthdistance.sgml
Allow the planner's estimate of the fraction of a cursor's rows that will be
[postgresql] / doc / src / sgml / earthdistance.sgml
1 <!-- $PostgreSQL: pgsql/doc/src/sgml/earthdistance.sgml,v 1.3 2007/12/06 04:12:10 tgl Exp $ -->
2
3 <sect1 id="earthdistance">
4  <title>earthdistance</title>
5
6  <indexterm zone="earthdistance">
7   <primary>earthdistance</primary>
8  </indexterm>
9
10  <para>
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</> package (which
14   <emphasis>must</> be installed before <filename>earthdistance</> can be
15   installed). The second one is based on the built-in <type>point</> datatype,
16   using longitude and latitude for the coordinates.
17  </para>
18
19  <para>
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></>
23   project.)
24  </para>
25
26  <sect2>
27   <title>Cube-based earth distances</title>
28
29   <para>
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.
35   </para>
36
37   <para>
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 appropiate.
42   </para>
43
44   <para>
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.
48   </para>
49
50   <para>
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.
55   </para>
56
57   <para>
58    The following functions are provided:
59   </para>
60
61   <table id="earthdistance-cube-functions">
62    <title>Cube-based earthdistance functions</title>
63    <tgroup cols="3">
64     <thead>
65      <row>
66       <entry>Function</entry>
67       <entry>Returns</entry>
68       <entry>Description</entry>
69      </row>
70     </thead>
71     <tbody>
72      <row>
73       <entry><function>earth()</function></entry>
74       <entry><type>float8</type></entry>
75       <entry>Returns the assumed radius of the Earth.</entry>
76      </row>
77      <row>
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.
83       </entry>
84      </row>
85      <row>
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
90        between them.
91       </entry>
92      </row>
93      <row>
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.
98       </entry>
99      </row>
100      <row>
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
104        Earth.
105       </entry>
106      </row>
107      <row>
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
111        Earth.
112       </entry>
113      </row>
114      <row>
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.
119       </entry>
120      </row>
121      <row>
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
125        <literal>@&gt;</>
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.
130       </entry>
131      </row>
132     </tbody>
133    </tgroup>
134   </table>
135
136  </sect2>
137
138  <sect2>
139   <title>Point-based earth distances</title>
140
141   <para>
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.
148   </para>
149
150   <para>
151    A single operator is provided:
152   </para>
153
154   <table id="earthdistance-point-operators">
155    <title>Point-based earthdistance operators</title>
156    <tgroup cols="3">
157     <thead>
158      <row>
159       <entry>Operator</entry>
160       <entry>Returns</entry>
161       <entry>Description</entry>
162      </row>
163     </thead>
164     <tbody>
165      <row>
166       <entry><type>point</> <literal>&lt;@&gt;</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.
170       </entry>
171      </row>
172     </tbody>
173    </tgroup>
174   </table>
175
176   <para>
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.
180   </para>
181
182   <para>
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.
187   </para>
188
189  </sect2>
190
191 </sect1>