-->

科技资讯

您的当前位置:首页 > 资讯 > postgresql

postgresql 计算距离

2019-01-04 09:27:23  来源:admin 点击:1014

转载至:https://blog.csdn.net/duanmuxiao/article/details/45667221

之前用的是ST_Distance 函数,但是貌似需要进行一次单位的转换,而且网上有说那种转换不是特别准确,现在暂时将该算法记录在此:

select st_distance(ST_GeomFromText('POINT(120.451737 36.520975)',900913),ST_GeomFromText('POINT(120.455636 36.520885)',900913))*60*1.852;

这里的计算方式倒是可以换坐标系,但是,测试了两个坐标系都没有起作用。而且该种方式转换过单位后跟arcgis计算出的结果相差甚远,最终决定使用下面的方式;

今天发现了另外一种方式来计算距离,这种方式可以直接生成单位为米的结果:

select ST_Length(Geography(ST_GeomFromText('LINESTRING(120.451737 36.520975,120.455636 36.520885)')));

这种方式的不便在于:

1.要把点转换成线或者其他的图形而不是点;

2.geography函数现在只支持4326坐标系,不能换成其他的。


追加:

上面的方式是计算点到点的距离,但是如果要想知道某一个点到某条线的距离是不是在某个范围内,又该如何计算呢;如下:

select ST_Contains(St_Astext(ST_Buffer(geography(geomfromtext('MULTILINESTRING((线的坐标点))')),25.00{以米为单位的距离})),st_astext(geography(geomfromtext('POINT(121.37805 37.54142)')))) as result

使用类似上面的方式,就可以输入以米为单位的距离判断某个点是否在某个距离范围内;

1实例求最近距离

$coordinate = 'POINT('.$long.' '.$lat.')';$zfxkm = DB::select("select id,name,
st_distance(ST_GeomFromText('".$coordinate."',900913),ST_GeomFromText
(ST_AsText(coordinate),900913))*60*1.852 as res from 表 where 字段= '".$字段."' order by res;");
$data = $zfxkm[0];

SELECT events.id events.name, eaerthdiatance(ll_to_earth({currentuserlat}, {currentuserlng}), llto_earth(events.lat, events.lng)) 

as distancefromcurrentlocation FROM events 

ORDER BY distancefromcurretnlocation ASC;

2找到某个半径范围内的记录:

SELECT events.id, events.name FROM events 

WHERE earth_box({currentuserlat}, {currentuserlng}, {radiusinmetres}) @> ll_to_earth(events.lat, events.lng);

3实例e:


 
/*创建表*/
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;
 
/*对经纬度列创建索引*/
CREATE INDEX ll_idx on picture USING gist(ll_to_earth(lat, lng));
 
/*根据半径(1000米)选择记录*/
SELECT * FROM picture where earth_box(ll_to_earth(40.059286,116.418773),1000) @> ll_to_earth(picture.lat, picture.lng); 
 
/*选择距离当前用户的距离*/
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;
 
/*
 * 以下内容是网上的一篇教程
 * 地址: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.把直线距离转成大圆距离
--
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)




相关文章更多 >

© 2024版权所有 水水网

电话:171780793