2017-10-27 17:15:18 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2024-02-03 21:01:53 +01:00
|
|
|
|
// Copyright © 2024 Clemens Fischer
|
2017-10-27 17:15:18 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
2017-10-27 17:15:18 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2022-02-23 23:19:32 +01:00
|
|
|
|
/// Elliptical Mercator Projection - EPSG:3395.
|
2018-12-20 21:55:12 +01:00
|
|
|
|
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.44-45.
|
2017-10-27 17:15:18 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class WorldMercatorProjection : MapProjection
|
|
|
|
|
|
{
|
2024-07-12 13:57:27 +02:00
|
|
|
|
public const int EpsgCode = 3395;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2024-07-12 13:57:27 +02:00
|
|
|
|
public WorldMercatorProjection(string crsId = "EPSG:3395")
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2022-03-05 18:40:57 +01:00
|
|
|
|
Type = MapProjectionType.NormalCylindrical;
|
2024-07-12 13:57:27 +02:00
|
|
|
|
CrsId = crsId;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-19 17:24:18 +02:00
|
|
|
|
public override Point GetRelativeScale(Location location)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
|
var lat = location.Latitude * Math.PI / 180d;
|
|
|
|
|
|
var eSinLat = Wgs84Eccentricity * Math.Sin(lat);
|
2018-12-20 21:55:12 +01:00
|
|
|
|
var k = Math.Sqrt(1d - eSinLat * eSinLat) / Math.Cos(lat); // p.44 (7-8)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2024-05-19 17:24:18 +02:00
|
|
|
|
return new Point(k, k);
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-02 16:50:10 +01:00
|
|
|
|
public override Point? LocationToMap(Location location)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
|
return new Point(
|
2022-03-02 22:03:18 +01:00
|
|
|
|
Wgs84MeterPerDegree * location.Longitude,
|
|
|
|
|
|
Wgs84MeterPerDegree * LatitudeToY(location.Latitude));
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public override Location MapToLocation(Point point)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
|
return new Location(
|
2022-03-02 22:03:18 +01:00
|
|
|
|
YToLatitude(point.Y / Wgs84MeterPerDegree),
|
|
|
|
|
|
point.X / Wgs84MeterPerDegree);
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double LatitudeToY(double latitude)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (latitude <= -90d)
|
|
|
|
|
|
{
|
|
|
|
|
|
return double.NegativeInfinity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (latitude >= 90d)
|
|
|
|
|
|
{
|
|
|
|
|
|
return double.PositiveInfinity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var lat = latitude * Math.PI / 180d;
|
2022-12-13 18:22:18 +01:00
|
|
|
|
var eSinLat = Wgs84Eccentricity * Math.Sin(lat);
|
|
|
|
|
|
var f = Math.Pow((1d - eSinLat) / (1d + eSinLat), Wgs84Eccentricity / 2d);
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2022-12-13 18:22:18 +01:00
|
|
|
|
return Math.Log(Math.Tan(lat / 2d + Math.PI / 4d) * f) * 180d / Math.PI; // p.44 (7-7)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double YToLatitude(double y)
|
|
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
var t = Math.Exp(-y * Math.PI / 180d); // p.44 (7-10)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2022-12-13 18:22:18 +01:00
|
|
|
|
return LatitudeFromSeriesApproximation(Wgs84Eccentricity, t) * 180d / Math.PI;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-13 18:22:18 +01:00
|
|
|
|
internal static double LatitudeFromSeriesApproximation(double e, double t)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2022-12-13 18:22:18 +01:00
|
|
|
|
var e_2 = e * e;
|
|
|
|
|
|
var e_4 = e_2 * e_2;
|
|
|
|
|
|
var e_6 = e_2 * e_4;
|
|
|
|
|
|
var e_8 = e_2 * e_6;
|
|
|
|
|
|
|
|
|
|
|
|
var lat = Math.PI / 2d - 2d * Math.Atan(t); // p.45 (7-13)
|
|
|
|
|
|
|
|
|
|
|
|
return lat
|
|
|
|
|
|
+ (e_2 / 2d + 5d * e_4 / 24d + e_6 / 12d + 13d * e_8 / 360d) * Math.Sin(2d * lat)
|
|
|
|
|
|
+ (7d * e_4 / 48d + 29d * e_6 / 240d + 811d * e_8 / 11520d) * Math.Sin(4d * lat)
|
|
|
|
|
|
+ (7d * e_6 / 120d + 81d * e_8 / 1120d) * Math.Sin(6d * lat)
|
|
|
|
|
|
+ (4279d * e_8 / 161280d) * Math.Sin(8d * lat); // p.45 (3-5)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|