XAML-Map-Control/MapControl/Shared/MapProjection.cs

136 lines
5.4 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
#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
}
/// <summary>
/// Defines a map projection between geographic coordinates and cartesian map coordinates.
/// </summary>
2017-08-04 21:38:58 +02:00
public abstract class MapProjection
{
public const double Wgs84EquatorialRadius = 6378137d;
2022-03-02 22:03:18 +01:00
public const double Wgs84MeterPerDegree = Wgs84EquatorialRadius * Math.PI / 180d;
2018-12-20 21:55:12 +01:00
public const double Wgs84Flattening = 1d / 298.257223563;
public static readonly double Wgs84Eccentricity = Math.Sqrt((2d - Wgs84Flattening) * Wgs84Flattening);
/// <summary>
2022-03-05 18:40:57 +01:00
/// Gets the type of the projection.
/// </summary>
2022-03-05 18:40:57 +01:00
public MapProjectionType Type { get; protected set; } = MapProjectionType.Other;
/// <summary>
2022-03-05 18:40:57 +01:00
/// Gets the WMS 1.3.0 CRS identifier.
/// </summary>
2024-07-12 18:42:18 +02:00
public string CrsId { get; protected set; } = "";
/// <summary>
2022-03-06 17:28:27 +01:00
/// Gets or sets an optional projection center.
/// </summary>
2024-07-12 13:57:27 +02:00
public virtual Location Center { get; protected internal set; } = new Location();
/// <summary>
/// Gets the relative map scale at the specified Location.
/// </summary>
public virtual Point GetRelativeScale(Location location) => new Point(1d, 1d);
/// <summary>
2022-11-05 17:32:29 +01:00
/// Transforms a Location in geographic coordinates to a Point in projected map coordinates.
/// Returns null when the Location can not be transformed.
/// </summary>
public abstract Point? LocationToMap(Location location);
/// <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.
/// </summary>
public abstract Location MapToLocation(Point point);
/// <summary>
/// Transforms a BoundingBox in geographic coordinates to a Rect in projected map coordinates.
/// Returns null when the BoundingBox can not be transformed.
/// </summary>
public virtual Rect? BoundingBoxToMap(BoundingBox boundingBox)
{
2024-09-09 16:44:45 +02:00
Rect? rect = null;
2024-09-08 14:03:55 +02:00
var southWest = LocationToMap(new Location(boundingBox.South, boundingBox.West));
var northEast = LocationToMap(new Location(boundingBox.North, boundingBox.East));
2022-12-08 10:37:56 +01:00
2024-09-08 14:03:55 +02:00
if (southWest.HasValue && northEast.HasValue)
2022-12-07 23:34:42 +01:00
{
2024-09-09 16:44:45 +02:00
rect = new Rect(southWest.Value, northEast.Value);
2022-12-07 23:34:42 +01:00
}
2024-09-09 16:44:45 +02:00
return rect;
}
/// <summary>
/// Transforms a Rect in projected map coordinates to a BoundingBox in geographic coordinates.
/// Returns null when the MapRect can not be transformed.
/// </summary>
2024-09-09 16:44:45 +02:00
public virtual BoundingBox MapToBoundingBox(Rect rect)
{
2024-09-08 14:03:55 +02:00
BoundingBox boundingBox = null;
2024-09-09 16:44:45 +02:00
var southWest = MapToLocation(new Point(rect.X, rect.Y));
var northEast = MapToLocation(new Point(rect.X + rect.Width, rect.Y + rect.Height));
2024-09-08 14:03:55 +02:00
if (southWest != null && northEast != null)
{
boundingBox = new BoundingBox(southWest, northEast);
}
2024-09-08 14:03:55 +02:00
return boundingBox;
}
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>
public virtual Tuple<Rect, double> LatLonBoxToMap(LatLonBox latLonBox)
{
Tuple<Rect, double> rotatedRect = null;
Point? center, north, south, west, east;
var boxCenter = latLonBox.Center;
if ((center = LocationToMap(boxCenter)).HasValue &&
(north = LocationToMap(new Location(latLonBox.North, boxCenter.Longitude))).HasValue &&
(south = LocationToMap(new Location(latLonBox.South, boxCenter.Longitude))).HasValue &&
(west = LocationToMap(new Location(boxCenter.Latitude, latLonBox.West))).HasValue &&
(east = LocationToMap(new Location(boxCenter.Latitude, latLonBox.East))).HasValue)
{
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;
rotatedRect = new Tuple<Rect, double>(new Rect(x, y, width, height), latLonBox.Rotation + (r1 + r2) / 2d);
}
return rotatedRect;
}
}
}