×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
1
Language: SQL
Posted by: Hariharan Radhakrishnan
Added: Nov 14, 2011 4:53 AM
Views: 46
Tags: no tags
This routine calculates the distance between two points (given the
latitude/longitude of those points). It is being used to calculate
the distance between two locations using GeoDataSource (TM) prodducts
Calculate distance between two points lat1,lon1 and lat2,lon2
Uses radius of earth in kilometers or miles as an argurments
  1. --
  2. 02
  3.  -- This routine calculates the distance between two points (given the
  4. 03
  5.  -- latitude/longitude of those points). It is being used to calculate
  6. 04
  7.  -- the distance between two locations using GeoDataSource (TM) prodducts
  8. 05
  9.  --
  10. 06
  11.  -- Calculate distance between two points lat1,lon1 and lat2,lon2
  12. 07
  13.  -- Uses radius of earth in kilometers or miles as an argurments
  14. 08
  15.  --
  16. 09
  17.  -- Typical radius:  3963.0 (miles) (Default if no value specified)
  18. 10
  19.  --                  6387.7 (km)
  20. 11
  21.  --
  22. 12
  23.  -- Note: NVL function is used on all variables to replace NULL values with 0 (zero).
  24. 13
  25.  --
  26. 14
  27.  -- For enquiries, please contact sales@geodatasource.com
  28. 15
  29.  -- Official Web site: http://www.geodatasource.com
  30. 16
  31.  --
  32. 17
  33.  -- Thanks to Bill Dykstra for contributing the source code.
  34. 18
  35.  --
  36. 19
  37.  -- GeoDataSource.com (C) All Rights Reserved 2011
  38. 20
  39.  --
  40. 21
  41.  
  42. 22
  43.  
  44. 23
  45. CREATE OR REPLACE FUNCTION distance (Lat1 IN NUMBER,
  46. 24
  47.                                      Lon1 IN NUMBER,
  48. 25
  49.                                      Lat2 IN NUMBER,
  50. 26
  51.                                      Lon2 IN NUMBER,
  52. 27
  53.                                      Radius IN NUMBER DEFAULT 3963) RETURN NUMBER IS
  54. 28
  55.  -- Convert degrees to radians
  56. 29
  57.  DegToRad NUMBER := 57.29577951;
  58. 30
  59.  
  60. 31
  61. BEGIN
  62. 32
  63.   RETURN(NVL(Radius,0) * ACOS((sin(NVL(Lat1,0) / DegToRad) * SIN(NVL(Lat2,0) / DegToRad)) +
  64. 33
  65.         (COS(NVL(Lat1,0) / DegToRad) * COS(NVL(Lat2,0) / DegToRad) *
  66. 34
  67.          COS(NVL(Lon2,0) / DegToRad - NVL(Lon1,0)/ DegToRad))));
  68. 35
  69. END;
  70. 36
  71. .
  72. 37
  73. RUN
  74.