From d8c416d552e5f17873ff8f7f068599b5db06b053 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Wed, 7 Dec 2022 23:34:42 +0100 Subject: [PATCH] Simplify BoundingBox --- MapControl/Shared/AzimuthalProjection.cs | 20 ------------- MapControl/Shared/BoundingBox.cs | 23 +++++---------- MapControl/Shared/MapProjection.cs | 37 ++++++++++++++++++++---- 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/MapControl/Shared/AzimuthalProjection.cs b/MapControl/Shared/AzimuthalProjection.cs index e5cfcb5f..a5e40a41 100644 --- a/MapControl/Shared/AzimuthalProjection.cs +++ b/MapControl/Shared/AzimuthalProjection.cs @@ -3,9 +3,6 @@ // Licensed under the Microsoft Public License (Ms-PL) using System; -#if !WINUI && !UWP -using System.Windows; -#endif namespace MapControl { @@ -19,23 +16,6 @@ namespace MapControl Type = MapProjectionType.Azimuthal; } - public override MapRect BoundingBoxToMapRect(BoundingBox boundingBox) - { - var center = LocationToMap(boundingBox.Center); - - if (!center.HasValue) - { - return null; - } - - var width = boundingBox.Width * Wgs84MeterPerDegree; - var height = boundingBox.Height * Wgs84MeterPerDegree; - var x = center.Value.X - width / 2d; - var y = center.Value.Y - height / 2d; - - return new MapRect(x, y, x + width, y + height); - } - public override BoundingBox MapRectToBoundingBox(MapRect mapRect) { var center = MapToLocation(mapRect.Center); diff --git a/MapControl/Shared/BoundingBox.cs b/MapControl/Shared/BoundingBox.cs index 0e56ab0b..80252737 100644 --- a/MapControl/Shared/BoundingBox.cs +++ b/MapControl/Shared/BoundingBox.cs @@ -39,9 +39,6 @@ namespace MapControl { } - public double West { get; set; } - public double East { get; set; } - public double South { get => south; @@ -54,20 +51,16 @@ namespace MapControl set => north = Math.Min(Math.Max(value, -90d), 90d); } - public virtual double Width - { - get => East - West; - } + public double West { get; set; } + public double East { get; set; } - public virtual double Height - { - get => North - South; - } + public virtual double Width => East - West; + public virtual double Height => North - South; - public virtual Location Center - { - get => new Location((South + North) / 2d, (West + East) / 2d); - } + public virtual Location Center => + double.IsNaN(South) || double.IsNaN(North) || double.IsNaN(West) || double.IsNaN(East) + ? null + : new Location((South + North) / 2d, (West + East) / 2d); public static BoundingBox Parse(string boundingBox) { diff --git a/MapControl/Shared/MapProjection.cs b/MapControl/Shared/MapProjection.cs index 0909bb08..1c4d4296 100644 --- a/MapControl/Shared/MapProjection.cs +++ b/MapControl/Shared/MapProjection.cs @@ -72,10 +72,33 @@ namespace MapControl /// public virtual MapRect BoundingBoxToMapRect(BoundingBox boundingBox) { - var p1 = LocationToMap(new Location(boundingBox.South, boundingBox.West)); - var p2 = LocationToMap(new Location(boundingBox.North, boundingBox.East)); + if (!double.IsNaN(boundingBox.South) && !double.IsNaN(boundingBox.West) && + !double.IsNaN(boundingBox.North) && !double.IsNaN(boundingBox.East)) + { + var p1 = LocationToMap(new Location(boundingBox.South, boundingBox.West)); + var p2 = LocationToMap(new Location(boundingBox.North, boundingBox.East)); - return p1.HasValue && p2.HasValue ? new MapRect(p1.Value, p2.Value) : null; + if (p1.HasValue && p2.HasValue) + { + return new MapRect(p1.Value, p2.Value); + } + } + else if (boundingBox.Center != null) + { + var center = LocationToMap(boundingBox.Center); + + if (center.HasValue) + { + var width = boundingBox.Width * Wgs84MeterPerDegree; + var height = boundingBox.Height * Wgs84MeterPerDegree; + var x = center.Value.X - width / 2d; + var y = center.Value.Y - height / 2d; + + return new MapRect(x, y, x + width, y + height); + } + } + + return null; } /// @@ -84,10 +107,12 @@ namespace MapControl /// public virtual BoundingBox MapRectToBoundingBox(MapRect mapRect) { - var sw = MapToLocation(new Point(mapRect.XMin, mapRect.YMin)); - var ne = MapToLocation(new Point(mapRect.XMax, mapRect.YMax)); + var southWest = MapToLocation(new Point(mapRect.XMin, mapRect.YMin)); + var northEast = MapToLocation(new Point(mapRect.XMax, mapRect.YMax)); - return sw != null && ne != null ? new BoundingBox(sw, ne) : null; + return southWest != null && northEast != null + ? new BoundingBox(southWest, northEast) + : null; } ///