diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index 1749b315..3c110094 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -208,14 +208,14 @@ namespace MapControl private Rect? GetViewRect(BoundingBox boundingBox) { - var mapRect = parentMap.MapProjection.BoundingBoxToMap(boundingBox); + var rect = parentMap.MapProjection.BoundingBoxToMap(boundingBox); - if (mapRect.HasValue) + if (rect.HasValue) { - return GetViewRect(mapRect.Value); + rect = GetViewRect(rect.Value); } - return null; + return rect; } private Rect GetViewRect(Rect mapRect) diff --git a/MapControl/Shared/WmsImageLayer.cs b/MapControl/Shared/WmsImageLayer.cs index 69465079..535a8bee 100644 --- a/MapControl/Shared/WmsImageLayer.cs +++ b/MapControl/Shared/WmsImageLayer.cs @@ -138,7 +138,9 @@ namespace MapControl ParentMap.ActualWidth > 0d && ParentMap.ActualHeight > 0d) { - var uri = GetFeatureInfoRequestUri(position, format); + var boundingBox = ParentMap.ViewRectToBoundingBox(new Rect(0d, 0d, ParentMap.ActualWidth, ParentMap.ActualHeight)); + + var uri = GetFeatureInfoRequestUri(boundingBox, position, format); if (!string.IsNullOrEmpty(uri)) { @@ -229,73 +231,75 @@ namespace MapControl /// protected virtual string GetMapRequestUri(BoundingBox boundingBox) { + string uri = null; var mapRect = ParentMap.MapProjection.BoundingBoxToMap(boundingBox); - if (!mapRect.HasValue) + if (mapRect.HasValue) { - return null; + var width = ParentMap.ViewTransform.Scale * mapRect.Value.Width; + var height = ParentMap.ViewTransform.Scale * mapRect.Value.Height; + + uri = GetRequestUri(new Dictionary + { + { "SERVICE", "WMS" }, + { "VERSION", "1.3.0" }, + { "REQUEST", "GetMap" }, + { "LAYERS", WmsLayers ?? "" }, + { "STYLES", WmsStyles ?? "" }, + { "FORMAT", "image/png" }, + { "CRS", GetCrsValue() }, + { "BBOX", GetBboxValue(boundingBox, mapRect.Value) }, + { "WIDTH", Math.Round(width).ToString("F0") }, + { "HEIGHT", Math.Round(height).ToString("F0") } + }); } - var width = ParentMap.ViewTransform.Scale * mapRect.Value.Width; - var height = ParentMap.ViewTransform.Scale * mapRect.Value.Height; - - return GetRequestUri(new Dictionary - { - { "SERVICE", "WMS" }, - { "VERSION", "1.3.0" }, - { "REQUEST", "GetMap" }, - { "LAYERS", WmsLayers ?? "" }, - { "STYLES", WmsStyles ?? "" }, - { "FORMAT", "image/png" }, - { "CRS", GetCrsValue() }, - { "BBOX", GetBboxValue(mapRect.Value) }, - { "WIDTH", Math.Round(width).ToString("F0") }, - { "HEIGHT", Math.Round(height).ToString("F0") } - }); + return uri; } /// /// Returns a GetFeatureInfo request URL string. /// - protected virtual string GetFeatureInfoRequestUri(Point position, string format) + protected virtual string GetFeatureInfoRequestUri(BoundingBox boundingBox, Point position, string format) { - var viewport = new Rect(0d, 0d, ParentMap.ActualWidth, ParentMap.ActualHeight); - var boundingBox = ParentMap.ViewRectToBoundingBox(viewport); + string uri = null; var mapRect = ParentMap.MapProjection.BoundingBoxToMap(boundingBox); - if (!mapRect.HasValue) + if (mapRect.HasValue) { - return null; + var width = ParentMap.ViewTransform.Scale * mapRect.Value.Width; + var height = ParentMap.ViewTransform.Scale * mapRect.Value.Height; + + var transform = ViewTransform.CreateTransformMatrix( + -ParentMap.ActualWidth / 2d, -ParentMap.ActualWidth / 2d, + -ParentMap.ViewTransform.Rotation, + width / 2d, height / 2d); + + var imagePos = transform.Transform(position); + + var queryParameters = new Dictionary + { + { "SERVICE", "WMS" }, + { "VERSION", "1.3.0" }, + { "REQUEST", "GetFeatureInfo" }, + { "LAYERS", WmsLayers ?? "" }, + { "STYLES", WmsStyles ?? "" }, + { "FORMAT", "image/png" }, + { "INFO_FORMAT", format }, + { "CRS", GetCrsValue() }, + { "BBOX", GetBboxValue(boundingBox, mapRect.Value) }, + { "WIDTH", Math.Round(width).ToString("F0") }, + { "HEIGHT", Math.Round(height).ToString("F0") }, + { "I", Math.Round(imagePos.X).ToString("F0") }, + { "J", Math.Round(imagePos.Y).ToString("F0") } + }; + + // GetRequestUri may modify queryParameters["LAYERS"] + // + uri = GetRequestUri(queryParameters) + "&QUERY_LAYERS=" + queryParameters["LAYERS"]; } - var width = ParentMap.ViewTransform.Scale * mapRect.Value.Width; - var height = ParentMap.ViewTransform.Scale * mapRect.Value.Height; - - var transform = ViewTransform.CreateTransformMatrix( - -viewport.Width / 2d, -viewport.Height / 2d, - -ParentMap.ViewTransform.Rotation, - width / 2d, height / 2d); - - var imagePos = transform.Transform(position); - - var queryParameters = new Dictionary - { - { "SERVICE", "WMS" }, - { "VERSION", "1.3.0" }, - { "REQUEST", "GetFeatureInfo" }, - { "LAYERS", WmsLayers ?? "" }, - { "STYLES", WmsStyles ?? "" }, - { "FORMAT", "image/png" }, - { "INFO_FORMAT", format }, - { "CRS", GetCrsValue() }, - { "BBOX", GetBboxValue(mapRect.Value) }, - { "WIDTH", Math.Round(width).ToString("F0") }, - { "HEIGHT", Math.Round(height).ToString("F0") }, - { "I", Math.Round(imagePos.X).ToString("F0") }, - { "J", Math.Round(imagePos.Y).ToString("F0") } - }; - - return GetRequestUri(queryParameters) + "&QUERY_LAYERS=" + queryParameters["LAYERS"]; + return uri; } protected virtual string GetCrsValue() @@ -311,22 +315,27 @@ namespace MapControl return crsId; } - protected virtual string GetBboxValue(Rect rect) + protected virtual string GetBboxValue(BoundingBox boundingBox, Rect mapRect) { 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; + string format; + double x1, y1, x2, y2; 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; + x1 = boundingBox.West; + y1 = boundingBox.South; + x2 = boundingBox.East; + y2 = boundingBox.North; + } + else + { + format = "{0:F2},{1:F2},{2:F2},{3:F2}"; + x1 = mapRect.X; + y1 = mapRect.Y; + x2 = mapRect.X + mapRect.Width; + y2 = mapRect.Y + mapRect.Height; } return string.Format(CultureInfo.InvariantCulture, format, x1, y1, x2, y2); diff --git a/MapControl/WPF/MapItemsImageLayer.WPF.cs b/MapControl/WPF/MapItemsImageLayer.WPF.cs index 8b16bef8..3cb887be 100644 --- a/MapControl/WPF/MapItemsImageLayer.WPF.cs +++ b/MapControl/WPF/MapItemsImageLayer.WPF.cs @@ -48,7 +48,7 @@ namespace MapControl return image; } - private DrawingImage GetImage(MapProjection projection, Rect rect, IEnumerable items) + private DrawingImage GetImage(MapProjection projection, Rect mapRect, IEnumerable items) { var scale = ParentMap.ViewTransform.Scale; var rotation = ParentMap.ViewTransform.Rotation; @@ -62,13 +62,13 @@ namespace MapControl .Select(point => point.Value) .ToList(); - if (points.Any(point => rect.Contains(point))) + if (points.Any(point => mapRect.Contains(point))) { for (int i = 0; i < points.Count; i++) { points[i] = new Point( - scale * (points[i].X - rect.X), - scale * ((rect.Y + rect.Height) - points[i].Y)); + scale * (points[i].X - mapRect.X), + scale * ((mapRect.Y + mapRect.Height) - points[i].Y)); } drawings.Children.Add(item.GetDrawing(points, scale, rotation)); @@ -79,7 +79,7 @@ namespace MapControl { Drawing = drawings, ViewboxUnits = BrushMappingMode.Absolute, - Viewbox = new Rect(0, 0, scale * rect.Width, scale * rect.Height), + Viewbox = new Rect(0, 0, scale * mapRect.Width, scale * mapRect.Height), }; var drawing = new GeometryDrawing(