Beispiel #1
0
        /// <summary>
        /// Returns the distance (in metres) between two geospatial points defined
        /// by their longitude and latitude.
        /// </summary>
        /// <param name="u"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static float Haversine(IGeospatial u, IGeospatial v)
        {
            var R   = 6371e3; // Earth radius in metres
            var lat = DegreesToRadians(v.Latitude - u.Latitude);
            var lng = DegreesToRadians(v.Longitude - u.Longitude);
            var h1  = Math.Sin(lat / 2) * Math.Sin(lat / 2) +
                      Math.Cos(DegreesToRadians(u.Latitude)) * Math.Cos(DegreesToRadians(v.Latitude)) *
                      Math.Sin(lng / 2) * Math.Sin(lng / 2);
            var h2 = 2 * Math.Asin(Math.Min(1, Math.Sqrt(h1)));

            return((float)(R * h2));
        }
        public static double GetDistanceInMiles(this IGeospatial from, IGeospatial to)
        {
            // Based on https://www.geodatasource.com/developers/c-sharp

            double theta = from.Longitude - to.Longitude;
            double dist  =
                Math.Sin(from.Latitude.ConvertDegreesToRadians()) * Math.Sin(to.Latitude.ConvertDegreesToRadians()) +
                Math.Cos(from.Latitude.ConvertDegreesToRadians()) * Math.Cos(to.Latitude.ConvertDegreesToRadians()) *
                Math.Cos(theta.ConvertDegreesToRadians());

            dist = Math.Acos(dist).ConvertRadiansToDegrees();
            dist = dist * 60 * 1.1515;
            return(dist);
        }