XAML-Map-Control/MapProjections/Shared/ProjNetMapProjection.cs

207 lines
7.6 KiB
C#
Raw Normal View History

2026-01-15 11:04:11 +01:00
using ProjNet.CoordinateSystems;
2021-07-07 16:52:31 +02:00
using ProjNet.CoordinateSystems.Transformations;
using System;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
2026-01-25 18:04:59 +01:00
using System.Windows.Media;
#elif AVALONIA
using Avalonia;
#endif
namespace MapControl.Projections
{
/// <summary>
2026-01-15 11:04:11 +01:00
/// MapProjection based on ProjNet.
/// </summary>
2026-01-15 11:04:11 +01:00
public class ProjNetMapProjection : MapProjection
{
2026-02-01 17:25:04 +01:00
public ProjNetMapProjection(ProjectedCoordinateSystem coordinateSystem)
2022-01-21 00:17:01 +01:00
{
2026-02-01 17:25:04 +01:00
CoordinateSystem = coordinateSystem;
2022-01-21 16:55:00 +01:00
}
2026-01-15 11:04:11 +01:00
public ProjNetMapProjection(string coordinateSystemWkt)
2022-01-21 16:55:00 +01:00
{
2022-01-24 20:43:22 +01:00
CoordinateSystemWkt = coordinateSystemWkt;
2022-01-21 00:17:01 +01:00
}
/// <summary>
/// Gets or sets an OGC Well-known text representation of a coordinate system,
/// i.e. a PROJCS[...] or GEOGCS[...] string as used by https://epsg.io or http://spatialreference.org.
/// Setting this property updates the CoordinateSystem property with an ICoordinateSystem created from the WKT string.
/// </summary>
2022-01-24 20:43:22 +01:00
public string CoordinateSystemWkt
2022-01-21 00:17:01 +01:00
{
2022-08-06 11:04:49 +02:00
get => CoordinateSystem?.WKT;
2026-01-15 11:04:11 +01:00
protected set => CoordinateSystem = new CoordinateSystemFactory().CreateFromWkt(value) as ProjectedCoordinateSystem;
2022-01-21 00:17:01 +01:00
}
/// <summary>
2019-10-27 17:46:44 +01:00
/// Gets or sets the ICoordinateSystem of the MapProjection.
/// </summary>
2026-01-15 11:04:11 +01:00
public ProjectedCoordinateSystem CoordinateSystem
{
2025-12-27 21:24:01 +01:00
get;
2022-01-21 16:55:00 +01:00
protected set
{
2026-01-24 17:42:00 +01:00
field = value ??
throw new ArgumentNullException(nameof(value));
2026-01-29 23:27:39 +01:00
var projection = field.Projection ??
2026-01-24 17:42:00 +01:00
throw new ArgumentException("CoordinateSystem.Projection must not be null.", nameof(value));
2026-02-01 17:25:04 +01:00
IsNormalCylindrical = projection.Name.Contains("Pseudo-Mercator") ||
projection.Name.StartsWith("Mercator");
2019-10-25 19:56:23 +02:00
var transformFactory = new CoordinateTransformationFactory();
2018-12-20 21:55:12 +01:00
2026-02-01 01:42:24 +01:00
CrsId = !string.IsNullOrEmpty(field.Authority) && field.AuthorityCode > 0
? $"{field.Authority}:{field.AuthorityCode}"
: string.Empty;
LocationToMapTransform = transformFactory
2025-12-27 21:24:01 +01:00
.CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, field)
2019-10-25 19:56:23 +02:00
.MathTransform;
MapToLocationTransform = transformFactory
2025-12-27 21:24:01 +01:00
.CreateFromCoordinateSystems(field, GeographicCoordinateSystem.WGS84)
2019-10-25 19:56:23 +02:00
.MathTransform;
2018-12-20 21:55:12 +01:00
2026-02-01 17:25:04 +01:00
var ellipsoid = field.HorizontalDatum.Ellipsoid;
EquatorialRadius = ellipsoid.SemiMajorAxis;
Flattening = 1d / ellipsoid.InverseFlattening;
var parameter = projection.GetParameter("scale_factor");
ScaleFactor = parameter != null ? parameter.Value : 1d;
parameter = projection.GetParameter("central_meridian");
CentralMeridian = parameter != null ? parameter.Value : 0d;
parameter = projection.GetParameter("latitude_of_origin");
LatitudeOfOrigin = parameter != null ? parameter.Value : 0d;
parameter = projection.GetParameter("false_easting");
FalseEasting = parameter != null ? parameter.Value : 0d;
parameter = projection.GetParameter("false_northing");
FalseNorthing = parameter != null ? parameter.Value : 0d;
}
}
2026-01-15 11:04:11 +01:00
public MathTransform LocationToMapTransform { get; private set; }
2022-01-21 00:17:01 +01:00
2026-01-15 11:04:11 +01:00
public MathTransform MapToLocationTransform { get; private set; }
public override Point LocationToMap(double latitude, double longitude)
{
if (LocationToMapTransform == null)
{
2018-12-20 21:55:12 +01:00
throw new InvalidOperationException("The CoordinateSystem property is not set.");
}
var coordinate = LocationToMapTransform.Transform([longitude, latitude]);
return new Point(coordinate[0], coordinate[1]);
}
2025-12-13 07:28:45 +01:00
public override Location MapToLocation(double x, double y)
{
if (MapToLocationTransform == null)
{
2018-12-20 21:55:12 +01:00
throw new InvalidOperationException("The CoordinateSystem property is not set.");
}
var coordinate = MapToLocationTransform.Transform([x, y]);
return new Location(coordinate[1], coordinate[0]);
}
2026-02-01 17:25:04 +01:00
public override double GridConvergence(double latitude, double longitude)
{
var projection = CoordinateSystem.Projection.Name;
if (projection.StartsWith("Transverse_Mercator"))
{
return TransverseMercatorGridConvergence(latitude, longitude);
}
if (projection.StartsWith("Polar_Stereographic"))
{
return PolarStereographicGridConvergence(longitude);
}
return base.GridConvergence(latitude, longitude);
}
public override Matrix RelativeTransform(double latitude, double longitude)
{
2026-02-01 17:25:04 +01:00
var projection = CoordinateSystem.Projection.Name;
if (projection.Contains("Pseudo-Mercator"))
{
return WebMercatorRelativeTransform(latitude);
}
if (projection.StartsWith("Mercator"))
{
return WorldMercatorRelativeTransform(latitude);
}
if (projection.StartsWith("Polar_Stereographic"))
{
return PolarStereographicRelativeTransform(latitude, longitude);
}
return base.RelativeTransform(latitude, longitude);
}
protected static Matrix WebMercatorRelativeTransform(double latitude)
{
var k = 1d / Math.Cos(latitude * Math.PI / 180d); // p.44 (7-3)
return new Matrix(k, 0d, 0d, k, 0d, 0d);
}
protected Matrix WorldMercatorRelativeTransform(double latitude)
{
var e2 = (2d - Flattening) * Flattening;
var phi = latitude * Math.PI / 180d;
var sinPhi = Math.Sin(phi);
var k = Math.Sqrt(1d - e2 * sinPhi * sinPhi) / Math.Cos(phi); // p.44 (7-8)
return new Matrix(k, 0d, 0d, k, 0d, 0d);
}
protected double TransverseMercatorGridConvergence(double latitude, double longitude)
{
return 180d / Math.PI * Math.Atan(
Math.Tan((longitude - CentralMeridian) * Math.PI / 180d) *
Math.Sin(latitude * Math.PI / 180d));
}
protected double PolarStereographicGridConvergence(double longitude)
{
return Math.Sign(LatitudeOfOrigin) * (longitude - CentralMeridian);
}
2026-02-01 17:25:04 +01:00
protected Matrix PolarStereographicRelativeTransform(double latitude, double longitude)
{
2026-02-01 17:25:04 +01:00
var sign = Math.Sign(LatitudeOfOrigin);
var phi = sign * latitude * Math.PI / 180d;
var e = Math.Sqrt((2d - Flattening) * Flattening);
var eSinPhi = e * Math.Sin(phi);
var t = Math.Tan(Math.PI / 4d - phi / 2d)
/ Math.Pow((1d - eSinPhi) / (1d + eSinPhi), e / 2d); // p.161 (15-9)
// r == ρ/a
var r = 2d * ScaleFactor * t / Math.Sqrt(Math.Pow(1d + e, 1d + e) * Math.Pow(1d - e, 1d - e)); // p.161 (21-33)
var m = Math.Cos(phi) / Math.Sqrt(1d - eSinPhi * eSinPhi); // p.160 (14-15)
var k = r / m; // p.161 (21-32)
var transform = new Matrix(k, 0d, 0d, k, 0d, 0d);
transform.Rotate(-sign * (longitude - CentralMeridian));
return transform;
}
}
}