postgresql计算两点欧式距离(经纬度地理位置)

create or replace function getdistance
(  
    lon1 numeric,
    lat1 numeric,  
    lon2 numeric,  
    lat2 numeric  
) 
 
returns int  
as  
$body$


declare

v_distance numeric;
v_earth_radius numeric;
radLat1 numeric;
radLat2 numeric;
v_radlatdiff numeric;
v_radlngdiff numeric;

begin  

    --地球半径
    v_earth_radius:=6378137;
     
    radLat1 := lat1 * pi()/180.0;
    radLat2   := lat2 * pi()/180.0;
    v_radlatdiff  := radLat1 - radLat2;
    v_radlngdiff  := lon1 * pi()/180.0 - lon2 * pi()/180.0;

    v_distance := 2 * asin(sqrt(power(sin(v_radlatdiff / 2), 2) + cos(radLat1) * cos(radLat2) * power(sin(v_radlngdiff/2),2)));
    v_distance := round(v_distance * v_earth_radius);
      
    return v_distance; 
end;
$body$
  
language 'plpgsql' volatile;
create or replace function getdistance
(  
    i_lngbegin real,
    i_latbegin real,  
    i_lngend real,  
    i_latend real  
) 
 
returns float  
as  
$body$
/*
 * 
 * select  getdistance_bygispoint(116.281524,39.957202,117.648673,38.42584)  as distance;
 * */

declare

v_distance real;
v_earth_radius real;
v_radlatbegin real;
v_radlatend real;
v_radlatdiff real;
v_radlngdiff real;

begin  

    --地球半径
    v_earth_radius:=6378.137;
     
    v_radlatbegin := i_latbegin * pi()/180.0;
    v_radlatend   := i_latend * pi()/180.0;
    v_radlatdiff  := v_radlatbegin - v_radlatend;
    v_radlngdiff  := i_lngbegin * pi()/180.0 - i_lngend * pi()/180.0;

    v_distance := 2 * asin(sqrt(power(sin(v_radlatdiff / 2), 2) + cos(v_radlatbegin) * cos(v_radlatend) * power(sin(v_radlngdiff/2),2)));
    v_distance := v_distance * v_earth_radius*1000;
      
    return v_distance; 
end;
$body$
  
language 'plpgsql' volatile;

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值