Version 4.12. Revised projections

This commit is contained in:
ClemensF 2018-12-20 21:55:12 +01:00
parent 8cafe207cb
commit 90aa92def0
25 changed files with 390 additions and 167 deletions

View file

@ -3,14 +3,12 @@
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Text;
#if !WINDOWS_UWP
using System.Windows;
#endif
using GeoAPI.CoordinateSystems;
using GeoAPI.CoordinateSystems.Transformations;
using GeoAPI.Geometries;
using ProjNet.Converters.WellKnownText;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
@ -21,17 +19,17 @@ namespace MapControl.Projections
/// </summary>
public class GeoApiProjection : MapProjection
{
private ICoordinateTransformation coordinateTransform;
private IMathTransform mathTransform;
private IMathTransform inverseTransform;
private IProjectedCoordinateSystem coordinateSystem;
public IMathTransform MathTransform { get; private set; }
public IMathTransform InverseTransform { get; private set; }
/// <summary>
/// Gets or sets the underlying ICoordinateTransformation instance.
/// Setting this property updates the CrsId property.
/// Gets or sets the IProjectedCoordinateSystem of the MapProjection.
/// </summary>
public ICoordinateTransformation CoordinateTransform
public IProjectedCoordinateSystem CoordinateSystem
{
get { return coordinateTransform; }
get { return coordinateSystem; }
set
{
if (value == null)
@ -39,15 +37,45 @@ namespace MapControl.Projections
throw new ArgumentNullException("The property value must not be null.");
}
coordinateTransform = value;
mathTransform = coordinateTransform.MathTransform;
inverseTransform = mathTransform.Inverse();
coordinateSystem = value;
if (coordinateTransform.TargetCS != null &&
!string.IsNullOrEmpty(coordinateTransform.TargetCS.Authority) &&
coordinateTransform.TargetCS.AuthorityCode > 0)
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)
{
CrsId = string.Format("{0}:{1}", coordinateTransform.TargetCS.Authority, coordinateTransform.TargetCS.AuthorityCode);
IsWebMercator = CrsId == "EPSG:3857" || CrsId == "EPSG:900913";
}
var projection = coordinateSystem.Projection;
var scaleFactor = projection.GetParameter("scale_factor");
if (scaleFactor != null)
{
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;
}
}
}
}
@ -55,40 +83,34 @@ namespace MapControl.Projections
/// <summary>
/// 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.
/// Setting this property updates the CoordinateTransform property.
/// Setting this property updates the CoordinateSystem property with an IProjectedCoordinateSystem created from the WKT string.
/// </summary>
public string WKT
{
get { return coordinateTransform?.TargetCS?.WKT; }
set
{
var sourceCs = GeographicCoordinateSystem.WGS84;
var targetCs = (ICoordinateSystem)CoordinateSystemWktReader.Parse(value, Encoding.UTF8);
CoordinateTransform = new CoordinateTransformationFactory().CreateFromCoordinateSystems(sourceCs, targetCs);
}
get { return CoordinateSystem?.WKT; }
set { CoordinateSystem = (IProjectedCoordinateSystem)new CoordinateSystemFactory().CreateFromWkt(value); }
}
public override Point LocationToPoint(Location location)
{
if (mathTransform == null)
if (MathTransform == null)
{
throw new InvalidOperationException("The CoordinateTransformation property is not set.");
throw new InvalidOperationException("The CoordinateSystem property is not set.");
}
var coordinate = mathTransform.Transform(new Coordinate(location.Longitude, location.Latitude));
var coordinate = MathTransform.Transform(new Coordinate(location.Longitude, location.Latitude));
return new Point(coordinate.X, coordinate.Y);
}
public override Location PointToLocation(Point point)
{
if (inverseTransform == null)
if (InverseTransform == null)
{
throw new InvalidOperationException("The CoordinateTransformation property is not set.");
throw new InvalidOperationException("The CoordinateSystem property is not set.");
}
var coordinate = inverseTransform.Transform(new Coordinate(point.X, point.Y));
var coordinate = InverseTransform.Transform(new Coordinate(point.X, point.Y));
return new Location(coordinate.Y, coordinate.X);
}