2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using System.Windows;
|
2025-08-19 19:43:02 +02:00
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using Avalonia;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2022-02-23 23:19:32 +01:00
|
|
|
|
/// Spherical Mercator Projection - EPSG:3857.
|
2025-02-24 11:22:17 +01:00
|
|
|
|
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/publication/pp1395), p.41-44.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class WebMercatorProjection : MapProjection
|
|
|
|
|
|
{
|
2024-07-12 14:14:42 +02:00
|
|
|
|
public const string DefaultCrsId = "EPSG:3857";
|
2022-01-19 16:43:00 +01:00
|
|
|
|
|
2024-08-28 14:58:06 +02:00
|
|
|
|
public WebMercatorProjection()
|
|
|
|
|
|
: this(DefaultCrsId)
|
|
|
|
|
|
{
|
|
|
|
|
|
// XAML needs parameterless constructor
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public WebMercatorProjection(string crsId)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2022-03-05 18:40:57 +01:00
|
|
|
|
Type = MapProjectionType.WebMercator;
|
2024-07-12 13:57:27 +02:00
|
|
|
|
CrsId = crsId;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-19 17:24:18 +02:00
|
|
|
|
public override Point GetRelativeScale(Location location)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
var k = 1d / Math.Cos(location.Latitude * Math.PI / 180d); // p.44 (7-3)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2024-05-19 17:24:18 +02:00
|
|
|
|
return new Point(k, k);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-02 16:50:10 +01:00
|
|
|
|
public override Point? LocationToMap(Location location)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
return new Point(
|
2022-03-02 22:03:18 +01:00
|
|
|
|
Wgs84MeterPerDegree * location.Longitude,
|
|
|
|
|
|
Wgs84MeterPerDegree * LatitudeToY(location.Latitude));
|
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
|
|
|
|
{
|
|
|
|
|
|
return new Location(
|
2022-03-02 22:03:18 +01:00
|
|
|
|
YToLatitude(point.Y / Wgs84MeterPerDegree),
|
|
|
|
|
|
point.X / Wgs84MeterPerDegree);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double LatitudeToY(double latitude)
|
|
|
|
|
|
{
|
2018-02-09 17:43:47 +01:00
|
|
|
|
if (latitude <= -90d)
|
|
|
|
|
|
{
|
|
|
|
|
|
return double.NegativeInfinity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (latitude >= 90d)
|
|
|
|
|
|
{
|
|
|
|
|
|
return double.PositiveInfinity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
return Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) * 180d / Math.PI;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static double YToLatitude(double y)
|
|
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
return 90d - Math.Atan(Math.Exp(-y * Math.PI / 180d)) * 360d / Math.PI;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|