XAML-Map-Control/MapControl/MercatorTransform.cs

64 lines
1.7 KiB
C#
Raw Normal View History

2012-05-04 12:52:20 +02:00
// WPF MapControl - http://wpfmapcontrol.codeplex.com/
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2012-04-25 22:02:53 +02:00
using System.Windows;
using System.Windows.Media;
namespace MapControl
{
/// <summary>
/// Transforms latitude and longitude values in degrees to cartesian coordinates
/// according to the Mercator transform.
/// </summary>
public class MercatorTransform : MapTransform
{
public override double MaxLatitude
{
get { return 85.0511; }
}
2012-05-04 12:52:20 +02:00
public override double RelativeScale(Location location)
2012-04-25 22:02:53 +02:00
{
2012-05-04 12:52:20 +02:00
if (location.Latitude <= -90d)
2012-04-25 22:02:53 +02:00
{
return double.NegativeInfinity;
}
2012-05-04 12:52:20 +02:00
if (location.Latitude >= 90d)
2012-04-25 22:02:53 +02:00
{
return double.PositiveInfinity;
}
2012-05-04 12:52:20 +02:00
return 1d / Math.Cos(location.Latitude * Math.PI / 180d);
2012-04-25 22:02:53 +02:00
}
2012-05-04 12:52:20 +02:00
public override Point Transform(Location location)
2012-04-25 22:02:53 +02:00
{
2012-05-04 12:52:20 +02:00
Point result = new Point(location.Longitude, 0d);
2012-04-25 22:02:53 +02:00
2012-05-04 12:52:20 +02:00
if (location.Latitude <= -90d)
2012-04-25 22:02:53 +02:00
{
result.Y = double.NegativeInfinity;
}
2012-05-04 12:52:20 +02:00
else if (location.Latitude >= 90d)
2012-04-25 22:02:53 +02:00
{
result.Y = double.PositiveInfinity;
}
else
{
2012-05-04 12:52:20 +02:00
double lat = location.Latitude * Math.PI / 180d;
2012-04-25 22:02:53 +02:00
result.Y = (Math.Log(Math.Tan(lat) + 1d / Math.Cos(lat))) / Math.PI * 180d;
}
2012-05-04 12:52:20 +02:00
return result;
2012-04-25 22:02:53 +02:00
}
2012-05-04 12:52:20 +02:00
public override Location TransformBack(Point point)
2012-04-25 22:02:53 +02:00
{
2012-05-04 12:52:20 +02:00
return new Location(Math.Atan(Math.Sinh(point.Y * Math.PI / 180d)) / Math.PI * 180d, point.X);
2012-04-25 22:02:53 +02:00
}
}
}