RefLib

Reference library for AI

Back

postgresql_16:9.6.15.1 Cube-Based Earth Distances

PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > earthdistance — calculate great-circle distances > Cube-Based Earth Distances

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 domain earth over type cube is provided, which includes constraint checks that the value meets these restrictions and is reasonably close to the actual surface of the Earth.

The radius of the Earth is obtained from the earth() 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.

This package has applications to astronomical databases as well. Astronomers will probably want to change earth() to return a radius of 180/pi() so that distances are in degrees.

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.

The provided functions are shown in (see earthdistance-cube-functions).

| Function Description | | --- | | earth () float8 Returns the assumed radius of the Earth. | | sec_to_gc ( float8 ) float8 Converts the normal straight line (secant) distance between two points on the surface of the Earth to the great circle distance between them. | | gc_to_sec ( float8 ) float8 Converts the great circle distance between two points on the surface of the Earth to the normal straight line (secant) distance between them. | | ll_to_earth ( float8, float8 ) earth Returns the location of a point on the surface of the Earth given its latitude (argument 1) and longitude (argument 2) in degrees. | | latitude ( earth ) float8 Returns the latitude in degrees of a point on the surface of the Earth. | | longitude ( earth ) float8 Returns the longitude in degrees of a point on the surface of the Earth. | | earth_distance ( earth, earth ) float8 Returns the great circle distance between two points on the surface of the Earth. | | earth_box ( earth, float8 ) cube Returns a box suitable for an indexed search using the cube @> operator for points within a given great circle distance of a location. Some points in this box are further than the specified great circle distance from the location, so a second check using earth_distance should be included in the query. |

Back