2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2017-06-25 23:05:48 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#if !WINUI && !UWP
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2018-12-20 21:55:12 +01:00
|
|
|
|
/// Spherical Orthographic Projection.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class OrthographicProjection : AzimuthalProjection
|
|
|
|
|
|
{
|
2022-01-19 16:43:00 +01:00
|
|
|
|
public const string DefaultCrsId = "AUTO2:42003";
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public OrthographicProjection()
|
|
|
|
|
|
{
|
2022-01-19 16:43:00 +01:00
|
|
|
|
CrsId = DefaultCrsId;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public override Point LocationToMap(Location location)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
if (location.Equals(Center))
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
return new Point();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var lat0 = Center.Latitude * Math.PI / 180d;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var lat = location.Latitude * Math.PI / 180d;
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var dLon = (location.Longitude - Center.Longitude) * Math.PI / 180d;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
return new Point(
|
2020-04-02 18:06:39 +02:00
|
|
|
|
Wgs84EquatorialRadius * Math.Cos(lat) * Math.Sin(dLon),
|
|
|
|
|
|
Wgs84EquatorialRadius * (Math.Cos(lat0) * Math.Sin(lat) - Math.Sin(lat0) * Math.Cos(lat) * Math.Cos(dLon)));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public override Location MapToLocation(Point point)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (point.X == 0d && point.Y == 0d)
|
|
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
return new Location(Center.Latitude, Center.Longitude);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-02 18:06:39 +02:00
|
|
|
|
var x = point.X / Wgs84EquatorialRadius;
|
|
|
|
|
|
var y = point.Y / Wgs84EquatorialRadius;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
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);
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var lat0 = Center.Latitude * Math.PI / 180d;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var cosLat0 = Math.Cos(lat0);
|
|
|
|
|
|
var sinLat0 = Math.Sin(lat0);
|
|
|
|
|
|
|
|
|
|
|
|
return new Location(
|
|
|
|
|
|
180d / Math.PI * Math.Asin(cosC * sinLat0 + y * sinC * cosLat0 / r),
|
2020-03-26 19:08:20 +01:00
|
|
|
|
180d / Math.PI * Math.Atan2(x * sinC, r * cosC * cosLat0 - y * sinC * sinLat0) + Center.Longitude);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|