2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2018-02-09 17:43:47 +01:00
|
|
|
|
// © 2018 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
|
|
|
|
|
|
{
|
|
|
|
|
|
public WebMercatorProjection()
|
|
|
|
|
|
: this("EPSG:3857")
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public WebMercatorProjection(string crsId)
|
|
|
|
|
|
{
|
|
|
|
|
|
CrsId = crsId;
|
2018-12-20 21:55:12 +01:00
|
|
|
|
IsNormalCylindrical = true;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
IsWebMercator = true;
|
|
|
|
|
|
MaxLatitude = YToLatitude(180d);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-07 22:19:16 +01:00
|
|
|
|
public override Vector GetMapScale(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
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
return new Vector(ViewportScale * k, ViewportScale * k);
|
2017-06-25 23:05:48 +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-06-25 23:05:48 +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-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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|