2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2012-07-07 17:19:10 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2013-11-05 18:00:03 +01:00
|
|
|
|
/// A geographic location with latitude and longitude values in degrees.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#if !UWP
|
2018-08-08 23:31:52 +02:00
|
|
|
|
[System.ComponentModel.TypeConverter(typeof(LocationConverter))]
|
|
|
|
|
|
#endif
|
|
|
|
|
|
public class Location : IEquatable<Location>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2012-05-06 11:28:47 +02:00
|
|
|
|
private double latitude;
|
|
|
|
|
|
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public Location()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-18 19:49:17 +01:00
|
|
|
|
public Location(double latitude, double longitude)
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2013-11-21 21:16:29 +01:00
|
|
|
|
Latitude = latitude;
|
|
|
|
|
|
Longitude = longitude;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-05-06 11:28:47 +02:00
|
|
|
|
public double Latitude
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => latitude;
|
|
|
|
|
|
set => latitude = Math.Min(Math.Max(value, -90d), 90d);
|
2012-05-06 11:28:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-18 13:33:11 +02:00
|
|
|
|
public double Longitude { get; set; }
|
2012-05-06 11:28:47 +02:00
|
|
|
|
|
2014-07-09 21:27:28 +02:00
|
|
|
|
public bool Equals(Location location)
|
|
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
return location != null
|
2021-07-18 13:33:11 +02:00
|
|
|
|
&& Math.Abs(location.Latitude - Latitude) < 1e-9
|
|
|
|
|
|
&& Math.Abs(location.Longitude - Longitude) < 1e-9;
|
2014-07-09 21:27:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Equals(obj as Location);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
|
{
|
2021-07-18 13:33:11 +02:00
|
|
|
|
return Latitude.GetHashCode() ^ Longitude.GetHashCode();
|
2014-07-09 21:27:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
2021-07-18 13:33:11 +02:00
|
|
|
|
return string.Format(CultureInfo.InvariantCulture, "{0:F5},{1:F5}", Latitude, Longitude);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
public static Location Parse(string locationString)
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2019-04-05 19:13:58 +02:00
|
|
|
|
Location location = null;
|
2015-04-21 19:17:32 +02:00
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(locationString))
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2019-04-05 19:13:58 +02:00
|
|
|
|
var values = locationString.Split(new char[] { ',' });
|
|
|
|
|
|
|
|
|
|
|
|
if (values.Length != 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new FormatException("Location string must be a comma-separated pair of double values.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
location = new Location(
|
|
|
|
|
|
double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture),
|
|
|
|
|
|
double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
return location;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-19 19:36:03 +02:00
|
|
|
|
/// <summary>
|
2020-05-13 18:17:28 +02:00
|
|
|
|
/// Normalizes a longitude to a value in the interval [-180 .. 180].
|
2016-04-19 19:36:03 +02:00
|
|
|
|
/// </summary>
|
2012-11-22 21:42:29 +01:00
|
|
|
|
public static double NormalizeLongitude(double longitude)
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2013-12-20 17:05:10 +01:00
|
|
|
|
if (longitude < -180d)
|
2013-11-21 21:16:29 +01:00
|
|
|
|
{
|
2013-12-20 17:05:10 +01:00
|
|
|
|
longitude = ((longitude + 180d) % 360d) + 180d;
|
2013-11-21 21:16:29 +01:00
|
|
|
|
}
|
2013-12-20 17:05:10 +01:00
|
|
|
|
else if (longitude > 180d)
|
2013-11-21 21:16:29 +01:00
|
|
|
|
{
|
2013-12-20 17:05:10 +01:00
|
|
|
|
longitude = ((longitude - 180d) % 360d) - 180d;
|
2013-11-21 21:16:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return longitude;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
}
|
2021-02-13 18:50:30 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Calculates the great circle distance between this and the specified Location.
|
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Great_circle
|
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Great-circle_distance
|
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Great-circle_navigation
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double GetDistance(
|
|
|
|
|
|
Location location, double earthRadius = MapProjection.Wgs84EquatorialRadius)
|
|
|
|
|
|
{
|
2021-07-18 13:33:11 +02:00
|
|
|
|
var lat1 = Latitude * Math.PI / 180d;
|
|
|
|
|
|
var lon1 = Longitude * Math.PI / 180d;
|
|
|
|
|
|
var lat2 = location.Latitude * Math.PI / 180d;
|
|
|
|
|
|
var lon2 = location.Longitude * Math.PI / 180d;
|
2021-02-13 18:50:30 +01:00
|
|
|
|
var sinLat1 = Math.Sin(lat1);
|
|
|
|
|
|
var cosLat1 = Math.Cos(lat1);
|
|
|
|
|
|
var sinLat2 = Math.Sin(lat2);
|
|
|
|
|
|
var cosLat2 = Math.Cos(lat2);
|
|
|
|
|
|
var sinLon12 = Math.Sin(lon2 - lon1);
|
|
|
|
|
|
var cosLon12 = Math.Cos(lon2 - lon1);
|
|
|
|
|
|
var a = cosLat1 * sinLat2 - sinLat1 * cosLat2 * cosLon12;
|
|
|
|
|
|
var b = cosLat2 * sinLon12;
|
|
|
|
|
|
var s12 = Math.Atan2(Math.Sqrt(a * a + b * b), sinLat1 * sinLat2 + cosLat1 * cosLat2 * cosLon12);
|
|
|
|
|
|
|
|
|
|
|
|
return earthRadius * s12;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Calculates the Location on a great circle at the specified azimuth angle and distance from this Location.
|
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Great_circle
|
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Great-circle_navigation
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Location GetLocation(
|
|
|
|
|
|
double azimuth, double distance, double earthRadius = MapProjection.Wgs84EquatorialRadius)
|
|
|
|
|
|
{
|
|
|
|
|
|
var s12 = distance / earthRadius;
|
|
|
|
|
|
var az1 = azimuth * Math.PI / 180d;
|
2021-07-18 13:33:11 +02:00
|
|
|
|
var lat1 = Latitude * Math.PI / 180d;
|
|
|
|
|
|
var lon1 = Longitude * Math.PI / 180d;
|
2021-02-13 18:50:30 +01:00
|
|
|
|
var sinS12 = Math.Sin(s12);
|
|
|
|
|
|
var cosS12 = Math.Cos(s12);
|
|
|
|
|
|
var sinAz1 = Math.Sin(az1);
|
|
|
|
|
|
var cosAz1 = Math.Cos(az1);
|
|
|
|
|
|
var sinLat1 = Math.Sin(lat1);
|
|
|
|
|
|
var cosLat1 = Math.Cos(lat1);
|
|
|
|
|
|
var lat2 = Math.Asin(sinLat1 * cosS12 + cosLat1 * sinS12 * cosAz1);
|
2021-07-18 13:33:11 +02:00
|
|
|
|
var lon2 = lon1 + Math.Atan2(sinS12 * sinAz1, cosLat1 * cosS12 - sinLat1 * sinS12 * cosAz1);
|
2021-02-13 18:50:30 +01:00
|
|
|
|
|
|
|
|
|
|
return new Location(lat2 * 180d / Math.PI, lon2 * 180d / Math.PI);
|
|
|
|
|
|
}
|
2012-05-04 12:52:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|