diff --git a/MapControl/Shared/AzimuthalProjection.cs b/MapControl/Shared/AzimuthalProjection.cs index 2d4ba2ad..430b04ca 100644 --- a/MapControl/Shared/AzimuthalProjection.cs +++ b/MapControl/Shared/AzimuthalProjection.cs @@ -19,14 +19,36 @@ namespace MapControl Type = MapProjectionType.Azimuthal; } - public override BoundingBox MapToBoundingBox(Rect rect) + public override Rect? BoundingBoxToMap(BoundingBox boundingBox) { - var rectCenter = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d); + Rect? mapRect = 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; + + mapRect = new Rect(x, y, width, height); + } + + return mapRect; + } + + public override BoundingBox MapToBoundingBox(Rect mapRect) + { + BoundingBox boundingBox = null; + var rectCenter = new Point(mapRect.X + mapRect.Width / 2d, mapRect.Y + mapRect.Height / 2d); var center = MapToLocation(rectCenter); - return center != null - ? new CenteredBoundingBox(center, rect.Width / Wgs84MeterPerDegree, rect.Height / Wgs84MeterPerDegree) - : null; + if (center != null) + { + boundingBox = new CenteredBoundingBox(center, mapRect.Width / Wgs84MeterPerDegree, mapRect.Height / Wgs84MeterPerDegree); + } + + return boundingBox; } /// diff --git a/MapControl/Shared/EquirectangularProjection.cs b/MapControl/Shared/EquirectangularProjection.cs index be9ab1c0..b79c79d1 100644 --- a/MapControl/Shared/EquirectangularProjection.cs +++ b/MapControl/Shared/EquirectangularProjection.cs @@ -3,7 +3,6 @@ // Licensed under the Microsoft Public License (Ms-PL) using System; -using System.Globalization; #if WPF using System.Windows; #endif @@ -51,20 +50,5 @@ namespace MapControl point.Y / Wgs84MeterPerDegree, point.X / Wgs84MeterPerDegree); } - - public override string GetBboxValue(Rect rect) - { - if (CrsId == DefaultCrsId || CrsId == "CRS:84") - { - return string.Format(CultureInfo.InvariantCulture, - CrsId == DefaultCrsId ? "{1:F8},{0:F8},{3:F8},{2:F8}" : "{0:F8},{1:F8},{2:F8},{3:F8}", - rect.X / Wgs84MeterPerDegree, - rect.Y / Wgs84MeterPerDegree, - (rect.X + rect.Width) / Wgs84MeterPerDegree, - (rect.Y + rect.Height) / Wgs84MeterPerDegree); - } - - return base.GetBboxValue(rect); - } } } diff --git a/MapControl/Shared/MapBase.cs b/MapControl/Shared/MapBase.cs index 3a8c9de2..9ed20aec 100644 --- a/MapControl/Shared/MapBase.cs +++ b/MapControl/Shared/MapBase.cs @@ -360,16 +360,16 @@ namespace MapControl /// public void ZoomToBounds(BoundingBox boundingBox) { - var rect = MapProjection.BoundingBoxToMap(boundingBox); + var mapRect = MapProjection.BoundingBoxToMap(boundingBox); - if (rect.HasValue) + if (mapRect.HasValue) { - var rectCenter = new Point(rect.Value.X + rect.Value.Width / 2d, rect.Value.Y + rect.Value.Height / 2d); + var rectCenter = new Point(mapRect.Value.X + mapRect.Value.Width / 2d, mapRect.Value.Y + mapRect.Value.Height / 2d); var targetCenter = MapProjection.MapToLocation(rectCenter); if (targetCenter != null) { - var scale = Math.Min(ActualWidth / rect.Value.Width, ActualHeight / rect.Value.Height); + var scale = Math.Min(ActualWidth / mapRect.Value.Width, ActualHeight / mapRect.Value.Height); TargetZoomLevel = ScaleToZoomLevel(scale); TargetCenter = targetCenter; diff --git a/MapControl/Shared/MapProjection.cs b/MapControl/Shared/MapProjection.cs index c1e8bf18..0b1f810d 100644 --- a/MapControl/Shared/MapProjection.cs +++ b/MapControl/Shared/MapProjection.cs @@ -3,7 +3,6 @@ // Licensed under the Microsoft Public License (Ms-PL) using System; -using System.Globalization; #if WPF using System.Windows; #endif @@ -67,70 +66,34 @@ namespace MapControl /// public virtual Rect? BoundingBoxToMap(BoundingBox boundingBox) { - Rect? rect = null; + Rect? mapRect = null; + var southWest = LocationToMap(new Location(boundingBox.South, boundingBox.West)); + var northEast = LocationToMap(new Location(boundingBox.North, boundingBox.East)); - if (boundingBox.South < boundingBox.North && boundingBox.West < boundingBox.East) + if (southWest.HasValue && northEast.HasValue) { - var p1 = LocationToMap(new Location(boundingBox.South, boundingBox.West)); - var p2 = LocationToMap(new Location(boundingBox.North, boundingBox.East)); - - if (p1.HasValue && p2.HasValue) - { - rect = new Rect(p1.Value, p2.Value); - } - } - else if (boundingBox.Center != null) - { - // boundingBox is a CenteredBoundingBox - // - 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; - - rect = new Rect(x, y, width, height); - } + mapRect = new Rect(southWest.Value, northEast.Value); } - return rect; + return mapRect; } /// /// Transforms a Rect in projected map coordinates to a BoundingBox in geographic coordinates. /// Returns null when the MapRect can not be transformed. /// - public virtual BoundingBox MapToBoundingBox(Rect rect) + public virtual BoundingBox MapToBoundingBox(Rect mapRect) { - var southWest = MapToLocation(new Point(rect.X, rect.Y)); - var northEast = MapToLocation(new Point(rect.X + rect.Width, rect.Y + rect.Height)); + BoundingBox boundingBox = null; + var southWest = MapToLocation(new Point(mapRect.X, mapRect.Y)); + var northEast = MapToLocation(new Point(mapRect.X + mapRect.Width, mapRect.Y + mapRect.Height)); - return southWest != null && northEast != null - ? new BoundingBox(southWest, northEast) - : null; - } + if (southWest != null && northEast != null) + { + boundingBox = new BoundingBox(southWest, northEast); + } - /// - /// Gets the CRS parameter value for a WMS GetMap request. - /// - public virtual string GetCrsValue() - { - return CrsId.StartsWith("AUTO2:") || CrsId.StartsWith("AUTO:") - ? string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", CrsId, Center.Longitude, Center.Latitude) - : CrsId; - } - - /// - /// Gets the BBOX parameter value for a WMS GetMap request. - /// - public virtual string GetBboxValue(Rect rect) - { - return string.Format(CultureInfo.InvariantCulture, - "{0:F2},{1:F2},{2:F2},{3:F2}", - rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height); + return boundingBox; } } } diff --git a/MapControl/Shared/WmsImageLayer.cs b/MapControl/Shared/WmsImageLayer.cs index 55c82c93..c170aa42 100644 --- a/MapControl/Shared/WmsImageLayer.cs +++ b/MapControl/Shared/WmsImageLayer.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.Linq; using System.Threading.Tasks; using System.Xml.Linq; @@ -297,12 +298,36 @@ namespace MapControl protected virtual string GetCrsValue() { - return ParentMap.MapProjection.GetCrsValue(); + var projection = ParentMap.MapProjection; + var crsId = projection.CrsId; + + if (crsId.StartsWith("AUTO2:") || crsId.StartsWith("AUTO:")) + { + crsId = string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", crsId, projection.Center.Longitude, projection.Center.Latitude); + } + + return crsId; } protected virtual string GetBboxValue(Rect rect) { - return ParentMap.MapProjection.GetBboxValue(rect); + var crsId = ParentMap.MapProjection.CrsId; + var format = "{0:F2},{1:F2},{2:F2},{3:F2}"; + var x1 = rect.X; + var y1 = rect.Y; + var x2 = rect.X + rect.Width; + var y2 = rect.Y + rect.Height; + + if (crsId == "CRS:84" || crsId == "EPSG:4326") + { + format = crsId == "CRS:84" ? "{0:F8},{1:F8},{2:F8},{3:F8}" : "{1:F8},{0:F8},{3:F8},{2:F8}"; + x1 /= MapProjection.Wgs84MeterPerDegree; + y1 /= MapProjection.Wgs84MeterPerDegree; + x2 /= MapProjection.Wgs84MeterPerDegree; + y2 /= MapProjection.Wgs84MeterPerDegree; + } + + return string.Format(CultureInfo.InvariantCulture, format, x1, y1, x2, y2); } protected string GetRequestUri(IDictionary queryParameters) diff --git a/MapControl/WPF/MapItemsImageLayer.WPF.cs b/MapControl/WPF/MapItemsImageLayer.WPF.cs index 00733dd4..8b16bef8 100644 --- a/MapControl/WPF/MapItemsImageLayer.WPF.cs +++ b/MapControl/WPF/MapItemsImageLayer.WPF.cs @@ -37,11 +37,11 @@ namespace MapControl if (projection != null && items != null) { - var rect = projection.BoundingBoxToMap(boundingBox); + var mapRect = projection.BoundingBoxToMap(boundingBox); - if (rect.HasValue) + if (mapRect.HasValue) { - image = await Task.Run(() => GetImage(projection, rect.Value, items)); + image = await Task.Run(() => GetImage(projection, mapRect.Value, items)); } }