// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Globalization;
#if WINUI || UWP
using Windows.Foundation;
#else
using System.Windows;
#endif
namespace MapControl
{
///
/// Defines a map projection between geographic coordinates and cartesian map coordinates.
///
public abstract class MapProjection
{
public const double Wgs84EquatorialRadius = 6378137d;
public const double Wgs84MetersPerDegree = Wgs84EquatorialRadius * Math.PI / 180d;
public const double Wgs84Flattening = 1d / 298.257223563;
public static readonly double Wgs84Eccentricity = Math.Sqrt((2d - Wgs84Flattening) * Wgs84Flattening);
public static MapProjectionFactory Factory { get; set; } = new MapProjectionFactory();
///
/// Gets or sets the WMS 1.3.0 CRS identifier.
///
public string CrsId { get; set; } = string.Empty;
///
/// Gets or sets the projection center.
///
public Location Center { get; set; } = new Location();
///
/// Indicates if this is a normal cylindrical projection.
///
public virtual bool IsNormalCylindrical
{
get { return false; }
}
///
/// Indicates if this is a web mercator projection, i.e. compatible with MapTileLayer.
///
public virtual bool IsWebMercator
{
get { return false; }
}
///
/// Gets the absolute value of the minimum and maximum latitude that can be transformed.
///
public virtual double MaxLatitude
{
get { return 90d; }
}
///
/// Gets the relative map scale at the specified Location.
///
public virtual Vector GetRelativeScale(Location location)
{
return new Vector(1d, 1d);
}
///
/// Transforms a Location in geographic coordinates to a Point in cartesian map coordinates.
///
public abstract Point LocationToMap(Location location);
///
/// Transforms a Point in cartesian map coordinates to a Location in geographic coordinates.
///
public abstract Location MapToLocation(Point point);
///
/// Transforms a BoundingBox in geographic coordinates to a Rect in cartesian map coordinates.
///
public virtual Rect BoundingBoxToRect(BoundingBox boundingBox)
{
return new Rect(
LocationToMap(new Location(boundingBox.South, boundingBox.West)),
LocationToMap(new Location(boundingBox.North, boundingBox.East)));
}
///
/// Transforms a Rect in cartesian map coordinates to a BoundingBox in geographic coordinates.
///
public virtual BoundingBox RectToBoundingBox(Rect rect)
{
var sw = MapToLocation(new Point(rect.X, rect.Y));
var ne = MapToLocation(new Point(rect.X + rect.Width, rect.Y + rect.Height));
return new BoundingBox(sw.Latitude, sw.Longitude, ne.Latitude, ne.Longitude);
}
///
/// Gets the CRS parameter value for a WMS GetMap request.
///
public virtual string GetCrsValue()
{
return CrsId.StartsWith("AUTO:") || CrsId.StartsWith("AUTO2:")
? string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", CrsId, Center.Longitude, Center.Latitude)
: CrsId;
}
///
/// Gets the BBOX parameter value for a WMS GetMap request.
///
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));
}
}
}