2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2015-01-20 17:52:02 +01:00
|
|
|
|
// © 2015 Clemens Fischer
|
2012-05-04 12:52:20 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
#if NETFX_CORE
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
#else
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Windows;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms latitude and longitude values in degrees to cartesian coordinates
|
2012-11-26 19:17:12 +01:00
|
|
|
|
/// according to the Mercator projection.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MercatorTransform : MapTransform
|
|
|
|
|
|
{
|
2015-02-18 19:20:59 +01:00
|
|
|
|
public static readonly double MaxLatitudeValue = Math.Atan(Math.Sinh(Math.PI)) / Math.PI * 180d;
|
2013-04-04 18:01:13 +02:00
|
|
|
|
|
2015-02-18 19:20:59 +01:00
|
|
|
|
public static double RelativeScale(double latitude)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-02-18 19:20:59 +01:00
|
|
|
|
if (latitude <= -90d)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
return double.NegativeInfinity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-18 19:20:59 +01:00
|
|
|
|
if (latitude >= 90d)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
return double.PositiveInfinity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-18 19:20:59 +01:00
|
|
|
|
return 1d / Math.Cos(latitude * Math.PI / 180d);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-18 19:20:59 +01:00
|
|
|
|
public static double LatitudeToY(double latitude)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-02-18 19:20:59 +01:00
|
|
|
|
if (latitude <= -90d)
|
2013-11-21 21:16:29 +01:00
|
|
|
|
{
|
2015-02-18 19:20:59 +01:00
|
|
|
|
return double.NegativeInfinity;
|
2013-11-21 21:16:29 +01:00
|
|
|
|
}
|
2015-02-18 19:20:59 +01:00
|
|
|
|
|
|
|
|
|
|
if (latitude >= 90d)
|
2013-11-21 21:16:29 +01:00
|
|
|
|
{
|
2015-02-18 19:20:59 +01:00
|
|
|
|
return double.PositiveInfinity;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-18 19:20:59 +01:00
|
|
|
|
latitude *= Math.PI / 180d;
|
|
|
|
|
|
return Math.Log(Math.Tan(latitude) + 1d / Math.Cos(latitude)) / Math.PI * 180d;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-18 19:20:59 +01:00
|
|
|
|
public static double YToLatitude(double y)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Math.Atan(Math.Sinh(y * Math.PI / 180d)) / Math.PI * 180d;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override double MaxLatitude
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return MaxLatitudeValue; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override double RelativeScale(Location location)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-02-18 19:20:59 +01:00
|
|
|
|
return RelativeScale(location.Latitude);
|
|
|
|
|
|
}
|
2013-11-21 21:16:29 +01:00
|
|
|
|
|
2015-02-18 19:20:59 +01:00
|
|
|
|
public override Point Transform(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Point(location.Longitude, LatitudeToY(location.Latitude));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Location Transform(Point point)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Location(YToLatitude(point.Y), point.X);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|