XAML-Map-Control/MapControl/Shared/OrthographicProjection.cs

70 lines
2.1 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2019 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
#if !WINDOWS_UWP
using System.Windows;
#endif
namespace MapControl
{
/// <summary>
2018-12-20 21:55:12 +01:00
/// Spherical Orthographic Projection.
/// </summary>
public class OrthographicProjection : AzimuthalProjection
{
public OrthographicProjection()
{
CrsId = "AUTO2:42003";
}
public override Point LocationToPoint(Location location)
{
2017-08-16 21:27:12 +02:00
if (location.Equals(ProjectionCenter))
{
return new Point();
}
2017-08-16 21:27:12 +02:00
var lat0 = ProjectionCenter.Latitude * Math.PI / 180d;
var lat = location.Latitude * Math.PI / 180d;
2017-08-16 21:27:12 +02:00
var dLon = (location.Longitude - ProjectionCenter.Longitude) * Math.PI / 180d;
2018-12-20 21:55:12 +01:00
var s = TrueScale * 180d / Math.PI;
return new Point(
2018-12-20 21:55:12 +01:00
s * Math.Cos(lat) * Math.Sin(dLon),
s * (Math.Cos(lat0) * Math.Sin(lat) - Math.Sin(lat0) * Math.Cos(lat) * Math.Cos(dLon)));
}
public override Location PointToLocation(Point point)
{
if (point.X == 0d && point.Y == 0d)
{
return new Location(ProjectionCenter.Latitude, ProjectionCenter.Longitude);
}
2018-12-20 21:55:12 +01:00
var s = TrueScale * 180d / Math.PI;
var x = point.X / s;
var y = point.Y / s;
var r2 = x * x + y * y;
if (r2 > 1d)
{
return new Location(double.NaN, double.NaN);
}
var r = Math.Sqrt(r2);
var sinC = r;
var cosC = Math.Sqrt(1 - r2);
2017-08-16 21:27:12 +02:00
var lat0 = ProjectionCenter.Latitude * Math.PI / 180d;
var cosLat0 = Math.Cos(lat0);
var sinLat0 = Math.Sin(lat0);
return new Location(
180d / Math.PI * Math.Asin(cosC * sinLat0 + y * sinC * cosLat0 / r),
2017-08-16 21:27:12 +02:00
180d / Math.PI * Math.Atan2(x * sinC, r * cosC * cosLat0 - y * sinC * sinLat0) + ProjectionCenter.Longitude);
}
}
}