Improved MapProjections

This commit is contained in:
ClemensF 2019-10-25 19:56:23 +02:00
parent 82470435c7
commit 716cf950f3
6 changed files with 66 additions and 52 deletions

View file

@ -5,7 +5,6 @@
using System; using System;
#if WINDOWS_UWP #if WINDOWS_UWP
using Windows.Foundation; using Windows.Foundation;
using Windows.UI.Xaml.Media;
#else #else
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
@ -134,9 +133,17 @@ namespace MapControl
/// </summary> /// </summary>
public BoundingBox ViewportRectToBoundingBox(Rect rect) public BoundingBox ViewportRectToBoundingBox(Rect rect)
{ {
var transform = new MatrixTransform { Matrix = InverseViewportTransform }; var p1 = InverseViewportTransform.Transform(new Point(rect.X, rect.Y));
var p2 = InverseViewportTransform.Transform(new Point(rect.X, rect.Y + rect.Height));
var p3 = InverseViewportTransform.Transform(new Point(rect.X + rect.Width, rect.Y));
var p4 = InverseViewportTransform.Transform(new Point(rect.X + rect.Width, rect.Y + rect.Height));
return RectToBoundingBox(transform.TransformBounds(rect)); rect.X = Math.Min(p1.X, Math.Min(p2.X, Math.Min(p3.X, p4.X)));
rect.Y = Math.Min(p1.Y, Math.Min(p2.Y, Math.Min(p3.Y, p4.Y)));
rect.Width = Math.Max(p1.X, Math.Max(p2.X, Math.Max(p3.X, p4.X))) - rect.X;
rect.Height = Math.Max(p1.Y, Math.Max(p2.Y, Math.Max(p3.Y, p4.Y))) - rect.Y;
return RectToBoundingBox(rect);
} }
/// <summary> /// <summary>

View file

@ -6,6 +6,9 @@ using System;
namespace MapControl namespace MapControl
{ {
/// <summary>
/// Replaces Windows.UI.Xaml.Media.Matrix to achieve necessary floating point precision.
/// </summary>
public struct Matrix public struct Matrix
{ {
public double M11 { get; set; } public double M11 { get; set; }

View file

@ -4,6 +4,9 @@
namespace MapControl namespace MapControl
{ {
/// <summary>
/// Replaces Windows.Foundation.Point to achieve necessary floating point precision.
/// </summary>
public struct Point public struct Point
{ {
public double X { get; set; } public double X { get; set; }

View file

@ -19,15 +19,15 @@ namespace MapControl.Projections
/// </summary> /// </summary>
public class GeoApiProjection : MapProjection public class GeoApiProjection : MapProjection
{ {
private IProjectedCoordinateSystem coordinateSystem; private ICoordinateSystem coordinateSystem;
public IMathTransform MathTransform { get; private set; } public IMathTransform LocationToPointTransform { get; private set; }
public IMathTransform InverseTransform { get; private set; } public IMathTransform PointToLocationTransform { get; private set; }
/// <summary> /// <summary>
/// Gets or sets the IProjectedCoordinateSystem of the MapProjection. /// Gets or sets the IProjectedCoordinateSystem of the MapProjection.
/// </summary> /// </summary>
public IProjectedCoordinateSystem CoordinateSystem public ICoordinateSystem CoordinateSystem
{ {
get { return coordinateSystem; } get { return coordinateSystem; }
set set
@ -39,43 +39,45 @@ namespace MapControl.Projections
coordinateSystem = value; coordinateSystem = value;
var coordinateTransform = new CoordinateTransformationFactory() var transformFactory = new CoordinateTransformationFactory();
.CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, coordinateSystem);
MathTransform = coordinateTransform.MathTransform; LocationToPointTransform = transformFactory
InverseTransform = MathTransform.Inverse(); .CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, coordinateSystem)
.MathTransform;
PointToLocationTransform = transformFactory
.CreateFromCoordinateSystems(coordinateSystem, GeographicCoordinateSystem.WGS84)
.MathTransform;
CrsId = (!string.IsNullOrEmpty(coordinateSystem.Authority) && coordinateSystem.AuthorityCode > 0) CrsId = (!string.IsNullOrEmpty(coordinateSystem.Authority) && coordinateSystem.AuthorityCode > 0)
? string.Format("{0}:{1}", coordinateSystem.Authority, coordinateSystem.AuthorityCode) ? string.Format("{0}:{1}", coordinateSystem.Authority, coordinateSystem.AuthorityCode)
: null; : null;
if (!IsWebMercator) IsWebMercator = CrsId == "EPSG:3857" || CrsId == "EPSG:900913";
{
IsWebMercator = CrsId == "EPSG:3857" || CrsId == "EPSG:900913";
}
var projection = coordinateSystem.Projection; var projectedCoordinateSystem = coordinateSystem as IProjectedCoordinateSystem;
var scaleFactor = projection.GetParameter("scale_factor");
if (scaleFactor != null) if (projectedCoordinateSystem != null)
{
TrueScale = scaleFactor.Value * Wgs84MetersPerDegree;
}
if (!IsNormalCylindrical)
{ {
var projection = projectedCoordinateSystem.Projection;
var centralMeridian = projection.GetParameter("central_meridian") ?? projection.GetParameter("longitude_of_origin"); var centralMeridian = projection.GetParameter("central_meridian") ?? projection.GetParameter("longitude_of_origin");
var centralParallel = projection.GetParameter("latitude_of_origin") ?? projection.GetParameter("central_parallel"); var centralParallel = projection.GetParameter("latitude_of_origin") ?? projection.GetParameter("central_parallel");
var falseEasting = projection.GetParameter("false_easting"); var falseEasting = projection.GetParameter("false_easting");
var falseNorthing = projection.GetParameter("false_northing"); var falseNorthing = projection.GetParameter("false_northing");
var scaleFactor = projection.GetParameter("scale_factor");
if (centralMeridian != null && centralMeridian.Value == 0d && IsNormalCylindrical =
centralMeridian != null && centralMeridian.Value == 0d &&
centralParallel != null && centralParallel.Value == 0d && centralParallel != null && centralParallel.Value == 0d &&
(falseEasting == null || falseEasting.Value == 0d) && (falseEasting == null || falseEasting.Value == 0d) &&
(falseNorthing == null || falseNorthing.Value == 0d)) (falseNorthing == null || falseNorthing.Value == 0d);
{
IsNormalCylindrical = true; TrueScale = (scaleFactor != null ? scaleFactor.Value : 1d) * Wgs84MetersPerDegree;
} }
else
{
IsNormalCylindrical = true;
TrueScale = 1d;
} }
} }
} }
@ -88,29 +90,29 @@ namespace MapControl.Projections
public string WKT public string WKT
{ {
get { return CoordinateSystem?.WKT; } get { return CoordinateSystem?.WKT; }
set { CoordinateSystem = (IProjectedCoordinateSystem)new CoordinateSystemFactory().CreateFromWkt(value); } set { CoordinateSystem = new CoordinateSystemFactory().CreateFromWkt(value); }
} }
public override Point LocationToPoint(Location location) public override Point LocationToPoint(Location location)
{ {
if (MathTransform == null) if (LocationToPointTransform == null)
{ {
throw new InvalidOperationException("The CoordinateSystem 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 = LocationToPointTransform.Transform(new Coordinate(location.Longitude, location.Latitude));
return new Point(coordinate.X, coordinate.Y); return new Point(coordinate.X, coordinate.Y);
} }
public override Location PointToLocation(Point point) public override Location PointToLocation(Point point)
{ {
if (InverseTransform == null) if (PointToLocationTransform == null)
{ {
throw new InvalidOperationException("The CoordinateSystem 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 = PointToLocationTransform.Transform(new Coordinate(point.X, point.Y));
return new Location(coordinate.Y, coordinate.X); return new Location(coordinate.Y, coordinate.X);
} }

View file

@ -18,8 +18,6 @@ namespace MapControl.Projections
{ {
public WebMercatorProjection() public WebMercatorProjection()
{ {
IsWebMercator = true;
IsNormalCylindrical = true;
CoordinateSystem = ProjectedCoordinateSystem.WebMercator; CoordinateSystem = ProjectedCoordinateSystem.WebMercator;
} }

View file

@ -17,23 +17,24 @@ namespace MapControl.Projections
{ {
public WorldMercatorProjection() public WorldMercatorProjection()
{ {
IsNormalCylindrical = true; WKT = "PROJCS[\"WGS 84 / World Mercator\"," +
WKT = "PROJCS[\"WGS 84 / World Mercator\"," "GEOGCS[\"WGS 84\"," +
+ "GEOGCS[\"WGS 84\"," "DATUM[\"WGS_1984\"," +
+ "DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563, AUTHORITY[\"EPSG\", \"7030\"]], AUTHORITY[\"EPSG\", \"6326\"]]," "SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," +
+ "PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]]," "AUTHORITY[\"EPSG\",\"6326\"]]," +
+ "UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9122\"]]," "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]]," +
+ "AUTHORITY[\"EPSG\", \"4326\"]]," "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]]," +
+ "PROJECTION[\"Mercator_1SP\"]," "AUTHORITY[\"EPSG\",\"4326\"]]," +
+ "PARAMETER[\"latitude_of_origin\", 0]," "PROJECTION[\"Mercator_1SP\"]," +
+ "PARAMETER[\"central_meridian\", 0]," "PARAMETER[\"latitude_of_origin\",0]," +
+ "PARAMETER[\"scale_factor\", 1]," "PARAMETER[\"central_meridian\",0]," +
+ "PARAMETER[\"false_easting\", 0]," "PARAMETER[\"scale_factor\",1]," +
+ "PARAMETER[\"false_northing\", 0]," "PARAMETER[\"false_easting\",0]," +
+ "UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]]," "PARAMETER[\"false_northing\",0]," +
+ "AXIS[\"Easting\", EAST]," "UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]]," +
+ "AXIS[\"Northing\", NORTH]," "AXIS[\"Easting\",EAST]," +
+ "AUTHORITY[\"EPSG\", \"3395\"]]"; "AXIS[\"Northing\",NORTH]," +
"AUTHORITY[\"EPSG\",\"3395\"]]";
} }
public override Vector GetMapScale(Location location) public override Vector GetMapScale(Location location)