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

295 lines
12 KiB
C#
Raw Normal View History

2021-01-13 21:19:27 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 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.Xml.Linq;
using System.Threading.Tasks;
2021-11-17 23:17:11 +01:00
#if !WINUI && !UWP
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
{
public string LayerIdentifier { get; private set; }
public WmtsTileSource TileSource { get; private set; }
public List<WmtsTileMatrixSet> TileMatrixSets { get; private set; }
public static async Task<WmtsCapabilities> ReadCapabilitiesAsync(Uri capabilitiesUri, string layerIdentifier)
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))
{
2020-11-03 12:47:50 +01:00
capabilities = ReadCapabilities(XDocument.Load(stream).Root, layerIdentifier, capabilitiesUri.ToString());
2020-04-15 20:46:16 +02:00
}
}
else
{
2020-11-03 12:47:50 +01:00
capabilities = ReadCapabilities(XDocument.Load(capabilitiesUri.ToString()).Root, layerIdentifier, null);
2020-04-15 20:46:16 +02:00
}
return capabilities;
}
2020-11-03 12:47:50 +01:00
public static WmtsCapabilities ReadCapabilities(XElement capabilitiesElement, string layerIdentifier, string capabilitiesUrl)
2020-04-15 20:46:16 +02:00
{
XNamespace ns = capabilitiesElement.Name.Namespace;
XNamespace ows = "http://www.opengis.net/ows/1.1";
var contentsElement = capabilitiesElement.Element(ns + "Contents");
if (contentsElement == null)
{
throw new ArgumentException("Contents element not found.");
}
XElement layerElement;
if (!string.IsNullOrEmpty(layerIdentifier))
{
layerElement = contentsElement.Descendants(ns + "Layer")
.FirstOrDefault(e => e.Element(ows + "Identifier")?.Value == layerIdentifier);
if (layerElement == null)
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"Layer element \"{layerIdentifier}\" not found.");
2020-04-15 20:46:16 +02:00
}
}
else
{
layerElement = capabilitiesElement.Descendants(ns + "Layer").FirstOrDefault();
if (layerElement == null)
{
throw new ArgumentException("No Layer element found.");
}
layerIdentifier = layerElement.Element(ows + "Identifier")?.Value ?? "";
}
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))
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No valid Style element found in Layer \"{layerIdentifier}\".");
2020-04-15 20:46:16 +02:00
}
2020-11-03 13:27:50 +01:00
var urlTemplate = ReadUrlTemplate(layerElement, layerIdentifier, style, capabilitiesUrl);
2020-04-15 20:46:16 +02:00
var tileMatrixSetIds = layerElement
.Descendants(ns + "TileMatrixSetLink")
.Select(e => e.Element(ns + "TileMatrixSet")?.Value)
.Where(id => !string.IsNullOrEmpty(id))
.ToList();
var tileMatrixSets = new List<WmtsTileMatrixSet>();
foreach (var tileMatrixSetId in tileMatrixSetIds)
{
var tileMatrixSetElement = capabilitiesElement.Descendants(ns + "TileMatrixSet")
.FirstOrDefault(e => e.Element(ows + "Identifier")?.Value == tileMatrixSetId);
if (tileMatrixSetElement == null)
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"Linked TileMatrixSet element not found in Layer \"{layerIdentifier}\".");
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
{
LayerIdentifier = layerIdentifier,
2020-11-03 13:27:50 +01:00
TileSource = new WmtsTileSource { UriFormat = urlTemplate },
2020-04-15 20:46:16 +02:00
TileMatrixSets = tileMatrixSets
};
}
2020-11-03 13:27:50 +01:00
public static string ReadUrlTemplate(XElement layerElement, string layerIdentifier, string style, string capabilitiesUrl)
2020-11-03 12:47:50 +01:00
{
XNamespace ns = layerElement.Name.Namespace;
const string formatPng = "image/png";
const string formatJpg = "image/jpeg";
string urlTemplate = null;
var resourceUrls = layerElement.Descendants(ns + "ResourceURL")
.ToLookup(e => e.Attribute("format")?.Value ?? "", e => e.Attribute("template")?.Value ?? "");
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
2020-11-03 13:27:50 +01:00
urlTemplate = urlTemplates.First().Replace("{Style}", style);
2020-11-03 12:47:50 +01:00
}
2022-08-22 21:13:45 +02:00
else if (capabilitiesUrl != null &&
capabilitiesUrl.IndexOf("Request=GetCapabilities", StringComparison.OrdinalIgnoreCase) >= 0)
2020-11-03 12:47:50 +01:00
{
2022-08-22 22:09:49 +02:00
// no ResourceURL and GetCapabilities KVP request, use GetTile KVP request encoding
2022-08-22 21:13:45 +02:00
var formats = layerElement.Descendants(ns + "Format").Select(e => e.Value).ToList();
var format = formatPng;
2020-11-03 12:47:50 +01:00
2022-08-22 21:13:45 +02:00
if (formats.Count > 0)
2020-11-03 12:47:50 +01:00
{
2022-08-22 21:13:45 +02:00
format = formats.Contains(formatPng) ? formatPng
: formats.Contains(formatJpg) ? formatJpg
: formats[0];
2020-11-03 12:47:50 +01:00
}
2022-08-22 21:13:45 +02:00
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}";
2020-11-03 12:47:50 +01:00
}
if (string.IsNullOrEmpty(urlTemplate))
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No valid ResourceURL element found in Layer \"{layerIdentifier}\".");
2020-11-03 12:47:50 +01:00
}
return urlTemplate;
}
2020-04-15 20:46:16 +02:00
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.");
}
var supportedCrs = tileMatrixSetElement.Element(ows + "SupportedCRS")?.Value;
if (string.IsNullOrEmpty(supportedCrs))
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No ows: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.Descendants(ns + "TileMatrix"))
{
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
{
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.");
}
var valueString = tileMatrixElement.Element(ns + "ScaleDenominator")?.Value;
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(ns + "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(ns + "TileWidth")?.Value;
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(ns + "TileHeight")?.Value;
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(ns + "MatrixWidth")?.Value;
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(ns + "MatrixHeight")?.Value;
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
}
}
}