2018-08-29 20:54:42 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2018 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
#if !WINDOWS_UWP
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
using GeoAPI.CoordinateSystems;
|
|
|
|
|
|
using GeoAPI.CoordinateSystems.Transformations;
|
2018-08-29 21:51:33 +02:00
|
|
|
|
using GeoAPI.Geometries;
|
2018-08-29 20:54:42 +02:00
|
|
|
|
using ProjNet.CoordinateSystems;
|
|
|
|
|
|
using ProjNet.CoordinateSystems.Transformations;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Projections
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MapProjection based on ProjNET4GeoApi.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class GeoApiProjection : MapProjection
|
|
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
private IProjectedCoordinateSystem coordinateSystem;
|
|
|
|
|
|
|
|
|
|
|
|
public IMathTransform MathTransform { get; private set; }
|
|
|
|
|
|
public IMathTransform InverseTransform { get; private set; }
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-12-20 21:55:12 +01:00
|
|
|
|
/// Gets or sets the IProjectedCoordinateSystem of the MapProjection.
|
2018-08-29 20:54:42 +02:00
|
|
|
|
/// </summary>
|
2018-12-20 21:55:12 +01:00
|
|
|
|
public IProjectedCoordinateSystem 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
|
|
|
|
|
|
{
|
2018-10-03 01:06:40 +02:00
|
|
|
|
if (value == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The property value must not be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
coordinateSystem = value;
|
|
|
|
|
|
|
|
|
|
|
|
var coordinateTransform = new CoordinateTransformationFactory()
|
|
|
|
|
|
.CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, coordinateSystem);
|
|
|
|
|
|
|
|
|
|
|
|
MathTransform = coordinateTransform.MathTransform;
|
|
|
|
|
|
InverseTransform = MathTransform.Inverse();
|
|
|
|
|
|
|
|
|
|
|
|
CrsId = (!string.IsNullOrEmpty(coordinateSystem.Authority) && coordinateSystem.AuthorityCode > 0)
|
|
|
|
|
|
? string.Format("{0}:{1}", coordinateSystem.Authority, coordinateSystem.AuthorityCode)
|
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
|
|
if (!IsWebMercator)
|
|
|
|
|
|
{
|
|
|
|
|
|
IsWebMercator = CrsId == "EPSG:3857" || CrsId == "EPSG:900913";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var projection = coordinateSystem.Projection;
|
|
|
|
|
|
var scaleFactor = projection.GetParameter("scale_factor");
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
if (scaleFactor != null)
|
2018-08-29 20:54:42 +02:00
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
TrueScale = scaleFactor.Value * MetersPerDegree;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!IsNormalCylindrical)
|
|
|
|
|
|
{
|
|
|
|
|
|
var centralMeridian = projection.GetParameter("central_meridian") ?? projection.GetParameter("longitude_of_origin");
|
|
|
|
|
|
var centralParallel = projection.GetParameter("latitude_of_origin") ?? projection.GetParameter("central_parallel");
|
|
|
|
|
|
var falseEasting = projection.GetParameter("false_easting");
|
|
|
|
|
|
var falseNorthing = projection.GetParameter("false_northing");
|
|
|
|
|
|
|
|
|
|
|
|
if (centralMeridian != null && centralMeridian.Value == 0d &&
|
|
|
|
|
|
centralParallel != null && centralParallel.Value == 0d &&
|
|
|
|
|
|
(falseEasting == null || falseEasting.Value == 0d) &&
|
|
|
|
|
|
(falseNorthing == null || falseNorthing.Value == 0d))
|
|
|
|
|
|
{
|
|
|
|
|
|
IsNormalCylindrical = true;
|
|
|
|
|
|
}
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-08-29 21:19:24 +02:00
|
|
|
|
/// Gets or sets an OGC Well-known text representation of a projected coordinate system,
|
|
|
|
|
|
/// i.e. a PROJCS[...] string as used by https://epsg.io or http://spatialreference.org.
|
2018-12-20 21:55:12 +01:00
|
|
|
|
/// Setting this property updates the CoordinateSystem property with an IProjectedCoordinateSystem 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; }
|
|
|
|
|
|
set { CoordinateSystem = (IProjectedCoordinateSystem)new CoordinateSystemFactory().CreateFromWkt(value); }
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Point LocationToPoint(Location location)
|
|
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
if (MathTransform == 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
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
var coordinate = MathTransform.Transform(new Coordinate(location.Longitude, location.Latitude));
|
2018-08-29 20:54:42 +02:00
|
|
|
|
|
2018-08-29 21:51:33 +02:00
|
|
|
|
return new Point(coordinate.X, coordinate.Y);
|
2018-08-29 20:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Location PointToLocation(Point point)
|
|
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
if (InverseTransform == 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
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
var coordinate = InverseTransform.Transform(new Coordinate(point.X, point.Y));
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|