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);
}

View file

@ -0,0 +1,129 @@
// 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
namespace MapControl.Projections
{
/// <summary>
/// Elliptical Polar Stereographic Projection with a given scale factor at the pole and
/// optional false easting and northing, as used by the UPS North and UPS South projections.
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.160-162.
/// </summary>
public class PolarStereographicProjection : MapProjection
{
public static double ConvergenceTolerance = 1e-6;
public static int MaxIterations = 10;
private readonly bool north;
private readonly double scaleFactor;
private readonly double falseEasting;
private readonly double falseNorthing;
public PolarStereographicProjection(string crsId, bool north, double scaleFactor = 1d, double falseEasting = 0d, double falseNorthing = 0d)
{
CrsId = crsId;
TrueScale = scaleFactor * MetersPerDegree;
this.north = north;
this.scaleFactor = scaleFactor;
this.falseEasting = falseEasting;
this.falseNorthing = falseNorthing;
}
public override Vector GetMapScale(Location location)
{
var lat = (north ? location.Latitude : -location.Latitude) * Math.PI / 180d;
var a = Wgs84EquatorialRadius;
var e = Wgs84Eccentricity;
var s = Math.Sqrt(Math.Pow(1 + e, 1 + e) * Math.Pow(1 - e, 1 - e));
var t = Math.Tan(Math.PI / 4d - lat / 2d) / ConformalFactor(lat);
var rho = 2d * a * scaleFactor * t / s;
var eSinLat = e * Math.Sin(lat);
var m = Math.Cos(lat) / Math.Sqrt(1d - eSinLat * eSinLat);
var k = rho / (a * m);
return new Vector(ViewportScale * k, ViewportScale * k);
}
public override Point LocationToPoint(Location location)
{
var lat = location.Latitude * Math.PI / 180d;
var lon = location.Longitude * Math.PI / 180d;
if (north)
{
lon = Math.PI - lon;
}
else
{
lat = -lat;
}
var a = Wgs84EquatorialRadius;
var e = Wgs84Eccentricity;
var s = Math.Sqrt(Math.Pow(1 + e, 1 + e) * Math.Pow(1 - e, 1 - e));
var t = Math.Tan(Math.PI / 4d - lat / 2d) / ConformalFactor(lat);
var rho = 2d * a * scaleFactor * t / s;
return new Point(rho * Math.Sin(lon) + falseEasting, rho * Math.Cos(lon) + falseNorthing);
}
public override Location PointToLocation(Point point)
{
point.X -= falseEasting;
point.Y -= falseNorthing;
var lon = Math.Atan2(point.X, point.Y);
var rho = Math.Sqrt(point.X * point.X + point.Y * point.Y);
var a = Wgs84EquatorialRadius;
var e = Wgs84Eccentricity;
var s = Math.Sqrt(Math.Pow(1 + e, 1 + e) * Math.Pow(1 - e, 1 - e));
var t = rho * s / (2d * a * scaleFactor);
var lat = Math.PI / 2d - 2d * Math.Atan(t);
var relChange = 1d;
for (int i = 0; i < MaxIterations && relChange > ConvergenceTolerance; i++)
{
var newLat = Math.PI / 2d - 2d * Math.Atan(t * ConformalFactor(lat));
relChange = Math.Abs(1d - newLat / lat);
lat = newLat;
}
if (north)
{
lon = Math.PI - lon;
}
else
{
lat = -lat;
}
return new Location(lat * 180d / Math.PI, lon * 180d / Math.PI);
}
private static double ConformalFactor(double lat)
{
var eSinLat = Wgs84Eccentricity * Math.Sin(lat);
return Math.Pow((1d - eSinLat) / (1d + eSinLat), Wgs84Eccentricity / 2d);
}
}
public class UpsNorthProjection : PolarStereographicProjection
{
public UpsNorthProjection() : base("EPSG:32661", true, 0.994, 2e6, 2e6)
{
}
}
public class UpsSouthProjection : PolarStereographicProjection
{
public UpsSouthProjection() : base("EPSG:32761", false, 0.994, 2e6, 2e6)
{
}
}
}

View file

@ -3,21 +3,12 @@
// Licensed under the Microsoft Public License (Ms-PL)
using System;
#if !WINDOWS_UWP
using System.Windows;
#endif
using ProjNet.CoordinateSystems;
namespace MapControl.Projections
{
public class UtmProjection : GeoApiProjection
{
private const string wktFormat = "PROJCS[\"WGS 84 / UTM zone {0}\", GEOGCS[\"WGS 84\", DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563, AUTHORITY[\"EPSG\", \"7030\"]], AUTHORITY[\"EPSG\", \"6326\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.01745329251994328, AUTHORITY[\"EPSG\", \"9122\"]], AUTHORITY[\"EPSG\", \"4326\"]], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], PROJECTION[\"Transverse_Mercator\"], PARAMETER[\"latitude_of_origin\", 0], PARAMETER[\"central_meridian\", {1}], PARAMETER[\"scale_factor\", 0.9996], PARAMETER[\"false_easting\", 500000], PARAMETER[\"false_northing\", {2}], AUTHORITY[\"EPSG\", \"{3}\"], AXIS[\"Easting\", EAST], AXIS[\"Northing\", NORTH]]";
public UtmProjection()
{
TrueScale = 0.9996 * MetersPerDegree;
}
private string zone;
public string Zone
@ -48,7 +39,7 @@ namespace MapControl.Projections
public void SetZone(int zoneNumber, bool north)
{
if (zoneNumber < 1 || zoneNumber > 60)
if (zoneNumber < 1 || zoneNumber > 61)
{
throw new ArgumentException("Invalid UTM zone number.");
}
@ -57,12 +48,8 @@ namespace MapControl.Projections
if (zone != zoneName)
{
var centralMeridian = zoneNumber * 6 - 183;
var falseNorthing = north ? 0 : 10000000;
var authorityCode = (north ? 32600 : 32700) + zoneNumber;
zone = zoneName;
WKT = string.Format(wktFormat, zone, centralMeridian, falseNorthing, authorityCode);
CoordinateSystem = ProjectedCoordinateSystem.WGS84_UTM(zoneNumber, north);
}
}

View file

@ -0,0 +1,33 @@
// 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 ProjNet.CoordinateSystems;
namespace MapControl.Projections
{
/// <summary>
/// Spherical Mercator Projection implemented by setting the CoordinateSystem property of a GeoApiProjection.
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.41-44.
/// </summary>
public class WebMercatorProjection : GeoApiProjection
{
public WebMercatorProjection()
{
IsWebMercator = true;
IsNormalCylindrical = true;
CoordinateSystem = ProjectedCoordinateSystem.WebMercator;
}
public override Vector GetMapScale(Location location)
{
var k = 1d / Math.Cos(location.Latitude * Math.PI / 180d); // p.44 (7-3)
return new Vector(ViewportScale * k, ViewportScale * k);
}
}
}

View file

@ -0,0 +1,48 @@
// 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
namespace MapControl.Projections
{
/// <summary>
/// Elliptical Mercator Projection implemented by setting the WKT property of a GeoApiProjection.
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.44-45.
/// </summary>
public class WorldMercatorProjection : GeoApiProjection
{
public WorldMercatorProjection()
{
IsNormalCylindrical = true;
WKT = "PROJCS[\"WGS 84 / World Mercator\","
+ "GEOGCS[\"WGS 84\","
+ "DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563, AUTHORITY[\"EPSG\", \"7030\"]], AUTHORITY[\"EPSG\", \"6326\"]],"
+ "PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]],"
+ "UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9122\"]],"
+ "AUTHORITY[\"EPSG\", \"4326\"]],"
+ "PROJECTION[\"Mercator_1SP\"],"
+ "PARAMETER[\"latitude_of_origin\", 0],"
+ "PARAMETER[\"central_meridian\", 0],"
+ "PARAMETER[\"scale_factor\", 1],"
+ "PARAMETER[\"false_easting\", 0],"
+ "PARAMETER[\"false_northing\", 0],"
+ "UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]],"
+ "AXIS[\"Easting\", EAST],"
+ "AXIS[\"Northing\", NORTH],"
+ "AUTHORITY[\"EPSG\", \"3395\"]]";
}
public override Vector GetMapScale(Location location)
{
var lat = location.Latitude * Math.PI / 180d;
var eSinLat = Wgs84Eccentricity * Math.Sin(lat);
var k = Math.Sqrt(1d - eSinLat * eSinLat) / Math.Cos(lat); // p.44 (7-8)
return new Vector(ViewportScale * k, ViewportScale * k);
}
}
}