XAML-Map-Control/MapControl/Shared/StereographicProjection.cs
2025-12-12 21:28:45 +01:00

59 lines
1.7 KiB
C#

using System;
#if WPF
using System.Windows;
#elif AVALONIA
using Avalonia;
#endif
namespace MapControl
{
/// <summary>
/// Spherical Stereographic Projection - AUTO2:97002.
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/publication/pp1395), p.157-160.
/// </summary>
public class StereographicProjection : AzimuthalProjection
{
public const string DefaultCrsId = "AUTO2:97002"; // GeoServer non-standard CRS ID
public StereographicProjection()
: this(DefaultCrsId)
{
// XAML needs parameterless constructor
}
public StereographicProjection(string crsId)
{
CrsId = crsId;
}
public override Point? LocationToMap(double latitude, double longitude)
{
if (Location.Equals(latitude, Center.Latitude) &&
Location.Equals(longitude, Center.Longitude))
{
return new Point();
}
Center.GetAzimuthDistance(latitude, longitude, out double azimuth, out double distance);
var mapDistance = Math.Tan(distance / 2d) * 2d * Wgs84EquatorialRadius;
return new Point(mapDistance * Math.Sin(azimuth), mapDistance * Math.Cos(azimuth));
}
public override Location MapToLocation(double x, double y)
{
if (x == 0d && y == 0d)
{
return new Location(Center.Latitude, Center.Longitude);
}
var azimuth = Math.Atan2(x, y);
var mapDistance = Math.Sqrt(x * x + y * y);
var distance = 2d * Math.Atan(mapDistance / (2d * Wgs84EquatorialRadius));
return Center.GetLocation(azimuth, distance);
}
}
}