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:
- using System;
-
- namespace BusNY
- {
- internal enum UnitSystem { SI = 0, US = 1 }
-
- internal static class GIS
- {
- #region internal: properties (read-only)
- internal static double EarthRadiusKm { get {return _radiusEarthKM;} }
- internal static double EarthRadiusMiles { get { return _radiusEarthMiles; } }
- internal static double m2km { get { return _m2km; } }
- internal static double Deg2rad { get { return _toRad; } }
- #endregion
-
- #region private: const
- private const double _radiusEarthMiles = 3959;
- private const double _radiusEarthKM = 6371;
- private const double _m2km = 1.60934;
- private const double _toRad = Math.PI / 180;
- #endregion
-
- #region Method 1: Haversine algo
-
-
-
-
-
-
-
-
-
-
-
-
- internal static double DistanceHaversine(double Lat1,
- double Lon1,
- double Lat2,
- double Lon2,
- UnitSystem UnitSys ){
- try {
- double _radLat1 = Lat1 * _toRad;
- double _radLat2 = Lat2 * _toRad;
- double _dLatHalf = (_radLat2 - _radLat1) / 2;
- double _dLonHalf = Math.PI * (Lon2 - Lon1) / 360;
-
-
- double _a = Math.Sin(_dLatHalf);
- _a *= _a;
-
-
- double _b = Math.Sin(_dLonHalf);
- _b *= _b * Math.Cos(_radLat1) * Math.Cos(_radLat2);
-
-
- double _centralAngle = 2 * Math.Atan2(Math.Sqrt(_a + _b), Math.Sqrt(1 - _a - _b));
-
-
- if (UnitSys == UnitSystem.SI) { return _radiusEarthKM * _centralAngle; }
- else { return _radiusEarthMiles * _centralAngle; }
- }
- catch { throw; }
- }
- #endregion
-
- #region Method 2: Spherical Law of Cosines
-
-
-
-
-
-
-
-
-
-
-
-
- internal static double DistanceSLC(double Lat1,
- double Lon1,
- double Lat2,
- double Lon2,
- UnitSystem UnitSys ){
- try {
- double _radLat1 = Lat1 * _toRad;
- double _radLat2 = Lat2 * _toRad;
- double _radLon1 = Lon1 * _toRad;
- double _radLon2 = Lon2 * _toRad;
-
-
- double _centralAngle = Math.Acos(Math.Sin(_radLat1) * Math.Sin(_radLat2) +
- Math.Cos(_radLat1) * Math.Cos(_radLat2) * Math.Cos(_radLon2 - _radLon1));
-
-
- if (UnitSys == UnitSystem.SI) { return _radiusEarthKM * _centralAngle; }
- else { return _radiusEarthMiles * _centralAngle; }
- }
- catch { throw; }
- }
- #endregion
-
- #region Method 3: Spherical Earth projection
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static double DistanceSEP(double Lat1,
- double Lon1,
- double Lat2,
- double Lon2,
- UnitSystem UnitSys ){
- try
- {
- double _radLat1 = Lat1 * _toRad;
- double _radLat2 = Lat2 * _toRad;
- double _dLat = (_radLat2 - _radLat1);
- double _dLon = (Lon2 - Lon1) * _toRad;
-
- double _a = (_dLon) * Math.Cos((_radLat1 + _radLat2) / 2);
-
-
- double _centralAngle = Math.Sqrt(_a * _a + _dLat * _dLat);
-
-
- if (UnitSys == UnitSystem.SI) { return _radiusEarthKM * _centralAngle; }
- else { return _radiusEarthMiles * _centralAngle; }
- }
- catch { throw; }
- }
- #endregion
- }
- }