XAML-Map-Control/MapControl/MercatorTransform.cs

79 lines
2 KiB
C#
Raw Normal View History

// 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;
#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 = Math.Atan(Math.Sinh(Math.PI)) / Math.PI * 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
}
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
}
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
{
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
}
}
}