2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2017 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
#if WINDOWS_UWP
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2017-10-27 17:15:18 +02:00
|
|
|
|
/// Transforms map coordinates according to the Web (or Pseudo) Mercator Projection, EPSG:3857.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// Longitude values are transformed linearly to X values in meters, by multiplying with MetersPerDegree.
|
|
|
|
|
|
/// Latitude values in the interval [-MaxLatitude .. MaxLatitude] are transformed to Y values in meters
|
|
|
|
|
|
/// in the interval [-R*pi .. R*pi], R=Wgs84EquatorialRadius.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class WebMercatorProjection : MapProjection
|
|
|
|
|
|
{
|
|
|
|
|
|
public WebMercatorProjection()
|
|
|
|
|
|
: this("EPSG:3857")
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public WebMercatorProjection(string crsId)
|
|
|
|
|
|
{
|
|
|
|
|
|
CrsId = crsId;
|
|
|
|
|
|
IsWebMercator = true;
|
|
|
|
|
|
LongitudeScale = MetersPerDegree;
|
|
|
|
|
|
MaxLatitude = YToLatitude(180d);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override double GetViewportScale(double zoomLevel)
|
|
|
|
|
|
{
|
2017-08-04 21:38:58 +02:00
|
|
|
|
return DegreesToViewportScale(zoomLevel) / MetersPerDegree;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Point GetMapScale(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
var scale = ViewportScale / Math.Cos(location.Latitude * Math.PI / 180d);
|
|
|
|
|
|
|
|
|
|
|
|
return new Point(scale, scale);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Point LocationToPoint(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Point(
|
|
|
|
|
|
MetersPerDegree * location.Longitude,
|
|
|
|
|
|
MetersPerDegree * LatitudeToY(location.Latitude));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Location PointToLocation(Point point)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Location(
|
|
|
|
|
|
YToLatitude(point.Y / MetersPerDegree),
|
|
|
|
|
|
point.X / MetersPerDegree);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Location TranslateLocation(Location location, Point translation)
|
|
|
|
|
|
{
|
|
|
|
|
|
var scaleX = MetersPerDegree * ViewportScale;
|
|
|
|
|
|
var scaleY = scaleX / Math.Cos(location.Latitude * Math.PI / 180d);
|
|
|
|
|
|
|
|
|
|
|
|
return new Location(
|
|
|
|
|
|
location.Latitude - translation.Y / scaleY,
|
|
|
|
|
|
location.Longitude + translation.X / scaleX);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double LatitudeToY(double latitude)
|
|
|
|
|
|
{
|
|
|
|
|
|
return latitude <= -90d ? double.NegativeInfinity
|
|
|
|
|
|
: latitude >= 90d ? double.PositiveInfinity
|
|
|
|
|
|
: Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) / Math.PI * 180d;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double YToLatitude(double y)
|
|
|
|
|
|
{
|
2017-10-27 17:15:18 +02:00
|
|
|
|
return 90d - Math.Atan(Math.Exp(-y * Math.PI / 180d)) / Math.PI * 360d;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|