/// <summary> /// Distances the between. /// </summary> /// <param name="point1">The point1.</param> /// <param name="point2">The point2.</param> /// <param name="returnType">Type of the return.</param> /// <returns></returns> public static double DistanceBetween(LatitudeLongitudePoint point1, LatitudeLongitudePoint point2, DistanceType returnType) { // see http://www.mathforum.com/library/drmath/view/51711.html // TODO is this really correct, or do I have to first convert decimal to degrees and then to radians? if (point1.Latitude == point2.Latitude) { return(0); } double a = point1.Latitude / 57.29577951D; double b = point1.Longitude / 57.29577951D; double c = point2.Latitude / 57.29577951D; double d = point2.Longitude / 57.29577951D; double earthRadius = (returnType == DistanceType.StatuteMiles ? GlobalConstants.EarthEquatorialRadiusInStatuteMiles : (returnType == DistanceType.NauticalMiles ? GlobalConstants.EarthEquatorialRadiusInNauticalMiles : GlobalConstants.EarthEquatorialRadiusInKilometers)); double sina = Math.Sin(a); double sinc = Math.Sin(c); double cosa = Math.Cos(a); double cosc = Math.Cos(c); double cosbd = Math.Cos(b - d); double ans1 = ((sina * sinc) + (cosa * cosc * cosbd)); if (ans1 > 1D) { return(earthRadius * Math.Acos(1D)); } else { return(earthRadius * Math.Acos(ans1)); } }
/// <summary> /// Parses the specified latitude. /// </summary> /// <param name="latitude">The latitude.</param> /// <param name="longitude">The longitude.</param> /// <returns></returns> public static LatitudeLongitudePoint Parse(string latitude, string longitude) { if (!(latitude == null || longitude == null || latitude.ToLower() == "na" || longitude.ToLower() == "na")) { LatitudeLongitudePoint ret = new LatitudeLongitudePoint(); Regex r = new Regex(@"(\d+)\.(\d+)(\.(\d+))?(\w)"); double degrees, minutes, seconds = 0; Match m = r.Match(latitude); if (m.Success) { degrees = double.Parse(m.Groups[1].Value); minutes = double.Parse(m.Groups[2].Value); seconds = m.Groups[3].Success ? double.Parse(m.Groups[3].Value) : 0; ret.Latitude = degrees + (minutes / 60D) + (seconds / 3600D); if (m.Groups[5].Value.ToLower() == "s") { ret.Latitude *= -1; } } else { throw new Exception("Unsupported parse type!"); } m = r.Match(longitude); if (m.Success) { degrees = double.Parse(m.Groups[1].Value); minutes = double.Parse(m.Groups[2].Value); seconds = m.Groups[3].Success ? double.Parse(m.Groups[3].Value) : 0; ret.Longitude = degrees + (minutes / 60D) + (seconds / 3600D); if (m.Groups[5].Value.ToLower() == "w") { ret.Longitude *= -1; } } else { throw new Exception("Unsupported parse type!"); } return(ret); } return(null); }
/// <summary> /// Returns the distance between two latitude/longitude points, in miles. /// </summary> /// <param name="point1"></param> /// <param name="point2"></param> /// <returns></returns> public static double DistanceBetween(LatitudeLongitudePoint point1, LatitudeLongitudePoint point2) { return(DistanceBetween(point1, point2, DistanceType.StatuteMiles)); }