Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
F.18. earthdistance — calculate great-circle distances
Prev UpAppendix F. Additional Supplied Modules and Extensions Shipped inpostgrespro-std-17-contribHome Next

F.18. earthdistance — calculate great-circle distances#

Theearthdistance module provides two different approaches to calculating great circle distances on the surface of the Earth. The one described first depends on thecube module. The second one is based on the built-inpoint data type, using longitude and latitude for the coordinates.

In this module, the Earth is assumed to be perfectly spherical. (If that's too inaccurate for you, you might want to look at thePostGIS project.)

Thecube module must be installed beforeearthdistance can be installed (although you can use theCASCADE option ofCREATE EXTENSION to install both in one command).

Caution

It is strongly recommended thatearthdistance andcube be installed in the same schema, and that that schema be one for which CREATE privilege has not been and will not be granted to any untrusted users. Otherwise there are installation-time security hazards ifearthdistance's schema contains objects defined by a hostile user. Furthermore, when usingearthdistance's functions after installation, the entire search path should contain only trusted schemas.

F.18.1. 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. Adomainearth over typecube 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 theearth() 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 changeearth() to return a radius of180/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 inTable F.8.

Table F.8. Cube-Based Earthdistance 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 thecube@> 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 usingearth_distance should be included in the query.


F.18.2. Point-Based Earth Distances#

The second part of the module relies on representing Earth locations as values of typepoint, in which the first component is taken to represent longitude in degrees, and the second component is taken to represent latitude in degrees. Points are taken as (longitude, latitude) and not vice versa because longitude is closer to the intuitive idea of x-axis and latitude to y-axis.

A single operator is provided, shown inTable F.9.

Table F.9. Point-Based Earthdistance Operators

Operator

Description

point<@>pointfloat8

Computes the distance in statute miles between two points on the Earth's surface.


Note that unlike thecube-based part of the module, units are hardwired here: changing theearth() function will not affect the results of this operator.

One disadvantage of the longitude/latitude representation is that you need to be careful about the edge conditions near the poles and near +/- 180 degrees of longitude. Thecube-based representation avoids these discontinuities.


Prev Up Next
F.17. dump_stat — functions to backup and recover thepg_statistic table Home F.19. fasttrun — a transaction unsafe function to truncate temporary tables
pdfepub
Go to Postgres Pro Standard 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp