/// <summary> /// Create a new GeoLocation from lat/long in degrees /// </summary> /// <param name="latitude">The latitude.</param> /// <param name="longitude">The longitude.</param> /// <returns></returns> public static GeoLocation FromDegrees(double latitude, double longitude) { var geo = new GeoLocation { Latitude = latitude, Longitude = longitude }; geo.CheckBounds(); return geo; }
/// <summary> /// Calculate the great circle distance between this location and another location. /// <seealso cref="http://en.wikipedia.org/wiki/Great-circle_distance"/> /// </summary> /// <param name="location">The location.</param> /// <param name="radius">The radius of the sphere in KM. This defaults to the Earth's radius (6371.01km).</param> /// <returns></returns> public double GetDistanceTo(GeoLocation location, double radius = 6371.01) { if (radius < 0d) throw new ArgumentException("Radius must be greater than zero"); return Math.Acos(Math.Sin(LatitudeRadians) * Math.Sin(location.LatitudeRadians) + Math.Cos(LatitudeRadians) * Math.Cos(location.LatitudeRadians) * Math.Cos(LongitudeRadians - location.LongitudeRadians)) * radius; }
public void TestGetDistanceToRadiusLessThanZero() { var toLocation = GeoLocation.FromDegrees(20, 20); Assert.Throws <ArgumentException>(() => location.GetDistanceTo(toLocation, -1), "Radius must be greater than zero"); }
public void TestConstructorFromDegreesLongitudeTooLarge() { var exception = Assert.Throws <ArgumentException>(() => GeoLocation.FromDegrees(1, 185)); Assert.AreEqual(exception.Message, "Longitude must not be greater than the maximum value. (180)"); }
public void TestConstructorFromDegreesLongitudeTooSmall() { var exception = Assert.Throws <ArgumentException>(() => GeoLocation.FromDegrees(1, -185)); Assert.AreEqual(exception.Message, "Longitude must not be less than the minimum value. (-180)"); }
public void Setup() { location = GeoLocation.FromDegrees(0, 0); }