2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
2022-12-13 18:22:18 +01:00
|
|
|
|
using System.Windows;
|
2025-08-19 19:43:02 +02:00
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using Avalonia;
|
2022-12-13 18:22:18 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Elliptical Polar Stereographic Projection with a given scale factor at the pole and
|
|
|
|
|
|
/// optional false easting and northing, as used by the UPS North and UPS South projections.
|
2025-02-24 11:22:17 +01:00
|
|
|
|
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/publication/pp1395), p.154-163.
|
2022-12-13 18:22:18 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PolarStereographicProjection : MapProjection
|
|
|
|
|
|
{
|
|
|
|
|
|
public PolarStereographicProjection()
|
|
|
|
|
|
{
|
|
|
|
|
|
Type = MapProjectionType.Azimuthal;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double EquatorialRadius { get; set; } = Wgs84EquatorialRadius;
|
|
|
|
|
|
public double Flattening { get; set; } = Wgs84Flattening;
|
|
|
|
|
|
public double ScaleFactor { get; set; } = 0.994;
|
|
|
|
|
|
public double FalseEasting { get; set; } = 2e6;
|
|
|
|
|
|
public double FalseNorthing { get; set; } = 2e6;
|
2026-01-10 16:21:55 +01:00
|
|
|
|
public Hemisphere Hemisphere { get; set; }
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
2026-01-15 10:58:54 +01:00
|
|
|
|
public static double RelativeScale(Hemisphere hemisphere, double equatorialRadius, double flattening, double scaleFactor, double latitude)
|
2022-12-13 18:22:18 +01:00
|
|
|
|
{
|
2026-01-15 10:58:54 +01:00
|
|
|
|
var sign = hemisphere == Hemisphere.North ? 1d : -1d;
|
|
|
|
|
|
var phi = sign * latitude * Math.PI / 180d;
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
2026-01-15 10:58:54 +01:00
|
|
|
|
var e = Math.Sqrt((2d - flattening) * flattening);
|
2026-01-13 23:49:37 +01:00
|
|
|
|
var eSinPhi = e * Math.Sin(phi);
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
2026-01-13 23:49:37 +01:00
|
|
|
|
var t = Math.Tan(Math.PI / 4d - phi / 2d)
|
|
|
|
|
|
/ Math.Pow((1d - eSinPhi) / (1d + eSinPhi), e / 2d); // p.161 (15-9)
|
2026-01-15 10:58:54 +01:00
|
|
|
|
var r = 2d * equatorialRadius * scaleFactor * t
|
2022-12-13 18:22:18 +01:00
|
|
|
|
/ Math.Sqrt(Math.Pow(1d + e, 1d + e) * Math.Pow(1d - e, 1d - e)); // p.161 (21-33)
|
|
|
|
|
|
|
2026-01-13 23:49:37 +01:00
|
|
|
|
var m = Math.Cos(phi) / Math.Sqrt(1d - eSinPhi * eSinPhi); // p.160 (14-15)
|
2026-01-15 10:58:54 +01:00
|
|
|
|
|
|
|
|
|
|
return r / (equatorialRadius * m); // p.161 (21-32)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Point RelativeScale(double latitude, double longitude)
|
|
|
|
|
|
{
|
|
|
|
|
|
var k = RelativeScale(Hemisphere, EquatorialRadius, Flattening, ScaleFactor, latitude);
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
2024-05-19 17:24:18 +02:00
|
|
|
|
return new Point(k, k);
|
2022-12-13 18:22:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-12 21:28:45 +01:00
|
|
|
|
public override Point? LocationToMap(double latitude, double longitude)
|
2022-12-13 18:22:18 +01:00
|
|
|
|
{
|
2026-01-15 10:58:54 +01:00
|
|
|
|
var sign = Hemisphere == Hemisphere.North ? 1d : -1d;
|
|
|
|
|
|
var phi = sign * latitude * Math.PI / 180d;
|
|
|
|
|
|
var lambda = sign * longitude * Math.PI / 180d;
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
|
|
|
|
|
var e = Math.Sqrt((2d - Flattening) * Flattening);
|
2026-01-13 23:49:37 +01:00
|
|
|
|
var eSinPhi = e * Math.Sin(phi);
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
2026-01-13 23:49:37 +01:00
|
|
|
|
var t = Math.Tan(Math.PI / 4d - phi / 2d)
|
|
|
|
|
|
/ Math.Pow((1d - eSinPhi) / (1d + eSinPhi), e / 2d); // p.161 (15-9)
|
2026-01-15 10:58:54 +01:00
|
|
|
|
|
2022-12-13 18:22:18 +01:00
|
|
|
|
var r = 2d * EquatorialRadius * ScaleFactor * t
|
|
|
|
|
|
/ Math.Sqrt(Math.Pow(1d + e, 1d + e) * Math.Pow(1d - e, 1d - e)); // p.161 (21-33)
|
|
|
|
|
|
|
2026-01-15 10:58:54 +01:00
|
|
|
|
var x = sign * r * Math.Sin(lambda); // p.161 (21-30)
|
|
|
|
|
|
var y = sign * -r * Math.Cos(lambda); // p.161 (21-31)
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
|
|
|
|
|
return new Point(x + FalseEasting, y + FalseNorthing);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-12 21:28:45 +01:00
|
|
|
|
public override Location MapToLocation(double x, double y)
|
2022-12-13 18:22:18 +01:00
|
|
|
|
{
|
2026-01-15 10:58:54 +01:00
|
|
|
|
var sign = Hemisphere == Hemisphere.North ? 1d : -1d;
|
|
|
|
|
|
x = sign * (x - FalseEasting);
|
|
|
|
|
|
y = sign * (y - FalseNorthing);
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
|
|
|
|
|
var e = Math.Sqrt((2d - Flattening) * Flattening);
|
|
|
|
|
|
var r = Math.Sqrt(x * x + y * y); // p.162 (20-18)
|
|
|
|
|
|
var t = r * Math.Sqrt(Math.Pow(1d + e, 1d + e) * Math.Pow(1d - e, 1d - e))
|
|
|
|
|
|
/ (2d * EquatorialRadius * ScaleFactor); // p.162 (21-39)
|
|
|
|
|
|
|
2026-01-13 23:24:48 +01:00
|
|
|
|
var phi = WorldMercatorProjection.ApproximateLatitude(e, t); // p.162 (3-5)
|
|
|
|
|
|
var lambda = Math.Atan2(x, -y); // p.162 (20-16)
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
2026-01-15 10:58:54 +01:00
|
|
|
|
return new Location(sign * phi * 180d / Math.PI, sign * lambda * 180d / Math.PI);
|
2022-12-13 18:22:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Universal Polar Stereographic North Projection - EPSG:32661.
|
|
|
|
|
|
/// </summary>
|
2026-01-15 10:58:54 +01:00
|
|
|
|
public class Wgs84UpsNorthProjection : PolarStereographicProjection
|
2022-12-13 18:22:18 +01:00
|
|
|
|
{
|
2024-07-12 14:14:42 +02:00
|
|
|
|
public const string DefaultCrsId = "EPSG:32661";
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
2026-01-15 10:58:54 +01:00
|
|
|
|
public Wgs84UpsNorthProjection() // parameterless constructor for XAML
|
2024-08-28 14:58:06 +02:00
|
|
|
|
: this(DefaultCrsId)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-15 10:58:54 +01:00
|
|
|
|
public Wgs84UpsNorthProjection(string crsId)
|
2022-12-13 18:22:18 +01:00
|
|
|
|
{
|
2024-07-12 13:57:27 +02:00
|
|
|
|
CrsId = crsId;
|
2026-01-10 16:21:55 +01:00
|
|
|
|
Hemisphere = Hemisphere.North;
|
2022-12-13 18:22:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Universal Polar Stereographic South Projection - EPSG:32761.
|
|
|
|
|
|
/// </summary>
|
2026-01-15 10:58:54 +01:00
|
|
|
|
public class Wgs84UpsSouthProjection : PolarStereographicProjection
|
2022-12-13 18:22:18 +01:00
|
|
|
|
{
|
2024-07-12 14:14:42 +02:00
|
|
|
|
public const string DefaultCrsId = "EPSG:32761";
|
2022-12-13 18:22:18 +01:00
|
|
|
|
|
2026-01-15 10:58:54 +01:00
|
|
|
|
public Wgs84UpsSouthProjection() // parameterless constructor for XAML
|
2024-08-28 14:58:06 +02:00
|
|
|
|
: this(DefaultCrsId)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-15 10:58:54 +01:00
|
|
|
|
public Wgs84UpsSouthProjection(string crsId)
|
2022-12-13 18:22:18 +01:00
|
|
|
|
{
|
2024-07-12 13:57:27 +02:00
|
|
|
|
CrsId = crsId;
|
2026-01-10 16:21:55 +01:00
|
|
|
|
Hemisphere = Hemisphere.South;
|
2022-12-13 18:22:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|