Posted By

pauliehaha on 04/02/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

not_skeletor


Distance between points


 / Published in: SQL
 

  1. CREATE FUNCTION [dbo].[DistanceBetween] (@Lat1 AS real,
  2. @Long1 AS real, @Lat2 AS real, @Long2 AS real)
  3. RETURNS real
  4. AS
  5. BEGIN
  6. DECLARE @dLat1InRad AS float(53);
  7. SET @dLat1InRad = @Lat1 * (PI()/180.0);
  8. DECLARE @dLong1InRad AS float(53);
  9. SET @dLong1InRad = @Long1 * (PI()/180.0);
  10. DECLARE @dLat2InRad AS float(53);
  11. SET @dLat2InRad = @Lat2 * (PI()/180.0);
  12. DECLARE @dLong2InRad AS float(53);
  13. SET @dLong2InRad = @Long2 * (PI()/180.0);
  14. DECLARE @dLongitude AS float(53);
  15. SET @dLongitude = @dLong2InRad - @dLong1InRad;
  16. DECLARE @dLatitude AS float(53);
  17. SET @dLatitude = @dLat2InRad - @dLat1InRad;
  18. /* Intermediate result a. */
  19. DECLARE @a AS float(53);
  20. SET @a = SQUARE (SIN (@dLatitude / 2.0)) + COS (@dLat1InRad)
  21. * COS (@dLat2InRad)
  22. * SQUARE(SIN (@dLongitude / 2.0));
  23. /* Intermediate result c (great circle distance in Radians). */
  24. DECLARE @c AS real;
  25. SET @c = 2.0 * ATN2 (SQRT (@a), SQRT (1.0 - @a));
  26. DECLARE @kEarthRadius AS real;
  27. /* SET kEarthRadius = 3956.0 miles */
  28. SET @kEarthRadius = 6376.5; /* kms */
  29. DECLARE @dDistance AS real;
  30. SET @dDistance = @kEarthRadius * @c;
  31. RETURN (@dDistance);
  32. END

Report this snippet  

You need to login to post a comment.