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

137 lines
5.2 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;
using System.Windows.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
#endif
namespace MapControl
{
/// <summary>
2026-01-24 17:42:00 +01:00
/// Implements a map projection, a transformation between geographic coordinates,
/// i.e. latitude and longitude in degrees, and cartesian map coordinates in meters.
/// </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
public abstract class MapProjection
{
public const double Wgs84EquatorialRadius = 6378137d;
2018-12-20 21:55:12 +01:00
public const double Wgs84Flattening = 1d / 298.257223563;
public const double Wgs84MeterPerDegree = Wgs84EquatorialRadius * Math.PI / 180d;
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
}
/// <summary>
/// Creates a MapProjection instance from a CRS identifier string.
/// </summary>
public static MapProjection Parse(string crsId)
{
return Factory.GetProjection(crsId);
}
/// <summary>
2026-01-24 17:42:00 +01:00
/// Gets the WMS 1.3.0 CRS identifier.
/// </summary>
2026-01-24 17:42:00 +01:00
public string CrsId { get; protected set; } = "";
/// <summary>
2026-01-24 17:42:00 +01:00
/// Indicates whether the projection is normal cylindrical, see
/// https://en.wikipedia.org/wiki/Map_projection#Normal_cylindrical.
/// </summary>
2026-01-24 17:42:00 +01:00
public bool IsNormalCylindrical { get; protected set; }
/// <summary>
2026-01-24 17:42:00 +01:00
/// The earth ellipsoid semi-major axis, or spherical earth radius respectively, in meters.
/// </summary>
public double EquatorialRadius { get; set; } = Wgs84EquatorialRadius;
public double MeterPerDegree => EquatorialRadius * Math.PI / 180d;
/// <summary>
2026-01-27 22:56:09 +01:00
/// Gets the relative transform at the specified geographic coordinates.
/// The returned Matrix represents the local distortion of the map projection.
/// </summary>
2026-01-27 22:56:09 +01:00
public abstract Matrix RelativeTransform(double latitude, double longitude);
/// <summary>
/// Transforms geographic coordinates to a Point in projected map coordinates.
/// </summary>
public abstract Point LocationToMap(double latitude, double longitude);
/// <summary>
/// Transforms projected map coordinates to a Location in geographic coordinates.
/// </summary>
public abstract Location MapToLocation(double x, double y);
/// <summary>
2026-01-27 22:56:09 +01:00
/// Gets the relative transform at the specified geographic Location.
/// </summary>
2026-01-27 22:56:09 +01:00
public Matrix RelativeTransform(Location location) => RelativeTransform(location.Latitude, location.Longitude);
/// <summary>
2022-11-05 17:32:29 +01:00
/// Transforms a Location in geographic coordinates to a Point in projected map coordinates.
/// </summary>
public Point LocationToMap(Location location) => LocationToMap(location.Latitude, location.Longitude);
/// <summary>
2022-11-05 17:32:29 +01:00
/// Transforms a Point in projected map coordinates to a Location in geographic coordinates.
/// </summary>
public Location MapToLocation(Point point) => MapToLocation(point.X, point.Y);
/// <summary>
/// Transforms a BoundingBox in geographic coordinates to a Rect in projected map coordinates
/// with an optional transform Matrix.
/// </summary>
public (Rect, Matrix?) BoundingBoxToMap(BoundingBox boundingBox)
{
Rect rect;
Matrix? transform = null;
2022-12-08 10:37:56 +01:00
if (IsNormalCylindrical)
{
var southWest = LocationToMap(boundingBox.South, boundingBox.West);
var northEast = LocationToMap(boundingBox.North, boundingBox.East);
2026-01-28 15:55:30 +01:00
rect = new Rect(southWest.X, southWest.Y, northEast.X - southWest.X, northEast.Y - southWest.Y);
}
else
{
var latitude = (boundingBox.South + boundingBox.North) / 2d;
var longitude = (boundingBox.West + boundingBox.East) / 2d;
var center = LocationToMap(latitude, longitude);
var width = MeterPerDegree * (boundingBox.East - boundingBox.West) * Math.Cos(latitude * Math.PI / 180d);
var height = MeterPerDegree * (boundingBox.North - boundingBox.South);
2026-01-28 15:55:30 +01:00
rect = new Rect(center.X - width / 2d, center.Y - height / 2d, width, height);
transform = RelativeTransform(latitude, longitude);
}
return (rect, transform);
}
/// <summary>
/// Transforms a Rect in projected map coordinates to a BoundingBox in geographic coordinates.
/// </summary>
2026-01-28 15:55:30 +01:00
public BoundingBox MapToBoundingBox(Rect rect)
{
var southWest = MapToLocation(rect.X, rect.Y);
var northEast = MapToLocation(rect.X + rect.Width, rect.Y + rect.Height);
return new BoundingBox(southWest.Latitude, southWest.Longitude, northEast.Latitude, northEast.Longitude);
2024-09-09 16:44:45 +02:00
}
2025-09-06 13:06:00 +02:00
public override string ToString()
{
return CrsId;
}
}
}