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

134 lines
4.9 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using GeoAPI.CoordinateSystems;
2021-07-07 16:52:31 +02:00
using GeoAPI.CoordinateSystems.Transformations;
using GeoAPI.Geometries;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
using System;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
#elif AVALONIA
using Avalonia;
#endif
namespace MapControl.Projections
{
/// <summary>
/// MapProjection based on ProjNET4GeoApi.
/// </summary>
public class GeoApiProjection : MapProjection
{
2022-01-21 16:55:00 +01:00
protected GeoApiProjection()
2022-01-21 00:17:01 +01:00
{
2022-01-21 16:55:00 +01:00
}
2022-01-24 20:43:22 +01:00
public GeoApiProjection(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;
protected set => CoordinateSystem = new CoordinateSystemFactory().CreateFromWkt(value) as IProjectedCoordinateSystem;
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>
public IProjectedCoordinateSystem CoordinateSystem
{
2025-12-27 21:24:01 +01:00
get;
2022-01-21 16:55:00 +01:00
protected set
{
2025-12-27 21:24:01 +01:00
field = value ?? throw new ArgumentNullException(nameof(value));
2018-12-20 21:55:12 +01:00
2019-10-25 19:56:23 +02:00
var transformFactory = new CoordinateTransformationFactory();
2018-12-20 21:55:12 +01:00
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
2025-12-27 21:24:01 +01:00
CrsId = !string.IsNullOrEmpty(field.Authority) && field.AuthorityCode > 0
? $"{field.Authority}:{field.AuthorityCode}"
2024-04-17 22:07:52 +02:00
: string.Empty;
2018-12-20 21:55:12 +01:00
if (CrsId == "EPSG:3857")
{
Type = MapProjectionType.WebMercator;
}
else
2018-12-20 21:55:12 +01:00
{
2025-12-27 21:24:01 +01:00
var projection = field.Projection ??
2022-12-13 22:12:49 +01:00
throw new ArgumentException("CoordinateSystem.Projection must not be null.", nameof(value));
2018-12-20 21:55:12 +01:00
var centralMeridian = projection.GetParameter("central_meridian") ?? projection.GetParameter("longitude_of_origin");
2020-04-19 20:25:10 +02:00
var centralParallel = projection.GetParameter("central_parallel") ?? projection.GetParameter("latitude_of_origin");
2018-12-20 21:55:12 +01:00
var falseEasting = projection.GetParameter("false_easting");
var falseNorthing = projection.GetParameter("false_northing");
if ((centralMeridian == null || centralMeridian.Value == 0d) &&
2020-04-19 21:31:31 +02:00
(centralParallel == null || centralParallel.Value == 0d) &&
2018-12-20 21:55:12 +01:00
(falseEasting == null || falseEasting.Value == 0d) &&
2022-03-05 18:40:57 +01:00
(falseNorthing == null || falseNorthing.Value == 0d))
{
Type = MapProjectionType.NormalCylindrical;
}
else if (projection.Name.StartsWith("UTM") || projection.Name.StartsWith("Transverse"))
2022-03-05 18:40:57 +01:00
{
Type = MapProjectionType.TransverseCylindrical;
}
}
}
}
2022-01-21 00:17:01 +01:00
public IMathTransform LocationToMapTransform { get; private set; }
public IMathTransform MapToLocationTransform { get; private set; }
2026-01-06 11:56:28 +01:00
public override Point RelativeScale(double latitude, double longitude)
{
2025-12-27 21:24:01 +01:00
var k = CoordinateSystem?.Projection?.GetParameter("scale_factor")?.Value ?? 1d;
return new Point(k, k);
}
2025-12-13 07:28:45 +01:00
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.");
}
2025-12-13 07:28:45 +01:00
var coordinate = LocationToMapTransform.Transform(new Coordinate(longitude, latitude));
2022-12-02 17:45:58 +01:00
if (coordinate == null)
{
return null;
}
return new Point(coordinate.X, coordinate.Y);
}
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.");
}
2025-12-13 07:28:45 +01:00
var coordinate = MapToLocationTransform.Transform(new Coordinate(x, y));
return new Location(coordinate.Y, coordinate.X);
}
}
}