2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2018-02-09 17:43:47 +01:00
|
|
|
|
// © 2018 Clemens Fischer
|
2017-06-25 23:05:48 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Globalization;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
#if WINDOWS_UWP
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// Defines a map projection between geographic coordinates, cartesian map coordinates and viewport 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
|
|
|
|
{
|
2017-10-27 17:15:18 +02:00
|
|
|
|
public const int TileSize = 256;
|
2018-02-09 17:43:47 +01:00
|
|
|
|
public const double PixelPerDegree = TileSize / 360d;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2018-02-09 17:43:47 +01:00
|
|
|
|
public const double Wgs84EquatorialRadius = 6378137d;
|
|
|
|
|
|
public const double MetersPerDegree = Wgs84EquatorialRadius * Math.PI / 180d;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
private Matrix inverseViewportTransformMatrix;
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// <summary>
|
2018-08-25 20:26:22 +02:00
|
|
|
|
/// Gets or sets the WMS 1.3.0 CRS Identifier.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2018-08-25 20:26:22 +02:00
|
|
|
|
public string CrsId { get; set; }
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-08-25 17:54:09 +02:00
|
|
|
|
/// Indicates if this is a normal cylindrical projection.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2018-08-25 17:54:09 +02:00
|
|
|
|
public bool IsCylindrical { get; protected set; } = false;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-02-09 17:43:47 +01:00
|
|
|
|
/// Indicates if this is a web mercator projection, i.e. compatible with MapTileLayer.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2018-02-09 17:43:47 +01:00
|
|
|
|
public bool IsWebMercator { get; protected set; } = false;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-08-25 17:54:09 +02:00
|
|
|
|
/// Gets the scale factor from geographic to cartesian coordinates, on the line of true scale of a
|
|
|
|
|
|
/// cylindrical projection (usually the equator), or at the projection center of an azimuthal projection.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2018-02-09 17:43:47 +01:00
|
|
|
|
public double TrueScale { get; protected set; } = MetersPerDegree;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-02-09 17:43:47 +01:00
|
|
|
|
/// Gets the absolute value of the minimum and maximum latitude that can be transformed.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2018-02-09 17:43:47 +01:00
|
|
|
|
public double MaxLatitude { get; protected set; } = 90d;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
/// <summary>
|
2018-04-10 22:34:34 +02:00
|
|
|
|
/// Gets the transform matrix from cartesian map coordinates to viewport coordinates (pixels).
|
2018-03-06 22:22:58 +01:00
|
|
|
|
/// </summary>
|
2018-04-10 22:34:34 +02:00
|
|
|
|
public Matrix ViewportTransform { get; private set; }
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-02-09 17:43:47 +01:00
|
|
|
|
/// Gets the scaling factor from cartesian map coordinates to viewport coordinates.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2018-03-06 23:30:24 +01:00
|
|
|
|
public double ViewportScale { get; private set; }
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the map scale at the specified Location as viewport coordinate units per meter (px/m).
|
|
|
|
|
|
/// </summary>
|
2018-08-29 20:54:42 +02:00
|
|
|
|
public virtual Vector GetMapScale(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Vector(ViewportScale, ViewportScale);
|
|
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Location in geographic coordinates to a Point in cartesian map coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract Point LocationToPoint(Location location);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Point in cartesian map coordinates to a Location in geographic coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract Location PointToLocation(Point point);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a BoundingBox in geographic coordinates to a Rect in cartesian map coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual Rect BoundingBoxToRect(BoundingBox boundingBox)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Rect(
|
|
|
|
|
|
LocationToPoint(new Location(boundingBox.South, boundingBox.West)),
|
|
|
|
|
|
LocationToPoint(new Location(boundingBox.North, boundingBox.East)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Rect in cartesian map coordinates to a BoundingBox in geographic coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual BoundingBox RectToBoundingBox(Rect rect)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sw = PointToLocation(new Point(rect.X, rect.Y));
|
|
|
|
|
|
var ne = PointToLocation(new Point(rect.X + rect.Width, rect.Y + rect.Height));
|
|
|
|
|
|
|
|
|
|
|
|
return new BoundingBox(sw.Latitude, sw.Longitude, ne.Latitude, ne.Longitude);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Location in geographic coordinates to a Point in viewport coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Point LocationToViewportPoint(Location location)
|
|
|
|
|
|
{
|
2018-04-10 22:34:34 +02:00
|
|
|
|
return ViewportTransform.Transform(LocationToPoint(location));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Point in viewport coordinates to a Location in geographic coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Location ViewportPointToLocation(Point point)
|
|
|
|
|
|
{
|
2018-03-06 22:22:58 +01:00
|
|
|
|
return PointToLocation(inverseViewportTransformMatrix.Transform(point));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Rect in viewport coordinates to a BoundingBox in geographic coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public BoundingBox ViewportRectToBoundingBox(Rect rect)
|
|
|
|
|
|
{
|
2018-03-06 22:22:58 +01:00
|
|
|
|
var transform = new MatrixTransform { Matrix = inverseViewportTransformMatrix };
|
|
|
|
|
|
|
|
|
|
|
|
return RectToBoundingBox(transform.TransformBounds(rect));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets ViewportScale and ViewportTransform values.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual void SetViewportTransform(Location projectionCenter, Location mapCenter, Point viewportCenter, double zoomLevel, double heading)
|
|
|
|
|
|
{
|
2018-02-09 17:43:47 +01:00
|
|
|
|
ViewportScale = Math.Pow(2d, zoomLevel) * PixelPerDegree / TrueScale;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2017-07-25 20:21:02 +02:00
|
|
|
|
var center = LocationToPoint(mapCenter);
|
2018-03-07 22:19:16 +01:00
|
|
|
|
var matrix = CreateTransformMatrix(center, ViewportScale, -ViewportScale, heading, viewportCenter);
|
2018-03-06 22:22:58 +01:00
|
|
|
|
|
2018-04-10 22:34:34 +02:00
|
|
|
|
ViewportTransform = matrix;
|
2018-03-06 22:22:58 +01:00
|
|
|
|
|
2018-03-07 22:19:16 +01:00
|
|
|
|
matrix.Invert();
|
|
|
|
|
|
inverseViewportTransformMatrix = matrix;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-06-09 00:11:44 +02:00
|
|
|
|
/// Gets a WMS query parameter string from the specified bounding box, e.g. "CRS=...&BBOX=...&WIDTH=...&HEIGHT=..."
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
2018-06-11 21:37:36 +02:00
|
|
|
|
public virtual string WmsQueryParameters(BoundingBox boundingBox)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2017-11-02 19:05:46 +01:00
|
|
|
|
if (string.IsNullOrEmpty(CrsId) || !boundingBox.HasValidBounds)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-11 21:37:36 +02:00
|
|
|
|
var format = CrsId == "EPSG:4326"
|
|
|
|
|
|
? "CRS={0}&BBOX={2},{1},{4},{3}&WIDTH={5}&HEIGHT={6}"
|
|
|
|
|
|
: "CRS={0}&BBOX={1},{2},{3},{4}&WIDTH={5}&HEIGHT={6}";
|
2018-06-11 22:10:05 +02:00
|
|
|
|
var rect = BoundingBoxToRect(boundingBox);
|
|
|
|
|
|
var width = (int)Math.Round(ViewportScale * rect.Width);
|
|
|
|
|
|
var height = (int)Math.Round(ViewportScale * rect.Height);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
return string.Format(CultureInfo.InvariantCulture, format, CrsId,
|
|
|
|
|
|
rect.X, rect.Y, (rect.X + rect.Width), (rect.Y + rect.Height), width, height);
|
|
|
|
|
|
}
|
2018-03-06 22:22:58 +01:00
|
|
|
|
|
2018-03-07 22:24:57 +01:00
|
|
|
|
internal static Matrix CreateTransformMatrix(
|
|
|
|
|
|
Point translation1, double scale, double rotation, Point translation2)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CreateTransformMatrix(translation1, scale, scale, rotation, translation2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-07 22:19:16 +01:00
|
|
|
|
internal static Matrix CreateTransformMatrix(
|
|
|
|
|
|
Point translation1, double scaleX, double scaleY, double rotation, Point translation2)
|
2018-03-06 22:22:58 +01:00
|
|
|
|
{
|
2018-03-07 22:19:16 +01:00
|
|
|
|
var matrix = new Matrix(scaleX, 0d, 0d, scaleY, -translation1.X * scaleX, -translation1.Y * scaleY);
|
|
|
|
|
|
matrix.Rotate(rotation);
|
|
|
|
|
|
matrix.Translate(translation2.X, translation2.Y);
|
2018-03-06 22:22:58 +01:00
|
|
|
|
return matrix;
|
|
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|