XAML-Map-Control/MapControl/Shared/WmtsCapabilities.cs

324 lines
13 KiB
C#
Raw Normal View History

2021-01-13 21:19:27 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
2020-04-15 20:46:16 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
2024-05-22 11:25:32 +02:00
#if WPF
2020-04-15 20:46:16 +02:00
using System.Windows;
#endif
namespace MapControl
{
2022-08-22 22:09:49 +02:00
/// <summary>
/// For reference see https://www.ogc.org/standards/wmts, 07-057r7_Web_Map_Tile_Service_Standard.pdf
/// </summary>
2020-04-15 20:46:16 +02:00
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 Layer { get; private set; }
2020-04-15 20:46:16 +02:00
public WmtsTileSource TileSource { get; private set; }
public List<WmtsTileMatrixSet> TileMatrixSets { get; private set; }
public static async Task<WmtsCapabilities> ReadCapabilitiesAsync(Uri capabilitiesUri, string layer)
2020-04-15 20:46:16 +02:00
{
WmtsCapabilities capabilities;
if (capabilitiesUri.IsAbsoluteUri && (capabilitiesUri.Scheme == "http" || capabilitiesUri.Scheme == "https"))
{
using (var stream = await ImageLoader.HttpClient.GetStreamAsync(capabilitiesUri))
{
capabilities = ReadCapabilities(XDocument.Load(stream).Root, layer, capabilitiesUri.ToString());
2020-04-15 20:46:16 +02:00
}
}
else
{
capabilities = ReadCapabilities(XDocument.Load(capabilitiesUri.ToString()).Root, layer, null);
2020-04-15 20:46:16 +02:00
}
return capabilities;
}
public static WmtsCapabilities ReadCapabilities(XElement capabilitiesElement, string layer, string capabilitiesUrl)
2020-04-15 20:46:16 +02:00
{
var contentsElement = capabilitiesElement.Element(wmts + "Contents");
2020-04-15 20:46:16 +02:00
if (contentsElement == null)
{
throw new ArgumentException("Contents element not found.");
}
XElement layerElement;
if (!string.IsNullOrEmpty(layer))
2020-04-15 20:46:16 +02:00
{
2022-09-01 17:32:43 +02:00
layerElement = contentsElement
.Elements(wmts + "Layer")
.FirstOrDefault(l => l.Element(ows + "Identifier")?.Value == layer);
2020-04-15 20:46:16 +02:00
if (layerElement == null)
{
throw new ArgumentException($"Layer element \"{layer}\" not found.");
2020-04-15 20:46:16 +02:00
}
}
else
{
2022-09-01 17:32:43 +02:00
layerElement = contentsElement
.Elements(wmts + "Layer")
.FirstOrDefault();
2020-04-15 20:46:16 +02:00
if (layerElement == null)
{
throw new ArgumentException("No Layer element found.");
}
layer = layerElement.Element(ows + "Identifier")?.Value ?? "";
2020-04-15 20:46:16 +02:00
}
2022-09-01 17:32:43 +02:00
var styleElement = layerElement
.Elements(wmts + "Style")
.FirstOrDefault(s => s.Attribute("isDefault")?.Value == "true");
2020-04-15 20:46:16 +02:00
if (styleElement == null)
{
2022-09-01 17:32:43 +02:00
styleElement = layerElement
.Elements(wmts + "Style")
.FirstOrDefault();
2020-04-15 20:46:16 +02:00
}
var style = styleElement?.Element(ows + "Identifier")?.Value;
2020-04-15 20:46:16 +02:00
if (string.IsNullOrEmpty(style))
2020-04-15 20:46:16 +02:00
{
throw new ArgumentException($"No Style element found in Layer \"{layer}\".");
2020-04-15 20:46:16 +02:00
}
var urlTemplate = ReadUrlTemplate(capabilitiesElement, layerElement, layer, style, capabilitiesUrl);
2020-11-03 13:27:50 +01:00
2020-04-15 20:46:16 +02:00
var tileMatrixSetIds = layerElement
.Elements(wmts + "TileMatrixSetLink")
.Select(l => l.Element(wmts + "TileMatrixSet")?.Value)
.Where(v => !string.IsNullOrEmpty(v));
2020-04-15 20:46:16 +02:00
var tileMatrixSets = new List<WmtsTileMatrixSet>();
foreach (var tileMatrixSetId in tileMatrixSetIds)
{
2022-09-01 17:32:43 +02:00
var tileMatrixSetElement = contentsElement
.Elements(wmts + "TileMatrixSet")
.FirstOrDefault(s => s.Element(ows + "Identifier")?.Value == tileMatrixSetId);
2020-04-15 20:46:16 +02:00
if (tileMatrixSetElement == null)
{
throw new ArgumentException($"Linked TileMatrixSet element not found in Layer \"{layer}\".");
2020-04-15 20:46:16 +02:00
}
2020-11-03 13:27:50 +01:00
tileMatrixSets.Add(ReadTileMatrixSet(tileMatrixSetElement));
2020-04-15 20:46:16 +02:00
}
return new WmtsCapabilities
{
Layer = layer,
TileSource = new WmtsTileSource { UriTemplate = urlTemplate },
2020-04-15 20:46:16 +02:00
TileMatrixSets = tileMatrixSets
};
}
public static string ReadUrlTemplate(XElement capabilitiesElement, XElement layerElement, string layer, string style, string capabilitiesUrl)
2020-11-03 12:47:50 +01:00
{
const string formatPng = "image/png";
const string formatJpg = "image/jpeg";
string urlTemplate = null;
2022-09-01 17:32:43 +02:00
var resourceUrls = layerElement
.Elements(wmts + "ResourceURL")
.Where(r => r.Attribute("resourceType")?.Value == "tile" &&
r.Attribute("format")?.Value != null &&
r.Attribute("template")?.Value != null)
.ToLookup(r => r.Attribute("format").Value,
r => r.Attribute("template").Value);
2020-11-03 12:47:50 +01:00
if (resourceUrls.Any())
{
2022-08-22 21:13:45 +02:00
var urlTemplates = resourceUrls.Contains(formatPng) ? resourceUrls[formatPng]
: resourceUrls.Contains(formatJpg) ? resourceUrls[formatJpg]
: resourceUrls.First();
2020-11-03 12:47:50 +01:00
urlTemplate = urlTemplates.First().Replace("{Style}", style);
2020-11-03 12:47:50 +01:00
}
else
2020-11-03 12:47:50 +01:00
{
2022-09-01 17:32:43 +02:00
urlTemplate = capabilitiesElement
.Elements(ows + "OperationsMetadata")
2022-09-01 17:32:43 +02:00
.Elements(ows + "Operation")
.Where(o => o.Attribute("name")?.Value == "GetTile")
.Elements(ows + "DCP")
.Elements(ows + "HTTP")
2022-09-01 17:32:43 +02:00
.Elements(ows + "Get")
.Where(g => g.Elements(ows + "Constraint")
.Any(con => con.Attribute("name")?.Value == "GetEncoding" &&
con.Element(ows + "AllowedValues")?.Element(ows + "Value")?.Value == "KVP"))
.Select(g => g.Attribute(xlink + "href")?.Value)
.Where(h => !string.IsNullOrEmpty(h))
.Select(h => h.Split('?')[0])
.FirstOrDefault();
2022-09-01 17:32:43 +02:00
if (urlTemplate == null &&
capabilitiesUrl != null &&
capabilitiesUrl.IndexOf("Request=GetCapabilities", StringComparison.OrdinalIgnoreCase) >= 0)
2020-11-03 12:47:50 +01:00
{
2022-09-01 17:32:43 +02:00
urlTemplate = capabilitiesUrl.Split('?')[0];
2020-11-03 12:47:50 +01:00
}
2022-08-22 21:13:45 +02:00
2022-09-01 17:32:43 +02:00
if (urlTemplate != null)
{
2022-09-01 17:32:43 +02:00
var formats = layerElement
.Elements(wmts + "Format")
.Select(f => f.Value);
2022-09-01 17:32:43 +02:00
var format = formats.Contains(formatPng) ? formatPng
: formats.Contains(formatJpg) ? formatJpg
: formats.FirstOrDefault();
if (string.IsNullOrEmpty(format))
{
format = formatPng;
}
2022-09-01 17:32:43 +02:00
urlTemplate += "?Service=WMTS"
+ "&Request=GetTile"
+ "&Version=1.0.0"
+ "&Layer=" + layer
+ "&Style=" + style
2022-09-01 17:32:43 +02:00
+ "&Format=" + format
+ "&TileMatrixSet={TileMatrixSet}"
+ "&TileMatrix={TileMatrix}"
+ "&TileRow={TileRow}"
+ "&TileCol={TileCol}";
}
2020-11-03 12:47:50 +01:00
}
if (string.IsNullOrEmpty(urlTemplate))
{
throw new ArgumentException($"No ResourceURL element in Layer \"{layer}\" and no GetTile KVP Operation Metadata found.");
2020-11-03 12:47:50 +01:00
}
return urlTemplate;
}
2020-04-15 20:46:16 +02:00
public static WmtsTileMatrixSet ReadTileMatrixSet(XElement tileMatrixSetElement)
{
var identifier = tileMatrixSetElement.Element(ows + "Identifier")?.Value;
if (string.IsNullOrEmpty(identifier))
{
throw new ArgumentException("No Identifier element found in TileMatrixSet.");
2020-04-15 20:46:16 +02:00
}
var supportedCrs = tileMatrixSetElement.Element(ows + "SupportedCRS")?.Value;
if (string.IsNullOrEmpty(supportedCrs))
{
throw new ArgumentException($"No SupportedCRS element found in TileMatrixSet \"{identifier}\".");
2020-04-15 20:46:16 +02:00
}
const string urnPrefix = "urn:ogc:def:crs:EPSG:";
if (supportedCrs.StartsWith(urnPrefix)) // e.g. "urn:ogc:def:crs:EPSG:6.18:3857")
2022-08-03 18:01:02 +02:00
{
var crs = supportedCrs.Substring(urnPrefix.Length).Split(':');
2022-08-03 18:01:02 +02:00
if (crs.Length > 1)
2022-08-03 18:01:02 +02:00
{
supportedCrs = "EPSG:" + crs[1];
2022-08-03 18:01:02 +02:00
}
}
2020-04-15 20:46:16 +02:00
var tileMatrixes = new List<WmtsTileMatrix>();
foreach (var tileMatrixElement in tileMatrixSetElement.Elements(wmts + "TileMatrix"))
2020-04-15 20:46:16 +02:00
{
2020-06-19 00:00:32 +02:00
tileMatrixes.Add(ReadTileMatrix(tileMatrixElement, supportedCrs));
2020-04-15 20:46:16 +02:00
}
if (tileMatrixes.Count <= 0)
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No TileMatrix elements found in TileMatrixSet \"{identifier}\".");
2020-04-15 20:46:16 +02:00
}
return new WmtsTileMatrixSet(identifier, supportedCrs, tileMatrixes);
}
2020-06-19 00:00:32 +02:00
public static WmtsTileMatrix ReadTileMatrix(XElement tileMatrixElement, string supportedCrs)
2020-04-15 20:46:16 +02:00
{
var identifier = tileMatrixElement.Element(ows + "Identifier")?.Value;
if (string.IsNullOrEmpty(identifier))
{
throw new ArgumentException("No Identifier element found in TileMatrix.");
2020-04-15 20:46:16 +02:00
}
var valueString = tileMatrixElement.Element(wmts + "ScaleDenominator")?.Value;
2020-04-15 20:46:16 +02:00
if (string.IsNullOrEmpty(valueString) ||
2020-04-16 19:24:38 +02:00
!double.TryParse(valueString, NumberStyles.Float, CultureInfo.InvariantCulture, out double scaleDenominator))
2020-04-15 20:46:16 +02:00
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No ScaleDenominator element found in TileMatrix \"{identifier}\".");
2020-04-15 20:46:16 +02:00
}
valueString = tileMatrixElement.Element(wmts + "TopLeftCorner")?.Value;
2020-04-16 19:24:38 +02:00
string[] topLeftCornerStrings;
2020-04-15 20:46:16 +02:00
if (string.IsNullOrEmpty(valueString) ||
(topLeftCornerStrings = valueString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)).Length < 2 ||
2020-04-16 19:24:38 +02:00
!double.TryParse(topLeftCornerStrings[0], NumberStyles.Float, CultureInfo.InvariantCulture, out double left) ||
!double.TryParse(topLeftCornerStrings[1], NumberStyles.Float, CultureInfo.InvariantCulture, out double top))
2020-04-15 20:46:16 +02:00
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No TopLeftCorner element found in TileMatrix \"{identifier}\".");
2020-04-15 20:46:16 +02:00
}
valueString = tileMatrixElement.Element(wmts + "TileWidth")?.Value;
2020-04-15 20:46:16 +02:00
2020-04-16 19:24:38 +02:00
if (string.IsNullOrEmpty(valueString) || !int.TryParse(valueString, out int tileWidth))
2020-04-15 20:46:16 +02:00
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No TileWidth element found in TileMatrix \"{identifier}\".");
2020-04-15 20:46:16 +02:00
}
valueString = tileMatrixElement.Element(wmts + "TileHeight")?.Value;
2020-04-15 20:46:16 +02:00
2020-04-16 19:24:38 +02:00
if (string.IsNullOrEmpty(valueString) || !int.TryParse(valueString, out int tileHeight))
2020-04-15 20:46:16 +02:00
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No TileHeight element found in TileMatrix \"{identifier}\".");
2020-04-15 20:46:16 +02:00
}
valueString = tileMatrixElement.Element(wmts + "MatrixWidth")?.Value;
2020-04-15 20:46:16 +02:00
2020-04-16 19:24:38 +02:00
if (string.IsNullOrEmpty(valueString) || !int.TryParse(valueString, out int matrixWidth))
2020-04-15 20:46:16 +02:00
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No MatrixWidth element found in TileMatrix \"{identifier}\".");
2020-04-15 20:46:16 +02:00
}
valueString = tileMatrixElement.Element(wmts + "MatrixHeight")?.Value;
2020-04-15 20:46:16 +02:00
2020-04-16 19:24:38 +02:00
if (string.IsNullOrEmpty(valueString) || !int.TryParse(valueString, out int matrixHeight))
2020-04-15 20:46:16 +02:00
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No MatrixHeight element found in TileMatrix \"{identifier}\".");
2020-04-15 20:46:16 +02:00
}
2020-06-19 00:00:32 +02:00
var topLeft = supportedCrs == "EPSG:4326"
2022-03-02 22:03:18 +01:00
? new Point(MapProjection.Wgs84MeterPerDegree * top, MapProjection.Wgs84MeterPerDegree * left)
2020-06-19 00:00:32 +02:00
: new Point(left, top);
2020-04-15 20:46:16 +02:00
return new WmtsTileMatrix(
2020-06-19 00:00:32 +02:00
identifier, scaleDenominator, topLeft, tileWidth, tileHeight, matrixWidth, matrixHeight);
2020-04-15 20:46:16 +02:00
}
}
}