From 7e6b187fd73b7dc5aa2fd6ef653b05c7c8749e31 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Thu, 1 Dec 2022 22:48:08 +0100 Subject: [PATCH] Minor improvements --- MapControl/Shared/BoundingBox.cs | 20 ++++++++----- MapControl/Shared/GroundOverlay.cs | 10 +++---- MapControl/Shared/Location.cs | 39 ++++++++++--------------- MapControl/Shared/LocationCollection.cs | 9 ++++-- MapControl/Shared/MapBase.cs | 4 +-- MapControl/Shared/MapGraticule.cs | 4 +-- MapControl/Shared/MapPanel.cs | 1 - MapControl/Shared/MapProjection.cs | 1 + MapControl/Shared/ViewRect.cs | 2 +- 9 files changed, 44 insertions(+), 46 deletions(-) diff --git a/MapControl/Shared/BoundingBox.cs b/MapControl/Shared/BoundingBox.cs index e34af0e5..47d5b190 100644 --- a/MapControl/Shared/BoundingBox.cs +++ b/MapControl/Shared/BoundingBox.cs @@ -15,8 +15,8 @@ namespace MapControl #endif public class BoundingBox { - private double south; - private double north; + private double south = double.NaN; + private double north = double.NaN; public BoundingBox() { @@ -30,9 +30,8 @@ namespace MapControl East = east; } - public double West { get; set; } - - public double East { get; set; } + public double West { get; set; } = double.NaN; + public double East { get; set; } = double.NaN; public double South { @@ -64,11 +63,16 @@ namespace MapControl protected set { } } - public static BoundingBox Parse(string s) + public static BoundingBox Parse(string boundingBox) { - var values = s.Split(new char[] { ',' }); + string[] values = null; - if (values.Length != 4) + if (!string.IsNullOrEmpty(boundingBox)) + { + values = boundingBox.Split(new char[] { ',' }); + } + + if (values?.Length != 4) { throw new FormatException("BoundingBox string must be a comma-separated list of four floating point numbers."); } diff --git a/MapControl/Shared/GroundOverlay.cs b/MapControl/Shared/GroundOverlay.cs index a96a90f7..540f6b99 100644 --- a/MapControl/Shared/GroundOverlay.cs +++ b/MapControl/Shared/GroundOverlay.cs @@ -242,11 +242,11 @@ namespace MapControl private static LatLonBox ReadLatLonBox(XmlElement element) { - double north = double.NaN; - double south = double.NaN; - double east = double.NaN; - double west = double.NaN; - double rotation = 0d; + var north = double.NaN; + var south = double.NaN; + var east = double.NaN; + var west = double.NaN; + var rotation = 0d; foreach (var childElement in element.ChildNodes.OfType()) { diff --git a/MapControl/Shared/Location.cs b/MapControl/Shared/Location.cs index e17217f3..780b1c74 100644 --- a/MapControl/Shared/Location.cs +++ b/MapControl/Shared/Location.cs @@ -57,42 +57,33 @@ namespace MapControl return string.Format(CultureInfo.InvariantCulture, "{0:F5},{1:F5}", Latitude, Longitude); } - public static Location Parse(string locationString) + public static Location Parse(string location) { - Location location = null; + string[] values = null; - if (!string.IsNullOrEmpty(locationString)) + if (!string.IsNullOrEmpty(location)) { - var values = locationString.Split(new char[] { ',' }); - - if (values.Length != 2) - { - throw new FormatException("Location string must be a comma-separated pair of double values."); - } - - location = new Location( - double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture), - double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture)); + values = location.Split(new char[] { ',' }); } - return location; + if (values?.Length != 2) + { + throw new FormatException("Location string must be a comma-separated pair of floating point numbers."); + } + + return new Location( + double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture), + double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture)); } /// - /// Normalizes a longitude to a value in the interval [-180 .. 180]. + /// Normalizes a longitude to a value in the interval [-180 .. 180). /// public static double NormalizeLongitude(double longitude) { - if (longitude < -180d) - { - longitude = ((longitude + 180d) % 360d) + 180d; - } - else if (longitude > 180d) - { - longitude = ((longitude - 180d) % 360d) - 180d; - } + var x = (longitude + 180d) % 360d; - return longitude; + return x < 0d ? x + 180d : x - 180d; } /// diff --git a/MapControl/Shared/LocationCollection.cs b/MapControl/Shared/LocationCollection.cs index 7eb96348..5b121756 100644 --- a/MapControl/Shared/LocationCollection.cs +++ b/MapControl/Shared/LocationCollection.cs @@ -50,9 +50,14 @@ namespace MapControl Add(new Location(latitude, longitude)); } - public static LocationCollection Parse(string s) + public static LocationCollection Parse(string locations) { - var strings = s.Split(new char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries); + if (string.IsNullOrEmpty(locations)) + { + return new LocationCollection(); + } + + var strings = locations.Split(new char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries); return new LocationCollection(strings.Select(l => Location.Parse(l))); } diff --git a/MapControl/Shared/MapBase.cs b/MapControl/Shared/MapBase.cs index 70066b06..1af85574 100644 --- a/MapControl/Shared/MapBase.cs +++ b/MapControl/Shared/MapBase.cs @@ -390,11 +390,11 @@ namespace MapControl if (offset > 180d) { - longitude = Center.Longitude - 360d + offset % 360d; + longitude = Center.Longitude + (offset % 360d) - 360d; } else if (offset < -180d) { - longitude = Center.Longitude + 360d + offset % 360d; + longitude = Center.Longitude + (offset % 360d) + 360d; } return longitude; diff --git a/MapControl/Shared/MapGraticule.cs b/MapControl/Shared/MapGraticule.cs index 410a725a..bd43f723 100644 --- a/MapControl/Shared/MapGraticule.cs +++ b/MapControl/Shared/MapGraticule.cs @@ -92,9 +92,7 @@ namespace MapControl { var hemisphere = hemispheres[0]; - value = (value + 540d) % 360d - 180d; - - if (value < -1e-8) // ~1mm + if (value < -1e-8) // ~1 mm { value = -value; hemisphere = hemispheres[1]; diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index 87c24107..5b7e30ad 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -211,7 +211,6 @@ namespace MapControl IsOutsideViewport(position)) { location = new Location(location.Latitude, parentMap.ConstrainedLongitude(location.Longitude)); - position = parentMap.LocationToView(location); } diff --git a/MapControl/Shared/MapProjection.cs b/MapControl/Shared/MapProjection.cs index fe505c88..9a75a65d 100644 --- a/MapControl/Shared/MapProjection.cs +++ b/MapControl/Shared/MapProjection.cs @@ -58,6 +58,7 @@ namespace MapControl /// /// Transforms a Location in geographic coordinates to a Point in projected map coordinates. + /// Returns new Point(double.NaN, double.NaN) when the Location can not be transformed. /// public abstract Point LocationToMap(Location location); diff --git a/MapControl/Shared/ViewRect.cs b/MapControl/Shared/ViewRect.cs index 8f942c72..7429ad54 100644 --- a/MapControl/Shared/ViewRect.cs +++ b/MapControl/Shared/ViewRect.cs @@ -7,7 +7,7 @@ namespace MapControl /// /// Rotated rectangle used to arrange and rotate an element with a BoundingBox. /// - public struct ViewRect + public class ViewRect { public ViewRect(double x, double y, double width, double height, double rotation) {