項目中用到了postgreSQL中的earthdistance()函數(shù)功能計算地球上兩點之間的距離,中文的資料太少了,我找到了一篇 英文的、講的很好的文章 ,特此翻譯,希望能夠幫助到以后用到earthdistance的同學(xué)。
做一個GEO應(yīng)用從來都不是一件容易的事。但是用一些身邊的開源項目就可以在幾分鐘內(nèi)輕松解決這個問題。 PostgreSQL有許多特性。是我的首選,它能夠把數(shù)據(jù)庫平臺提升到另一個層次。
這是件非常明顯的事。服務(wù)器存儲了所有的數(shù)據(jù),服務(wù)器拓展是用C/C++實現(xiàn)的,非??臁閿?shù)據(jù)表做索引也能加快計算速度。
計算2個坐標(biāo)之間的距離,我們要用到 earthdistance(lltoearth($latlngcube), lltoearth($latlng_cube)) 這個函數(shù)。 earthdistance()函數(shù)接受2組坐標(biāo)值,返回值一個以米為單位的的數(shù)值。
【譯者注】大圓距離(Great circle disstance)指的是從球面的一點A出發(fā)到達球面上另一點B,所經(jīng)過的最短路徑的長度。一般說來,球面上任意兩點A和B都可以與球心確定唯一的大圓,這個大圓被稱為黎曼圓,而在大圓上連接這兩點的較短的一條弧的長度就是大圓距離。如果想了解更多,請看wiki: 大圓距離
SELECT events.id, events.name FROM events WHERE earthbox({currentuserlat}, {currentuserlng}, {radiusinmetres}) @> llto_earth(events.lat, events.lng);
你可能會發(fā)現(xiàn)上面的查詢有不小的開銷。以我的經(jīng)驗,最好對一些字段建立索引。 (下面這條語句假定你又events表, 同時events表有字段lat和lng)
CREATE INDEX ${nameofindex} on events USING gits(lltoearth(lat, lng));
七、數(shù)據(jù)類型
我的應(yīng)用比較簡單,所以我把經(jīng)緯度(lat和lng)都設(shè)成了double類型。這使得我用Node.js開發(fā)起來更加快速,而不用再去自己定制針對GIST類型的解決方案。
/*
* postgreSQL之earthdistance學(xué)習(xí)筆記
* author: wusuopubupt
* date: 2013-03-31
*/
/*創(chuàng)建表*/
CREATE TABLE picture (
id serial PRIMARY KEY ,
p_uid char(12) NOT NULL,
p_key char(23) NOT NULL,
lat real not null,
lng real NOT NULL,
up int NOT NULL,
down int NOT NULL,
ip varchar(15) DEFAULT NULL,
address varchar(256) DEFAULT NULL
);
/*插入記錄*/
INSERT INTO picture(p_uid, p_key, lat, lng, up, down, ip, address)
VALUES('aaaabbbbcccc', '2014032008164023279.png', 40.043945, 116.413668, 0, 0, '', '');
/*插入記錄*/
INSERT INTO picture(p_uid, p_key, lat, lng, up, down, ip, address)
VALUES('xxxxccccmmmm', '2014032008164023111.png', 40.067183, 116.415230, 0, 0, '', '');
/*選擇記錄*/
SELECT * FROM picture;
/*更新記錄*/
UPDATE picture SET address='LiShuiqiao' WHERE id=1;
UPDATE picture SET address='TianTongyuan' WHERE id=2;
/*對經(jīng)緯度列創(chuàng)建索引*/
CREATE INDEX ll_idx on picture USING gist(ll_to_earth(lat, lng));
/*根據(jù)半徑(1000米)選擇記錄*/
SELECT * FROM picture where earth_box(ll_to_earth(40.059286,116.418773),1000) @> ll_to_earth(picture.lat, picture.lng);
/*選擇距離當(dāng)前用戶的距離*/
SELECT picture.id, earth_distance(ll_to_earth(picture.lat, picture.lng), ll_to_earth(40.059286,116.418773))
AS dis FROM picture
ORDER BY dis ASC;
/*
* 以下內(nèi)容是網(wǎng)上的一篇教程
* 地址:http://www.cse.iitb.ac.in/dbms/Data/Courses/CS631/PostgreSQL-Resources/postgresql-9.2.4/contrib/earthdistance/expected/earthdistance.out
*/
--
-- Test earthdistance extension
--
-- In this file we also do some testing of extension create/drop scenarios.
-- That's really exercising the core database's dependency logic, so ideally
-- we'd do it in the core regression tests, but we can't for lack of suitable
-- guaranteed-available extensions. earthdistance is a good test case because
-- it has a dependency on the cube extension.
--
CREATE EXTENSION earthdistance; -- fail, must install cube first
ERROR: required extension "cube" is not installed
CREATE EXTENSION cube;
CREATE EXTENSION earthdistance;
--
-- The radius of the Earth we are using.
--
SELECT earth()::numeric(20,5);
earth
---------------
6378168.00000
(1 row)
--
-- Convert straight line distances to great circle distances.把直線距離轉(zhuǎn)成大圓距離
--
SELECT (pi()*earth())::numeric(20,5);
numeric
----------------
20037605.73216
(1 row)
SELECT sec_to_gc(0)::numeric(20,5);
sec_to_gc
-----------
0.00000
(1 row)
--
-- Convert great circle distances to straight line distances.
--
SELECT gc_to_sec(0)::numeric(20,5);
gc_to_sec
-----------
0.00000
(1 row)
SELECT gc_to_sec(sec_to_gc(2*earth()))::numeric(20,5);
gc_to_sec
----------------
12756336.00000
(1 row)
--
-- Set coordinates using latitude and longitude.
-- Extract each coordinate separately so we can round them.
--
SELECT cube_ll_coord(ll_to_earth(0,0),1)::numeric(20,5),
cube_ll_coord(ll_to_earth(0,0),2)::numeric(20,5),
cube_ll_coord(ll_to_earth(0,0),3)::numeric(20,5);
cube_ll_coord | cube_ll_coord | cube_ll_coord
---------------+---------------+---------------
6378168.00000 | 0.00000 | 0.00000
(1 row)
SELECT cube_ll_coord(ll_to_earth(360,360),1)::numeric(20,5),
cube_ll_coord(ll_to_earth(360,360),2)::numeric(20,5),
cube_ll_coord(ll_to_earth(360,360),3)::numeric(20,5);
cube_ll_coord | cube_ll_coord | cube_ll_coord
---------------+---------------+---------------
6378168.00000 | 0.00000 | 0.00000
(1 row)
--
-- Test getting the latitude of a location.
--
SELECT latitude(ll_to_earth(0,0))::numeric(20,10);
latitude
--------------
0.0000000000
(1 row)
SELECT latitude(ll_to_earth(45,0))::numeric(20,10);
latitude
---------------
45.0000000000
(1 row)
--
-- Test getting the longitude of a location.
--
SELECT longitude(ll_to_earth(0,0))::numeric(20,10);
longitude
--------------
0.0000000000
(1 row)
SELECT longitude(ll_to_earth(45,0))::numeric(20,10);
longitude
--------------
0.0000000000
(1 row)
--
-- For the distance tests the following is some real life data.
--
-- Chicago has a latitude of 41.8 and a longitude of 87.6.
-- Albuquerque has a latitude of 35.1 and a longitude of 106.7.
-- (Note that latitude and longitude are specified differently
-- in the cube based functions than for the point based functions.)
--
--
-- Test getting the distance between two points using earth_distance.
--
SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,0))::numeric(20,5);
earth_distance
----------------
0.00000
(1 row)
SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,180))::numeric(20,5);
earth_distance
----------------
20037605.73216
(1 row)
--
-- Test getting the distance between two points using geo_distance.
--
SELECT geo_distance('(0,0)'::point,'(0,0)'::point)::numeric(20,5);
geo_distance
--------------
0.00000
(1 row)
SELECT geo_distance('(0,0)'::point,'(180,0)'::point)::numeric(20,5);
geo_distance
--------------
12436.77274
(1 row)
--
-- Test getting the distance between two points using the @> operator.
--
SELECT ('(0,0)'::point @> '(0,0)'::point)::numeric(20,5);
numeric
---------
0.00000
(1 row)
SELECT ('(0,0)'::point @> '(180,0)'::point)::numeric(20,5);
numeric
-------------
12436.77274
(1 row)
--
-- Test for points that should be in bounding boxes.
--
SELECT earth_box(ll_to_earth(0,0),
earth_distance(ll_to_earth(0,0),ll_to_earth(0,1))*1.00001) @>
ll_to_earth(0,1);
?column?
----------
t
(1 row)
SELECT earth_box(ll_to_earth(0,0),
earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.1))*1.00001) @>
ll_to_earth(0,0.1);
?column?
----------
t
(1 row)
--
-- Test for points that shouldn't be in bounding boxes. Note that we need
-- to make points way outside, since some points close may be in the box
-- but further away than the distance we are testing.
--
SELECT earth_box(ll_to_earth(0,0),
earth_distance(ll_to_earth(0,0),ll_to_earth(0,1))*.57735) @>
ll_to_earth(0,1);
?column?
----------
f
(1 row)
SELECT earth_box(ll_to_earth(0,0),
earth_distance(ll_to_earth(0,0),ll_to_earth(0,0.1))*.57735) @>
ll_to_earth(0,0.1);
?column?
----------
f
(1 row)