2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2017-06-25 23:05:48 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-12-12 19:23:41 +01:00
|
|
|
|
using System.Globalization;
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#if WINUI || UWP
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
#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>
|
2017-08-04 21:38:58 +02:00
|
|
|
|
public abstract class MapProjection
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2018-02-09 17:43:47 +01:00
|
|
|
|
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);
|
|
|
|
|
|
|
2022-01-19 16:43:00 +01:00
|
|
|
|
public static MapProjectionFactory Factory { get; set; } = new MapProjectionFactory();
|
|
|
|
|
|
|
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>
|
2022-03-05 18:40:57 +01:00
|
|
|
|
public string CrsId { get; protected set; } = string.Empty;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-03-06 17:28:27 +01:00
|
|
|
|
/// Gets or sets an optional projection center.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2022-03-05 18:40:57 +01:00
|
|
|
|
public Location Center { get; set; } = new Location();
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
/// <summary>
|
2020-03-26 19:08:20 +01:00
|
|
|
|
/// Gets the relative map scale at the specified Location.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public virtual Vector GetRelativeScale(Location location)
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2020-04-17 14:40:14 +02:00
|
|
|
|
return new Vector(1d, 1d);
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
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.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public abstract Point LocationToMap(Location location);
|
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>
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public abstract Location MapToLocation(Point point);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-05 17:32:29 +01:00
|
|
|
|
/// Transforms a BoundingBox in geographic coordinates to a Rect in projected map coordinates.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual Rect BoundingBoxToRect(BoundingBox boundingBox)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Rect(
|
2020-03-26 19:08:20 +01:00
|
|
|
|
LocationToMap(new Location(boundingBox.South, boundingBox.West)),
|
|
|
|
|
|
LocationToMap(new Location(boundingBox.North, boundingBox.East)));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-05 17:32:29 +01:00
|
|
|
|
/// Transforms a Rect in projected map coordinates to a BoundingBox in geographic coordinates.
|
2022-03-04 22:28:18 +01:00
|
|
|
|
/// Returns null when the Rect can not be transformed.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual BoundingBox RectToBoundingBox(Rect rect)
|
|
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var sw = MapToLocation(new Point(rect.X, rect.Y));
|
|
|
|
|
|
var ne = MapToLocation(new Point(rect.X + rect.Width, rect.Y + rect.Height));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2022-03-04 22:28:18 +01:00
|
|
|
|
return sw != null && ne != null
|
|
|
|
|
|
? new BoundingBox(sw.Latitude, sw.Longitude, ne.Latitude, ne.Longitude)
|
|
|
|
|
|
: null;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-12 19:23:41 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the CRS parameter value for a WMS GetMap request.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual string GetCrsValue()
|
|
|
|
|
|
{
|
|
|
|
|
|
return CrsId.StartsWith("AUTO:") || CrsId.StartsWith("AUTO2:")
|
2020-03-26 19:08:20 +01:00
|
|
|
|
? string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", CrsId, Center.Longitude, Center.Latitude)
|
2019-12-12 19:23:41 +01:00
|
|
|
|
: CrsId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the BBOX parameter value for a WMS GetMap request.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual string GetBboxValue(Rect rect)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format(CultureInfo.InvariantCulture,
|
|
|
|
|
|
"{0},{1},{2},{3}", rect.X, rect.Y, (rect.X + rect.Width), (rect.Y + rect.Height));
|
|
|
|
|
|
}
|
2022-03-04 22:28:18 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if the X and Y values of a Point are neither NaN nor Infinity.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool IsValid(Point point)
|
|
|
|
|
|
{
|
|
|
|
|
|
return !double.IsNaN(point.X) && !double.IsInfinity(point.X) &&
|
|
|
|
|
|
!double.IsNaN(point.Y) && !double.IsInfinity(point.Y);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Checks if the X, Y, Width and Height values of a Rect are neither NaN nor Infinity.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool IsValid(Rect rect)
|
|
|
|
|
|
{
|
|
|
|
|
|
return !double.IsNaN(rect.X) && !double.IsInfinity(rect.X) &&
|
|
|
|
|
|
!double.IsNaN(rect.Y) && !double.IsInfinity(rect.Y) &&
|
|
|
|
|
|
!double.IsNaN(rect.Width) && !double.IsInfinity(rect.Width) &&
|
|
|
|
|
|
!double.IsNaN(rect.Height) && !double.IsInfinity(rect.Height);
|
|
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|