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

@ -10,7 +10,7 @@ using System.Windows;
namespace MapControl
{
/// <summary>
/// Transforms map coordinates according to the Azimuthal Equidistant Projection.
/// Spherical Azimuthal Equidistant Projection.
/// </summary>
public class AzimuthalEquidistantProjection : AzimuthalProjection
{
@ -35,7 +35,7 @@ namespace MapControl
GetAzimuthDistance(ProjectionCenter, location, out azimuth, out distance);
distance *= Wgs84EquatorialRadius;
distance *= TrueScale * 180d / Math.PI;
return new Point(distance * Math.Sin(azimuth), distance * Math.Cos(azimuth));
}
@ -48,7 +48,7 @@ namespace MapControl
}
var azimuth = Math.Atan2(point.X, point.Y);
var distance = Math.Sqrt(point.X * point.X + point.Y * point.Y) / Wgs84EquatorialRadius;
var distance = Math.Sqrt(point.X * point.X + point.Y * point.Y) / (TrueScale * 180d / Math.PI);
return GetLocation(ProjectionCenter, azimuth, distance);
}

View file

@ -67,7 +67,7 @@ namespace MapControl
}
/// <summary>
/// Calculates azimuth and distance in radians from location1 to location2.
/// Calculates azimuth and spherical distance in radians from location1 to location2.
/// The returned distance has to be multiplied with an appropriate earth radius.
/// </summary>
public static void GetAzimuthDistance(Location location1, Location location2, out double azimuth, out double distance)
@ -89,7 +89,7 @@ namespace MapControl
}
/// <summary>
/// Calculates the Location of the point given by azimuth and distance in radians from location.
/// Calculates the Location of the point given by azimuth and spherical distance in radians from location.
/// </summary>
public static Location GetLocation(Location location, double azimuth, double distance)
{

View file

@ -56,11 +56,6 @@ namespace MapControl
get { return North - South; }
}
public bool HasValidBounds
{
get { return South < North && West < East; }
}
public virtual BoundingBox Clone()
{
return new BoundingBox(South, West, North, East);

View file

@ -2,6 +2,8 @@
// © 2018 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
namespace MapControl
{
public class CenteredBoundingBox : BoundingBox
@ -12,8 +14,8 @@ namespace MapControl
public CenteredBoundingBox(Location center, double width, double height)
{
Center = center;
this.width = width;
this.height = height;
this.width = Math.Max(width, 0d);
this.height = Math.Max(height, 0d);
}
public Location Center { get; private set; }

View file

@ -10,7 +10,7 @@ using System.Windows;
namespace MapControl
{
/// <summary>
/// Transforms map coordinates according to the Equirectangular Projection.
/// Equirectangular Projection.
/// Longitude and Latitude values are transformed identically to X and Y.
/// </summary>
public class EquirectangularProjection : MapProjection
@ -23,8 +23,8 @@ namespace MapControl
public EquirectangularProjection(string crsId)
{
CrsId = crsId;
IsCylindrical = true;
TrueScale = 1;
IsNormalCylindrical = true;
TrueScale = 1d;
}
public override Vector GetMapScale(Location location)

View file

@ -10,7 +10,7 @@ using System.Windows;
namespace MapControl
{
/// <summary>
/// Transforms map coordinates according to the Gnomonic Projection.
/// Spherical Gnomonic Projection.
/// </summary>
public class GnomonicProjection : AzimuthalProjection
{
@ -36,7 +36,7 @@ namespace MapControl
GetAzimuthDistance(ProjectionCenter, location, out azimuth, out distance);
var mapDistance = distance < Math.PI / 2d
? Wgs84EquatorialRadius * Math.Tan(distance)
? Math.Tan(distance) * TrueScale * 180d / Math.PI
: double.PositiveInfinity;
return new Point(mapDistance * Math.Sin(azimuth), mapDistance * Math.Cos(azimuth));
@ -51,7 +51,7 @@ namespace MapControl
var azimuth = Math.Atan2(point.X, point.Y);
var mapDistance = Math.Sqrt(point.X * point.X + point.Y * point.Y);
var distance = Math.Atan(mapDistance / Wgs84EquatorialRadius);
var distance = Math.Atan(mapDistance / (TrueScale * 180d / Math.PI));
return GetLocation(ProjectionCenter, azimuth, distance);
}

View file

@ -62,7 +62,7 @@ namespace MapControl
var lat2 = Math.Asin(sinLat1 * cosS12 + cosLat1 * sinS12 * cosAz1);
var lon2 = lon1 + Math.Atan2(sinS12 * sinAz1, (cosLat1 * cosS12 - sinLat1 * sinS12 * cosAz1));
return new Location(lat2 / Math.PI * 180d, lon2 / Math.PI * 180d);
return new Location(lat2 * 180d / Math.PI, lon2 * 180d / Math.PI);
}
public static LocationCollection CalculateMeridianLocations(this Location location, double latitude2, double resolution = 1d)

View file

@ -348,17 +348,14 @@ namespace MapControl
/// </summary>
public void ZoomToBounds(BoundingBox boundingBox)
{
if (boundingBox != null && boundingBox.HasValidBounds)
{
var rect = MapProjection.BoundingBoxToRect(boundingBox);
var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
var scale = Math.Min(RenderSize.Width / rect.Width, RenderSize.Height / rect.Height)
* MapProjection.TrueScale / MapProjection.PixelPerDegree;
var rect = MapProjection.BoundingBoxToRect(boundingBox);
var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
var scale = Math.Min(RenderSize.Width / rect.Width, RenderSize.Height / rect.Height)
* MapProjection.TrueScale / MapProjection.PixelPerDegree;
TargetZoomLevel = Math.Log(scale, 2d);
TargetCenter = MapProjection.PointToLocation(center);
TargetHeading = 0d;
}
TargetZoomLevel = Math.Log(scale, 2d);
TargetCenter = MapProjection.PointToLocation(center);
TargetHeading = 0d;
}
private void MapLayerPropertyChanged(UIElement oldLayer, UIElement newLayer)
@ -451,7 +448,7 @@ namespace MapControl
if (!targetCenter.Equals(Center))
{
var targetCenterLongitude = MapProjection.IsCylindrical
var targetCenterLongitude = MapProjection.IsNormalCylindrical
? Location.NearestLongitude(targetCenter.Longitude, Center.Longitude)
: targetCenter.Longitude;

View file

@ -256,7 +256,7 @@ namespace MapControl
boundingBox = ParentMap.MapProjection.ViewportRectToBoundingBox(rect);
if (boundingBox != null && boundingBox.HasValidBounds)
if (boundingBox != null)
{
if (!double.IsNaN(MinLatitude) && boundingBox.South < MinLatitude)
{
@ -289,7 +289,7 @@ namespace MapControl
private void AdjustBoundingBox(double longitudeOffset)
{
if (Math.Abs(longitudeOffset) > 180d && boundingBox != null && boundingBox.HasValidBounds)
if (Math.Abs(longitudeOffset) > 180d && boundingBox != null)
{
var offset = 360d * Math.Sign(longitudeOffset);
@ -300,7 +300,7 @@ namespace MapControl
{
var bbox = GetBoundingBox(element);
if (bbox != null && bbox.HasValidBounds)
if (bbox != null)
{
SetBoundingBox(element, new BoundingBox(bbox.South, bbox.West + offset, bbox.North, bbox.East + offset));
}

View file

@ -247,7 +247,7 @@ namespace MapControl
var projection = parentMap.MapProjection;
pos = projection.LocationToViewportPoint(location);
if (projection.IsCylindrical &&
if (projection.IsNormalCylindrical &&
(pos.X < 0d || pos.X > parentMap.RenderSize.Width ||
pos.Y < 0d || pos.Y > parentMap.RenderSize.Height))
{
@ -311,7 +311,7 @@ namespace MapControl
var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
var pos = projection.ViewportTransform.Transform(center);
if (projection.IsCylindrical &&
if (projection.IsNormalCylindrical &&
(pos.X < 0d || pos.X > parentMap.RenderSize.Width ||
pos.Y < 0d || pos.Y > parentMap.RenderSize.Height))
{

View file

@ -23,6 +23,9 @@ namespace MapControl
public const double PixelPerDegree = TileSize / 360d;
public const double Wgs84EquatorialRadius = 6378137d;
public const double Wgs84Flattening = 1d / 298.257223563;
public static readonly double Wgs84Eccentricity = Math.Sqrt((2d - Wgs84Flattening) * Wgs84Flattening);
public const double MetersPerDegree = Wgs84EquatorialRadius * Math.PI / 180d;
/// <summary>
@ -33,7 +36,7 @@ namespace MapControl
/// <summary>
/// Indicates if this is a normal cylindrical projection.
/// </summary>
public bool IsCylindrical { get; protected set; } = false;
public bool IsNormalCylindrical { get; protected set; } = false;
/// <summary>
/// Indicates if this is a web mercator projection, i.e. compatible with MapTileLayer.
@ -57,12 +60,13 @@ namespace MapControl
public Matrix ViewportTransform { get; private set; }
/// <summary>
/// Gets the transform matrix from viewport coordinates to cartesian map coordinates (pixels).
/// Gets the transform matrix from viewport coordinates (pixels) to cartesian map coordinates.
/// </summary>
public Matrix InverseViewportTransform { get; private set; }
/// <summary>
/// Gets the scaling factor from cartesian map coordinates to viewport coordinates.
/// Gets the scaling factor from cartesian map coordinates to viewport coordinates (pixels)
/// at the projection's point of true scale.
/// </summary>
public double ViewportScale { get; private set; }
@ -151,7 +155,7 @@ namespace MapControl
/// </summary>
public virtual string WmsQueryParameters(BoundingBox boundingBox)
{
if (string.IsNullOrEmpty(CrsId) || !boundingBox.HasValidBounds)
if (string.IsNullOrEmpty(CrsId))
{
return null;
}

View file

@ -106,7 +106,7 @@ namespace MapControl
{
var longitudeOffset = 0d;
if (parentMap.MapProjection.IsCylindrical && Location != null)
if (parentMap.MapProjection.IsNormalCylindrical && Location != null)
{
var viewportPosition = LocationToViewportPoint(Location);

View file

@ -10,7 +10,7 @@ using System.Windows;
namespace MapControl
{
/// <summary>
/// Transforms map coordinates according to the Orthographic Projection.
/// Spherical Orthographic Projection.
/// </summary>
public class OrthographicProjection : AzimuthalProjection
{
@ -34,10 +34,11 @@ namespace MapControl
var lat0 = ProjectionCenter.Latitude * Math.PI / 180d;
var lat = location.Latitude * Math.PI / 180d;
var dLon = (location.Longitude - ProjectionCenter.Longitude) * Math.PI / 180d;
var s = TrueScale * 180d / Math.PI;
return new Point(
Wgs84EquatorialRadius * Math.Cos(lat) * Math.Sin(dLon),
Wgs84EquatorialRadius * (Math.Cos(lat0) * Math.Sin(lat) - Math.Sin(lat0) * Math.Cos(lat) * Math.Cos(dLon)));
s * Math.Cos(lat) * Math.Sin(dLon),
s * (Math.Cos(lat0) * Math.Sin(lat) - Math.Sin(lat0) * Math.Cos(lat) * Math.Cos(dLon)));
}
public override Location PointToLocation(Point point)
@ -47,8 +48,9 @@ namespace MapControl
return ProjectionCenter;
}
var x = point.X / Wgs84EquatorialRadius;
var y = point.Y / Wgs84EquatorialRadius;
var s = TrueScale * 180d / Math.PI;
var x = point.X / s;
var y = point.Y / s;
var r2 = x * x + y * y;
if (r2 > 1d)

View file

@ -10,7 +10,7 @@ using System.Windows;
namespace MapControl
{
/// <summary>
/// Transforms map coordinates according to the Gnomonic Projection.
/// Spherical Stereographic Projection.
/// </summary>
public class StereographicProjection : AzimuthalProjection
{
@ -35,7 +35,7 @@ namespace MapControl
GetAzimuthDistance(ProjectionCenter, location, out azimuth, out distance);
var mapDistance = 2d * Wgs84EquatorialRadius * Math.Tan(distance / 2d);
var mapDistance = Math.Tan(distance / 2d) * TrueScale * 360d / Math.PI;
return new Point(mapDistance * Math.Sin(azimuth), mapDistance * Math.Cos(azimuth));
}
@ -49,7 +49,7 @@ namespace MapControl
var azimuth = Math.Atan2(point.X, point.Y);
var mapDistance = Math.Sqrt(point.X * point.X + point.Y * point.Y);
var distance = 2d * Math.Atan(mapDistance / (2d * Wgs84EquatorialRadius));
var distance = 2d * Math.Atan(mapDistance / (TrueScale * 360d / Math.PI));
return GetLocation(ProjectionCenter, azimuth, distance);
}

View file

@ -10,10 +10,8 @@ using System.Windows;
namespace MapControl
{
/// <summary>
/// Transforms map coordinates according to the Web (or Pseudo) Mercator Projection, EPSG:3857.
/// Longitude values are transformed linearly to X values in meters, by multiplying with TrueScale.
/// Latitude values in the interval [-MaxLatitude .. MaxLatitude] are transformed to Y values in meters
/// in the interval [-R*pi .. R*pi], R=Wgs84EquatorialRadius.
/// Spherical Mercator Projection, EPSG:3857.
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.41-44.
/// </summary>
public class WebMercatorProjection : MapProjection
{
@ -25,16 +23,16 @@ namespace MapControl
public WebMercatorProjection(string crsId)
{
CrsId = crsId;
IsCylindrical = true;
IsNormalCylindrical = true;
IsWebMercator = true;
MaxLatitude = YToLatitude(180d);
}
public override Vector GetMapScale(Location location)
{
var scale = ViewportScale / Math.Cos(location.Latitude * Math.PI / 180d);
var k = 1d / Math.Cos(location.Latitude * Math.PI / 180d); // p.44 (7-3)
return new Vector(scale, scale);
return new Vector(ViewportScale * k, ViewportScale * k);
}
public override Point LocationToPoint(Location location)
@ -63,12 +61,12 @@ namespace MapControl
return double.PositiveInfinity;
}
return Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) / Math.PI * 180d;
return Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) * 180d / Math.PI;
}
public static double YToLatitude(double y)
{
return 90d - Math.Atan(Math.Exp(-y * Math.PI / 180d)) / Math.PI * 360d;
return 90d - Math.Atan(Math.Exp(-y * Math.PI / 180d)) * 360d / Math.PI;
}
}
}

View file

@ -10,17 +10,12 @@ using System.Windows;
namespace MapControl
{
/// <summary>
/// Transforms map coordinates according to the "World Mercator" Projection, EPSG:3395.
/// Longitude values are transformed linearly to X values in meters, by multiplying with TrueScale.
/// Latitude values are transformed according to the elliptical versions of the Mercator equations,
/// as shown in "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.44.
/// Elliptical Mercator Projection, EPSG:3395.
/// See "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.44-45.
/// </summary>
public class WorldMercatorProjection : MapProjection
{
public const double Wgs84Flattening = 1d / 298.257223563;
public static readonly double Wgs84Eccentricity = Math.Sqrt((2d - Wgs84Flattening) * Wgs84Flattening);
public static double MinLatitudeDelta = 1d / Wgs84EquatorialRadius; // corresponds to 1 meter
public static double ConvergenceTolerance = 1e-6;
public static int MaxIterations = 10;
public WorldMercatorProjection()
@ -31,7 +26,7 @@ namespace MapControl
public WorldMercatorProjection(string crsId)
{
CrsId = crsId;
IsCylindrical = true;
IsNormalCylindrical = true;
MaxLatitude = YToLatitude(180d);
}
@ -39,9 +34,9 @@ namespace MapControl
{
var lat = location.Latitude * Math.PI / 180d;
var eSinLat = Wgs84Eccentricity * Math.Sin(lat);
var scale = ViewportScale * Math.Sqrt(1d - eSinLat * eSinLat) / Math.Cos(lat);
var k = Math.Sqrt(1d - eSinLat * eSinLat) / Math.Cos(lat); // p.44 (7-8)
return new Vector(scale, scale);
return new Vector(ViewportScale * k, ViewportScale * k);
}
public override Point LocationToPoint(Location location)
@ -72,24 +67,24 @@ namespace MapControl
var lat = latitude * Math.PI / 180d;
return Math.Log(Math.Tan(lat / 2d + Math.PI / 4d) * ConformalFactor(lat)) / Math.PI * 180d;
return Math.Log(Math.Tan(lat / 2d + Math.PI / 4d)
* ConformalFactor(lat)) * 180d / Math.PI; // p.44 (7-7)
}
public static double YToLatitude(double y)
{
var t = Math.Exp(-y * Math.PI / 180d);
var lat = Math.PI / 2d - 2d * Math.Atan(t);
var latDelta = 1d;
var t = Math.Exp(-y * Math.PI / 180d); // p.44 (7-10)
var lat = Math.PI / 2d - 2d * Math.Atan(t); // p.44 (7-11)
var relChange = 1d;
for (int i = 0; i < MaxIterations && latDelta > MinLatitudeDelta; i++)
for (var i = 0; i < MaxIterations && relChange > ConvergenceTolerance; i++)
{
var newLat = Math.PI / 2d - 2d * Math.Atan(t * ConformalFactor(lat));
latDelta = Math.Abs(newLat - lat);
var newLat = Math.PI / 2d - 2d * Math.Atan(t * ConformalFactor(lat)); // p.44 (7-9)
relChange = Math.Abs(1d - newLat / lat);
lat = newLat;
}
return lat / Math.PI * 180d;
return lat * 180d / Math.PI;
}
private static double ConformalFactor(double lat)

View file

@ -26,7 +26,7 @@ namespace MapControl
{
var projection = ParentMap.MapProjection;
if (projection.IsCylindrical)
if (projection.IsNormalCylindrical)
{
if (path == null)
{

View file

@ -44,7 +44,7 @@ namespace MapControl
var lineDistance = GetLineDistance();
var labelFormat = GetLabelFormat(lineDistance);
if (projection.IsCylindrical)
if (projection.IsNormalCylindrical)
{
DrawCylindricalGraticule(drawingContext, projection, lineDistance, labelFormat);
}
@ -58,52 +58,48 @@ namespace MapControl
private void DrawCylindricalGraticule(DrawingContext drawingContext, MapProjection projection, double lineDistance, string labelFormat)
{
var boundingBox = projection.ViewportRectToBoundingBox(new Rect(ParentMap.RenderSize));
var latLabelStart = Math.Ceiling(boundingBox.South / lineDistance) * lineDistance;
var lonLabelStart = Math.Ceiling(boundingBox.West / lineDistance) * lineDistance;
var latLabels = new List<Label>((int)((boundingBox.North - latLabelStart) / lineDistance) + 1);
var lonLabels = new List<Label>((int)((boundingBox.East - lonLabelStart) / lineDistance) + 1);
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pixelsPerDpi = VisualTreeHelper.GetDpi(this).PixelsPerDip;
var pen = CreatePen();
if (boundingBox.HasValidBounds)
for (var lat = latLabelStart; lat <= boundingBox.North; lat += lineDistance)
{
var latLabelStart = Math.Ceiling(boundingBox.South / lineDistance) * lineDistance;
var lonLabelStart = Math.Ceiling(boundingBox.West / lineDistance) * lineDistance;
var latLabels = new List<Label>((int)((boundingBox.North - latLabelStart) / lineDistance) + 1);
var lonLabels = new List<Label>((int)((boundingBox.East - lonLabelStart) / lineDistance) + 1);
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pixelsPerDpi = VisualTreeHelper.GetDpi(this).PixelsPerDip;
var pen = CreatePen();
latLabels.Add(new Label(lat, new FormattedText(
GetLabelText(lat, labelFormat, "NS"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDpi)));
for (var lat = latLabelStart; lat <= boundingBox.North; lat += lineDistance)
drawingContext.DrawLine(pen,
projection.LocationToViewportPoint(new Location(lat, boundingBox.West)),
projection.LocationToViewportPoint(new Location(lat, boundingBox.East)));
}
for (var lon = lonLabelStart; lon <= boundingBox.East; lon += lineDistance)
{
lonLabels.Add(new Label(lon, new FormattedText(
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDpi)));
drawingContext.DrawLine(pen,
projection.LocationToViewportPoint(new Location(boundingBox.South, lon)),
projection.LocationToViewportPoint(new Location(boundingBox.North, lon)));
}
foreach (var latLabel in latLabels)
{
foreach (var lonLabel in lonLabels)
{
latLabels.Add(new Label(lat, new FormattedText(
GetLabelText(lat, labelFormat, "NS"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDpi)));
var position = projection.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));
drawingContext.DrawLine(pen,
projection.LocationToViewportPoint(new Location(lat, boundingBox.West)),
projection.LocationToViewportPoint(new Location(lat, boundingBox.East)));
}
for (var lon = lonLabelStart; lon <= boundingBox.East; lon += lineDistance)
{
lonLabels.Add(new Label(lon, new FormattedText(
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDpi)));
drawingContext.DrawLine(pen,
projection.LocationToViewportPoint(new Location(boundingBox.South, lon)),
projection.LocationToViewportPoint(new Location(boundingBox.North, lon)));
}
foreach (var latLabel in latLabels)
{
foreach (var lonLabel in lonLabels)
{
var position = projection.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));
drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, position.X, position.Y));
drawingContext.DrawText(latLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
drawingContext.DrawText(lonLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
drawingContext.Pop();
}
drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, position.X, position.Y));
drawingContext.DrawText(latLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
drawingContext.DrawText(lonLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
drawingContext.Pop();
}
}
}