Improve TileMatrixSet selection in WmtsTileLayer

This commit is contained in:
ClemensFischer 2023-03-07 17:54:11 +01:00
parent 66e03e5334
commit fc2425ac41
2 changed files with 65 additions and 49 deletions

View file

@ -23,11 +23,11 @@ namespace MapControl
private static readonly XNamespace wmts = "http://www.opengis.net/wmts/1.0"; private static readonly XNamespace wmts = "http://www.opengis.net/wmts/1.0";
private static readonly XNamespace xlink = "http://www.w3.org/1999/xlink"; private static readonly XNamespace xlink = "http://www.w3.org/1999/xlink";
public string LayerIdentifier { get; private set; } public string Layer { get; private set; }
public WmtsTileSource TileSource { get; private set; } public WmtsTileSource TileSource { get; private set; }
public List<WmtsTileMatrixSet> TileMatrixSets { get; private set; } public List<WmtsTileMatrixSet> TileMatrixSets { get; private set; }
public static async Task<WmtsCapabilities> ReadCapabilitiesAsync(Uri capabilitiesUri, string layerIdentifier) public static async Task<WmtsCapabilities> ReadCapabilitiesAsync(Uri capabilitiesUri, string layer)
{ {
WmtsCapabilities capabilities; WmtsCapabilities capabilities;
@ -35,18 +35,18 @@ namespace MapControl
{ {
using (var stream = await ImageLoader.HttpClient.GetStreamAsync(capabilitiesUri)) using (var stream = await ImageLoader.HttpClient.GetStreamAsync(capabilitiesUri))
{ {
capabilities = ReadCapabilities(XDocument.Load(stream).Root, layerIdentifier, capabilitiesUri.ToString()); capabilities = ReadCapabilities(XDocument.Load(stream).Root, layer, capabilitiesUri.ToString());
} }
} }
else else
{ {
capabilities = ReadCapabilities(XDocument.Load(capabilitiesUri.ToString()).Root, layerIdentifier, null); capabilities = ReadCapabilities(XDocument.Load(capabilitiesUri.ToString()).Root, layer, null);
} }
return capabilities; return capabilities;
} }
public static WmtsCapabilities ReadCapabilities(XElement capabilitiesElement, string layerIdentifier, string capabilitiesUrl) public static WmtsCapabilities ReadCapabilities(XElement capabilitiesElement, string layer, string capabilitiesUrl)
{ {
var contentsElement = capabilitiesElement.Element(wmts + "Contents"); var contentsElement = capabilitiesElement.Element(wmts + "Contents");
@ -57,15 +57,15 @@ namespace MapControl
XElement layerElement; XElement layerElement;
if (!string.IsNullOrEmpty(layerIdentifier)) if (!string.IsNullOrEmpty(layer))
{ {
layerElement = contentsElement layerElement = contentsElement
.Elements(wmts + "Layer") .Elements(wmts + "Layer")
.FirstOrDefault(layer => layer.Element(ows + "Identifier")?.Value == layerIdentifier); .FirstOrDefault(l => l.Element(ows + "Identifier")?.Value == layer);
if (layerElement == null) if (layerElement == null)
{ {
throw new ArgumentException($"Layer element \"{layerIdentifier}\" not found."); throw new ArgumentException($"Layer element \"{layer}\" not found.");
} }
} }
else else
@ -79,12 +79,12 @@ namespace MapControl
throw new ArgumentException("No Layer element found."); throw new ArgumentException("No Layer element found.");
} }
layerIdentifier = layerElement.Element(ows + "Identifier")?.Value ?? ""; layer = layerElement.Element(ows + "Identifier")?.Value ?? "";
} }
var styleElement = layerElement var styleElement = layerElement
.Elements(wmts + "Style") .Elements(wmts + "Style")
.FirstOrDefault(style => style.Attribute("isDefault")?.Value == "true"); .FirstOrDefault(s => s.Attribute("isDefault")?.Value == "true");
if (styleElement == null) if (styleElement == null)
{ {
@ -93,19 +93,19 @@ namespace MapControl
.FirstOrDefault(); .FirstOrDefault();
} }
var styleIdentifier = styleElement?.Element(ows + "Identifier")?.Value; var style = styleElement?.Element(ows + "Identifier")?.Value;
if (string.IsNullOrEmpty(styleIdentifier)) if (string.IsNullOrEmpty(style))
{ {
throw new ArgumentException($"No Style element found in Layer \"{layerIdentifier}\"."); throw new ArgumentException($"No Style element found in Layer \"{layer}\".");
} }
var urlTemplate = ReadUrlTemplate(capabilitiesElement, layerElement, layerIdentifier, styleIdentifier, capabilitiesUrl); var urlTemplate = ReadUrlTemplate(capabilitiesElement, layerElement, layer, style, capabilitiesUrl);
var tileMatrixSetIds = layerElement var tileMatrixSetIds = layerElement
.Elements(wmts + "TileMatrixSetLink") .Elements(wmts + "TileMatrixSetLink")
.Select(tmsl => tmsl.Element(wmts + "TileMatrixSet")?.Value) .Select(l => l.Element(wmts + "TileMatrixSet")?.Value)
.Where(val => !string.IsNullOrEmpty(val)); .Where(v => !string.IsNullOrEmpty(v));
var tileMatrixSets = new List<WmtsTileMatrixSet>(); var tileMatrixSets = new List<WmtsTileMatrixSet>();
@ -113,11 +113,11 @@ namespace MapControl
{ {
var tileMatrixSetElement = contentsElement var tileMatrixSetElement = contentsElement
.Elements(wmts + "TileMatrixSet") .Elements(wmts + "TileMatrixSet")
.FirstOrDefault(tms => tms.Element(ows + "Identifier")?.Value == tileMatrixSetId); .FirstOrDefault(s => s.Element(ows + "Identifier")?.Value == tileMatrixSetId);
if (tileMatrixSetElement == null) if (tileMatrixSetElement == null)
{ {
throw new ArgumentException($"Linked TileMatrixSet element not found in Layer \"{layerIdentifier}\"."); throw new ArgumentException($"Linked TileMatrixSet element not found in Layer \"{layer}\".");
} }
tileMatrixSets.Add(ReadTileMatrixSet(tileMatrixSetElement)); tileMatrixSets.Add(ReadTileMatrixSet(tileMatrixSetElement));
@ -125,13 +125,13 @@ namespace MapControl
return new WmtsCapabilities return new WmtsCapabilities
{ {
LayerIdentifier = layerIdentifier, Layer = layer,
TileSource = new WmtsTileSource { UriTemplate = urlTemplate }, TileSource = new WmtsTileSource { UriTemplate = urlTemplate },
TileMatrixSets = tileMatrixSets TileMatrixSets = tileMatrixSets
}; };
} }
public static string ReadUrlTemplate(XElement capabilitiesElement, XElement layerElement, string layerIdentifier, string styleIdentifier, string capabilitiesUrl) public static string ReadUrlTemplate(XElement capabilitiesElement, XElement layerElement, string layer, string style, string capabilitiesUrl)
{ {
const string formatPng = "image/png"; const string formatPng = "image/png";
const string formatJpg = "image/jpeg"; const string formatJpg = "image/jpeg";
@ -139,11 +139,11 @@ namespace MapControl
var resourceUrls = layerElement var resourceUrls = layerElement
.Elements(wmts + "ResourceURL") .Elements(wmts + "ResourceURL")
.Where(res => res.Attribute("resourceType")?.Value == "tile" && .Where(r => r.Attribute("resourceType")?.Value == "tile" &&
res.Attribute("format")?.Value != null && r.Attribute("format")?.Value != null &&
res.Attribute("template")?.Value != null) r.Attribute("template")?.Value != null)
.ToLookup(res => res.Attribute("format").Value, .ToLookup(r => r.Attribute("format").Value,
res => res.Attribute("template").Value); r => r.Attribute("template").Value);
if (resourceUrls.Any()) if (resourceUrls.Any())
{ {
@ -151,23 +151,23 @@ namespace MapControl
: resourceUrls.Contains(formatJpg) ? resourceUrls[formatJpg] : resourceUrls.Contains(formatJpg) ? resourceUrls[formatJpg]
: resourceUrls.First(); : resourceUrls.First();
urlTemplate = urlTemplates.First().Replace("{Style}", styleIdentifier); urlTemplate = urlTemplates.First().Replace("{Style}", style);
} }
else else
{ {
urlTemplate = capabilitiesElement urlTemplate = capabilitiesElement
.Elements(ows + "OperationsMetadata") .Elements(ows + "OperationsMetadata")
.Elements(ows + "Operation") .Elements(ows + "Operation")
.Where(op => op.Attribute("name")?.Value == "GetTile") .Where(o => o.Attribute("name")?.Value == "GetTile")
.Elements(ows + "DCP") .Elements(ows + "DCP")
.Elements(ows + "HTTP") .Elements(ows + "HTTP")
.Elements(ows + "Get") .Elements(ows + "Get")
.Where(get => get.Elements(ows + "Constraint") .Where(g => g.Elements(ows + "Constraint")
.Any(con => con.Attribute("name")?.Value == "GetEncoding" && .Any(con => con.Attribute("name")?.Value == "GetEncoding" &&
con.Element(ows + "AllowedValues")?.Element(ows + "Value")?.Value == "KVP")) con.Element(ows + "AllowedValues")?.Element(ows + "Value")?.Value == "KVP"))
.Select(get => get.Attribute(xlink + "href")?.Value) .Select(g => g.Attribute(xlink + "href")?.Value)
.Where(href => !string.IsNullOrEmpty(href)) .Where(h => !string.IsNullOrEmpty(h))
.Select(href => href.Split('?')[0]) .Select(h => h.Split('?')[0])
.FirstOrDefault(); .FirstOrDefault();
if (urlTemplate == null && if (urlTemplate == null &&
@ -181,7 +181,7 @@ namespace MapControl
{ {
var formats = layerElement var formats = layerElement
.Elements(wmts + "Format") .Elements(wmts + "Format")
.Select(fmt => fmt.Value); .Select(f => f.Value);
var format = formats.Contains(formatPng) ? formatPng var format = formats.Contains(formatPng) ? formatPng
: formats.Contains(formatJpg) ? formatJpg : formats.Contains(formatJpg) ? formatJpg
@ -195,8 +195,8 @@ namespace MapControl
urlTemplate += "?Service=WMTS" urlTemplate += "?Service=WMTS"
+ "&Request=GetTile" + "&Request=GetTile"
+ "&Version=1.0.0" + "&Version=1.0.0"
+ "&Layer=" + layerIdentifier + "&Layer=" + layer
+ "&Style=" + styleIdentifier + "&Style=" + style
+ "&Format=" + format + "&Format=" + format
+ "&TileMatrixSet={TileMatrixSet}" + "&TileMatrixSet={TileMatrixSet}"
+ "&TileMatrix={TileMatrix}" + "&TileMatrix={TileMatrix}"
@ -207,7 +207,7 @@ namespace MapControl
if (string.IsNullOrEmpty(urlTemplate)) if (string.IsNullOrEmpty(urlTemplate))
{ {
throw new ArgumentException($"No ResourceURL element in Layer \"{layerIdentifier}\" and no GetTile KVP Operation Metadata found."); throw new ArgumentException($"No ResourceURL element in Layer \"{layer}\" and no GetTile KVP Operation Metadata found.");
} }
return urlTemplate; return urlTemplate;

View file

@ -28,8 +28,11 @@ namespace MapControl
nameof(CapabilitiesUri), typeof(Uri), typeof(WmtsTileLayer), nameof(CapabilitiesUri), typeof(Uri), typeof(WmtsTileLayer),
new PropertyMetadata(null, (o, e) => ((WmtsTileLayer)o).TileMatrixSets.Clear())); new PropertyMetadata(null, (o, e) => ((WmtsTileLayer)o).TileMatrixSets.Clear()));
public static readonly DependencyProperty LayerIdentifierProperty = DependencyProperty.Register( public static readonly DependencyProperty LayerProperty = DependencyProperty.Register(
nameof(LayerIdentifier), typeof(string), typeof(WmtsTileLayer), new PropertyMetadata(null)); nameof(Layer), typeof(string), typeof(WmtsTileLayer), new PropertyMetadata(null));
public static readonly DependencyProperty PreferredTileMatrixSetsProperty = DependencyProperty.Register(
nameof(PreferredTileMatrixSets), typeof(string[]), typeof(WmtsTileLayer), new PropertyMetadata(null));
public WmtsTileLayer() public WmtsTileLayer()
: this(new TileImageLoader()) : this(new TileImageLoader())
@ -52,12 +55,22 @@ namespace MapControl
} }
/// <summary> /// <summary>
/// The ows:Identifier of the Layer that should be displayed. If not set, the first Layer is displayed. /// The Identifier of the Layer that should be displayed. If not set, the first Layer is displayed.
/// </summary> /// </summary>
public string LayerIdentifier public string Layer
{ {
get => (string)GetValue(LayerIdentifierProperty); get => (string)GetValue(LayerProperty);
set => SetValue(LayerIdentifierProperty, value); set => SetValue(LayerProperty, value);
}
/// <summary>
/// In case there are TileMatrixSets with identical SupportedCRS values,
/// the ones with Identifiers contained in this collection take precedence.
/// </summary>
public string[] PreferredTileMatrixSets
{
get => (string[])GetValue(PreferredTileMatrixSetsProperty);
set => SetValue(PreferredTileMatrixSetsProperty, value);
} }
public IEnumerable<WmtsTileMatrixLayer> ChildLayers => Children.Cast<WmtsTileMatrixLayer>(); public IEnumerable<WmtsTileMatrixLayer> ChildLayers => Children.Cast<WmtsTileMatrixLayer>();
@ -168,9 +181,9 @@ namespace MapControl
if (!string.IsNullOrEmpty(cacheName)) if (!string.IsNullOrEmpty(cacheName))
{ {
if (!string.IsNullOrEmpty(LayerIdentifier)) if (!string.IsNullOrEmpty(Layer))
{ {
cacheName += "/" + LayerIdentifier.Replace(':', '_'); cacheName += "/" + Layer.Replace(':', '_');
} }
cacheName += "/" + tileMatrixSet.Identifier.Replace(':', '_'); cacheName += "/" + tileMatrixSet.Identifier.Replace(':', '_');
@ -184,19 +197,22 @@ namespace MapControl
private async void OnLoaded(object sender, RoutedEventArgs e) private async void OnLoaded(object sender, RoutedEventArgs e)
{ {
Loaded -= OnLoaded;
if (TileMatrixSets.Count == 0 && CapabilitiesUri != null) if (TileMatrixSets.Count == 0 && CapabilitiesUri != null)
{ {
try try
{ {
var capabilities = await WmtsCapabilities.ReadCapabilitiesAsync(CapabilitiesUri, LayerIdentifier); var capabilities = await WmtsCapabilities.ReadCapabilitiesAsync(CapabilitiesUri, Layer);
foreach (var tileMatrixSet in capabilities.TileMatrixSets foreach (var tileMatrixSet in capabilities.TileMatrixSets
.Where(s => !TileMatrixSets.ContainsKey(s.SupportedCrs))) .Where(s => !TileMatrixSets.ContainsKey(s.SupportedCrs) ||
PreferredTileMatrixSets != null && PreferredTileMatrixSets.Contains(s.Identifier)))
{ {
TileMatrixSets.Add(tileMatrixSet.SupportedCrs, tileMatrixSet); TileMatrixSets[tileMatrixSet.SupportedCrs] = tileMatrixSet;
} }
LayerIdentifier = capabilities.LayerIdentifier; Layer = capabilities.Layer;
TileSource = capabilities.TileSource; TileSource = capabilities.TileSource;
} }
catch (Exception ex) catch (Exception ex)