public static double DistanceTo(this Coordinates baseCoordinates, Coordinates targetCoordinates, UnitOfLength unitOfLength) { var baseRad = Math.PI * baseCoordinates.Latitude / 180; var targetRad = Math.PI * targetCoordinates.Latitude / 180; var theta = baseCoordinates.Longitude - targetCoordinates.Longitude; var thetaRad = Math.PI * theta / 180; double dist = Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad); dist = Math.Acos(dist); dist = dist * 180 / Math.PI; dist = dist * 60 * 1.1515; return(unitOfLength.ConvertFromMiles(dist)); }
/// <summary> /// By Haversine https://en.wikipedia.org/wiki/Haversine_formula /// </summary> /// <returns></returns> public static double DistanceToByHaversine(this Models.Coordinate src, Models.Coordinate dest, UnitOfLength unitOfLength) { var dLat = (dest.Latitude - src.Latitude) * CoordinateConst.DegreesToRadians; var dLon = (dest.Longitude - src.Longitude) * CoordinateConst.DegreesToRadians; var a = Math.Pow(Math.Sin(dLat / 2), 2) + Math.Cos(src.Latitude * CoordinateConst.DegreesToRadians) * Math.Cos(dest.Latitude * CoordinateConst.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 dist = CoordinateConst.EarthRadiusMile * centralAngle; return(unitOfLength.ConvertFromMiles(dist)); }
public double DistanceTo(Coordinates i_TargetCoordinates, UnitOfLength i_UnitOfLength) { var baseRad = Math.PI * Latitude / 180; var targetRad = Math.PI * i_TargetCoordinates.Latitude / 180; var theta = Longitude - i_TargetCoordinates.Longitude; var thetaRad = Math.PI * theta / 180; double dist = (Math.Sin(baseRad) * Math.Sin(targetRad)) + (Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad)); dist = Math.Acos(dist); dist = dist * 180 / Math.PI; dist = dist * 60 * 1.1515; return(i_UnitOfLength.ConvertFromMiles(dist)); }
/// <summary> /// By Geographical distance http://en.wikipedia.org/wiki/Geographical_distance /// </summary> public static double DistanceToByGeo(this Models.Coordinate src, Models.Coordinate dest, UnitOfLength unitOfLength) { var radLatSrc = src.Latitude * CoordinateConst.DegreesToRadians; var radLatDest = dest.Latitude * CoordinateConst.DegreesToRadians; var dLat = radLatDest - radLatSrc; var dLon = (dest.Longitude - src.Longitude) * CoordinateConst.DegreesToRadians; var a = dLon * Math.Cos((radLatSrc + radLatDest) / 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 dist = CoordinateConst.EarthRadiusMile * centralAngle; return(unitOfLength.ConvertFromMiles(dist)); }
public static double DistanceTo(this LocationDto baseCoordinates, LocationDto targetCoordinates, UnitOfLength unitOfLength) { var baseRad = Math.PI * double.Parse(baseCoordinates.Latitude) / 180; var targetRad = Math.PI * double.Parse(targetCoordinates.Latitude) / 180; var theta = double.Parse(baseCoordinates.Longitude) - double.Parse(targetCoordinates.Longitude); var thetaRad = Math.PI * theta / 180; double dist = Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad); dist = Math.Acos(dist); dist = dist * 180 / Math.PI; dist = dist * 60 * 1.1515; return(unitOfLength.ConvertFromMiles(dist)); }
/// <summary> /// By Spherical law of cosines http://en.wikipedia.org/wiki/Spherical_law_of_cosines /// </summary> public static double DistanceTo(this Models.Coordinate src, Models.Coordinate dest, UnitOfLength unitOfLength) { var theta = src.Longitude - dest.Longitude; var thetaRad = theta * CoordinateConst.DegreesToRadians; var targetRad = dest.Latitude * CoordinateConst.DegreesToRadians; var baseRad = src.Latitude * CoordinateConst.DegreesToRadians; var dist = Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad); dist = Math.Acos(dist); // calculate to earth radius by miles dist = dist * CoordinateConst.EarthRadiusMile; return(unitOfLength.ConvertFromMiles(dist)); }