codekingpro/portable-devtools
114k
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.15. 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.14. dict_xsyn — example synonym full-text search dictionary" /><link rel="next" href="file-fdw.html" title="F.16. 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.15. earthdistance — calculate great-circle distances</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="dict-xsyn.html" title="F.14. 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 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="file-fdw.html" title="F.16. 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.15. 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.15.1. Cube-Based Earth Distances</a></span></dt><dt><span class="sect2"><a href="earthdistance.html#EARTHDISTANCE-POINT-BASED">F.15.2. Point-Based Earth Distances</a></span></dt></dl></div><a id="id-1.11.7.25.2" class="indexterm"></a><p>3 The <code class="filename">earthdistance</code> module provides two different approaches to4 calculating great circle distances on the surface of the Earth. The one5 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.8 </p><p>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 the11 <span class="application"><a class="ulink" href="https://postgis.net/" target="_top">PostGIS</a></span>12 project.)13 </p><p>14 The <code class="filename">cube</code> module must be installed15 before <code class="filename">earthdistance</code> can be installed16 (although you can use the <code class="literal">CASCADE</code> option17 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 that21 that schema be one for which CREATE privilege has not been and will not22 be granted to any untrusted users.23 Otherwise there are installation-time security hazards24 if <code class="filename">earthdistance</code>'s schema contains objects defined25 by a hostile user.26 Furthermore, when using <code class="filename">earthdistance</code>'s functions27 after installation, the entire search path should contain only trusted28 schemas.29 </p></div><div class="sect2" id="EARTHDISTANCE-CUBE-BASED"><div class="titlepage"><div><div><h3 class="title">F.15.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 331 coordinates representing the x, y, and z distance from the center of the32 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, which34 includes constraint checks that the value meets these restrictions and35 is reasonably close to the actual surface of the Earth.36 </p><p>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 can39 change the module to use some other units, or to use a different value of40 the radius that you feel is more appropriate.41 </p><p>42 This package has applications to astronomical databases as well.43 Astronomers will probably want to change <code class="function">earth()</code> to return a44 radius of <code class="literal">180/pi()</code> so that distances are in degrees.45 </p><p>46 Functions are provided to support input in latitude and longitude (in47 degrees), to support output of latitude and longitude, to calculate48 the great circle distance between two points and to easily specify a49 bounding box usable for index searches.50 </p><p>51 The provided functions are shown52 in <a class="xref" href="earthdistance.html#EARTHDISTANCE-CUBE-FUNCTIONS" title="Table F.5. Cube-Based Earthdistance Functions">Table F.5</a>.53 </p><div class="table" id="EARTHDISTANCE-CUBE-FUNCTIONS"><p class="title"><strong>Table F.5. 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">54 Function55 </p>56 <p>57 Description58 </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">59 <a id="id-1.11.7.25.7.7.2.2.1.1.1.1" class="indexterm"></a>60 <code class="function">earth</code> ()61 → <code class="returnvalue">float8</code>62 </p>63 <p>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.25.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>69 </p>70 <p>71 Converts the normal straight line72 (secant) distance between two points on the surface of the Earth73 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.25.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>78 </p>79 <p>80 Converts the great circle distance between two points on the81 surface of the Earth to the normal straight line (secant) distance82 between them.83 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">84 <a id="id-1.11.7.25.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>87 </p>88 <p>89 Returns the location of a point on the surface of the Earth given90 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.25.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>95 </p>96 <p>97 Returns the latitude in degrees of a point on the surface of the98 Earth.99 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">100 <a id="id-1.11.7.25.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>103 </p>104 <p>105 Returns the longitude in degrees of a point on the surface of the106 Earth.107 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">108 <a id="id-1.11.7.25.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>111 </p>112 <p>113 Returns the great circle distance between two points on the114 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.25.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>119 </p>120 <p>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 circle125 distance from the location, so a second check using126 <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.15.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 as129 values of type <code class="type">point</code>, in which the first component is taken to130 represent longitude in degrees, and the second component is taken to131 represent latitude in degrees. Points are taken as (longitude, latitude)132 and not vice versa because longitude is closer to the intuitive idea of133 x-axis and latitude to y-axis.134 </p><p>135 A single operator is provided, shown136 in <a class="xref" href="earthdistance.html#EARTHDISTANCE-POINT-OPERATORS" title="Table F.6. Point-Based Earthdistance Operators">Table F.6</a>.137 </p><div class="table" id="EARTHDISTANCE-POINT-OPERATORS"><p class="title"><strong>Table F.6. 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">138 Operator139 </p>140 <p>141 Description142 </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>145 </p>146 <p>147 Computes the distance in statute miles between148 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, units151 are hardwired here: changing the <code class="function">earth()</code> function will152 not affect the results of this operator.153 </p><p>154 One disadvantage of the longitude/latitude representation is that155 you need to be careful about the edge conditions near the poles156 and near +/- 180 degrees of longitude. The <code class="type">cube</code>-based157 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.14. 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.16. file_fdw — access data files in the server's file system">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.14. dict_xsyn — example synonym full-text search dictionary </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> F.16. file_fdw — access data files in the server's file system</td></tr></table></div></body></html>