2018-08-29 20:54:42 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2018-08-29 20:54:42 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2021-07-07 16:52:31 +02:00
|
|
|
|
using GeoAPI.CoordinateSystems;
|
|
|
|
|
|
using GeoAPI.CoordinateSystems.Transformations;
|
|
|
|
|
|
using GeoAPI.Geometries;
|
|
|
|
|
|
using ProjNet.CoordinateSystems;
|
|
|
|
|
|
using ProjNet.CoordinateSystems.Transformations;
|
2018-08-29 20:54:42 +02:00
|
|
|
|
using System;
|
2019-12-12 19:23:41 +01:00
|
|
|
|
using System.Globalization;
|
2021-07-07 16:52:31 +02:00
|
|
|
|
#if WINUI
|
|
|
|
|
|
using Windows.Foundation;
|
2021-11-17 23:46:48 +01:00
|
|
|
|
#elif UWP
|
2020-01-06 18:36:16 +01:00
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
#else
|
2018-08-29 20:54:42 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Projections
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MapProjection based on ProjNET4GeoApi.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class GeoApiProjection : MapProjection
|
|
|
|
|
|
{
|
2019-10-25 19:56:23 +02:00
|
|
|
|
private ICoordinateSystem coordinateSystem;
|
2019-12-12 19:23:41 +01:00
|
|
|
|
private bool isNormalCylindrical;
|
|
|
|
|
|
private bool isWebMercator;
|
2020-04-19 21:31:31 +02:00
|
|
|
|
private double scaleFactor;
|
2019-12-12 19:23:41 +01:00
|
|
|
|
private string bboxFormat;
|
2018-12-20 21:55:12 +01:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public IMathTransform LocationToMapTransform { get; private set; }
|
|
|
|
|
|
public IMathTransform MapToLocationTransform { get; private set; }
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-10-27 17:46:44 +01:00
|
|
|
|
/// Gets or sets the ICoordinateSystem of the MapProjection.
|
2018-08-29 20:54:42 +02:00
|
|
|
|
/// </summary>
|
2019-10-25 19:56:23 +02:00
|
|
|
|
public ICoordinateSystem CoordinateSystem
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
get { return coordinateSystem; }
|
2018-08-29 20:54:42 +02:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2021-02-11 23:34:37 +01:00
|
|
|
|
coordinateSystem = 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
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
LocationToMapTransform = transformFactory
|
2019-10-25 19:56:23 +02:00
|
|
|
|
.CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, coordinateSystem)
|
|
|
|
|
|
.MathTransform;
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
MapToLocationTransform = transformFactory
|
2019-10-25 19:56:23 +02:00
|
|
|
|
.CreateFromCoordinateSystems(coordinateSystem, GeographicCoordinateSystem.WGS84)
|
|
|
|
|
|
.MathTransform;
|
2018-12-20 21:55:12 +01:00
|
|
|
|
|
|
|
|
|
|
CrsId = (!string.IsNullOrEmpty(coordinateSystem.Authority) && coordinateSystem.AuthorityCode > 0)
|
|
|
|
|
|
? string.Format("{0}:{1}", coordinateSystem.Authority, coordinateSystem.AuthorityCode)
|
2020-04-19 23:05:51 +02:00
|
|
|
|
: "";
|
2018-12-20 21:55:12 +01:00
|
|
|
|
|
2019-10-27 17:46:44 +01:00
|
|
|
|
var projection = (coordinateSystem as IProjectedCoordinateSystem)?.Projection;
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
2019-10-27 17:46:44 +01:00
|
|
|
|
if (projection != null)
|
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");
|
|
|
|
|
|
|
2019-12-12 19:23:41 +01:00
|
|
|
|
isNormalCylindrical =
|
2020-04-19 21:31:31 +02:00
|
|
|
|
(centralMeridian == null || centralMeridian.Value == 0d) &&
|
|
|
|
|
|
(centralParallel == null || centralParallel.Value == 0d) &&
|
2018-12-20 21:55:12 +01:00
|
|
|
|
(falseEasting == null || falseEasting.Value == 0d) &&
|
2019-10-25 19:56:23 +02:00
|
|
|
|
(falseNorthing == null || falseNorthing.Value == 0d);
|
2019-12-12 19:23:41 +01:00
|
|
|
|
isWebMercator = CrsId == "EPSG:3857" || CrsId == "EPSG:900913";
|
2020-04-19 21:31:31 +02:00
|
|
|
|
scaleFactor = 1d;
|
2019-12-12 19:23:41 +01:00
|
|
|
|
bboxFormat = "{0},{1},{2},{3}";
|
2019-10-25 19:56:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-12-12 19:23:41 +01:00
|
|
|
|
isNormalCylindrical = true;
|
|
|
|
|
|
isWebMercator = false;
|
2020-04-19 21:31:31 +02:00
|
|
|
|
scaleFactor = Wgs84MetersPerDegree;
|
2019-12-12 19:23:41 +01:00
|
|
|
|
bboxFormat = "{1},{0},{3},{2}";
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-10-27 17:46:44 +01:00
|
|
|
|
/// 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.
|
2018-08-29 20:54:42 +02:00
|
|
|
|
/// </summary>
|
2018-08-29 21:06:56 +02:00
|
|
|
|
public string WKT
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
get { return CoordinateSystem?.WKT; }
|
2019-10-25 19:56:23 +02:00
|
|
|
|
set { CoordinateSystem = new CoordinateSystemFactory().CreateFromWkt(value); }
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-12 19:23:41 +01:00
|
|
|
|
public override bool IsNormalCylindrical
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return isNormalCylindrical; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool IsWebMercator
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return isWebMercator; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-19 23:40:05 +01:00
|
|
|
|
|
|
|
|
|
|
public GeoApiProjection(string wkt = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (wkt != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
WKT = wkt;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public override Point LocationToMap(Location location)
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
if (LocationToMapTransform == null)
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
throw new InvalidOperationException("The CoordinateSystem property is not set.");
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-19 21:31:31 +02:00
|
|
|
|
var coordinate = LocationToMapTransform.Transform(
|
|
|
|
|
|
new Coordinate(location.Longitude, location.Latitude));
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
2020-04-19 21:31:31 +02:00
|
|
|
|
return new Point(coordinate.X * scaleFactor, coordinate.Y * scaleFactor);
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public override Location MapToLocation(Point point)
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
if (MapToLocationTransform == null)
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
throw new InvalidOperationException("The CoordinateSystem property is not set.");
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-19 21:31:31 +02:00
|
|
|
|
var coordinate = MapToLocationTransform.Transform(
|
|
|
|
|
|
new Coordinate(point.X / scaleFactor, point.Y / scaleFactor));
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
2018-08-29 21:51:33 +02:00
|
|
|
|
return new Location(coordinate.Y, coordinate.X);
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
2019-12-12 19:23:41 +01:00
|
|
|
|
|
|
|
|
|
|
public override string GetBboxValue(Rect rect)
|
|
|
|
|
|
{
|
2020-04-19 21:31:31 +02:00
|
|
|
|
return string.Format(CultureInfo.InvariantCulture, bboxFormat,
|
|
|
|
|
|
rect.X / scaleFactor, rect.Y / scaleFactor,
|
|
|
|
|
|
(rect.X + rect.Width) / scaleFactor, (rect.Y + rect.Height) / scaleFactor);
|
2019-12-12 19:23:41 +01:00
|
|
|
|
}
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|