From de6a586baed11484358e73fbbefb9334d180e9c8 Mon Sep 17 00:00:00 2001 From: ClemensF Date: Mon, 11 Jun 2018 21:37:36 +0200 Subject: [PATCH] Reworked WmsImageLayer --- MapControl/Shared/AzimuthalProjection.cs | 7 +- MapControl/Shared/MapProjection.cs | 20 +--- MapControl/Shared/WmsImageLayer.cs | 111 ++++++++--------------- SampleApps/Shared/MapLayers.cs | 9 +- 4 files changed, 52 insertions(+), 95 deletions(-) diff --git a/MapControl/Shared/AzimuthalProjection.cs b/MapControl/Shared/AzimuthalProjection.cs index e0a039d0..f29a3da1 100644 --- a/MapControl/Shared/AzimuthalProjection.cs +++ b/MapControl/Shared/AzimuthalProjection.cs @@ -60,7 +60,7 @@ namespace MapControl base.SetViewportTransform(projectionCenter, mapCenter, viewportCenter, zoomLevel, heading); } - public override string WmsQueryParameters(BoundingBox boundingBox, bool useSrs) + public override string WmsQueryParameters(BoundingBox boundingBox) { if (string.IsNullOrEmpty(CrsId)) { @@ -72,9 +72,8 @@ namespace MapControl var height = (int)Math.Round(ViewportScale * rect.Height); return string.Format(CultureInfo.InvariantCulture, - "{0}={1},1,{2},{3}&BBOX={4},{5},{6},{7}&WIDTH={8}&HEIGHT={9}", - (useSrs ? "SRS" : "CRS"), CrsId, - ProjectionCenter.Longitude, ProjectionCenter.Latitude, + "CRS={0},1,{1},{2}&BBOX={3},{4},{5},{6}&WIDTH={7}&HEIGHT={8}", + CrsId, ProjectionCenter.Longitude, ProjectionCenter.Latitude, rect.X, rect.Y, (rect.X + rect.Width), (rect.Y + rect.Height), width, height); } diff --git a/MapControl/Shared/MapProjection.cs b/MapControl/Shared/MapProjection.cs index e6fb52b8..7322c82d 100644 --- a/MapControl/Shared/MapProjection.cs +++ b/MapControl/Shared/MapProjection.cs @@ -149,31 +149,19 @@ namespace MapControl /// /// Gets a WMS query parameter string from the specified bounding box, e.g. "CRS=...&BBOX=...&WIDTH=...&HEIGHT=..." /// - public virtual string WmsQueryParameters(BoundingBox boundingBox, bool useSrs = false) + public virtual string WmsQueryParameters(BoundingBox boundingBox) { if (string.IsNullOrEmpty(CrsId) || !boundingBox.HasValidBounds) { return null; } - string format; - - if (useSrs) - { - format = "SRS={0}&BBOX={1},{2},{3},{4}&WIDTH={5}&HEIGHT={6}"; - } - else if (CrsId == "EPSG:4326") - { - format = "CRS={0}&BBOX={2},{1},{4},{3}&WIDTH={5}&HEIGHT={6}"; - } - else - { - format = "CRS={0}&BBOX={1},{2},{3},{4}&WIDTH={5}&HEIGHT={6}"; - } - var rect = BoundingBoxToRect(boundingBox); var width = (int)Math.Round(ViewportScale * rect.Width); var height = (int)Math.Round(ViewportScale * rect.Height); + var format = CrsId == "EPSG:4326" + ? "CRS={0}&BBOX={2},{1},{4},{3}&WIDTH={5}&HEIGHT={6}" + : "CRS={0}&BBOX={1},{2},{3},{4}&WIDTH={5}&HEIGHT={6}"; return string.Format(CultureInfo.InvariantCulture, format, CrsId, rect.X, rect.Y, (rect.X + rect.Width), (rect.Y + rect.Height), width, height); diff --git a/MapControl/Shared/WmsImageLayer.cs b/MapControl/Shared/WmsImageLayer.cs index 6810044e..c6bab315 100644 --- a/MapControl/Shared/WmsImageLayer.cs +++ b/MapControl/Shared/WmsImageLayer.cs @@ -21,16 +21,10 @@ namespace MapControl { public partial class WmsImageLayer : MapImageLayer { - private const string DefaultVersion = "1.3.0"; - public static readonly DependencyProperty ServiceUriProperty = DependencyProperty.Register( nameof(ServiceUri), typeof(Uri), typeof(WmsImageLayer), new PropertyMetadata(null, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); - public static readonly DependencyProperty VersionProperty = DependencyProperty.Register( - nameof(Version), typeof(string), typeof(WmsImageLayer), - new PropertyMetadata(DefaultVersion, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); - public static readonly DependencyProperty LayersProperty = DependencyProperty.Register( nameof(Layers), typeof(string), typeof(WmsImageLayer), new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); @@ -43,20 +37,12 @@ namespace MapControl nameof(Format), typeof(string), typeof(WmsImageLayer), new PropertyMetadata("image/png", async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); - private string layers = string.Empty; - public Uri ServiceUri { get { return (Uri)GetValue(ServiceUriProperty); } set { SetValue(ServiceUriProperty, value); } } - public string Version - { - get { return (string)GetValue(VersionProperty); } - set { SetValue(VersionProperty, value); } - } - public string Layers { get { return (string)GetValue(LayersProperty); } @@ -75,57 +61,13 @@ namespace MapControl set { SetValue(FormatProperty, value); } } - protected override async Task GetImageAsync(BoundingBox boundingBox) - { - ImageSource imageSource = null; - - if (ServiceUri != null) - { - var version = Version; - var uri = GetRequestUri(ref version) + "REQUEST=GetMap&"; - var projectionParameters = ParentMap.MapProjection.WmsQueryParameters(boundingBox, version.StartsWith("1.1.")); - - if (!string.IsNullOrEmpty(projectionParameters)) - { - if (uri.IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) < 0) - { - uri += "LAYERS=" + (Layers ?? "") + "&"; - } - - if (uri.IndexOf("STYLES=", StringComparison.OrdinalIgnoreCase) < 0) - { - uri += "STYLES=" + (Styles ?? "") + "&"; - } - - if (uri.IndexOf("FORMAT=", StringComparison.OrdinalIgnoreCase) < 0) - { - uri += "FORMAT=" + (Format ?? "") + "&"; - } - - uri += projectionParameters; - - try - { - imageSource = await ImageLoader.LoadImageAsync(new Uri(uri.Replace(" ", "%20")), false); - } - catch (Exception ex) - { - Debug.WriteLine("WmsImageLayer: {0}: {1}", uri, ex.Message); - } - } - } - - return imageSource; - } - public async Task> GetLayerNamesAsync() { IList layerNames = null; if (ServiceUri != null) { - var version = Version; - var uri = GetRequestUri(ref version) + "REQUEST=GetCapabilities"; + var uri = GetRequestUri("GetCapabilities"); try { @@ -158,13 +100,45 @@ namespace MapControl return layerNames; } - private string GetRequestUri(ref string version) + protected override async Task GetImageAsync(BoundingBox boundingBox) { - if (version == null) + ImageSource imageSource = null; + var projectionParameters = ParentMap.MapProjection.WmsQueryParameters(boundingBox); + + if (ServiceUri != null && !string.IsNullOrEmpty(projectionParameters)) { - version = DefaultVersion; + var uri = GetRequestUri("GetMap&" + projectionParameters); + + if (uri.IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) < 0 && Layers != null) + { + uri += "&LAYERS=" + Layers; + } + + if (uri.IndexOf("STYLES=", StringComparison.OrdinalIgnoreCase) < 0 && Styles != null) + { + uri += "&STYLES=" + Styles; + } + + if (uri.IndexOf("FORMAT=", StringComparison.OrdinalIgnoreCase) < 0 && Format != null) + { + uri += "&FORMAT=" + Format; + } + + try + { + imageSource = await ImageLoader.LoadImageAsync(new Uri(uri.Replace(" ", "%20")), false); + } + catch (Exception ex) + { + Debug.WriteLine("WmsImageLayer: {0}: {1}", uri, ex.Message); + } } + return imageSource; + } + + private string GetRequestUri(string request) + { var uri = ServiceUri.ToString(); if (!uri.EndsWith("?") && !uri.EndsWith("&")) @@ -177,19 +151,12 @@ namespace MapControl uri += "SERVICE=WMS&"; } - int versionStart = uri.IndexOf("VERSION=", StringComparison.OrdinalIgnoreCase); - int versionEnd; - - if (versionStart < 0) + if (uri.IndexOf("VERSION=", StringComparison.OrdinalIgnoreCase) < 0) { - uri += "VERSION=" + version + "&"; - } - else if ((versionEnd = uri.IndexOf("&", versionStart + 8)) >= versionStart + 8) - { - version = uri.Substring(versionStart, versionEnd - versionStart); + uri += "VERSION=1.3.0&"; } - return uri; + return uri + "REQUEST=" + request; } private static IEnumerable ChildElements(XmlElement element, string name) diff --git a/SampleApps/Shared/MapLayers.cs b/SampleApps/Shared/MapLayers.cs index 953a010c..870e4a63 100644 --- a/SampleApps/Shared/MapLayers.cs +++ b/SampleApps/Shared/MapLayers.cs @@ -99,7 +99,8 @@ namespace ViewModel new WmsImageLayer { Description = "© [terrestris GmbH & Co. KG](http://ows.terrestris.de/)\nData © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)", - ServiceUri = new Uri("http://ows.terrestris.de/osm/service?SERVICE=WMS&VERSION=1.3.0&LAYERS=OSM-WMS&STYLES=&FORMAT=image/png") + ServiceUri = new Uri("http://ows.terrestris.de/osm/service"), + Layers = "OSM-WMS" } }, { @@ -107,7 +108,8 @@ namespace ViewModel new WmsImageLayer { Description = "© [terrestris GmbH & Co. KG](http://ows.terrestris.de/)\nData © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)", - ServiceUri = new Uri("http://ows.terrestris.de/osm/service?SERVICE=WMS&VERSION=1.3.0&LAYERS=TOPO-OSM-WMS&STYLES=&FORMAT=image/png") + ServiceUri = new Uri("http://ows.terrestris.de/osm/service"), + Layers = "TOPO-OSM-WMS" } }, { @@ -115,7 +117,8 @@ namespace ViewModel new WmsImageLayer { Description = "© [SevenCs GmbH](http://www.sevencs.com)", - ServiceUri = new Uri("http://chartserver4.sevencs.com:8080?SERVICE=WMS&VERSION=1.3.0&LAYERS=ENC&STYLES=&FORMAT=image/png"), + ServiceUri = new Uri("http://chartserver4.sevencs.com:8080"), + Layers = "ENC", MaxBoundingBoxWidth = 360 } }