2020-03-20 18:12:56 +01:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2020 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public class WmtsTileLayer : MapTileLayerBase
|
|
|
|
|
|
{
|
2020-03-20 20:40:13 +01:00
|
|
|
|
public static readonly DependencyProperty CapabilitiesUriProperty = DependencyProperty.Register(
|
2020-03-20 21:09:19 +01:00
|
|
|
|
nameof(CapabilitiesUri), typeof(Uri), typeof(WmtsTileLayer),
|
2020-03-21 01:52:17 +01:00
|
|
|
|
new PropertyMetadata(null, (o, e) => ((WmtsTileLayer)o).TileMatrixSets.Clear()));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty LayerIdentifierProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(LayerIdentifier), typeof(string), typeof(WmtsTileLayer), new PropertyMetadata(null));
|
|
|
|
|
|
|
|
|
|
|
|
public WmtsTileLayer()
|
|
|
|
|
|
: this(new TileImageLoader())
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public WmtsTileLayer(ITileImageLoader tileImageLoader)
|
|
|
|
|
|
: base(tileImageLoader)
|
|
|
|
|
|
{
|
|
|
|
|
|
Loaded += OnLoaded;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 20:40:13 +01:00
|
|
|
|
public Uri CapabilitiesUri
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2020-03-20 20:40:13 +01:00
|
|
|
|
get { return (Uri)GetValue(CapabilitiesUriProperty); }
|
|
|
|
|
|
set { SetValue(CapabilitiesUriProperty, value); }
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string LayerIdentifier
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(LayerIdentifierProperty); }
|
|
|
|
|
|
set { SetValue(LayerIdentifierProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
public Dictionary<string, WmtsTileMatrixSet> TileMatrixSets { get; } = new Dictionary<string, WmtsTileMatrixSet>();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var layer in Children.Cast<WmtsTileMatrixLayer>())
|
|
|
|
|
|
{
|
|
|
|
|
|
layer.Measure(availableSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var layer in Children.Cast<WmtsTileMatrixLayer>())
|
|
|
|
|
|
{
|
|
|
|
|
|
layer.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return finalSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void TileSourcePropertyChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void UpdateTileLayer()
|
|
|
|
|
|
{
|
|
|
|
|
|
UpdateTimer.Stop();
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
WmtsTileMatrixSet tileMatrixSet;
|
|
|
|
|
|
|
2020-03-20 18:12:56 +01:00
|
|
|
|
if (ParentMap == null ||
|
2020-03-21 01:52:17 +01:00
|
|
|
|
!TileMatrixSets.TryGetValue(ParentMap.MapProjection.CrsId, out tileMatrixSet))
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
Children.Clear();
|
2020-03-21 01:52:17 +01:00
|
|
|
|
UpdateTiles(null);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
2020-03-21 01:52:17 +01:00
|
|
|
|
else if (UpdateChildLayers(tileMatrixSet))
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
SetRenderTransform();
|
2020-03-21 01:52:17 +01:00
|
|
|
|
UpdateTiles(tileMatrixSet);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void SetRenderTransform()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var layer in Children.Cast<WmtsTileMatrixLayer>())
|
|
|
|
|
|
{
|
|
|
|
|
|
layer.SetRenderTransform(ParentMap.MapProjection, ParentMap.Heading);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
private bool UpdateChildLayers(WmtsTileMatrixSet tileMatrixSet)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
bool layersChanged = false;
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
// show all TileMatrix layers with Scale <= ViewportScale, or at least the first layer
|
|
|
|
|
|
//
|
|
|
|
|
|
var currentMatrixes = tileMatrixSet.TileMatrixes
|
|
|
|
|
|
.Where((matrix, i) => i == 0 || matrix.Scale <= ParentMap.MapProjection.ViewportScale)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
if (this != ParentMap.MapLayer) // do not load background tiles
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2020-03-21 01:52:17 +01:00
|
|
|
|
currentMatrixes = currentMatrixes.Skip(currentMatrixes.Count - 1).ToList(); // last element only
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (currentMatrixes.Count > MaxBackgroundLevels + 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentMatrixes = currentMatrixes.Skip(currentMatrixes.Count - MaxBackgroundLevels - 1).ToList();
|
|
|
|
|
|
}
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
var currentLayers = Children.Cast<WmtsTileMatrixLayer>()
|
|
|
|
|
|
.Where(layer => currentMatrixes.Contains(layer.TileMatrix))
|
|
|
|
|
|
.ToList();
|
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
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
foreach (var tileMatrix in currentMatrixes)
|
|
|
|
|
|
{
|
|
|
|
|
|
var layer = currentLayers.FirstOrDefault(l => l.TileMatrix == tileMatrix);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
if (layer == null)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2020-03-21 01:52:17 +01:00
|
|
|
|
layer = new WmtsTileMatrixLayer(tileMatrix, tileMatrixSet.TileMatrixes.IndexOf(tileMatrix));
|
|
|
|
|
|
layersChanged = true;
|
|
|
|
|
|
}
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
if (layer.SetBounds(ParentMap.MapProjection, ParentMap.Heading, ParentMap.RenderSize))
|
|
|
|
|
|
{
|
|
|
|
|
|
layersChanged = true;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
2020-03-21 01:52:17 +01:00
|
|
|
|
|
|
|
|
|
|
Children.Add(layer);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return layersChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
private void UpdateTiles(WmtsTileMatrixSet tileMatrixSet)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
var tiles = new List<Tile>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var layer in Children.Cast<WmtsTileMatrixLayer>())
|
|
|
|
|
|
{
|
|
|
|
|
|
layer.UpdateTiles();
|
|
|
|
|
|
|
|
|
|
|
|
tiles.AddRange(layer.Tiles);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
var tileSource = TileSource as WmtsTileSource;
|
|
|
|
|
|
var sourceName = SourceName;
|
|
|
|
|
|
|
|
|
|
|
|
if (tileSource != null && tileMatrixSet != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
tileSource.TileMatrixSet = tileMatrixSet;
|
|
|
|
|
|
|
|
|
|
|
|
if (sourceName != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
sourceName += "/" + tileMatrixSet.Identifier;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TileImageLoader.LoadTilesAsync(tiles, TileSource, sourceName);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void OnLoaded(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
2020-03-21 01:52:17 +01:00
|
|
|
|
if (TileMatrixSets.Count == 0 && CapabilitiesUri != null)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-03-20 20:40:13 +01:00
|
|
|
|
if (CapabilitiesUri.IsAbsoluteUri && (CapabilitiesUri.Scheme == "http" || CapabilitiesUri.Scheme == "https"))
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2020-03-20 20:40:13 +01:00
|
|
|
|
using (var stream = await ImageLoader.HttpClient.GetStreamAsync(CapabilitiesUri))
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
ReadCapabilities(XDocument.Load(stream).Root);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-03-20 20:40:13 +01:00
|
|
|
|
ReadCapabilities(XDocument.Load(CapabilitiesUri.ToString()).Root);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2020-03-20 20:40:13 +01:00
|
|
|
|
Debug.WriteLine("WmtsTileLayer: {0}: {1}", CapabilitiesUri, ex.Message);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
private void ReadCapabilities(XElement capabilitiesElement)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2020-03-21 01:52:17 +01:00
|
|
|
|
TileMatrixSets.Clear();
|
|
|
|
|
|
|
2020-03-21 08:17:15 +01:00
|
|
|
|
XNamespace ns = capabilitiesElement.Name.Namespace;
|
|
|
|
|
|
XNamespace ows = "http://www.opengis.net/ows/1.1";
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
var contentsElement = capabilitiesElement.Element(ns + "Contents");
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("Layer element \"" + LayerIdentifier + "\" not found.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-03-21 01:52:17 +01:00
|
|
|
|
layerElement = capabilitiesElement.Descendants(ns + "Layer").FirstOrDefault();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
if (layerElement == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("No Layer element found.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LayerIdentifier = layerElement.Element(ows + "Identifier")?.Value ?? "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
var urlTemplate = layerElement.Element(ns + "ResourceURL")?.Attribute("template")?.Value;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
if (string.IsNullOrEmpty(urlTemplate))
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2020-03-21 01:52:17 +01:00
|
|
|
|
throw new ArgumentException("No valid ResourceURL element found in Layer \"" + LayerIdentifier + "\".");
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 20:40:13 +01:00
|
|
|
|
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))
|
|
|
|
|
|
{
|
2020-03-21 01:52:17 +01:00
|
|
|
|
throw new ArgumentException("No valid Style element found in Layer \"" + LayerIdentifier + "\".");
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
var tileMatrixSetIds = layerElement
|
|
|
|
|
|
.Descendants(ns + "TileMatrixSetLink")
|
|
|
|
|
|
.Select(e => e.Element(ns + "TileMatrixSet")?.Value)
|
|
|
|
|
|
.Where(id => !string.IsNullOrEmpty(id))
|
|
|
|
|
|
.ToList();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
foreach (var tileMatrixSetId in tileMatrixSetIds)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2020-03-21 01:52:17 +01:00
|
|
|
|
var tileMatrixSetElement = capabilitiesElement.Descendants(ns + "TileMatrixSet")
|
|
|
|
|
|
.FirstOrDefault(e => e.Element(ows + "Identifier")?.Value == tileMatrixSetId);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
if (tileMatrixSetElement == null)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2020-03-21 01:52:17 +01:00
|
|
|
|
throw new ArgumentException("Linked TileMatrixSet element not found in Layer \"" + LayerIdentifier + "\".");
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
var tileMatrixSet = WmtsTileMatrixSet.Create(tileMatrixSetElement);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
TileMatrixSets.Add(tileMatrixSet.SupportedCrs, tileMatrixSet);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
TileSource = new WmtsTileSource(urlTemplate.Replace("{Style}", style));
|
2020-03-20 20:40:13 +01:00
|
|
|
|
|
2020-03-21 01:52:17 +01:00
|
|
|
|
UpdateTileLayer();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|