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

90 lines
2.9 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;
using System.Windows.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
#endif
namespace MapControl
{
/// <summary>
2022-02-23 23:19:32 +01:00
/// Elliptical Mercator Projection - EPSG:3395.
2025-02-24 11:22:17 +01:00
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/publication/pp1395), p.44-45.
/// </summary>
public class WorldMercatorProjection : MapProjection
{
2024-07-12 14:14:42 +02:00
public const string DefaultCrsId = "EPSG:3395";
2026-01-10 23:29:42 +01:00
public WorldMercatorProjection() // parameterless constructor for XAML
: this(DefaultCrsId)
{
}
public WorldMercatorProjection(string crsId)
{
2026-01-24 17:42:00 +01:00
IsNormalCylindrical = true;
2024-07-12 13:57:27 +02:00
CrsId = crsId;
}
2026-01-27 22:56:09 +01:00
public override Matrix RelativeTransform(double latitude, double longitude)
{
2026-01-29 23:27:39 +01:00
var e2 = (2d - Flattening) * Flattening;
2026-02-01 17:25:04 +01:00
var phi = latitude * Math.PI / 180d;
2026-01-29 23:27:39 +01:00
var sinPhi = Math.Sin(phi);
var k = Math.Sqrt(1d - e2 * sinPhi * sinPhi) / Math.Cos(phi); // p.44 (7-8)
return new Matrix(k, 0d, 0d, k, 0d, 0d);
}
public override Point LocationToMap(double latitude, double longitude)
{
2026-01-29 23:27:39 +01:00
var x = EquatorialRadius * longitude * Math.PI / 180d;
double y;
if (latitude <= -90d)
{
2026-01-29 23:27:39 +01:00
y = double.NegativeInfinity;
}
2026-01-29 23:27:39 +01:00
else if (latitude >= 90d)
{
2026-01-29 23:27:39 +01:00
y = double.PositiveInfinity;
}
2026-01-29 23:27:39 +01:00
else
{
var phi = latitude * Math.PI / 180d;
var e = Math.Sqrt((2d - Flattening) * Flattening);
var eSinPhi = e * Math.Sin(phi);
2026-02-03 20:46:32 +01:00
var p = Math.Pow((1d - eSinPhi) / (1d + eSinPhi), e / 2d);
2026-02-03 20:46:32 +01:00
y = EquatorialRadius * Math.Log(Math.Tan(phi / 2d + Math.PI / 4d) * p); // p.44 (7-7)
2026-01-29 23:27:39 +01:00
}
2026-01-29 23:27:39 +01:00
return new Point(x, y);
}
2026-01-29 23:27:39 +01:00
public override Location MapToLocation(double x, double y)
{
2026-01-29 23:27:39 +01:00
var t = Math.Exp(-y / EquatorialRadius); // p.44 (7-10)
var phi = ApproximateLatitude((2d - Flattening) * Flattening, t); // p.45 (3-5)
var lambda = x / EquatorialRadius;
2026-01-29 23:27:39 +01:00
return new Location(phi * 180d / Math.PI, lambda * 180d / Math.PI);
}
2026-01-29 23:27:39 +01:00
internal static double ApproximateLatitude(double e2, double t)
{
2026-01-13 23:24:48 +01:00
var e4 = e2 * e2;
var e6 = e2 * e4;
var e8 = e2 * e6;
var chi = Math.PI / 2d - 2d * Math.Atan(t); // p.45 (7-13)
2026-01-16 20:22:45 +01:00
return chi +
(e2 / 2d + e4 * 5d / 24d + e6 / 12d + e8 * 13d / 360d) * Math.Sin(2d * chi) +
(e4 * 7d / 48d + e6 * 29d / 240d + e8 * 811d / 11520d) * Math.Sin(4d * chi) +
(e6 * 7d / 120d + e8 * 81d / 1120d) * Math.Sin(6d * chi) +
e8 * 4279d / 161280d * Math.Sin(8d * chi); // p.45 (3-5)
}
}
}