2017-10-27 17:15:18 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2018-02-09 17:43:47 +01:00
|
|
|
|
// © 2018 Clemens Fischer
|
2017-10-27 17:15:18 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-03-06 22:22:58 +01:00
|
|
|
|
#if !WINDOWS_UWP
|
2017-10-27 17:15:18 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2018-12-20 21:55:12 +01:00
|
|
|
|
/// Elliptical Mercator Projection, EPSG:3395.
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
public static double ConvergenceTolerance = 1e-6;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
public static int MaxIterations = 10;
|
|
|
|
|
|
|
|
|
|
|
|
public WorldMercatorProjection()
|
|
|
|
|
|
: this("EPSG:3395")
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public WorldMercatorProjection(string crsId)
|
|
|
|
|
|
{
|
|
|
|
|
|
CrsId = crsId;
|
2018-12-20 21:55:12 +01:00
|
|
|
|
IsNormalCylindrical = true;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
MaxLatitude = YToLatitude(180d);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-07 22:19:16 +01:00
|
|
|
|
public override Vector GetMapScale(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
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
return new Vector(ViewportScale * k, ViewportScale * k);
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Point LocationToPoint(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Point(
|
2018-02-09 17:43:47 +01:00
|
|
|
|
TrueScale * location.Longitude,
|
|
|
|
|
|
TrueScale * LatitudeToY(location.Latitude));
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Location PointToLocation(Point point)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Location(
|
2018-02-09 17:43:47 +01:00
|
|
|
|
YToLatitude(point.Y / TrueScale),
|
|
|
|
|
|
point.X / TrueScale);
|
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;
|
|
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
return Math.Log(Math.Tan(lat / 2d + Math.PI / 4d)
|
|
|
|
|
|
* ConformalFactor(lat)) * 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)
|
|
|
|
|
|
var lat = Math.PI / 2d - 2d * Math.Atan(t); // p.44 (7-11)
|
|
|
|
|
|
var relChange = 1d;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
for (var i = 0; i < MaxIterations && relChange > ConvergenceTolerance; i++)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
var newLat = Math.PI / 2d - 2d * Math.Atan(t * ConformalFactor(lat)); // p.44 (7-9)
|
|
|
|
|
|
relChange = Math.Abs(1d - newLat / lat);
|
2017-10-27 17:15:18 +02:00
|
|
|
|
lat = newLat;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
return lat * 180d / Math.PI;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static double ConformalFactor(double lat)
|
|
|
|
|
|
{
|
|
|
|
|
|
var eSinLat = Wgs84Eccentricity * Math.Sin(lat);
|
|
|
|
|
|
|
|
|
|
|
|
return Math.Pow((1d - eSinLat) / (1d + eSinLat), Wgs84Eccentricity / 2d);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|