Esempio n. 1
0
        /// <summary>
        ///     By Haversine https://en.wikipedia.org/wiki/Haversine_formula
        /// </summary>
        /// <returns></returns>
        public static double GetDistanceByHaversine([NotNull] CoordinateModel origin, [NotNull] CoordinateModel destination, [NotNull] UnitOfLengthModel unitOfLength)
        {
            var dLat = (destination.Latitude - origin.Latitude) * ElectLocationConstants.DegreesToRadians;

            var dLon = (destination.Longitude - origin.Longitude) * ElectLocationConstants.DegreesToRadians;

            var a = Math.Pow(Math.Sin(dLat / 2), 2) +
                    Math.Cos(origin.Latitude * ElectLocationConstants.DegreesToRadians) *
                    Math.Cos(destination.Latitude * ElectLocationConstants.DegreesToRadians) *
                    Math.Pow(Math.Sin(dLon / 2), 2);

            // central angle, aka arc segment angular distance
            var centralAngle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

            var distance = ElectLocationConstants.EarthRadiusMile * centralAngle;

            return(unitOfLength.ConvertFromMiles(distance));
        }
Esempio n. 2
0
        /// <summary>
        ///     By Spherical law of cosines http://en.wikipedia.org/wiki/Spherical_law_of_cosines
        /// </summary>
        public static double GetDistance([NotNull] CoordinateModel origin, [NotNull] CoordinateModel destination, [NotNull] UnitOfLengthModel unitOfLength)
        {
            var theta    = origin.Longitude - destination.Longitude;
            var thetaRad = theta * ElectLocationConstants.DegreesToRadians;

            var targetRad = destination.Latitude * ElectLocationConstants.DegreesToRadians;
            var baseRad   = origin.Latitude * ElectLocationConstants.DegreesToRadians;

            var distance =
                Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) *
                Math.Cos(targetRad) * Math.Cos(thetaRad);

            distance = Math.Acos(distance);

            // calculate to earth radius by miles
            distance = distance * ElectLocationConstants.EarthRadiusMile;

            return(unitOfLength.ConvertFromMiles(distance));
        }
Esempio n. 3
0
        /// <summary>
        ///     By Geographical distance http://en.wikipedia.org/wiki/Geographical_distance
        /// </summary>
        public static double GetDistanceByGeo([NotNull] CoordinateModel origin, [NotNull] CoordinateModel destination, [NotNull] UnitOfLengthModel unitOfLength)
        {
            var radLatOrigin = origin.Latitude * ElectLocationConstants.DegreesToRadians;

            var radLatDestination = destination.Latitude * ElectLocationConstants.DegreesToRadians;

            var dLat = radLatDestination - radLatOrigin;

            var dLon = (destination.Longitude - origin.Longitude) * ElectLocationConstants.DegreesToRadians;

            var a = dLon * Math.Cos((radLatOrigin + radLatDestination) / 2);

            // central angle, aka arc segment angular distance
            var centralAngle = Math.Sqrt(a * a + dLat * dLat);

            // great-circle (orthodromic) distance on Earth between 2 points
            var distance = ElectLocationConstants.EarthRadiusMile * centralAngle;

            return(unitOfLength.ConvertFromMiles(distance));
        }