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

245 lines
8.5 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2020-03-20 18:12:56 +01:00
using System.Collections.Generic;
2025-08-20 00:29:56 +02:00
using System.Linq;
using System.Threading;
2021-07-05 00:03:44 +02:00
using System.Threading.Tasks;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
#elif UWP
using Windows.UI.Composition;
2024-05-22 11:25:32 +02:00
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
2024-05-22 11:25:32 +02:00
using Windows.UI.Xaml.Media;
2024-05-20 23:24:34 +02:00
#elif WINUI
using Microsoft.UI.Composition;
2021-06-14 21:41:37 +02:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Hosting;
2021-06-14 21:41:37 +02:00
using Microsoft.UI.Xaml.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
2020-03-20 18:12:56 +01:00
#endif
namespace MapControl
{
2024-12-31 16:11:37 +01:00
public abstract class MapTileLayerBase : Panel, IMapLayer
2020-03-20 18:12:56 +01:00
{
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty TileSourceProperty =
2024-05-23 18:22:52 +02:00
DependencyPropertyHelper.Register<MapTileLayerBase, TileSource>(nameof(TileSource), null,
2025-01-27 23:13:51 +01:00
async (layer, oldValue, newValue) => await layer.UpdateTileLayer(true));
2020-03-20 18:12:56 +01:00
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty SourceNameProperty =
DependencyPropertyHelper.Register<MapTileLayerBase, string>(nameof(SourceName));
2020-03-20 18:12:56 +01:00
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty DescriptionProperty =
DependencyPropertyHelper.Register<MapTileLayerBase, string>(nameof(Description));
2020-03-20 18:12:56 +01:00
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty MaxBackgroundLevelsProperty =
DependencyPropertyHelper.Register<MapTileLayerBase, int>(nameof(MaxBackgroundLevels), 5);
2020-03-20 18:12:56 +01:00
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty UpdateIntervalProperty =
2025-01-16 17:08:56 +01:00
DependencyPropertyHelper.Register<MapTileLayerBase, TimeSpan>(nameof(UpdateInterval), TimeSpan.FromSeconds(0.2),
(layer, oldValue, newValue) => layer.updateTimer.Interval = newValue);
2020-03-20 18:12:56 +01:00
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty UpdateWhileViewportChangingProperty =
DependencyPropertyHelper.Register<MapTileLayerBase, bool>(nameof(UpdateWhileViewportChanging));
2020-03-20 18:12:56 +01:00
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty MapBackgroundProperty =
DependencyPropertyHelper.Register<MapTileLayerBase, Brush>(nameof(MapBackground));
2020-03-20 18:12:56 +01:00
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty MapForegroundProperty =
DependencyPropertyHelper.Register<MapTileLayerBase, Brush>(nameof(MapForeground));
2020-03-20 18:12:56 +01:00
2024-05-20 23:24:34 +02:00
public static readonly DependencyProperty LoadingProgressProperty =
DependencyPropertyHelper.Register<MapTileLayerBase, double>(nameof(LoadingProgress), 1d);
2023-08-13 07:52:11 +02:00
private readonly Progress<double> loadingProgress;
2022-01-12 23:56:05 +01:00
private readonly DispatcherTimer updateTimer;
private ITileImageLoader tileImageLoader;
2025-08-20 00:29:56 +02:00
private CancellationTokenSource cancellationTokenSource;
2020-03-20 18:12:56 +01:00
private MapBase parentMap;
protected MapTileLayerBase()
2020-03-20 18:12:56 +01:00
{
2024-05-25 18:58:51 +02:00
IsHitTestVisible = false;
2024-05-20 23:24:34 +02:00
loadingProgress = new Progress<double>(p => SetValue(LoadingProgressProperty, p));
2022-01-12 23:56:05 +01:00
updateTimer = this.CreateTimer(UpdateInterval);
2025-01-27 23:13:51 +01:00
updateTimer.Tick += async (s, e) => await UpdateTileLayer(false);
2020-03-20 18:12:56 +01:00
2024-05-25 19:01:00 +02:00
MapPanel.SetRenderTransform(this, new MatrixTransform());
#if WPF
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
#elif UWP || WINUI
ElementCompositionPreview.GetElementVisual(this).BorderMode = CompositionBorderMode.Hard;
2020-03-20 18:12:56 +01:00
MapPanel.InitMapElement(this);
2021-01-17 00:31:30 +01:00
#endif
2020-03-20 18:12:56 +01:00
}
public ITileImageLoader TileImageLoader
{
get => tileImageLoader ?? (tileImageLoader = new TileImageLoader());
set => tileImageLoader = value;
}
2020-03-20 18:12:56 +01:00
/// <summary>
/// Provides map tile URIs or images.
/// </summary>
public TileSource TileSource
{
2022-08-06 10:40:59 +02:00
get => (TileSource)GetValue(TileSourceProperty);
set => SetValue(TileSourceProperty, value);
2020-03-20 18:12:56 +01:00
}
/// <summary>
2024-07-13 08:19:17 +02:00
/// Name of the tile source that is used as component of a tile cache key.
/// Tile images are not cached when SourceName is null or empty.
2020-03-20 18:12:56 +01:00
/// </summary>
public string SourceName
{
2022-08-06 10:40:59 +02:00
get => (string)GetValue(SourceNameProperty);
set => SetValue(SourceNameProperty, value);
2020-03-20 18:12:56 +01:00
}
/// <summary>
2021-11-28 23:50:17 +01:00
/// Description of the layer. Used to display copyright information on top of the map.
2020-03-20 18:12:56 +01:00
/// </summary>
public string Description
{
2022-08-06 10:40:59 +02:00
get => (string)GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
2020-03-20 18:12:56 +01:00
}
/// <summary>
2022-08-22 21:13:45 +02:00
/// Maximum number of background tile levels. Default value is 5.
2020-03-20 21:45:54 +01:00
/// Only effective in a MapTileLayer or WmtsTileLayer that is the MapLayer of its ParentMap.
2020-03-20 18:12:56 +01:00
/// </summary>
public int MaxBackgroundLevels
{
2022-08-06 10:40:59 +02:00
get => (int)GetValue(MaxBackgroundLevelsProperty);
set => SetValue(MaxBackgroundLevelsProperty, value);
2020-03-20 18:12:56 +01:00
}
/// <summary>
/// Minimum time interval between tile updates.
/// </summary>
public TimeSpan UpdateInterval
{
2022-08-06 10:40:59 +02:00
get => (TimeSpan)GetValue(UpdateIntervalProperty);
set => SetValue(UpdateIntervalProperty, value);
2020-03-20 18:12:56 +01:00
}
/// <summary>
/// Controls if tiles are updated while the viewport is still changing.
/// </summary>
public bool UpdateWhileViewportChanging
{
2022-08-06 10:40:59 +02:00
get => (bool)GetValue(UpdateWhileViewportChangingProperty);
set => SetValue(UpdateWhileViewportChangingProperty, value);
2020-03-20 18:12:56 +01:00
}
/// <summary>
/// Optional background brush. Sets MapBase.Background if not null and this layer is the base map layer.
/// </summary>
public Brush MapBackground
{
2022-08-06 10:40:59 +02:00
get => (Brush)GetValue(MapBackgroundProperty);
set => SetValue(MapBackgroundProperty, value);
2020-03-20 18:12:56 +01:00
}
/// <summary>
/// Optional foreground brush. Sets MapBase.Foreground if not null and this layer is the base map layer.
/// </summary>
public Brush MapForeground
{
2022-08-06 10:40:59 +02:00
get => (Brush)GetValue(MapForegroundProperty);
set => SetValue(MapForegroundProperty, value);
2020-03-20 18:12:56 +01:00
}
/// <summary>
/// Gets the progress of the TileImageLoader as a double value between 0 and 1.
/// </summary>
2024-05-20 23:24:34 +02:00
public double LoadingProgress => (double)GetValue(LoadingProgressProperty);
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
2020-03-20 18:12:56 +01:00
public MapBase ParentMap
{
2022-08-06 10:40:59 +02:00
get => parentMap;
2020-03-20 18:12:56 +01:00
set
{
if (parentMap != null)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = value;
if (parentMap != null)
{
parentMap.ViewportChanged += OnViewportChanged;
}
2022-01-12 23:56:05 +01:00
updateTimer.Run();
2020-03-20 18:12:56 +01:00
}
}
2022-08-06 10:40:59 +02:00
protected bool IsBaseMapLayer => parentMap != null && parentMap.Children.Count > 0 && parentMap.Children[0] == this;
2021-11-10 22:31:39 +01:00
2025-08-20 00:29:56 +02:00
protected async Task LoadTilesAsync(IEnumerable<Tile> tiles, string cacheName)
2023-08-12 17:36:37 +02:00
{
2025-08-20 00:29:56 +02:00
cancellationTokenSource?.Cancel();
if (TileSource != null && tiles != null && tiles.Any(tile => tile.IsPending))
{
using (cancellationTokenSource = new CancellationTokenSource())
{
await TileImageLoader.LoadTilesAsync(tiles, TileSource, cacheName, loadingProgress, cancellationTokenSource.Token);
}
cancellationTokenSource = null;
}
2023-08-12 17:36:37 +02:00
}
2021-11-14 19:29:35 +01:00
2025-08-19 23:21:51 +02:00
protected void CancelLoadTiles()
2025-08-19 23:20:11 +02:00
{
2025-08-20 00:29:56 +02:00
cancellationTokenSource?.Cancel();
2025-08-19 23:20:11 +02:00
ClearValue(LoadingProgressProperty);
}
2025-01-28 17:52:40 +01:00
protected abstract void SetRenderTransform();
2025-08-18 22:23:19 +02:00
protected abstract Task UpdateTileLayerAsync(bool resetTiles);
2025-01-28 17:52:40 +01:00
2025-08-18 22:23:19 +02:00
private Task UpdateTileLayer(bool resetTiles)
2021-11-22 23:53:01 +01:00
{
updateTimer.Stop();
2025-08-18 22:23:19 +02:00
return UpdateTileLayerAsync(resetTiles);
2021-11-22 23:53:01 +01:00
}
2021-07-05 00:03:44 +02:00
private async void OnViewportChanged(object sender, ViewportChangedEventArgs e)
2020-03-20 18:12:56 +01:00
{
2022-11-25 19:05:48 +01:00
if (e.TransformCenterChanged || e.ProjectionChanged || Children.Count == 0)
2020-03-20 18:12:56 +01:00
{
2025-01-27 23:13:51 +01:00
await UpdateTileLayer(false); // update immediately
2020-03-20 18:12:56 +01:00
}
else
{
SetRenderTransform();
2022-01-12 23:56:05 +01:00
updateTimer.Run(!UpdateWhileViewportChanging);
2020-03-20 18:12:56 +01:00
}
}
}
}