XAML-Map-Control/MapControl/MercatorTransform.cs

78 lines
2 KiB
C#
Raw Normal View History

// XAML Map Control - http://xamlmapcontrol.codeplex.com/
2016-02-23 20:07:30 +01:00
// © 2016 Clemens Fischer
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
#if NETFX_CORE
using Windows.Foundation;
#else
2012-04-25 22:02:53 +02:00
using System.Windows;
#endif
2012-04-25 22:02:53 +02:00
namespace MapControl
{
/// <summary>
/// Transforms latitude and longitude values in degrees to cartesian coordinates
/// according to the Mercator projection.
2012-04-25 22:02:53 +02:00
/// </summary>
public class MercatorTransform : MapTransform
{
public static readonly double MaxLatitudeValue = YToLatitude(180d);
public static double RelativeScale(double latitude)
2012-04-25 22:02:53 +02:00
{
if (latitude <= -90d)
2012-04-25 22:02:53 +02:00
{
return double.NegativeInfinity;
}
if (latitude >= 90d)
2012-04-25 22:02:53 +02:00
{
return double.PositiveInfinity;
}
return 1d / Math.Cos(latitude * Math.PI / 180d);
2012-04-25 22:02:53 +02:00
}
public static double LatitudeToY(double latitude)
2012-04-25 22:02:53 +02:00
{
if (latitude <= -90d)
{
return double.NegativeInfinity;
}
if (latitude >= 90d)
{
return double.PositiveInfinity;
2012-04-25 22:02:53 +02:00
}
return Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) / Math.PI * 180d;
2012-04-25 22:02:53 +02:00
}
public static double YToLatitude(double y)
{
return Math.Atan(Math.Exp(y * Math.PI / 180d)) / Math.PI * 360d - 90d;
}
public override double MaxLatitude
{
get { return MaxLatitudeValue; }
}
public override double RelativeScale(Location location)
2012-04-25 22:02:53 +02:00
{
return RelativeScale(location.Latitude);
}
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
}
}
}