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

60 lines
1.7 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
#endif
namespace MapControl
{
/// <summary>
2026-01-10 22:48:14 +01:00
/// Spherical Azimuthal Equidistant Projection - No standard CRS identifier.
2025-02-24 11:22:17 +01:00
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/publication/pp1395), p.195-197.
/// </summary>
2026-01-17 12:06:21 +01:00
public class AzimuthalEquidistantProjection : MapProjection
{
2026-01-10 22:48:14 +01:00
public const string DefaultCrsId = "AUTO2:97003"; // proprietary CRS identifier
2024-07-12 13:57:27 +02:00
2026-01-10 23:29:42 +01:00
public AzimuthalEquidistantProjection() // parameterless constructor for XAML
: this(DefaultCrsId)
{
}
public AzimuthalEquidistantProjection(string crsId)
2022-03-05 18:40:57 +01:00
{
2026-01-17 12:06:21 +01:00
Type = MapProjectionType.Azimuthal;
2022-03-05 18:40:57 +01:00
CrsId = crsId;
}
2026-01-17 12:06:21 +01:00
public double EarthRadius { get; set; } = Wgs84MeanRadius;
public override Point? LocationToMap(double latitude, double longitude)
{
2026-01-09 23:43:39 +01:00
if (Center.Equals(latitude, longitude))
{
return new Point();
}
Center.GetAzimuthDistance(latitude, longitude, out double azimuth, out double distance);
2026-01-16 20:22:45 +01:00
var mapDistance = distance * EarthRadius;
return new Point(mapDistance * Math.Sin(azimuth), mapDistance * Math.Cos(azimuth));
}
public override Location MapToLocation(double x, double y)
{
if (x == 0d && y == 0d)
{
return new Location(Center.Latitude, Center.Longitude);
}
var azimuth = Math.Atan2(x, y);
var mapDistance = Math.Sqrt(x * x + y * y);
2026-01-16 20:22:45 +01:00
var distance = mapDistance / EarthRadius;
2025-03-10 15:47:33 +01:00
return Center.GetLocation(azimuth, distance);
}
}
}