From 916dc7d4af966b25a4666fa9fdabd2ceb7dc3a6f Mon Sep 17 00:00:00 2001 From: Clemens Date: Tue, 23 Aug 2022 17:20:16 +0200 Subject: [PATCH] Renamed TileSource.UriFormat, improved WmtsCapabilities --- MapControl/Shared/BingMapsTileLayer.cs | 2 +- MapControl/Shared/BingMapsTileSource.cs | 5 +- MapControl/Shared/BoundingBoxTileSource.cs | 8 +- MapControl/Shared/MapTileLayer.cs | 2 +- MapControl/Shared/TileImageLoader.cs | 2 +- MapControl/Shared/TileSource.cs | 16 +-- MapControl/Shared/TypeConverters.cs | 2 +- MapControl/Shared/WmsImageLayer.cs | 2 - MapControl/Shared/WmtsCapabilities.cs | 123 +++++++++++---------- MapControl/Shared/WmtsTileSource.cs | 4 +- SampleApps/UniversalApp/MainPage.xaml | 10 +- SampleApps/WinUiApp/MainWindow.xaml | 18 +-- SampleApps/WpfApplication/MapLayers.xaml | 2 +- 13 files changed, 98 insertions(+), 98 deletions(-) diff --git a/MapControl/Shared/BingMapsTileLayer.cs b/MapControl/Shared/BingMapsTileLayer.cs index 8fbd67c0..066e7dbb 100644 --- a/MapControl/Shared/BingMapsTileLayer.cs +++ b/MapControl/Shared/BingMapsTileLayer.cs @@ -106,7 +106,7 @@ namespace MapControl TileSource = new BingMapsTileSource { - UriFormat = imageUrl.Replace("{culture}", Culture), + UriTemplate = imageUrl.Replace("{culture}", Culture), Subdomains = subdomains }; } diff --git a/MapControl/Shared/BingMapsTileSource.cs b/MapControl/Shared/BingMapsTileSource.cs index 500b3827..02e90490 100644 --- a/MapControl/Shared/BingMapsTileSource.cs +++ b/MapControl/Shared/BingMapsTileSource.cs @@ -12,8 +12,7 @@ namespace MapControl { Uri uri = null; - if (UriFormat != null && Subdomains != null && Subdomains.Length > 0 && - x >= 0 && y >= 0 && zoomLevel > 0) + if (UriTemplate != null && Subdomains != null && Subdomains.Length > 0 && zoomLevel > 0) { var subdomain = Subdomains[(x + y) % Subdomains.Length]; var quadkey = new char[zoomLevel]; @@ -23,7 +22,7 @@ namespace MapControl quadkey[z] = (char)('0' + 2 * (y % 2) + (x % 2)); } - uri = new Uri(UriFormat + uri = new Uri(UriTemplate .Replace("{subdomain}", subdomain) .Replace("{quadkey}", new string(quadkey))); } diff --git a/MapControl/Shared/BoundingBoxTileSource.cs b/MapControl/Shared/BoundingBoxTileSource.cs index 672ca01f..f1287af5 100644 --- a/MapControl/Shared/BoundingBoxTileSource.cs +++ b/MapControl/Shared/BoundingBoxTileSource.cs @@ -13,7 +13,7 @@ namespace MapControl { Uri uri = null; - if (UriFormat != null) + if (UriTemplate != null) { var tileSize = 360d / (1 << zoomLevel); // tile width in degrees var west = MapProjection.Wgs84MeterPerDegree * (x * tileSize - 180d); @@ -21,14 +21,14 @@ namespace MapControl var south = MapProjection.Wgs84MeterPerDegree * (180d - (y + 1) * tileSize); var north = MapProjection.Wgs84MeterPerDegree * (180d - y * tileSize); - if (UriFormat.Contains("{bbox}")) + if (UriTemplate.Contains("{bbox}")) { - uri = new Uri(UriFormat.Replace("{bbox}", + uri = new Uri(UriTemplate.Replace("{bbox}", string.Format(CultureInfo.InvariantCulture, "{0:F2},{1:F2},{2:F2},{3:F2}", west, south, east, north))); } else { - uri = new Uri(UriFormat + uri = new Uri(UriTemplate .Replace("{west}", west.ToString("F2", CultureInfo.InvariantCulture)) .Replace("{south}", south.ToString("F2", CultureInfo.InvariantCulture)) .Replace("{east}", east.ToString("F2", CultureInfo.InvariantCulture)) diff --git a/MapControl/Shared/MapTileLayer.cs b/MapControl/Shared/MapTileLayer.cs index 7a3cdcc7..09f64d01 100644 --- a/MapControl/Shared/MapTileLayer.cs +++ b/MapControl/Shared/MapTileLayer.cs @@ -36,7 +36,7 @@ namespace MapControl /// public static MapTileLayer OpenStreetMapTileLayer => new MapTileLayer { - TileSource = new TileSource { UriFormat = "https://tile.openstreetmap.org/{z}/{x}/{y}.png" }, + TileSource = new TileSource { UriTemplate = "https://tile.openstreetmap.org/{z}/{x}/{y}.png" }, SourceName = "OpenStreetMap", Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)" }; diff --git a/MapControl/Shared/TileImageLoader.cs b/MapControl/Shared/TileImageLoader.cs index 60a33f02..d22a361d 100644 --- a/MapControl/Shared/TileImageLoader.cs +++ b/MapControl/Shared/TileImageLoader.cs @@ -105,7 +105,7 @@ namespace MapControl Progress.Report(0d); } - if (Cache == null || tileSource.UriFormat == null || !tileSource.UriFormat.StartsWith("http")) + if (Cache == null || tileSource.UriTemplate == null || !tileSource.UriTemplate.StartsWith("http")) { cacheName = null; // no tile caching } diff --git a/MapControl/Shared/TileSource.cs b/MapControl/Shared/TileSource.cs index 9c7f86a6..8608dc56 100644 --- a/MapControl/Shared/TileSource.cs +++ b/MapControl/Shared/TileSource.cs @@ -22,19 +22,19 @@ namespace MapControl #endif public class TileSource { - private string uriFormat; + private string uriTemplate; /// - /// Gets or sets the format string to produce tile request Uris. + /// Gets or sets the template string for tile request Uris. /// - public string UriFormat + public string UriTemplate { - get => uriFormat; + get => uriTemplate; set { - uriFormat = value; + uriTemplate = value; - if (Subdomains == null && uriFormat != null && uriFormat.Contains("{s}")) + if (Subdomains == null && uriTemplate != null && uriTemplate.Contains("{s}")) { Subdomains = new string[] { "a", "b", "c" }; // default OpenStreetMap subdomains } @@ -53,9 +53,9 @@ namespace MapControl { Uri uri = null; - if (UriFormat != null && x >= 0 && y >= 0 && zoomLevel >= 0) + if (UriTemplate != null && x >= 0 && y >= 0 && zoomLevel >= 0) { - var uriString = UriFormat + var uriString = UriTemplate .Replace("{x}", x.ToString()) .Replace("{y}", y.ToString()) .Replace("{z}", zoomLevel.ToString()); diff --git a/MapControl/Shared/TypeConverters.cs b/MapControl/Shared/TypeConverters.cs index c0a5137c..1a3818ce 100644 --- a/MapControl/Shared/TypeConverters.cs +++ b/MapControl/Shared/TypeConverters.cs @@ -56,7 +56,7 @@ namespace MapControl public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { - return new TileSource { UriFormat = (string)value }; + return new TileSource { UriTemplate = (string)value }; } } } diff --git a/MapControl/Shared/WmsImageLayer.cs b/MapControl/Shared/WmsImageLayer.cs index b32aa70b..ce69c944 100644 --- a/MapControl/Shared/WmsImageLayer.cs +++ b/MapControl/Shared/WmsImageLayer.cs @@ -328,8 +328,6 @@ namespace MapControl var uri = ServiceUri.GetLeftPart(UriPartial.Path) + "?" + string.Join("&", queryParameters.Select(kv => kv.Key + "=" + kv.Value)); - Debug.WriteLine(uri); - return uri.Replace(" ", "%20"); } } diff --git a/MapControl/Shared/WmtsCapabilities.cs b/MapControl/Shared/WmtsCapabilities.cs index d505c51a..9d0343ef 100644 --- a/MapControl/Shared/WmtsCapabilities.cs +++ b/MapControl/Shared/WmtsCapabilities.cs @@ -6,8 +6,8 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; -using System.Xml.Linq; using System.Threading.Tasks; +using System.Xml.Linq; #if !WINUI && !UWP using System.Windows; #endif @@ -19,6 +19,10 @@ namespace MapControl /// public class WmtsCapabilities { + private static readonly XNamespace ows = "http://www.opengis.net/ows/1.1"; + private static readonly XNamespace wmts = "http://www.opengis.net/wmts/1.0"; + private static readonly XNamespace xlink = "http://www.w3.org/1999/xlink"; + public string LayerIdentifier { get; private set; } public WmtsTileSource TileSource { get; private set; } public List TileMatrixSets { get; private set; } @@ -44,10 +48,7 @@ namespace MapControl public static WmtsCapabilities ReadCapabilities(XElement capabilitiesElement, string layerIdentifier, string capabilitiesUrl) { - XNamespace ns = capabilitiesElement.Name.Namespace; - XNamespace ows = "http://www.opengis.net/ows/1.1"; - - var contentsElement = capabilitiesElement.Element(ns + "Contents"); + var contentsElement = capabilitiesElement.Element(wmts + "Contents"); if (contentsElement == null) { @@ -58,7 +59,7 @@ namespace MapControl if (!string.IsNullOrEmpty(layerIdentifier)) { - layerElement = contentsElement.Descendants(ns + "Layer") + layerElement = contentsElement.Elements(wmts + "Layer") .FirstOrDefault(e => e.Element(ows + "Identifier")?.Value == layerIdentifier); if (layerElement == null) @@ -68,7 +69,7 @@ namespace MapControl } else { - layerElement = capabilitiesElement.Descendants(ns + "Layer").FirstOrDefault(); + layerElement = contentsElement.Elements(wmts + "Layer").FirstOrDefault(); if (layerElement == null) { @@ -78,34 +79,33 @@ namespace MapControl layerIdentifier = layerElement.Element(ows + "Identifier")?.Value ?? ""; } - var styleElement = layerElement.Descendants(ns + "Style") + var styleElement = layerElement.Elements(wmts + "Style") .FirstOrDefault(e => e.Attribute("isDefault")?.Value == "true"); if (styleElement == null) { - styleElement = layerElement.Descendants(ns + "Style").FirstOrDefault(); + styleElement = layerElement.Elements(wmts + "Style").FirstOrDefault(); } - var style = styleElement?.Element(ows + "Identifier")?.Value; + var styleIdentifier = styleElement?.Element(ows + "Identifier")?.Value; - if (string.IsNullOrEmpty(style)) + if (string.IsNullOrEmpty(styleIdentifier)) { - throw new ArgumentException($"No valid Style element found in Layer \"{layerIdentifier}\"."); + throw new ArgumentException($"No Style element found in Layer \"{layerIdentifier}\"."); } - var urlTemplate = ReadUrlTemplate(layerElement, layerIdentifier, style, capabilitiesUrl); + var urlTemplate = ReadUrlTemplate(capabilitiesElement, layerElement, layerIdentifier, styleIdentifier, capabilitiesUrl); var tileMatrixSetIds = layerElement - .Descendants(ns + "TileMatrixSetLink") - .Select(e => e.Element(ns + "TileMatrixSet")?.Value) - .Where(id => !string.IsNullOrEmpty(id)) - .ToList(); + .Elements(wmts + "TileMatrixSetLink") + .Select(e => e.Element(wmts + "TileMatrixSet")?.Value) + .Where(id => !string.IsNullOrEmpty(id)); var tileMatrixSets = new List(); foreach (var tileMatrixSetId in tileMatrixSetIds) { - var tileMatrixSetElement = capabilitiesElement.Descendants(ns + "TileMatrixSet") + var tileMatrixSetElement = contentsElement.Elements(wmts + "TileMatrixSet") .FirstOrDefault(e => e.Element(ows + "Identifier")?.Value == tileMatrixSetId); if (tileMatrixSetElement == null) @@ -119,19 +119,18 @@ namespace MapControl return new WmtsCapabilities { LayerIdentifier = layerIdentifier, - TileSource = new WmtsTileSource { UriFormat = urlTemplate }, + TileSource = new WmtsTileSource { UriTemplate = urlTemplate }, TileMatrixSets = tileMatrixSets }; } - public static string ReadUrlTemplate(XElement layerElement, string layerIdentifier, string style, string capabilitiesUrl) + public static string ReadUrlTemplate(XElement capabilitiesElement, XElement layerElement, string layerIdentifier, string styleIdentifier, string capabilitiesUrl) { - XNamespace ns = layerElement.Name.Namespace; const string formatPng = "image/png"; const string formatJpg = "image/jpeg"; string urlTemplate = null; - var resourceUrls = layerElement.Descendants(ns + "ResourceURL") + var resourceUrls = layerElement.Elements(wmts + "ResourceURL") .ToLookup(e => e.Attribute("format")?.Value ?? "", e => e.Attribute("template")?.Value ?? ""); if (resourceUrls.Any()) @@ -140,39 +139,47 @@ namespace MapControl : resourceUrls.Contains(formatJpg) ? resourceUrls[formatJpg] : resourceUrls.First(); - urlTemplate = urlTemplates.First().Replace("{Style}", style); + urlTemplate = urlTemplates.First().Replace("{Style}", styleIdentifier); } - else if (capabilitiesUrl != null && - capabilitiesUrl.IndexOf("Request=GetCapabilities", StringComparison.OrdinalIgnoreCase) >= 0) + else { - // no ResourceURL and GetCapabilities KVP request, use GetTile KVP request encoding + var requestUrl = capabilitiesElement + .Elements(ows + "OperationsMetadata") + .Elements(ows + "Operation").Where(e => e.Attribute("name")?.Value == "GetTile") + .Elements(ows + "DCP") + .Elements(ows + "HTTP") + .Elements(ows + "Get").Select(e => e.Attribute(xlink + "href")?.Value) + .FirstOrDefault(); - var formats = layerElement.Descendants(ns + "Format").Select(e => e.Value).ToList(); - var format = formatPng; - - if (formats.Count > 0) + if (requestUrl == null && + capabilitiesUrl != null && + capabilitiesUrl.IndexOf("Request=GetCapabilities", StringComparison.OrdinalIgnoreCase) >= 0) { - format = formats.Contains(formatPng) ? formatPng - : formats.Contains(formatJpg) ? formatJpg - : formats[0]; + requestUrl = capabilitiesUrl; } - urlTemplate = capabilitiesUrl.Split('?')[0] - + "?Service=WMTS" - + "&Request=GetTile" - + "&Version=1.0.0" - + "&Layer=" + layerIdentifier - + "&Style=" + style - + "&Format=" + format - + "&TileMatrixSet={TileMatrixSet}" - + "&TileMatrix={TileMatrix}" - + "&TileRow={TileRow}" - + "&TileCol={TileCol}"; + if (requestUrl != null) + { + var formats = layerElement.Elements(wmts + "Format").Select(e => e.Value); + var format = formats.Contains(formatPng) ? formatPng + : formats.Contains(formatJpg) ? formatJpg + : formats.FirstOrDefault(); + + if (string.IsNullOrEmpty(format)) + { + format = formatPng; + } + + urlTemplate = requestUrl.Split('?')[0] + + "?Service=WMTS&Request=GetTile&Version=1.0.0" + + "&Layer=" + layerIdentifier + "&Style=" + styleIdentifier + "&Format=" + format + + "&TileMatrixSet={TileMatrixSet}&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}"; + } } if (string.IsNullOrEmpty(urlTemplate)) { - throw new ArgumentException($"No valid ResourceURL element found in Layer \"{layerIdentifier}\"."); + throw new ArgumentException($"No ResourceURL element in Layer \"{layerIdentifier}\" and no GetTile KVP Operation Metadata found."); } return urlTemplate; @@ -180,21 +187,18 @@ namespace MapControl public static WmtsTileMatrixSet ReadTileMatrixSet(XElement tileMatrixSetElement) { - XNamespace ns = tileMatrixSetElement.Name.Namespace; - XNamespace ows = "http://www.opengis.net/ows/1.1"; - var identifier = tileMatrixSetElement.Element(ows + "Identifier")?.Value; if (string.IsNullOrEmpty(identifier)) { - throw new ArgumentException("No ows:Identifier element found in TileMatrixSet."); + throw new ArgumentException("No Identifier element found in TileMatrixSet."); } var supportedCrs = tileMatrixSetElement.Element(ows + "SupportedCRS")?.Value; if (string.IsNullOrEmpty(supportedCrs)) { - throw new ArgumentException($"No ows:SupportedCRS element found in TileMatrixSet \"{identifier}\"."); + throw new ArgumentException($"No SupportedCRS element found in TileMatrixSet \"{identifier}\"."); } const string urnPrefix = "urn:ogc:def:crs:EPSG:"; @@ -211,7 +215,7 @@ namespace MapControl var tileMatrixes = new List(); - foreach (var tileMatrixElement in tileMatrixSetElement.Descendants(ns + "TileMatrix")) + foreach (var tileMatrixElement in tileMatrixSetElement.Elements(wmts + "TileMatrix")) { tileMatrixes.Add(ReadTileMatrix(tileMatrixElement, supportedCrs)); } @@ -226,17 +230,14 @@ namespace MapControl public static WmtsTileMatrix ReadTileMatrix(XElement tileMatrixElement, string supportedCrs) { - XNamespace ns = tileMatrixElement.Name.Namespace; - XNamespace ows = "http://www.opengis.net/ows/1.1"; - var identifier = tileMatrixElement.Element(ows + "Identifier")?.Value; if (string.IsNullOrEmpty(identifier)) { - throw new ArgumentException("No ows:Identifier element found in TileMatrix."); + throw new ArgumentException("No Identifier element found in TileMatrix."); } - var valueString = tileMatrixElement.Element(ns + "ScaleDenominator")?.Value; + var valueString = tileMatrixElement.Element(wmts + "ScaleDenominator")?.Value; if (string.IsNullOrEmpty(valueString) || !double.TryParse(valueString, NumberStyles.Float, CultureInfo.InvariantCulture, out double scaleDenominator)) @@ -244,7 +245,7 @@ namespace MapControl throw new ArgumentException($"No ScaleDenominator element found in TileMatrix \"{identifier}\"."); } - valueString = tileMatrixElement.Element(ns + "TopLeftCorner")?.Value; + valueString = tileMatrixElement.Element(wmts + "TopLeftCorner")?.Value; string[] topLeftCornerStrings; if (string.IsNullOrEmpty(valueString) || @@ -255,28 +256,28 @@ namespace MapControl throw new ArgumentException($"No TopLeftCorner element found in TileMatrix \"{identifier}\"."); } - valueString = tileMatrixElement.Element(ns + "TileWidth")?.Value; + valueString = tileMatrixElement.Element(wmts + "TileWidth")?.Value; if (string.IsNullOrEmpty(valueString) || !int.TryParse(valueString, out int tileWidth)) { throw new ArgumentException($"No TileWidth element found in TileMatrix \"{identifier}\"."); } - valueString = tileMatrixElement.Element(ns + "TileHeight")?.Value; + valueString = tileMatrixElement.Element(wmts + "TileHeight")?.Value; if (string.IsNullOrEmpty(valueString) || !int.TryParse(valueString, out int tileHeight)) { throw new ArgumentException($"No TileHeight element found in TileMatrix \"{identifier}\"."); } - valueString = tileMatrixElement.Element(ns + "MatrixWidth")?.Value; + valueString = tileMatrixElement.Element(wmts + "MatrixWidth")?.Value; if (string.IsNullOrEmpty(valueString) || !int.TryParse(valueString, out int matrixWidth)) { throw new ArgumentException($"No MatrixWidth element found in TileMatrix \"{identifier}\"."); } - valueString = tileMatrixElement.Element(ns + "MatrixHeight")?.Value; + valueString = tileMatrixElement.Element(wmts + "MatrixHeight")?.Value; if (string.IsNullOrEmpty(valueString) || !int.TryParse(valueString, out int matrixHeight)) { diff --git a/MapControl/Shared/WmtsTileSource.cs b/MapControl/Shared/WmtsTileSource.cs index e0069103..a5a3328e 100644 --- a/MapControl/Shared/WmtsTileSource.cs +++ b/MapControl/Shared/WmtsTileSource.cs @@ -14,11 +14,11 @@ namespace MapControl { Uri uri = null; - if (UriFormat != null && + if (UriTemplate != null && TileMatrixSet != null && TileMatrixSet.TileMatrixes.Count > zoomLevel && x >= 0 && y >= 0 && zoomLevel >= 0) { - uri = new Uri(UriFormat + uri = new Uri(UriTemplate .Replace("{TileMatrixSet}", TileMatrixSet.Identifier) .Replace("{TileMatrix}", TileMatrixSet.TileMatrixes[zoomLevel].Identifier) .Replace("{TileCol}", x.ToString()) diff --git a/SampleApps/UniversalApp/MainPage.xaml b/SampleApps/UniversalApp/MainPage.xaml index 9ffba1fc..f3cb3e11 100644 --- a/SampleApps/UniversalApp/MainPage.xaml +++ b/SampleApps/UniversalApp/MainPage.xaml @@ -148,7 +148,7 @@ SourceName="OpenStreetMap" Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"> - + @@ -157,7 +157,7 @@ SourceName="OpenStreetMap German" Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"> - + @@ -166,7 +166,7 @@ SourceName="OpenStreetMap French" Description="© [OpenStreetMap France](https://www.openstreetmap.fr/mentions-legales/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"> - + @@ -175,7 +175,7 @@ SourceName="OpenTopoMap" Description="© [OpenTopoMap](https://opentopomap.org/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"> - + @@ -206,7 +206,7 @@ - + diff --git a/SampleApps/WinUiApp/MainWindow.xaml b/SampleApps/WinUiApp/MainWindow.xaml index 0e782036..c55ef756 100644 --- a/SampleApps/WinUiApp/MainWindow.xaml +++ b/SampleApps/WinUiApp/MainWindow.xaml @@ -143,7 +143,7 @@ - + @@ -155,7 +155,7 @@ SourceName="OpenStreetMap" Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"> - + @@ -164,7 +164,7 @@ SourceName="OpenStreetMap German" Description="© [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"> - + @@ -173,7 +173,7 @@ SourceName="OpenStreetMap French" Description="© [OpenStreetMap France](https://www.openstreetmap.fr/mentions-legales/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"> - + @@ -182,7 +182,7 @@ SourceName="OpenTopoMap" Description="© [OpenTopoMap](https://opentopomap.org/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"> - + @@ -190,12 +190,14 @@ + CapabilitiesUri="https://sgx.geodatenzentrum.de/wmts_topplus_open/1.0.0/WMTSCapabilities.xml" + LayerIdentifier="web_light_grau"/> + ServiceUri="https://sgx.geodatenzentrum.de/wms_topplus_open" + Layers="web_grau"/> - + diff --git a/SampleApps/WpfApplication/MapLayers.xaml b/SampleApps/WpfApplication/MapLayers.xaml index 9332eb11..c916bfda 100644 --- a/SampleApps/WpfApplication/MapLayers.xaml +++ b/SampleApps/WpfApplication/MapLayers.xaml @@ -24,7 +24,7 @@ Description="© [OpenTopoMap](https://opentopomap.org/) © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)"/>