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

84 lines
2.6 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>
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.
/// </summary>
2026-01-17 12:06:21 +01:00
public class OrthographicProjection : MapProjection
{
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
: this(DefaultCrsId)
{
}
public OrthographicProjection(string crsId)
{
2026-01-17 12:06:21 +01:00
Type = MapProjectionType.Azimuthal;
2024-07-12 13:57:27 +02: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();
}
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
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
{
return null;
2022-03-04 22:28:18 +01:00
}
2026-01-16 20:22:45 +01:00
var x = EarthRadius * Math.Cos(phi) * Math.Sin(dLambda); // p.149 (20-3)
var y = EarthRadius * (Math.Cos(phi1) * Math.Sin(phi) -
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);
}
public override Location MapToLocation(double x, double y)
{
if (x == 0d && y == 0d)
{
return new Location(Center.Latitude, Center.Longitude);
}
2026-01-16 20:22:45 +01:00
x /= EarthRadius;
y /= EarthRadius;
var r2 = x * x + y * y;
if (r2 > 1d)
{
2022-03-04 22:28:18 +01:00
return null;
}
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);
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);
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);
}
}
}