Advertisement
Guest User

Untitled

a guest
May 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.91 KB | None | 0 0
  1. CREATE FUNCTION CalculateDistance
  2. (@StartLongitude float,
  3.  @StartLatitude float,
  4.  @EndLongitude float,
  5.  @EndLatitude float)
  6.  
  7. RETURNS decimal(7,1)
  8.  
  9. AS
  10.  
  11. BEGIN
  12.     DECLARE @lat1 float
  13.     DECLARE @lat2 float
  14.     DECLARE @long1 float
  15.     DECLARE @long2 float
  16.     DECLARE @LongDistance float
  17.     DECLARE @LatDistance float
  18.     DECLARE @EarthRadius smallint
  19.     DECLARE @ChordLength float
  20.     DECLARE @CentralAngle float
  21.    
  22.     SET @lat1 = RADIANS(@StartLatitude)
  23.     SET @lat2 = RADIANS(@EndLatitude)
  24.     SET @long1 = RADIANS(@StartLongitude)
  25.     SET @long2 = RADIANS(@EndLongitude)
  26.     SET @LongDistance = @long2 - @long1
  27.     SET @LatDistance = @lat2 - @lat1
  28.     SET @EarthRadius = 3959
  29.    
  30.     -- Calucate Chord Length
  31.     SET @ChordLength = SQUARE(SIN(@LatDistance /2)) + COS(@lat1) * COS(@lat2) * SQUARE(SIN(@LongDistance /2))
  32.     SET @CentralAngle = 2 * ATN2(SQUARE(@ChordLength),SQUARE(1-@ChordLength))
  33.    
  34.     RETURN ROUND(@CentralAngle * @EarthRadius, 1)
  35. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement