From 6c2b27831ef5455406b4d2c3e7a68e252845a6e6 Mon Sep 17 00:00:00 2001 From: ClemensF Date: Fri, 20 Mar 2020 20:40:13 +0100 Subject: [PATCH] Version 4.17.0: Added support for WMTS --- MapControl/Shared/WmtsTileLayer.cs | 49 ++++++++++++++++++++--------- MapControl/Shared/WmtsTileSource.cs | 6 ++-- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/MapControl/Shared/WmtsTileLayer.cs b/MapControl/Shared/WmtsTileLayer.cs index d76fb6e4..3bd1039a 100644 --- a/MapControl/Shared/WmtsTileLayer.cs +++ b/MapControl/Shared/WmtsTileLayer.cs @@ -19,8 +19,8 @@ namespace MapControl { public class WmtsTileLayer : MapTileLayerBase { - public static readonly DependencyProperty CapabilitiesProperty = DependencyProperty.Register( - nameof(Capabilities), typeof(Uri), typeof(WmtsTileLayer), new PropertyMetadata(null)); + public static readonly DependencyProperty CapabilitiesUriProperty = DependencyProperty.Register( + nameof(CapabilitiesUri), typeof(Uri), typeof(WmtsTileLayer), new PropertyMetadata(null)); public static readonly DependencyProperty LayerIdentifierProperty = DependencyProperty.Register( nameof(LayerIdentifier), typeof(string), typeof(WmtsTileLayer), new PropertyMetadata(null)); @@ -40,10 +40,10 @@ namespace MapControl Loaded += OnLoaded; } - public Uri Capabilities + public Uri CapabilitiesUri { - get { return (Uri)GetValue(CapabilitiesProperty); } - set { SetValue(CapabilitiesProperty, value); } + get { return (Uri)GetValue(CapabilitiesUriProperty); } + set { SetValue(CapabilitiesUriProperty, value); } } public string LayerIdentifier @@ -169,25 +169,25 @@ namespace MapControl private async void OnLoaded(object sender, RoutedEventArgs e) { - if (Capabilities != null) + if (CapabilitiesUri != null) { try { - if (Capabilities.IsAbsoluteUri && (Capabilities.Scheme == "http" || Capabilities.Scheme == "https")) + if (CapabilitiesUri.IsAbsoluteUri && (CapabilitiesUri.Scheme == "http" || CapabilitiesUri.Scheme == "https")) { - using (var stream = await ImageLoader.HttpClient.GetStreamAsync(Capabilities)) + using (var stream = await ImageLoader.HttpClient.GetStreamAsync(CapabilitiesUri)) { ReadCapabilities(XDocument.Load(stream).Root); } } else { - ReadCapabilities(XDocument.Load(Capabilities.ToString()).Root); + ReadCapabilities(XDocument.Load(CapabilitiesUri.ToString()).Root); } } catch (Exception ex) { - Debug.WriteLine("WmtsTileLayer: {0}: {1}", Capabilities, ex.Message); + Debug.WriteLine("WmtsTileLayer: {0}: {1}", CapabilitiesUri, ex.Message); } } } @@ -229,14 +229,29 @@ namespace MapControl var tileMatrixSetId = layerElement.Element(ns + "TileMatrixSetLink")?.Element(ns + "TileMatrixSet")?.Value; - if (tileMatrixSetId == null) + if (string.IsNullOrEmpty(tileMatrixSetId)) { throw new ArgumentException("TileMatrixSetLink element not found."); } + var styleElement = layerElement.Descendants(ns + "Style") + .FirstOrDefault(e => e.Attribute("isDefault")?.Value == "true"); + + if (styleElement == null) + { + styleElement = layerElement.Descendants(ns + "Style").FirstOrDefault(); + } + + var style = styleElement?.Element(ows + "Identifier")?.Value; + + if (string.IsNullOrEmpty(style)) + { + throw new ArgumentException("Style element not found."); + } + var urlTemplate = layerElement.Element(ns + "ResourceURL")?.Attribute("template")?.Value; - if (urlTemplate == null) + if (string.IsNullOrEmpty(urlTemplate)) { throw new ArgumentException("ResourceURL element (or template attribute) not found in Layer \"" + LayerIdentifier + "\"."); } @@ -251,7 +266,7 @@ namespace MapControl var supportedCrs = tileMatrixSetElement.Element(ows + "SupportedCRS")?.Value; - if (supportedCrs == null) + if (string.IsNullOrEmpty(supportedCrs)) { throw new ArgumentException("ows:SupportedCRS element not found in TileMatrixSet \"" + tileMatrixSetId + "\"."); } @@ -328,8 +343,12 @@ namespace MapControl var tileMatrixSet = new WmtsTileMatrixSet(tileMatrixSetId, supportedCrs, tileMatrixes); - SourceName = tileMatrixSet.Identifier; - TileSource = new WmtsTileSource(urlTemplate, tileMatrixSet); + urlTemplate = urlTemplate + .Replace("{Style}", style) + .Replace("{TileMatrixSet}", tileMatrixSet.Identifier); + + TileSource = new WmtsTileSource(urlTemplate, tileMatrixSet.TileMatrixes); + TileMatrixSet = tileMatrixSet; // calls UpdateTileLayer() } } diff --git a/MapControl/Shared/WmtsTileSource.cs b/MapControl/Shared/WmtsTileSource.cs index d71993fb..21606ff6 100644 --- a/MapControl/Shared/WmtsTileSource.cs +++ b/MapControl/Shared/WmtsTileSource.cs @@ -11,10 +11,10 @@ namespace MapControl { private readonly IList tileMatrixes; - public WmtsTileSource(string uriFormat, WmtsTileMatrixSet tileMatrixSet) - : base(uriFormat.Replace("{TileMatrixSet}", tileMatrixSet.Identifier)) + public WmtsTileSource(string uriFormat, IList tileMatrixes) + : base(uriFormat) { - tileMatrixes = tileMatrixSet.TileMatrixes; + this.tileMatrixes = tileMatrixes; } public override Uri GetUri(int x, int y, int zoomLevel)