2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using System.Windows;
|
2025-08-19 19:43:02 +02:00
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using Avalonia;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2022-02-23 23:19:32 +01:00
|
|
|
|
/// Spherical Orthographic Projection - AUTO2:42003.
|
2025-02-24 11:22:17 +01:00
|
|
|
|
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/publication/pp1395), p.148-150.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class OrthographicProjection : AzimuthalProjection
|
|
|
|
|
|
{
|
2022-01-19 16:43:00 +01:00
|
|
|
|
public const string DefaultCrsId = "AUTO2:42003";
|
|
|
|
|
|
|
2026-01-10 23:29:42 +01:00
|
|
|
|
public OrthographicProjection() // parameterless constructor for XAML
|
2024-08-28 14:58:06 +02:00
|
|
|
|
: this(DefaultCrsId)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public OrthographicProjection(string crsId)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2024-07-12 13:57:27 +02:00
|
|
|
|
CrsId = crsId;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-12 21:28:45 +01:00
|
|
|
|
public override Point? LocationToMap(double latitude, double longitude)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2026-01-09 23:43:39 +01:00
|
|
|
|
if (Center.Equals(latitude, longitude))
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
return new Point();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-13 23:24:48 +01:00
|
|
|
|
var phi = latitude * Math.PI / 180d;
|
|
|
|
|
|
var phi1 = Center.Latitude * Math.PI / 180d;
|
2026-01-14 09:18:40 +01:00
|
|
|
|
var dLambda = (longitude - Center.Longitude) * Math.PI / 180d; // λ - λ0
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2026-01-14 09:18:40 +01:00
|
|
|
|
if (Math.Abs(phi - phi1) > Math.PI / 2d || Math.Abs(dLambda) > Math.PI / 2d)
|
2022-03-04 22:28:18 +01:00
|
|
|
|
{
|
2022-12-02 16:50:10 +01:00
|
|
|
|
return null;
|
2022-03-04 22:28:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 09:18:40 +01:00
|
|
|
|
var x = Wgs84MeanRadius * Math.Cos(phi) * Math.Sin(dLambda); // p.149 (20-3)
|
2026-01-13 23:24:48 +01:00
|
|
|
|
var y = Wgs84MeanRadius * (Math.Cos(phi1) * Math.Sin(phi) -
|
2026-01-14 09:18:40 +01:00
|
|
|
|
Math.Sin(phi1) * Math.Cos(phi) * Math.Cos(dLambda)); // p.149 (20-4)
|
2026-01-13 23:24:48 +01:00
|
|
|
|
return new Point(x, y);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-12 21:28:45 +01:00
|
|
|
|
public override Location MapToLocation(double x, double y)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2025-12-12 21:28:45 +01:00
|
|
|
|
if (x == 0d && y == 0d)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
return new Location(Center.Latitude, Center.Longitude);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 08:13:07 +01:00
|
|
|
|
x /= Wgs84MeanRadius;
|
|
|
|
|
|
y /= Wgs84MeanRadius;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var r2 = x * x + y * y;
|
|
|
|
|
|
|
|
|
|
|
|
if (r2 > 1d)
|
|
|
|
|
|
{
|
2022-03-04 22:28:18 +01:00
|
|
|
|
return null;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 09:18:40 +01:00
|
|
|
|
var r = Math.Sqrt(r2); // p.150 (20-18), r=ρ/R
|
|
|
|
|
|
var sinC = r; // p.150 (20-19)
|
|
|
|
|
|
var cosC = Math.Sqrt(1d - r2);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2026-01-13 23:24:48 +01:00
|
|
|
|
var phi1 = Center.Latitude * Math.PI / 180d;
|
|
|
|
|
|
var cosPhi1 = Math.Cos(phi1);
|
|
|
|
|
|
var sinPhi1 = Math.Sin(phi1);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2026-01-13 23:24:48 +01:00
|
|
|
|
var phi = Math.Asin(cosC * sinPhi1 + y * sinC * cosPhi1 / r); // p.150 (20-14)
|
2026-01-14 09:18:40 +01:00
|
|
|
|
var dLambda = Math.Atan2(x * sinC, r * cosC * cosPhi1 - y * sinC * sinPhi1); // p.150 (20-15)
|
2026-01-13 23:24:48 +01:00
|
|
|
|
|
2026-01-14 09:18:40 +01:00
|
|
|
|
return new Location(180d / Math.PI * phi, 180d / Math.PI * dLambda + Center.Longitude);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|