From 0dbdd4c81091b662de45ff4942c6bbb16a2484ca Mon Sep 17 00:00:00 2001 From: ClemensF Date: Mon, 16 Dec 2019 18:35:04 +0100 Subject: [PATCH] Version 4.16.0. Improved MapProjection. --- .../Shared/AzimuthalEquidistantProjection.cs | 2 +- MapControl/Shared/AzimuthalProjection.cs | 31 ++++++++++++------- MapControl/Shared/GnomonicProjection.cs | 2 +- MapControl/Shared/OrthographicProjection.cs | 2 +- MapControl/Shared/StereographicProjection.cs | 2 +- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/MapControl/Shared/AzimuthalEquidistantProjection.cs b/MapControl/Shared/AzimuthalEquidistantProjection.cs index 3630cecf..aacbcfa1 100644 --- a/MapControl/Shared/AzimuthalEquidistantProjection.cs +++ b/MapControl/Shared/AzimuthalEquidistantProjection.cs @@ -36,7 +36,7 @@ namespace MapControl { if (point.X == 0d && point.Y == 0d) { - return ProjectionCenter; + return new Location(ProjectionCenter.Latitude, ProjectionCenter.Longitude); } var azimuth = Math.Atan2(point.X, point.Y); diff --git a/MapControl/Shared/AzimuthalProjection.cs b/MapControl/Shared/AzimuthalProjection.cs index 79728336..79f51956 100644 --- a/MapControl/Shared/AzimuthalProjection.cs +++ b/MapControl/Shared/AzimuthalProjection.cs @@ -71,18 +71,27 @@ namespace MapControl /// public static Location GetLocation(Location location, double azimuth, double distance) { - var lat1 = location.Latitude * Math.PI / 180d; - var sinDistance = Math.Sin(distance); - var cosDistance = Math.Cos(distance); - var cosAzimuth = Math.Cos(azimuth); - var sinAzimuth = Math.Sin(azimuth); - var cosLat1 = Math.Cos(lat1); - var sinLat1 = Math.Sin(lat1); - var sinLat2 = sinLat1 * cosDistance + cosLat1 * sinDistance * cosAzimuth; - var lat2 = Math.Asin(Math.Min(Math.Max(sinLat2, -1d), 1d)); - var dLon = Math.Atan2(sinDistance * sinAzimuth, cosLat1 * cosDistance - sinLat1 * sinDistance * cosAzimuth); + var lat = location.Latitude; + var lon = location.Longitude; - return new Location(lat2 * 180d / Math.PI, location.Longitude + dLon * 180d / Math.PI); + if (distance > 0d) + { + var lat1 = lat * Math.PI / 180d; + var sinDistance = Math.Sin(distance); + var cosDistance = Math.Cos(distance); + var cosAzimuth = Math.Cos(azimuth); + var sinAzimuth = Math.Sin(azimuth); + var cosLat1 = Math.Cos(lat1); + var sinLat1 = Math.Sin(lat1); + var sinLat2 = sinLat1 * cosDistance + cosLat1 * sinDistance * cosAzimuth; + var lat2 = Math.Asin(Math.Min(Math.Max(sinLat2, -1d), 1d)); + var dLon = Math.Atan2(sinDistance * sinAzimuth, cosLat1 * cosDistance - sinLat1 * sinDistance * cosAzimuth); + + lat = lat2 * 180d / Math.PI; + lon += dLon * 180d / Math.PI; + } + + return new Location(lat, lon); } } } diff --git a/MapControl/Shared/GnomonicProjection.cs b/MapControl/Shared/GnomonicProjection.cs index 26ea93cf..f1f07f9e 100644 --- a/MapControl/Shared/GnomonicProjection.cs +++ b/MapControl/Shared/GnomonicProjection.cs @@ -41,7 +41,7 @@ namespace MapControl { if (point.X == 0d && point.Y == 0d) { - return ProjectionCenter; + return new Location(ProjectionCenter.Latitude, ProjectionCenter.Longitude); } var azimuth = Math.Atan2(point.X, point.Y); diff --git a/MapControl/Shared/OrthographicProjection.cs b/MapControl/Shared/OrthographicProjection.cs index acea7799..08855756 100644 --- a/MapControl/Shared/OrthographicProjection.cs +++ b/MapControl/Shared/OrthographicProjection.cs @@ -40,7 +40,7 @@ namespace MapControl { if (point.X == 0d && point.Y == 0d) { - return ProjectionCenter; + return new Location(ProjectionCenter.Latitude, ProjectionCenter.Longitude); } var s = TrueScale * 180d / Math.PI; diff --git a/MapControl/Shared/StereographicProjection.cs b/MapControl/Shared/StereographicProjection.cs index cd9c7171..ef9fa244 100644 --- a/MapControl/Shared/StereographicProjection.cs +++ b/MapControl/Shared/StereographicProjection.cs @@ -39,7 +39,7 @@ namespace MapControl { if (point.X == 0d && point.Y == 0d) { - return ProjectionCenter; + return new Location(ProjectionCenter.Latitude, ProjectionCenter.Longitude); } var azimuth = Math.Atan2(point.X, point.Y);