XAML-Map-Control/MapControl/Shared/WebMercatorProjection.cs
ClemensFischer e45180b26a Add map projections
Add TransverseMercatorProjection, PolarStereographicProjection and derived UTM/UPS projections to the base library.
2022-12-13 18:22:18 +01:00

69 lines
2 KiB
C#

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
#if !WINUI && !UWP
using System.Windows;
#endif
namespace MapControl
{
/// <summary>
/// Spherical Mercator Projection - EPSG:3857.
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.41-44.
/// </summary>
public class WebMercatorProjection : MapProjection
{
public const int DefaultEpsgCode = 3857;
public static readonly string DefaultCrsId = $"EPSG:{DefaultEpsgCode}";
public WebMercatorProjection()
{
Type = MapProjectionType.WebMercator;
CrsId = DefaultCrsId;
}
public override Scale GetRelativeScale(Location location)
{
var k = 1d / Math.Cos(location.Latitude * Math.PI / 180d); // p.44 (7-3)
return new Scale(k, k);
}
public override Point? LocationToMap(Location location)
{
return new Point(
Wgs84MeterPerDegree * location.Longitude,
Wgs84MeterPerDegree * LatitudeToY(location.Latitude));
}
public override Location MapToLocation(Point point)
{
return new Location(
YToLatitude(point.Y / Wgs84MeterPerDegree),
point.X / Wgs84MeterPerDegree);
}
public static double LatitudeToY(double latitude)
{
if (latitude <= -90d)
{
return double.NegativeInfinity;
}
if (latitude >= 90d)
{
return double.PositiveInfinity;
}
return Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) * 180d / Math.PI;
}
public static double YToLatitude(double y)
{
return 90d - Math.Atan(Math.Exp(-y * Math.PI / 180d)) * 360d / Math.PI;
}
}
}