Version 4.10.0: Added MapProjections library project.

This commit is contained in:
ClemensF 2018-08-29 20:54:42 +02:00
parent 30498bf593
commit 8c2cd92fe4
13 changed files with 635 additions and 7 deletions

View file

@ -0,0 +1,92 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
// 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 ProjNet.Converters.WellKnownText;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
namespace MapControl.Projections
{
/// <summary>
/// MapProjection based on ProjNET4GeoApi.
/// </summary>
public class GeoApiProjection : MapProjection
{
private ICoordinateTransformation coordinateTransform;
private IMathTransform mathTransform;
private IMathTransform inverseTransform;
private string wkt;
/// <summary>
/// Gets or sets the underlying ICoordinateTransformation instance.
/// Setting this property updates the CrsId property.
/// </summary>
public ICoordinateTransformation CoordinateTransform
{
get { return coordinateTransform; }
set
{
coordinateTransform = value;
mathTransform = coordinateTransform.MathTransform;
inverseTransform = mathTransform.Inverse();
if (coordinateTransform.TargetCS != null &&
!string.IsNullOrEmpty(coordinateTransform.TargetCS.Authority) &&
coordinateTransform.TargetCS.AuthorityCode > 0)
{
CrsId = string.Format("{0}:{1}", coordinateTransform.TargetCS.Authority, coordinateTransform.TargetCS.AuthorityCode);
}
}
}
/// <summary>
/// Get or sets an OGC Well-known text representation of a projected coordinate system,
/// e.g. a PROJCS[...] string as used by https://epsg.io or http://spatialreference.org.
/// Setting this property updates the CoordinateTransform property.
/// </summary>
public string Wkt
{
get { return wkt; }
set
{
var sourceCs = GeographicCoordinateSystem.WGS84;
var targetCs = (ICoordinateSystem)CoordinateSystemWktReader.Parse(value, Encoding.UTF8);
CoordinateTransform = new CoordinateTransformationFactory().CreateFromCoordinateSystems(sourceCs, targetCs);
wkt = value;
}
}
public override Point LocationToPoint(Location location)
{
if (mathTransform == null)
{
throw new InvalidOperationException("The Wkt property is not set or not valid.");
}
var xy = mathTransform.Transform(new double[] { location.Longitude, location.Latitude });
return new Point(xy[0], xy[1]);
}
public override Location PointToLocation(Point point)
{
if (inverseTransform == null)
{
throw new InvalidOperationException("The Wkt property is not set or not valid.");
}
var lonLat = inverseTransform.Transform(new double[] { point.X, point.Y });
return new Location(lonLat[1], lonLat[0]);
}
}
}

View file

@ -0,0 +1,55 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
namespace MapControl.Projections
{
public class UtmProjection : GeoApiProjection
{
private static readonly 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]]";
private string zoneName;
public string ZoneName
{
get { return zoneName; }
set
{
var zoneNumber = 0;
var falseNorthing = 0;
var epsgCode = 0;
if (!string.IsNullOrEmpty(value))
{
if (value.EndsWith("N"))
{
epsgCode = 32600;
}
else if (value.EndsWith("S"))
{
falseNorthing = 10000000;
epsgCode = 32700;
}
if (epsgCode > 0)
{
int.TryParse(value.Substring(0, value.Length - 1), out zoneNumber);
}
}
if (zoneNumber < 1 || zoneNumber > 60)
{
throw new ArgumentException("Invalid UTM zone name.");
}
zoneName = value;
epsgCode += zoneNumber;
Wkt = string.Format(wktFormat, zoneName, 6 * zoneNumber - 183, falseNorthing, epsgCode);
TrueScale = 0.9996 * MetersPerDegree;
}
}
}
}