当前位置:主页 > 查看内容

数据库实用脚本:计算地球上两个坐标点之间的里程

发布时间:2021-04-16 00:00| 位朋友查看

简介:今天给大家分享计算地球上两个坐标点之间里程不同数据库版本的脚本。 1、SQLServer脚本 -计算地球上两个坐标点(经度,纬度)之间距离sql函数 CREATE FUNCTION [dbo].[fnGetDistance](@LatBegin REAL ,@LngBegin REAL , @LatEnd REAL ,@LngEnd REAL ) RETURN……

今天给大家分享计算地球上两个坐标点之间里程不同数据库版本的脚本。

1、SQLServer脚本

  1. –-计算地球上两个坐标点(经度,纬度)之间距离sql函数  
  2. CREATE FUNCTION [dbo].[fnGetDistance](@LatBegin REAL, @LngBegin REAL,  
  3.                                       @LatEnd REAL, @LngEnd REAL
  4. RETURNS FLOAT 
  5. AS 
  6. BEGIN 
  7. –-距离(千米) 
  8. DECLARE @Distance REAL 
  9. DECLARE @EARTH_RADIUS REAL 
  10. SET @EARTH_RADIUS = 6378.137 
  11. DECLARE @RadLatBegin REAL,@RadLatEnd REAL,@RadLatDiff REAL,@RadLngDiff REAL 
  12. SET @RadLatBegin = @LatBegin *PI()/180.0 
  13. SET @RadLatEnd = @LatEnd *PI()/180.0 
  14. SET @RadLatDiff = @RadLatBegin - @RadLatEnd 
  15. SET @RadLngDiff = @LngBegin *PI()/180.0 - @LngEnd *PI()/180.0 
  16. SET @Distance = 2 *ASIN(SQRT(POWER(SIN(@RadLatDiff/2), 2) 
  17.                              +COS(@RadLatBegin)*COS(@RadLatEnd)*POWER(SIN(@RadLngDiff/2), 2))) 
  18. SET @Distance = @Distance * @EARTH_RADIUS 
  19. RETURN @Distance 
  20. END 
  21. --使用方法如下: 
  22. SELECT dbo.fnGetDistance(25,30,12.56,15.5) ; 

2、MySQl脚本

  1. –-计算地球上两个坐标点(经度,纬度)之间距离sql函数  
  2. CREATE FUNCTION [dbo].[fnGetDistance](@LatBegin REAL, @LngBegin REAL
  3.                                       @LatEnd REAL, @LngEnd REAL
  4. RETURNS FLOAT 
  5. AS 
  6. BEGIN 
  7. –-距离(千米) 
  8. DECLARE @Distance REAL 
  9. DECLARE @EARTH_RADIUS REAL 
  10. SET @EARTH_RADIUS = 6378.137 
  11. DECLARE @RadLatBegin REAL,@RadLatEnd REAL,@RadLatDiff REAL,@RadLngDiff REAL 
  12. SET @RadLatBegin = @LatBegin *PI()/180.0 
  13. SET @RadLatEnd = @LatEnd *PI()/180.0 
  14. SET @RadLatDiff = @RadLatBegin - @RadLatEnd 
  15. SET @RadLngDiff = @LngBegin *PI()/180.0 - @LngEnd *PI()/180.0 
  16. SET @Distance = 2 *ASIN(SQRT(POWER(SIN(@RadLatDiff/2), 2) 
  17.                              +COS(@RadLatBegin)*COS(@RadLatEnd)*POWER(SIN(@RadLngDiff/2), 2))) 
  18. SET @Distance = @Distance * @EARTH_RADIUS 
  19. RETURN @Distance 
  20. END 
  21. --使用方法如下: 
  22. SELECT dbo.fnGetDistance(25,30,12.56,15.5) ; 

3、Orcale脚本

  1. CREATE OR REPLACE FUNCTION GetDistance 
  2. (lat1 number, lng1 number,lat2 number,lng2 number)  
  3. RETURN NUMBER is  
  4.   earth_padius number := 6378.137;  
  5.   radLat1      number := Radian(lat1);  
  6.   radLat2      number := Radian(lat2);  
  7.   a            number := radLat1 - radLat2;  
  8.   b            number := Radian(lng1) - Radian(lng2);  
  9.   s            number := 0;  
  10. begin  
  11.   s := 2 *  
  12.        Asin(Sqrt(power(sin(a / 2), 2) +  
  13.                  cos(radLat1) * cos(radLat2) * power(sin(b / 2), 2)));  
  14.   s := s * earth_padius;  
  15.   s := Round(s * 10000) / 10000;  
  16.   return s;  
  17. end;  
  18. --使用方法 
  19. select GetDistance(25,30,12.56,15.5) from dual 

本文转载自网络,原文链接:https://www.toutiao.com/i6915763062846915079/
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐