2025-03-31 21:33:52 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
|
|
|
|
|
using System.Windows;
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#elif UWP
|
2020-03-20 18:12:56 +01:00
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Windows.UI.Xaml;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#elif WINUI
|
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Microsoft.UI.Xaml;
|
2025-08-19 19:43:02 +02:00
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using Avalonia;
|
|
|
|
|
|
using Avalonia.Interactivity;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2020-04-19 10:53:25 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Displays map tiles from a Web Map Tile Service (WMTS).
|
|
|
|
|
|
/// </summary>
|
2025-11-14 23:59:24 +01:00
|
|
|
|
public class WmtsTileLayer : TilePyramidLayer
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2025-08-22 11:06:37 +02:00
|
|
|
|
private static ILogger logger;
|
2025-09-19 18:49:12 +02:00
|
|
|
|
private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(WmtsTileLayer));
|
2025-08-22 11:06:37 +02:00
|
|
|
|
|
2024-05-22 11:47:57 +02:00
|
|
|
|
public static readonly DependencyProperty CapabilitiesUriProperty =
|
2025-11-28 23:22:10 +01:00
|
|
|
|
DependencyPropertyHelper.Register<WmtsTileLayer, Uri>(nameof(CapabilitiesUri));
|
|
|
|
|
|
|
2025-12-01 13:59:18 +01:00
|
|
|
|
public static readonly DependencyProperty UriTemplateProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<WmtsTileLayer, string>(nameof(UriTemplate));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-22 11:47:57 +02:00
|
|
|
|
public static readonly DependencyProperty LayerProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<WmtsTileLayer, string>(nameof(Layer));
|
2023-03-07 17:54:11 +01:00
|
|
|
|
|
2024-05-22 11:47:57 +02:00
|
|
|
|
public static readonly DependencyProperty PreferredTileMatrixSetsProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<WmtsTileLayer, string[]>(nameof(PreferredTileMatrixSets));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
public WmtsTileLayer()
|
|
|
|
|
|
{
|
|
|
|
|
|
Loaded += OnLoaded;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-19 10:53:25 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The Uri of a XML file or web response that contains the service capabilities.
|
|
|
|
|
|
/// </summary>
|
2020-03-20 20:40:13 +01:00
|
|
|
|
public Uri CapabilitiesUri
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (Uri)GetValue(CapabilitiesUriProperty);
|
|
|
|
|
|
set => SetValue(CapabilitiesUriProperty, value);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-19 10:53:25 +02:00
|
|
|
|
/// <summary>
|
2025-11-28 23:22:10 +01:00
|
|
|
|
/// The Uri template string used for the UriTemplate property of WmtsTileSource instances.
|
|
|
|
|
|
/// Usually set internally from WmtsCapabilities requested by a Loaded event handler.
|
|
|
|
|
|
/// </summary>
|
2025-12-01 13:59:18 +01:00
|
|
|
|
public string UriTemplate
|
2025-11-28 23:22:10 +01:00
|
|
|
|
{
|
2025-12-01 13:59:18 +01:00
|
|
|
|
get => (string)GetValue(UriTemplateProperty);
|
|
|
|
|
|
set => SetValue(UriTemplateProperty, value);
|
2025-11-28 23:22:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The Identifier of the Layer that should be displayed.
|
|
|
|
|
|
/// If not set, the value is defined by WmtsCapabilities.
|
2020-04-19 10:53:25 +02:00
|
|
|
|
/// </summary>
|
2023-03-07 17:54:11 +01:00
|
|
|
|
public string Layer
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2023-03-07 17:54:11 +01:00
|
|
|
|
get => (string)GetValue(LayerProperty);
|
|
|
|
|
|
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);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-19 18:49:12 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a dictionary of all tile matrix sets supported by a WMTS, with their CRS as dictionary key.
|
|
|
|
|
|
/// </summary>
|
2025-09-15 17:46:31 +02:00
|
|
|
|
public Dictionary<string, WmtsTileMatrixSet> TileMatrixSets { get; } = [];
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2025-09-19 18:49:12 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a collection of all CRSs supported by a WMTS.
|
|
|
|
|
|
/// </summary>
|
2025-09-19 20:22:10 +02:00
|
|
|
|
public override IReadOnlyCollection<string> SupportedCrsIds => TileMatrixSets.Keys;
|
2025-09-19 18:49:12 +02:00
|
|
|
|
|
|
|
|
|
|
protected IEnumerable<WmtsTileMatrixLayer> ChildLayers => Children.Cast<WmtsTileMatrixLayer>();
|
|
|
|
|
|
|
2020-03-20 18:12:56 +01:00
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
|
|
{
|
2020-03-23 17:13:50 +01:00
|
|
|
|
foreach (var layer in ChildLayers)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
layer.Measure(availableSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
|
|
|
|
{
|
2020-03-23 17:13:50 +01:00
|
|
|
|
foreach (var layer in ChildLayers)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
layer.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return finalSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-23 17:29:30 +01:00
|
|
|
|
protected override void UpdateRenderTransform()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var layer in ChildLayers)
|
|
|
|
|
|
{
|
|
|
|
|
|
layer.UpdateRenderTransform(ParentMap.ViewTransform);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-28 23:22:10 +01:00
|
|
|
|
protected override void UpdateTileCollection()
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (ParentMap == null ||
|
2020-04-16 19:24:38 +02:00
|
|
|
|
!TileMatrixSets.TryGetValue(ParentMap.MapProjection.CrsId, out WmtsTileMatrixSet tileMatrixSet))
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
Children.Clear();
|
2025-08-19 23:21:51 +02:00
|
|
|
|
CancelLoadTiles();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
2025-12-01 19:55:04 +01:00
|
|
|
|
else if (UpdateChildLayers(tileMatrixSet.TileMatrixes))
|
2022-11-25 19:05:48 +01:00
|
|
|
|
{
|
2025-12-01 13:59:18 +01:00
|
|
|
|
var tileSource = new WmtsTileSource(UriTemplate, tileMatrixSet);
|
2025-01-17 08:25:10 +01:00
|
|
|
|
var cacheName = SourceName;
|
|
|
|
|
|
|
2025-01-17 08:25:26 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(cacheName))
|
2025-01-17 08:25:10 +01:00
|
|
|
|
{
|
2025-01-17 08:25:26 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(Layer))
|
2025-01-17 08:25:10 +01:00
|
|
|
|
{
|
2025-01-17 08:25:26 +01:00
|
|
|
|
cacheName += "/" + Layer.Replace(':', '_');
|
2025-01-17 08:25:10 +01:00
|
|
|
|
}
|
2025-01-17 08:25:26 +01:00
|
|
|
|
|
2025-10-29 18:59:19 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(tileMatrixSet.Identifier))
|
|
|
|
|
|
{
|
|
|
|
|
|
cacheName += "/" + tileMatrixSet.Identifier.Replace(':', '_');
|
|
|
|
|
|
}
|
2025-01-17 08:25:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-28 23:22:10 +01:00
|
|
|
|
BeginLoadTiles(ChildLayers.SelectMany(layer => layer.Tiles), tileSource, cacheName);
|
2022-11-25 19:05:48 +01:00
|
|
|
|
}
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-01 19:55:04 +01:00
|
|
|
|
private bool UpdateChildLayers(IList<WmtsTileMatrix> tileMatrixSet)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2025-11-27 20:06:48 +01:00
|
|
|
|
// Multiply scale by 1.001 to avoid floating point precision issues
|
|
|
|
|
|
// and get all WmtsTileMatrixes with Scale <= maxScale.
|
2022-11-30 22:18:45 +01:00
|
|
|
|
//
|
|
|
|
|
|
var maxScale = 1.001 * ParentMap.ViewTransform.Scale;
|
2025-12-01 19:55:04 +01:00
|
|
|
|
var tileMatrixes = tileMatrixSet.Where(matrix => matrix.Scale <= maxScale).ToList();
|
2025-11-28 21:59:25 +01:00
|
|
|
|
|
|
|
|
|
|
if (tileMatrixes.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Children.Clear();
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var maxLayers = Math.Max(MaxBackgroundLevels, 0) + 1;
|
|
|
|
|
|
|
|
|
|
|
|
if (!IsBaseMapLayer)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Show only the last layer.
|
|
|
|
|
|
//
|
|
|
|
|
|
tileMatrixes = tileMatrixes.GetRange(tileMatrixes.Count - 1, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (tileMatrixes.Count > maxLayers)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Show not more than MaxBackgroundLevels + 1 layers.
|
|
|
|
|
|
//
|
|
|
|
|
|
tileMatrixes = tileMatrixes.GetRange(tileMatrixes.Count - maxLayers, maxLayers);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get reusable layers.
|
|
|
|
|
|
//
|
|
|
|
|
|
var layers = ChildLayers.Where(layer => tileMatrixes.Contains(layer.WmtsTileMatrix)).ToList();
|
2022-11-25 19:05:48 +01:00
|
|
|
|
var tilesChanged = false;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
Children.Clear();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2025-11-28 21:59:25 +01:00
|
|
|
|
foreach (var tileMatrix in tileMatrixes)
|
2020-03-21 01:52:17 +01:00
|
|
|
|
{
|
2025-12-01 19:55:04 +01:00
|
|
|
|
// Pass index of tileMatrix in tileMatrixSet as zoom level to WmtsTileMatrixLayer ctor.
|
2025-11-28 21:59:25 +01:00
|
|
|
|
//
|
|
|
|
|
|
var layer = layers.FirstOrDefault(layer => layer.WmtsTileMatrix == tileMatrix) ??
|
2025-12-01 19:55:04 +01:00
|
|
|
|
new WmtsTileMatrixLayer(tileMatrix, tileMatrixSet.IndexOf(tileMatrix));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2025-11-28 21:59:25 +01:00
|
|
|
|
if (layer.UpdateTiles(ParentMap.ViewTransform, ParentMap.ActualWidth, ParentMap.ActualHeight))
|
2025-11-27 20:06:48 +01:00
|
|
|
|
{
|
2025-11-28 21:59:25 +01:00
|
|
|
|
tilesChanged = true;
|
2020-03-21 01:52:17 +01:00
|
|
|
|
}
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2025-11-28 21:59:25 +01:00
|
|
|
|
layer.UpdateRenderTransform(ParentMap.ViewTransform);
|
2025-11-27 20:06:48 +01:00
|
|
|
|
|
2025-11-28 21:59:25 +01:00
|
|
|
|
Children.Add(layer);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-25 19:05:48 +01:00
|
|
|
|
return tilesChanged;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void OnLoaded(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
2023-03-07 17:54:11 +01:00
|
|
|
|
Loaded -= OnLoaded;
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
if (TileMatrixSets.Count == 0 && CapabilitiesUri != null)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-03-07 17:54:11 +01:00
|
|
|
|
var capabilities = await WmtsCapabilities.ReadCapabilitiesAsync(CapabilitiesUri, Layer);
|
2020-03-21 08:17:15 +01:00
|
|
|
|
|
2025-11-20 23:53:08 +01:00
|
|
|
|
foreach (var tms in capabilities.TileMatrixSets
|
|
|
|
|
|
.Where(tms => !TileMatrixSets.ContainsKey(tms.SupportedCrsId) ||
|
|
|
|
|
|
PreferredTileMatrixSets != null &&
|
|
|
|
|
|
PreferredTileMatrixSets.Contains(tms.Identifier)))
|
2020-06-18 18:30:57 +02:00
|
|
|
|
{
|
2025-11-20 23:53:08 +01:00
|
|
|
|
TileMatrixSets[tms.SupportedCrsId] = tms;
|
2020-06-18 18:30:57 +02:00
|
|
|
|
}
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2023-03-07 17:54:11 +01:00
|
|
|
|
Layer = capabilities.Layer;
|
2025-12-01 13:59:18 +01:00
|
|
|
|
UriTemplate = capabilities.UriTemplate;
|
2025-11-28 23:22:10 +01:00
|
|
|
|
|
|
|
|
|
|
UpdateTileCollection();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
2020-04-15 20:46:16 +02:00
|
|
|
|
catch (Exception ex)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2025-08-22 11:06:37 +02:00
|
|
|
|
Logger?.LogError(ex, "Failed reading capabilities from {uri}", CapabilitiesUri);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|