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;
|
2026-01-20 09:48:16 +01:00
|
|
|
|
using System.Windows.Media;
|
2025-08-19 19:43:02 +02:00
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using Avalonia;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2022-03-05 18:40:57 +01:00
|
|
|
|
public enum MapProjectionType
|
|
|
|
|
|
{
|
|
|
|
|
|
WebMercator, // normal cylindrical projection compatible with MapTileLayer
|
|
|
|
|
|
NormalCylindrical,
|
|
|
|
|
|
TransverseCylindrical,
|
|
|
|
|
|
Azimuthal,
|
|
|
|
|
|
Other
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// <summary>
|
2020-03-26 19:08:20 +01:00
|
|
|
|
/// Defines a map projection between geographic coordinates and cartesian map coordinates.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2025-09-06 13:06:00 +02:00
|
|
|
|
#if UWP || WINUI
|
|
|
|
|
|
[Windows.Foundation.Metadata.CreateFromString(MethodName = "Parse")]
|
|
|
|
|
|
#else
|
|
|
|
|
|
[System.ComponentModel.TypeConverter(typeof(MapProjectionConverter))]
|
|
|
|
|
|
#endif
|
2026-01-20 22:21:26 +01:00
|
|
|
|
public abstract class MapProjection(bool hasCenter = false)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2018-02-09 17:43:47 +01:00
|
|
|
|
public const double Wgs84EquatorialRadius = 6378137d;
|
2018-12-20 21:55:12 +01:00
|
|
|
|
public const double Wgs84Flattening = 1d / 298.257223563;
|
2026-01-20 15:48:30 +01:00
|
|
|
|
public const double Wgs84MeterPerDegree = Wgs84EquatorialRadius * Math.PI / 180d;
|
2026-01-09 08:13:07 +01:00
|
|
|
|
|
2026-01-10 23:02:13 +01:00
|
|
|
|
// Arithmetic mean radius (2*a + b) / 3 == (1 - f/3) * a.
|
|
|
|
|
|
// See https://en.wikipedia.org/wiki/Earth_radius#Arithmetic_mean_radius.
|
2026-01-09 08:13:07 +01:00
|
|
|
|
//
|
|
|
|
|
|
public const double Wgs84MeanRadius = (1d - Wgs84Flattening / 3d) * Wgs84EquatorialRadius;
|
2018-12-20 21:55:12 +01:00
|
|
|
|
|
2025-09-20 14:01:51 +02:00
|
|
|
|
public static MapProjectionFactory Factory
|
|
|
|
|
|
{
|
2025-12-27 21:24:01 +01:00
|
|
|
|
get => field ??= new MapProjectionFactory();
|
|
|
|
|
|
set;
|
2025-09-20 14:01:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-17 16:06:50 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates a MapProjection instance from a CRS identifier string.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static MapProjection Parse(string crsId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Factory.GetProjection(crsId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
/// <summary>
|
2022-03-05 18:40:57 +01:00
|
|
|
|
/// Gets the type of the projection.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2022-03-05 18:40:57 +01:00
|
|
|
|
public MapProjectionType Type { get; protected set; } = MapProjectionType.Other;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-03-05 18:40:57 +01:00
|
|
|
|
/// Gets the WMS 1.3.0 CRS identifier.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2024-07-12 18:42:18 +02:00
|
|
|
|
public string CrsId { get; protected set; } = "";
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2026-01-20 22:21:26 +01:00
|
|
|
|
private Location center;
|
|
|
|
|
|
private bool updateCenter = hasCenter;
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// <summary>
|
2026-01-20 22:21:26 +01:00
|
|
|
|
/// Gets or sets an optional projection center. If the property is set to a non-null value,
|
|
|
|
|
|
/// it overrides the projection center set by MapBase.Center or MapBase.ProjectionCenter.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2026-01-17 16:06:50 +01:00
|
|
|
|
public Location Center
|
|
|
|
|
|
{
|
2026-01-20 22:21:26 +01:00
|
|
|
|
get => center ??= new Location();
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
updateCenter = true;
|
|
|
|
|
|
|
|
|
|
|
|
if (value != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetCenter(value);
|
|
|
|
|
|
updateCenter = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Called by MapBase.UpdateTransform().
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal void SetCenter(Location value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (updateCenter)
|
2026-01-17 16:06:50 +01:00
|
|
|
|
{
|
2026-01-20 22:21:26 +01:00
|
|
|
|
var latitude = value.Latitude;
|
2026-01-17 16:06:50 +01:00
|
|
|
|
var longitude = Location.NormalizeLongitude(value.Longitude);
|
|
|
|
|
|
|
2026-01-20 22:21:26 +01:00
|
|
|
|
if (center == null || !center.Equals(latitude, longitude))
|
2026-01-17 16:06:50 +01:00
|
|
|
|
{
|
2026-01-20 22:21:26 +01:00
|
|
|
|
center = new Location(latitude, longitude);
|
2026-01-17 16:06:50 +01:00
|
|
|
|
CenterChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void CenterChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
/// <summary>
|
2026-01-20 09:48:16 +01:00
|
|
|
|
/// Gets the relative scale at the specified geographic coordinates.
|
|
|
|
|
|
/// The returned Matrix represents the local distortion of the map projection.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2026-01-20 09:48:16 +01:00
|
|
|
|
public virtual Matrix RelativeScale(double latitude, double longitude) => new Matrix(1d, 0d, 0d, 1d, 0d, 0d);
|
2025-12-12 21:28:45 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms geographic coordinates to a Point in projected map coordinates.
|
|
|
|
|
|
/// Returns null when the location can not be transformed.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract Point? LocationToMap(double latitude, double longitude);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms projected map coordinates to a Location in geographic coordinates.
|
|
|
|
|
|
/// Returns null when the coordinates can not be transformed.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract Location MapToLocation(double x, double y);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the relative map scale at the specified geographic Location.
|
|
|
|
|
|
/// </summary>
|
2026-01-20 09:48:16 +01:00
|
|
|
|
public Matrix RelativeScale(Location location) => RelativeScale(location.Latitude, location.Longitude);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-05 17:32:29 +01:00
|
|
|
|
/// Transforms a Location in geographic coordinates to a Point in projected map coordinates.
|
2022-12-02 16:50:10 +01:00
|
|
|
|
/// Returns null when the Location can not be transformed.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2025-12-12 21:28:45 +01:00
|
|
|
|
public Point? LocationToMap(Location location) => LocationToMap(location.Latitude, location.Longitude);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-05 17:32:29 +01:00
|
|
|
|
/// Transforms a Point in projected map coordinates to a Location in geographic coordinates.
|
2022-03-04 22:28:18 +01:00
|
|
|
|
/// Returns null when the Point can not be transformed.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2025-12-12 21:28:45 +01:00
|
|
|
|
public Location MapToLocation(Point point) => MapToLocation(point.X, point.Y);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-19 17:24:18 +02:00
|
|
|
|
/// Transforms a BoundingBox in geographic coordinates to a Rect in projected map coordinates.
|
2022-12-02 16:50:10 +01:00
|
|
|
|
/// Returns null when the BoundingBox can not be transformed.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2024-05-19 17:24:18 +02:00
|
|
|
|
public virtual Rect? BoundingBoxToMap(BoundingBox boundingBox)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2025-12-12 21:28:45 +01:00
|
|
|
|
var southWest = LocationToMap(boundingBox.South, boundingBox.West);
|
|
|
|
|
|
var northEast = LocationToMap(boundingBox.North, boundingBox.East);
|
2022-12-08 10:37:56 +01:00
|
|
|
|
|
2025-12-13 18:28:07 +01:00
|
|
|
|
return southWest.HasValue && northEast.HasValue ? new Rect(southWest.Value, northEast.Value) : null;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-19 17:24:18 +02:00
|
|
|
|
/// Transforms a Rect in projected map coordinates to a BoundingBox in geographic coordinates.
|
2022-12-02 16:50:10 +01:00
|
|
|
|
/// Returns null when the MapRect can not be transformed.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2024-09-09 16:44:45 +02:00
|
|
|
|
public virtual BoundingBox MapToBoundingBox(Rect rect)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2025-12-12 21:28:45 +01:00
|
|
|
|
var southWest = MapToLocation(rect.X, rect.Y);
|
|
|
|
|
|
var northEast = MapToLocation(rect.X + rect.Width, rect.Y + rect.Height);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2025-12-13 18:28:07 +01:00
|
|
|
|
return southWest != null && northEast != null ? new BoundingBox(southWest, northEast) : null;
|
2019-12-12 19:23:41 +01:00
|
|
|
|
}
|
2024-09-09 16:44:45 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a LatLonBox in geographic coordinates to a rotated Rect in projected map coordinates.
|
|
|
|
|
|
/// Returns null when the LatLonBox can not be transformed.
|
|
|
|
|
|
/// </summary>
|
2025-12-30 08:27:52 +01:00
|
|
|
|
public virtual (Rect?, double) LatLonBoxToMap(LatLonBox latLonBox)
|
2024-09-09 16:44:45 +02:00
|
|
|
|
{
|
2025-12-30 08:27:52 +01:00
|
|
|
|
Rect? rect = null;
|
|
|
|
|
|
var rotation = 0d;
|
2026-01-17 12:06:21 +01:00
|
|
|
|
var centerLatitude = (latLonBox.South + latLonBox.North) / 2d;
|
|
|
|
|
|
var centerLongitude = (latLonBox.West + latLonBox.East) / 2d;
|
2025-12-30 08:27:52 +01:00
|
|
|
|
Point? center, north, south, west, east;
|
2025-12-12 21:28:45 +01:00
|
|
|
|
|
|
|
|
|
|
if ((center = LocationToMap(centerLatitude, centerLongitude)).HasValue &&
|
|
|
|
|
|
(north = LocationToMap(latLonBox.North, centerLongitude)).HasValue &&
|
|
|
|
|
|
(south = LocationToMap(latLonBox.South, centerLongitude)).HasValue &&
|
|
|
|
|
|
(west = LocationToMap(centerLatitude, latLonBox.West)).HasValue &&
|
|
|
|
|
|
(east = LocationToMap(centerLatitude, latLonBox.East)).HasValue)
|
2024-09-09 16:44:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
var dx1 = east.Value.X - west.Value.X;
|
|
|
|
|
|
var dy1 = east.Value.Y - west.Value.Y;
|
|
|
|
|
|
var dx2 = north.Value.X - south.Value.X;
|
|
|
|
|
|
var dy2 = north.Value.Y - south.Value.Y;
|
|
|
|
|
|
var width = Math.Sqrt(dx1 * dx1 + dy1 * dy1);
|
|
|
|
|
|
var height = Math.Sqrt(dx2 * dx2 + dy2 * dy2);
|
|
|
|
|
|
var x = center.Value.X - width / 2d;
|
|
|
|
|
|
var y = center.Value.Y - height / 2d;
|
|
|
|
|
|
|
2024-09-11 18:33:16 +02:00
|
|
|
|
// Additional rotation caused by the projection, calculated as mean value
|
|
|
|
|
|
// of the two angles measured relative to the east and north axis.
|
|
|
|
|
|
//
|
2024-09-09 16:44:45 +02:00
|
|
|
|
var r1 = (Math.Atan2(dy1, dx1) * 180d / Math.PI + 180d) % 360d - 180d;
|
|
|
|
|
|
var r2 = (Math.Atan2(-dx2, dy2) * 180d / Math.PI + 180d) % 360d - 180d;
|
|
|
|
|
|
|
2025-12-30 08:27:52 +01:00
|
|
|
|
rect = new Rect(x, y, width, height);
|
|
|
|
|
|
rotation = latLonBox.Rotation + (r1 + r2) / 2d;
|
2024-09-09 16:44:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 08:27:52 +01:00
|
|
|
|
return (rect, rotation);
|
2024-09-09 16:44:45 +02:00
|
|
|
|
}
|
2025-09-06 13:06:00 +02:00
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return CrsId;
|
|
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|