2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2024-02-03 21:01:53 +01:00
|
|
|
|
// Copyright © 2024 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;
|
2022-12-01 23:49:57 +01:00
|
|
|
|
#if !WINUI && !UWP
|
2017-06-25 23:05:48 +02:00
|
|
|
|
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-12-13 18:22:18 +01:00
|
|
|
|
public virtual 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>
|
2022-12-13 18:22:18 +01:00
|
|
|
|
public virtual Scale GetRelativeScale(Location location) => new Scale(1d, 1d);
|
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>
|
2022-12-02 16:50:10 +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-12-02 16:50:10 +01:00
|
|
|
|
/// Transforms a BoundingBox in geographic coordinates to a MapRect in projected map coordinates.
|
|
|
|
|
|
/// Returns null when the BoundingBox can not be transformed.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2022-12-02 16:50:10 +01:00
|
|
|
|
public virtual MapRect BoundingBoxToMapRect(BoundingBox boundingBox)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2022-12-08 10:37:56 +01:00
|
|
|
|
MapRect mapRect = null;
|
|
|
|
|
|
|
2022-12-08 15:05:19 +01:00
|
|
|
|
if (boundingBox.HasValidBounds)
|
2022-12-07 23:34:42 +01:00
|
|
|
|
{
|
|
|
|
|
|
var p1 = LocationToMap(new Location(boundingBox.South, boundingBox.West));
|
|
|
|
|
|
var p2 = LocationToMap(new Location(boundingBox.North, boundingBox.East));
|
|
|
|
|
|
|
|
|
|
|
|
if (p1.HasValue && p2.HasValue)
|
|
|
|
|
|
{
|
2022-12-08 10:37:56 +01:00
|
|
|
|
mapRect = new MapRect(p1.Value, p2.Value);
|
2022-12-07 23:34:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (boundingBox.Center != null)
|
|
|
|
|
|
{
|
2022-12-08 15:05:19 +01:00
|
|
|
|
// boundingBox is a CenteredBoundingBox
|
|
|
|
|
|
//
|
2022-12-07 23:34:42 +01:00
|
|
|
|
var center = LocationToMap(boundingBox.Center);
|
|
|
|
|
|
|
|
|
|
|
|
if (center.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var width = boundingBox.Width * Wgs84MeterPerDegree;
|
|
|
|
|
|
var height = boundingBox.Height * Wgs84MeterPerDegree;
|
|
|
|
|
|
var x = center.Value.X - width / 2d;
|
|
|
|
|
|
var y = center.Value.Y - height / 2d;
|
|
|
|
|
|
|
2022-12-08 10:37:56 +01:00
|
|
|
|
mapRect = new MapRect(x, y, x + width, y + height);
|
2022-12-07 23:34:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-08 10:37:56 +01:00
|
|
|
|
return mapRect;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-12-02 16:50:10 +01:00
|
|
|
|
/// Transforms a MapRect in projected map coordinates to a BoundingBox in geographic coordinates.
|
|
|
|
|
|
/// Returns null when the MapRect can not be transformed.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2022-12-07 17:00:25 +01:00
|
|
|
|
public virtual BoundingBox MapRectToBoundingBox(MapRect mapRect)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2022-12-07 23:34:42 +01:00
|
|
|
|
var southWest = MapToLocation(new Point(mapRect.XMin, mapRect.YMin));
|
|
|
|
|
|
var northEast = MapToLocation(new Point(mapRect.XMax, mapRect.YMax));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2022-12-07 23:34:42 +01:00
|
|
|
|
return southWest != null && northEast != null
|
|
|
|
|
|
? new BoundingBox(southWest, northEast)
|
|
|
|
|
|
: 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>
|
2022-12-07 17:00:25 +01:00
|
|
|
|
public virtual string GetBboxValue(MapRect mapRect)
|
2019-12-12 19:23:41 +01:00
|
|
|
|
{
|
2022-12-07 17:00:25 +01:00
|
|
|
|
// Truncate bounding box to 1 cm precision.
|
|
|
|
|
|
//
|
|
|
|
|
|
const double p = 0.01;
|
|
|
|
|
|
|
2022-12-14 18:02:19 +01:00
|
|
|
|
return string.Format(CultureInfo.InvariantCulture, "{0:F2},{1:F2},{2:F2},{3:F2}",
|
2022-12-07 17:00:25 +01:00
|
|
|
|
p * Math.Ceiling(mapRect.XMin / p),
|
|
|
|
|
|
p * Math.Ceiling(mapRect.YMin / p),
|
|
|
|
|
|
p * Math.Floor(mapRect.XMax / p),
|
|
|
|
|
|
p * Math.Floor(mapRect.YMax / p));
|
2019-12-12 19:23:41 +01:00
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|