相思资源网 Design By www.200059.com

postgresql计算两点距离

下面两种方法:

select 
ST_Distance(
 ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)::geography,
 ST_SetSRID(ST_MakePoint(106.00231199774656,29.719258550486572),4326)::geography
),
ST_Length(
 ST_MakeLine(
 ST_MakePoint(115.97166453999147,28.716493914230423),
 ST_MakePoint(106.00231199774656,29.719258550486572)
 )::geography
)

备注:

ST_GeomFromText('LINESTRING(115.97166453999147 28.716493914230423,106.00231199774656 29.719258550486572)')与
ST_MakeLine(
	ST_MakePoint(115.97166453999147,28.716493914230423),
	ST_MakePoint(106.00231199774656,29.719258550486572)
)等价
 
ST_GeomFromText('POINT(115.97166453999147 28.716493914230423)',4326)与
ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)等价
 
ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)::geography与
Geography(ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326))、
ST_GeographyFromText('SRID=4326;POINT(115.97166453999147 28.716493914230423)')等价
(::geography是postgis中的转换类型语法,把geometry转成geography)

补充: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;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

标签:
postgresql,计算两点距离

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“postgresql 计算两点距离的2种方法小结”

暂无postgresql 计算两点距离的2种方法小结的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。