Introduction

Calculation of the great-circle (orthodromic) distance between two geo-points on the Earth surface is one of the core Geographic Information System (GIS) problems. This seemingly trivial task requires quite non-trivial algorithmic solution. Indeed, should the problem pertains to the plane geometry, then Pythagorean Theorem will provide a simple solution. But the actual GIS computations are dealing with 3d-models, namely spherical Earth representation, which requires more elaborate solution. Another level of complexity relates to more accurate ellipsoidal Earth model, which is sort of "overkill" for the majority of practical application. Spherical model results in systemic error margin within 0.3% which is acceptable for most commercial-grade apps. The second one (i.e. ellipsoidal model of the Earth ) theoretically limits error margin to the fraction of mm while dramatically increasing the computational complexity. The following solution is based on the spherical Earth model, describing 3 practical algorithms written in C#, which differ by the computational performance/accuracy.

Background

Mathematically speaking, all three algos described below result in computation of a great-circle (orthodromic) distance on Earth between 2 points, though the accuracy and performance are different. They all are based on spherical model of the Earth and provide reasonably good approximation with error margin typically not exceeding couple meters within NY City boundaries. More accurate ellipsoidal Earth model and corresponding high-accuracy Vincenty’s solution exists reducing the error margin to the fraction of mm, but also substantially increasing the computational complexity beyond the reasonable level. Therefore, 3 following algorithms based on spherical Earth model has been developed and recommended for general commercial apps, having good computational performance and reasonable accuracy.

Using the code

Below you can find three algorithmic solutions pertinent to the calculation of the great-circle (orthodromic) distance between two geo-points on the Earth surface:

  1. using System;
  2. namespace BusNY
  3. {
  4. internal enum UnitSystem { SI = 0, US = 1 }
  5. internal static class GIS
  6. {
  7. #region internal: properties (read-only)
  8. internal static double EarthRadiusKm { get {return _radiusEarthKM;} }
  9. internal static double EarthRadiusMiles { get { return _radiusEarthMiles; } }
  10. internal static double m2km { get { return _m2km; } }
  11. internal static double Deg2rad { get { return _toRad; } }
  12. #endregion
  13. #region private: const
  14. private const double _radiusEarthMiles = 3959;
  15. private const double _radiusEarthKM = 6371;
  16. private const double _m2km = 1.60934;
  17. private const double _toRad = Math.PI / 180;
  18. #endregion
  19. #region Method 1: Haversine algo
  20. /// <summary>
  21. /// Distance between two geographic points on surface, km/miles
  22. /// Haversine formula to calculate
  23. /// great-circle (orthodromic) distance on Earth
  24. /// High Accuracy, Medium speed
  25. /// re: http://en.wikipedia.org/wiki/Haversine_formula
  26. /// </summary>
  27. /// <param name="Lat1">double: 1st point Latitude</param>
  28. /// <param name="Lon1">double: 1st point Longitude</param>
  29. /// <param name="Lat2">double: 2nd point Latitude</param>
  30. /// <param name="Lon2">double: 2nd point Longitude</param>
  31. /// <returns>double: distance, km/miles</returns>
  32. internal static double DistanceHaversine(double Lat1,
  33. double Lon1,
  34. double Lat2,
  35. double Lon2,
  36. UnitSystem UnitSys ){
  37. try {
  38. double _radLat1 = Lat1 * _toRad;
  39. double _radLat2 = Lat2 * _toRad;
  40. double _dLatHalf = (_radLat2 - _radLat1) / 2;
  41. double _dLonHalf = Math.PI * (Lon2 - Lon1) / 360;
  42. // intermediate result
  43. double _a = Math.Sin(_dLatHalf);
  44. _a *= _a;
  45. // intermediate result
  46. double _b = Math.Sin(_dLonHalf);
  47. _b *= _b * Math.Cos(_radLat1) * Math.Cos(_radLat2);
  48. // central angle, aka arc segment angular distance
  49. double _centralAngle = 2 * Math.Atan2(Math.Sqrt(_a + _b), Math.Sqrt(1 - _a - _b));
  50. // great-circle (orthodromic) distance on Earth between 2 points
  51. if (UnitSys == UnitSystem.SI) { return _radiusEarthKM * _centralAngle; }
  52. else { return _radiusEarthMiles * _centralAngle; }
  53. }
  54. catch { throw; }
  55. }
  56. #endregion
  57. #region Method 2: Spherical Law of Cosines
  58. /// <summary>
  59. /// Distance between two geographic points on surface, km/miles
  60. /// Spherical Law of Cosines formula to calculate
  61. /// great-circle (orthodromic) distance on Earth;
  62. /// High Accuracy, Medium speed
  63. /// re: http://en.wikipedia.org/wiki/Spherical_law_of_cosines
  64. /// </summary>
  65. /// <param name="Lat1">double: 1st point Latitude</param>
  66. /// <param name="Lon1">double: 1st point Longitude</param>
  67. /// <param name="Lat2">double: 2nd point Latitude</param>
  68. /// <param name="Lon2">double: 2nd point Longitude</param>
  69. /// <returns>double: distance, km/miles</returns>
  70. internal static double DistanceSLC(double Lat1,
  71. double Lon1,
  72. double Lat2,
  73. double Lon2,
  74. UnitSystem UnitSys ){
  75. try {
  76. double _radLat1 = Lat1 * _toRad;
  77. double _radLat2 = Lat2 * _toRad;
  78. double _radLon1 = Lon1 * _toRad;
  79. double _radLon2 = Lon2 * _toRad;
  80. // central angle, aka arc segment angular distance
  81. double _centralAngle = Math.Acos(Math.Sin(_radLat1) * Math.Sin(_radLat2) +
  82. Math.Cos(_radLat1) * Math.Cos(_radLat2) * Math.Cos(_radLon2 - _radLon1));
  83. // great-circle (orthodromic) distance on Earth between 2 points
  84. if (UnitSys == UnitSystem.SI) { return _radiusEarthKM * _centralAngle; }
  85. else { return _radiusEarthMiles * _centralAngle; }
  86. }
  87. catch { throw; }
  88. }
  89. #endregion
  90. #region Method 3: Spherical Earth projection
  91. /// <summary>
  92. /// Distance between two geographic points on surface, km/miles
  93. /// Spherical Earth projection to a plane formula (using Pythagorean Theorem)
  94. /// to calculate great-circle (orthodromic) distance on Earth.
  95. /// central angle =
  96. /// Sqrt((_radLat2 - _radLat1)^2 + (Cos((_radLat1 + _radLat2)/2) * (Lon2 - Lon1))^2)
  97. /// Medium Accuracy, Fast,
  98. /// relative error less than 0.1% in search area smaller than 250 miles
  99. /// re: http://en.wikipedia.org/wiki/Geographical_distance
  100. /// </summary>
  101. /// <param name="Lat1">double: 1st point Latitude</param>
  102. /// <param name="Lon1">double: 1st point Longitude</param>
  103. /// <param name="Lat2">double: 2nd point Latitude</param>
  104. /// <param name="Lon2">double: 2nd point Longitude</param>
  105. /// <returns>double: distance, km/miles</returns>
  106. public static double DistanceSEP(double Lat1,
  107. double Lon1,
  108. double Lat2,
  109. double Lon2,
  110. UnitSystem UnitSys ){
  111. try
  112. {
  113. double _radLat1 = Lat1 * _toRad;
  114. double _radLat2 = Lat2 * _toRad;
  115. double _dLat = (_radLat2 - _radLat1);
  116. double _dLon = (Lon2 - Lon1) * _toRad;
  117. double _a = (_dLon) * Math.Cos((_radLat1 + _radLat2) / 2);
  118. // central angle, aka arc segment angular distance
  119. double _centralAngle = Math.Sqrt(_a * _a + _dLat * _dLat);
  120. // great-circle (orthodromic) distance on Earth between 2 points
  121. if (UnitSys == UnitSystem.SI) { return _radiusEarthKM * _centralAngle; }
  122. else { return _radiusEarthMiles * _centralAngle; }
  123. }
  124. catch { throw; }
  125. }
  126. #endregion
  127. }
  128. }