1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>F.14. earthdistance — calculate great-circle distances</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="dict-xsyn.html" title="F.13. dict_xsyn — example synonym full-text search dictionary" /><link rel="next" href="file-fdw.html" title="F.15. file_fdw — access data files in the server's file system" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">F.14. earthdistance — calculate great-circle distances</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="dict-xsyn.html" title="F.13. dict_xsyn — example synonym full-text search dictionary">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Up</a></td><th width="60%" align="center">Appendix F. Additional Supplied Modules and Extensions</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="file-fdw.html" title="F.15. file_fdw — access data files in the server's file system">Next</a></td></tr></table><hr /></div><div class="sect1" id="EARTHDISTANCE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">F.14. earthdistance — calculate great-circle distances <a href="#EARTHDISTANCE" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="earthdistance.html#EARTHDISTANCE-CUBE-BASED">F.14.1. Cube-Based Earth Distances</a></span></dt><dt><span class="sect2"><a href="earthdistance.html#EARTHDISTANCE-POINT-BASED">F.14.2. Point-Based Earth Distances</a></span></dt></dl></div><a id="id-1.11.7.24.2" class="indexterm"></a><p>
3 The <code class="filename">earthdistance</code> module provides two different approaches to
4 calculating great circle distances on the surface of the Earth. The one
5 described first depends on the <code class="filename">cube</code> module.
6 The second one is based on the built-in <code class="type">point</code> data type,
7 using longitude and latitude for the coordinates.
9 In this module, the Earth is assumed to be perfectly spherical.
10 (If that's too inaccurate for you, you might want to look at the
11 <span class="application"><a class="ulink" href="https://postgis.net/" target="_top">PostGIS</a></span>
14 The <code class="filename">cube</code> module must be installed
15 before <code class="filename">earthdistance</code> can be installed
16 (although you can use the <code class="literal">CASCADE</code> option
17 of <code class="command">CREATE EXTENSION</code> to install both in one command).
18 </p><div class="caution"><h3 class="title">Caution</h3><p>
19 It is strongly recommended that <code class="filename">earthdistance</code>
20 and <code class="filename">cube</code> be installed in the same schema, and that
21 that schema be one for which CREATE privilege has not been and will not
22 be granted to any untrusted users.
23 Otherwise there are installation-time security hazards
24 if <code class="filename">earthdistance</code>'s schema contains objects defined
26 Furthermore, when using <code class="filename">earthdistance</code>'s functions
27 after installation, the entire search path should contain only trusted
29 </p></div><div class="sect2" id="EARTHDISTANCE-CUBE-BASED"><div class="titlepage"><div><div><h3 class="title">F.14.1. Cube-Based Earth Distances <a href="#EARTHDISTANCE-CUBE-BASED" class="id_link">#</a></h3></div></div></div><p>
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 <a class="glossterm" href="glossary.html#GLOSSARY-DOMAIN"><em class="glossterm"><a class="glossterm" href="glossary.html#GLOSSARY-DOMAIN" title="Domain">domain</a></em></a>
33 <code class="type">earth</code> over type <code class="type">cube</code> is provided, which
34 includes constraint checks that the value meets these restrictions and
35 is reasonably close to the actual surface of the Earth.
37 The radius of the Earth is obtained from the <code class="function">earth()</code>
38 function. It is given in meters. But by changing this one function you can
39 change the module to use some other units, or to use a different value of
40 the radius that you feel is more appropriate.
42 This package has applications to astronomical databases as well.
43 Astronomers will probably want to change <code class="function">earth()</code> to return a
44 radius of <code class="literal">180/pi()</code> so that distances are in degrees.
46 Functions are provided to support input in latitude and longitude (in
47 degrees), to support output of latitude and longitude, to calculate
48 the great circle distance between two points and to easily specify a
49 bounding box usable for index searches.
51 The provided functions are shown
52 in <a class="xref" href="earthdistance.html#EARTHDISTANCE-CUBE-FUNCTIONS" title="Table F.4. Cube-Based Earthdistance Functions">Table F.4</a>.
53 </p><div class="table" id="EARTHDISTANCE-CUBE-FUNCTIONS"><p class="title"><strong>Table F.4. Cube-Based Earthdistance Functions</strong></p><div class="table-contents"><table class="table" summary="Cube-Based Earthdistance Functions" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
58 </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
59 <a id="id-1.11.7.24.7.7.2.2.1.1.1.1" class="indexterm"></a>
60 <code class="function">earth</code> ()
61 → <code class="returnvalue">float8</code>
64 Returns the assumed radius of the Earth.
65 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
66 <a id="id-1.11.7.24.7.7.2.2.2.1.1.1" class="indexterm"></a>
67 <code class="function">sec_to_gc</code> ( <code class="type">float8</code> )
68 → <code class="returnvalue">float8</code>
71 Converts the normal straight line
72 (secant) distance between two points on the surface of the Earth
73 to the great circle distance between them.
74 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
75 <a id="id-1.11.7.24.7.7.2.2.3.1.1.1" class="indexterm"></a>
76 <code class="function">gc_to_sec</code> ( <code class="type">float8</code> )
77 → <code class="returnvalue">float8</code>
80 Converts the great circle distance between two points on the
81 surface of the Earth to the normal straight line (secant) distance
83 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
84 <a id="id-1.11.7.24.7.7.2.2.4.1.1.1" class="indexterm"></a>
85 <code class="function">ll_to_earth</code> ( <code class="type">float8</code>, <code class="type">float8</code> )
86 → <code class="returnvalue">earth</code>
89 Returns the location of a point on the surface of the Earth given
90 its latitude (argument 1) and longitude (argument 2) in degrees.
91 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
92 <a id="id-1.11.7.24.7.7.2.2.5.1.1.1" class="indexterm"></a>
93 <code class="function">latitude</code> ( <code class="type">earth</code> )
94 → <code class="returnvalue">float8</code>
97 Returns the latitude in degrees of a point on the surface of the
99 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
100 <a id="id-1.11.7.24.7.7.2.2.6.1.1.1" class="indexterm"></a>
101 <code class="function">longitude</code> ( <code class="type">earth</code> )
102 → <code class="returnvalue">float8</code>
105 Returns the longitude in degrees of a point on the surface of the
107 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
108 <a id="id-1.11.7.24.7.7.2.2.7.1.1.1" class="indexterm"></a>
109 <code class="function">earth_distance</code> ( <code class="type">earth</code>, <code class="type">earth</code> )
110 → <code class="returnvalue">float8</code>
113 Returns the great circle distance between two points on the
114 surface of the Earth.
115 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
116 <a id="id-1.11.7.24.7.7.2.2.8.1.1.1" class="indexterm"></a>
117 <code class="function">earth_box</code> ( <code class="type">earth</code>, <code class="type">float8</code> )
118 → <code class="returnvalue">cube</code>
121 Returns a box suitable for an indexed search using the <code class="type">cube</code>
122 <code class="literal">@></code>
123 operator for points within a given great circle distance of a location.
124 Some points in this box are further than the specified great circle
125 distance from the location, so a second check using
126 <code class="function">earth_distance</code> should be included in the query.
127 </p></td></tr></tbody></table></div></div><br class="table-break" /></div><div class="sect2" id="EARTHDISTANCE-POINT-BASED"><div class="titlepage"><div><div><h3 class="title">F.14.2. Point-Based Earth Distances <a href="#EARTHDISTANCE-POINT-BASED" class="id_link">#</a></h3></div></div></div><p>
128 The second part of the module relies on representing Earth locations as
129 values of type <code class="type">point</code>, in which the first component is taken to
130 represent longitude in degrees, and the second component is taken to
131 represent latitude in degrees. Points are taken as (longitude, latitude)
132 and not vice versa because longitude is closer to the intuitive idea of
133 x-axis and latitude to y-axis.
135 A single operator is provided, shown
136 in <a class="xref" href="earthdistance.html#EARTHDISTANCE-POINT-OPERATORS" title="Table F.5. Point-Based Earthdistance Operators">Table F.5</a>.
137 </p><div class="table" id="EARTHDISTANCE-POINT-OPERATORS"><p class="title"><strong>Table F.5. Point-Based Earthdistance Operators</strong></p><div class="table-contents"><table class="table" summary="Point-Based Earthdistance Operators" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
142 </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
143 <code class="type">point</code> <code class="literal"><@></code> <code class="type">point</code>
144 → <code class="returnvalue">float8</code>
147 Computes the distance in statute miles between
148 two points on the Earth's surface.
149 </p></td></tr></tbody></table></div></div><br class="table-break" /><p>
150 Note that unlike the <code class="type">cube</code>-based part of the module, units
151 are hardwired here: changing the <code class="function">earth()</code> function will
152 not affect the results of this operator.
154 One disadvantage of the longitude/latitude representation is that
155 you need to be careful about the edge conditions near the poles
156 and near +/- 180 degrees of longitude. The <code class="type">cube</code>-based
157 representation avoids these discontinuities.
158 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dict-xsyn.html" title="F.13. dict_xsyn — example synonym full-text search dictionary">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="file-fdw.html" title="F.15. file_fdw — access data files in the server's file system">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.13. dict_xsyn — example synonym full-text search dictionary </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> F.15. file_fdw — access data files in the server's file system</td></tr></table></div></body></html>