XAML-Map-Control/MapControl/Shared/Location.cs

148 lines
5.3 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2012-05-04 12:52:20 +02:00
using System.Globalization;
namespace MapControl
{
/// <summary>
/// A geographic location with latitude and longitude values in degrees.
2025-03-10 15:47:33 +01:00
/// For calculations with azimuth and distance on great circles, see
/// https://en.wikipedia.org/wiki/Great_circle
/// https://en.wikipedia.org/wiki/Great-circle_distance
/// https://en.wikipedia.org/wiki/Great-circle_navigation
2012-05-04 12:52:20 +02:00
/// </summary>
2024-05-22 11:25:32 +02:00
#if UWP || WINUI
2024-04-11 15:41:05 +02:00
[Windows.Foundation.Metadata.CreateFromString(MethodName = "Parse")]
2024-04-11 13:21:38 +02:00
#else
[System.ComponentModel.TypeConverter(typeof(LocationConverter))]
#endif
public class Location : IEquatable<Location>
2012-05-04 12:52:20 +02:00
{
public Location()
{
}
public Location(double latitude, double longitude)
2012-05-04 12:52:20 +02:00
{
2024-04-11 13:21:38 +02:00
Latitude = Math.Min(Math.Max(latitude, -90d), 90d);
Longitude = longitude;
2012-05-04 12:52:20 +02:00
}
2024-04-11 13:21:38 +02:00
public double Latitude { get; }
public double Longitude { get; }
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;
}
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();
}
public override string ToString()
{
2024-04-17 07:20:21 +02:00
return string.Format(CultureInfo.InvariantCulture, "{0},{1}", Latitude, Longitude);
}
2024-04-11 15:59:07 +02:00
/// <summary>
/// Creates a Location instance from a string containing a comma-separated pair of floating point numbers.
/// </summary>
2022-12-01 22:48:08 +01:00
public static Location Parse(string location)
2012-05-04 12:52:20 +02:00
{
2022-12-01 22:48:08 +01:00
string[] values = null;
2022-12-01 22:48:08 +01:00
if (!string.IsNullOrEmpty(location))
{
2022-12-01 22:48:08 +01:00
values = location.Split(new char[] { ',' });
}
2022-12-01 22:48:08 +01:00
if (values?.Length != 2)
{
throw new FormatException($"{nameof(Location)} string must contain a comma-separated pair of floating point numbers.");
}
2022-12-01 22:48:08 +01:00
return new Location(
double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture),
double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture));
2012-05-04 12:52:20 +02:00
}
/// <summary>
2022-12-01 22:48:08 +01:00
/// Normalizes a longitude to a value in the interval [-180 .. 180).
/// </summary>
public static double NormalizeLongitude(double longitude)
2012-05-04 12:52:20 +02:00
{
2022-12-01 22:48:08 +01:00
var x = (longitude + 180d) % 360d;
2022-12-01 22:48:08 +01:00
return x < 0d ? x + 180d : x - 180d;
2012-05-04 12:52:20 +02:00
}
2021-02-13 18:50:30 +01:00
/// <summary>
2025-03-10 15:47:33 +01:00
/// Calculates great circle azimuth and distance in radians between this and the specified Location.
2021-02-13 18:50:30 +01:00
/// </summary>
2025-03-10 15:47:33 +01:00
public void GetAzimuthDistance(Location location, out double azimuth, out double distance)
2021-02-13 18:50:30 +01:00
{
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 cosLat1 = Math.Cos(lat1);
2025-03-10 15:47:33 +01:00
var sinLat1 = Math.Sin(lat1);
2021-02-13 18:50:30 +01:00
var cosLat2 = Math.Cos(lat2);
2025-03-10 15:47:33 +01:00
var sinLat2 = Math.Sin(lat2);
2021-02-13 18:50:30 +01:00
var cosLon12 = Math.Cos(lon2 - lon1);
2025-03-10 15:47:33 +01:00
var sinLon12 = Math.Sin(lon2 - lon1);
var a = cosLat2 * sinLon12;
var b = cosLat1 * sinLat2 - sinLat1 * cosLat2 * cosLon12;
// α1
azimuth = Math.Atan2(a, b);
// σ12
distance = Math.Atan2(Math.Sqrt(a * a + b * b), sinLat1 * sinLat2 + cosLat1 * cosLat2 * cosLon12);
}
/// <summary>
/// Calculates the great circle distance in meters between this and the specified Location.
/// </summary>
public double GetDistance(Location location, double earthRadius = MapProjection.Wgs84EquatorialRadius)
{
GetAzimuthDistance(location, out double _, out double distance);
2021-02-13 18:50:30 +01:00
2025-03-10 15:47:33 +01:00
return earthRadius * distance;
2021-02-13 18:50:30 +01:00
}
/// <summary>
2025-03-10 15:47:33 +01:00
/// Calculates the Location on a great circle at the specified azimuth and distance in radians from this Location.
2021-02-13 18:50:30 +01:00
/// </summary>
2025-03-10 15:47:33 +01:00
public Location GetLocation(double azimuth, double distance)
2021-02-13 18:50:30 +01:00
{
2021-07-18 13:33:11 +02:00
var lat1 = Latitude * Math.PI / 180d;
var lon1 = Longitude * Math.PI / 180d;
2025-03-10 15:47:33 +01:00
var cosD = Math.Cos(distance);
var sinD = Math.Sin(distance);
var cosA = Math.Cos(azimuth);
var sinA = Math.Sin(azimuth);
2021-02-13 18:50:30 +01:00
var cosLat1 = Math.Cos(lat1);
2025-03-10 15:47:33 +01:00
var sinLat1 = Math.Sin(lat1);
var lat2 = Math.Asin(sinLat1 * cosD + cosLat1 * sinD * cosA);
var lon2 = lon1 + Math.Atan2(sinD * sinA, cosLat1 * cosD - sinLat1 * sinD * cosA);
2021-02-13 18:50:30 +01:00
return new Location(lat2 * 180d / Math.PI, lon2 * 180d / Math.PI);
}
2025-03-10 15:47:33 +01:00
/// <summary>
/// Calculates the Location on a great circle at the specified azimuth in degrees and distance in meters from this Location.
/// </summary>
public Location GetLocation(double azimuth, double distance, double earthRadius = MapProjection.Wgs84EquatorialRadius)
{
return GetLocation(azimuth * Math.PI / 180d, distance / earthRadius);
}
2012-05-04 12:52:20 +02:00
}
}