2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2020-01-09 19:40:10 +01:00
|
|
|
|
// © 2020 Clemens Fischer
|
2017-06-25 23:05:48 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-03-06 22:22:58 +01:00
|
|
|
|
#if !WINDOWS_UWP
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2018-12-20 21:55:12 +01:00
|
|
|
|
/// Spherical Mercator Projection, EPSG:3857.
|
|
|
|
|
|
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.41-44.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class WebMercatorProjection : MapProjection
|
|
|
|
|
|
{
|
2019-12-12 19:23:41 +01:00
|
|
|
|
private static readonly double maxLatitude = YToLatitude(180d);
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public WebMercatorProjection()
|
|
|
|
|
|
{
|
2019-12-12 19:23:41 +01:00
|
|
|
|
CrsId = "EPSG:3857";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool IsWebMercator
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return true; }
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-12 19:23:41 +01:00
|
|
|
|
public override double MaxLatitude
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2019-12-12 19:23:41 +01:00
|
|
|
|
get { return maxLatitude; }
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public override Vector GetRelativeScale(Location location)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
var k = 1d / Math.Cos(location.Latitude * Math.PI / 180d); // p.44 (7-3)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
return new Vector(k, k);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public override Point LocationToMap(Location location)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
return new Point(
|
2020-04-02 18:06:39 +02:00
|
|
|
|
Wgs84MetersPerDegree * location.Longitude,
|
|
|
|
|
|
Wgs84MetersPerDegree * LatitudeToY(location.Latitude));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public override Location MapToLocation(Point point)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
return new Location(
|
2020-04-02 18:06:39 +02:00
|
|
|
|
YToLatitude(point.Y / Wgs84MetersPerDegree),
|
|
|
|
|
|
point.X / Wgs84MetersPerDegree);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double LatitudeToY(double latitude)
|
|
|
|
|
|
{
|
2018-02-09 17:43:47 +01:00
|
|
|
|
if (latitude <= -90d)
|
|
|
|
|
|
{
|
|
|
|
|
|
return double.NegativeInfinity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (latitude >= 90d)
|
|
|
|
|
|
{
|
|
|
|
|
|
return double.PositiveInfinity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
return Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) * 180d / Math.PI;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double YToLatitude(double y)
|
|
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
return 90d - Math.Atan(Math.Exp(-y * Math.PI / 180d)) * 360d / Math.PI;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|