mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Improved TileImageLoader
This commit is contained in:
parent
d9c5455a83
commit
c6f7b2d665
|
|
@ -123,7 +123,7 @@ namespace MapControl
|
|||
return finalSize;
|
||||
}
|
||||
|
||||
protected override Task UpdateTileLayer()
|
||||
protected override Task UpdateTileLayer(bool tileSourceChanged)
|
||||
{
|
||||
var updateTiles = false;
|
||||
|
||||
|
|
@ -134,7 +134,7 @@ namespace MapControl
|
|||
}
|
||||
else
|
||||
{
|
||||
if (TileSource != TileImageLoader.TileSource)
|
||||
if (tileSourceChanged)
|
||||
{
|
||||
Tiles = new TileCollection(); // clear all
|
||||
updateTiles = true;
|
||||
|
|
@ -152,7 +152,7 @@ namespace MapControl
|
|||
{
|
||||
UpdateTiles();
|
||||
|
||||
return TileImageLoader.LoadTiles(Tiles, TileSource, SourceName);
|
||||
return LoadTiles(Tiles, SourceName);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
|
|||
|
|
@ -25,18 +25,14 @@ namespace MapControl
|
|||
{
|
||||
public interface ITileImageLoader
|
||||
{
|
||||
IProgress<double> Progress { get; set; }
|
||||
|
||||
TileSource TileSource { get; }
|
||||
|
||||
Task LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName);
|
||||
Task LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress);
|
||||
}
|
||||
|
||||
public abstract class MapTileLayerBase : Panel, IMapLayer
|
||||
{
|
||||
public static readonly DependencyProperty TileSourceProperty = DependencyProperty.Register(
|
||||
nameof(TileSource), typeof(TileSource), typeof(MapTileLayerBase),
|
||||
new PropertyMetadata(null, async (o, e) => await ((MapTileLayerBase)o).Update()));
|
||||
new PropertyMetadata(null, async (o, e) => await ((MapTileLayerBase)o).Update(true)));
|
||||
|
||||
public static readonly DependencyProperty SourceNameProperty = DependencyProperty.Register(
|
||||
nameof(SourceName), typeof(string), typeof(MapTileLayerBase), new PropertyMetadata(null));
|
||||
|
|
@ -63,6 +59,7 @@ namespace MapControl
|
|||
public static readonly DependencyProperty LoadingProgressProperty = DependencyProperty.Register(
|
||||
nameof(LoadingProgress), typeof(double), typeof(MapTileLayerBase), new PropertyMetadata(1d));
|
||||
|
||||
private readonly IProgress<double> loadingProgress;
|
||||
private readonly DispatcherTimer updateTimer;
|
||||
private MapBase parentMap;
|
||||
|
||||
|
|
@ -71,10 +68,11 @@ namespace MapControl
|
|||
RenderTransform = new MatrixTransform();
|
||||
|
||||
TileImageLoader = tileImageLoader;
|
||||
TileImageLoader.Progress = new Progress<double>(p => LoadingProgress = p);
|
||||
|
||||
loadingProgress = new Progress<double>(p => LoadingProgress = p);
|
||||
|
||||
updateTimer = this.CreateTimer(UpdateInterval);
|
||||
updateTimer.Tick += async (s, e) => await Update();
|
||||
updateTimer.Tick += async (s, e) => await Update(false);
|
||||
|
||||
#if WINUI || UWP
|
||||
MapPanel.InitMapElement(this);
|
||||
|
|
@ -193,20 +191,25 @@ namespace MapControl
|
|||
|
||||
protected abstract void SetRenderTransform();
|
||||
|
||||
protected abstract Task UpdateTileLayer();
|
||||
protected abstract Task UpdateTileLayer(bool tileSourceChanged);
|
||||
|
||||
private Task Update()
|
||||
protected Task LoadTiles(IEnumerable<Tile> tiles, string cacheName)
|
||||
{
|
||||
return TileImageLoader.LoadTiles(tiles, TileSource, cacheName, loadingProgress);
|
||||
}
|
||||
|
||||
private Task Update(bool tileSourceChanged)
|
||||
{
|
||||
updateTimer.Stop();
|
||||
|
||||
return UpdateTileLayer();
|
||||
return UpdateTileLayer(tileSourceChanged);
|
||||
}
|
||||
|
||||
private async void OnViewportChanged(object sender, ViewportChangedEventArgs e)
|
||||
{
|
||||
if (e.TransformCenterChanged || e.ProjectionChanged || Children.Count == 0)
|
||||
{
|
||||
await Update(); // update immediately
|
||||
await Update(false); // update immediately
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -45,11 +45,11 @@ namespace MapControl
|
|||
IsHitTestVisible = false // avoid touch capture issues
|
||||
};
|
||||
|
||||
public bool IsLoaded { get; set; }
|
||||
public bool IsPending { get; set; } = true;
|
||||
|
||||
public void SetImageSource(ImageSource image, bool animateOpacity = true)
|
||||
{
|
||||
IsLoaded = true;
|
||||
IsPending = false;
|
||||
Image.Source = image;
|
||||
|
||||
if (image != null && animateOpacity && MapBase.ImageFadeDuration > TimeSpan.Zero)
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace MapControl
|
|||
tile = new Tile(zoomLevel, x, y, columnCount);
|
||||
|
||||
var equivalentTile = this.FirstOrDefault(
|
||||
t => t.IsLoaded && t.ZoomLevel == tile.ZoomLevel && t.Column == tile.Column && t.Row == tile.Row);
|
||||
t => t.Image.Source != null && t.ZoomLevel == tile.ZoomLevel && t.Column == tile.Column && t.Row == tile.Row);
|
||||
|
||||
if (equivalentTile != null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ namespace MapControl
|
|||
private class TileQueue : ConcurrentStack<Tile>
|
||||
{
|
||||
public TileQueue(IEnumerable<Tile> tiles)
|
||||
: base(tiles.Where(tile => !tile.IsLoaded).Reverse())
|
||||
: base(tiles.Where(tile => tile.IsPending).Reverse())
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -30,14 +30,13 @@ namespace MapControl
|
|||
|
||||
public bool TryDequeue(out Tile tile)
|
||||
{
|
||||
tile = null;
|
||||
|
||||
if (IsCanceled || !TryPop(out tile))
|
||||
{
|
||||
tile = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
tile.IsLoaded = true;
|
||||
tile.IsPending = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -65,86 +64,68 @@ namespace MapControl
|
|||
/// </summary>
|
||||
public static TimeSpan MaxCacheExpiration { get; set; } = TimeSpan.FromDays(10);
|
||||
|
||||
/// <summary>
|
||||
/// Reports tile loading process as double value between 0 and 1.
|
||||
/// </summary>
|
||||
public IProgress<double> Progress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The current TileSource, passed to the most recent LoadTiles call.
|
||||
/// </summary>
|
||||
public TileSource TileSource { get; private set; }
|
||||
|
||||
private TileQueue unloadedTiles;
|
||||
private int progressTotal;
|
||||
private int progressLoaded;
|
||||
private TileQueue pendingTiles;
|
||||
|
||||
/// <summary>
|
||||
/// Loads all unloaded tiles from the tiles collection.
|
||||
/// If tileSource.UriFormat starts with "http" and cacheName is a non-empty string,
|
||||
/// tile images will be cached in the TileImageLoader's Cache - if that is not null.
|
||||
/// </summary>
|
||||
public Task LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName)
|
||||
public Task LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress)
|
||||
{
|
||||
unloadedTiles?.Cancel();
|
||||
pendingTiles?.Cancel();
|
||||
|
||||
TileSource = tileSource;
|
||||
|
||||
if (tileSource != null)
|
||||
if (tiles != null && tileSource != null)
|
||||
{
|
||||
unloadedTiles = new TileQueue(tiles);
|
||||
pendingTiles = new TileQueue(tiles);
|
||||
|
||||
var numTasks = Math.Min(unloadedTiles.Count, MaxLoadTasks);
|
||||
var numTasks = Math.Min(pendingTiles.Count, MaxLoadTasks);
|
||||
|
||||
if (numTasks > 0)
|
||||
{
|
||||
if (Progress != null)
|
||||
{
|
||||
progressTotal = unloadedTiles.Count;
|
||||
progressLoaded = 0;
|
||||
Progress.Report(0d);
|
||||
}
|
||||
|
||||
if (Cache == null || tileSource.UriTemplate == null || !tileSource.UriTemplate.StartsWith("http"))
|
||||
{
|
||||
cacheName = null; // no tile caching
|
||||
}
|
||||
|
||||
return Task.WhenAll(Enumerable.Range(0, numTasks).Select(
|
||||
_ => Task.Run(() => LoadPendingTiles(unloadedTiles, tileSource, cacheName))));
|
||||
}
|
||||
}
|
||||
var progressLoaded = 0;
|
||||
var progressTotal = 0;
|
||||
|
||||
if (Progress != null && progressLoaded < progressTotal)
|
||||
{
|
||||
Progress.Report(1d);
|
||||
if (progress != null)
|
||||
{
|
||||
progressTotal = pendingTiles.Count;
|
||||
progress.Report(0d);
|
||||
}
|
||||
|
||||
async Task LoadPendingTiles()
|
||||
{
|
||||
while (pendingTiles.TryDequeue(out var tile))
|
||||
{
|
||||
try
|
||||
{
|
||||
await LoadTile(tile, tileSource, cacheName).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"TileImageLoader: {tile.ZoomLevel}/{tile.Column}/{tile.Row}: {ex.Message}");
|
||||
}
|
||||
|
||||
if (progress != null && !pendingTiles.IsCanceled)
|
||||
{
|
||||
Interlocked.Increment(ref progressLoaded);
|
||||
|
||||
progress.Report((double)progressLoaded / progressTotal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Task.WhenAll(Enumerable.Range(0, numTasks).Select(_ => Task.Run(LoadPendingTiles)));
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task LoadPendingTiles(TileQueue tileQueue, TileSource tileSource, string cacheName)
|
||||
{
|
||||
while (tileQueue.TryDequeue(out var tile))
|
||||
{
|
||||
try
|
||||
{
|
||||
await LoadTile(tile, tileSource, cacheName).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"TileImageLoader: {tile.ZoomLevel}/{tile.Column}/{tile.Row}: {ex.Message}");
|
||||
}
|
||||
|
||||
if (Progress != null && !tileQueue.IsCanceled)
|
||||
{
|
||||
Interlocked.Increment(ref progressLoaded);
|
||||
|
||||
Progress.Report((double)progressLoaded / progressTotal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Task LoadTile(Tile tile, TileSource tileSource, string cacheName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(cacheName))
|
||||
|
|
|
|||
|
|
@ -73,6 +73,7 @@ namespace MapControl
|
|||
|
||||
/// <summary>
|
||||
/// Loads a tile ImageSource asynchronously from GetUri(column, row, zoomLevel).
|
||||
/// This method is called by a TileImageLoader that does not perform caching.
|
||||
/// </summary>
|
||||
public virtual Task<ImageSource> LoadImageAsync(int column, int row, int zoomLevel)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -97,14 +97,16 @@ namespace MapControl
|
|||
return finalSize;
|
||||
}
|
||||
|
||||
protected override Task UpdateTileLayer()
|
||||
protected override Task UpdateTileLayer(bool tileSourceChanged)
|
||||
{
|
||||
// tileSourceChanged is ignored here because it is always false.
|
||||
|
||||
if (ParentMap == null ||
|
||||
!TileMatrixSets.TryGetValue(ParentMap.MapProjection.CrsId, out WmtsTileMatrixSet tileMatrixSet))
|
||||
{
|
||||
Children.Clear();
|
||||
|
||||
return LoadTiles(null); // stop TileImageLoader
|
||||
return LoadTiles(null, null); // stop TileImageLoader
|
||||
}
|
||||
|
||||
if (UpdateChildLayers(tileMatrixSet))
|
||||
|
|
@ -192,7 +194,7 @@ namespace MapControl
|
|||
|
||||
var tiles = ChildLayers.SelectMany(layer => layer.Tiles);
|
||||
|
||||
return TileImageLoader.LoadTiles(tiles, TileSource, cacheName);
|
||||
return LoadTiles(tiles, cacheName);
|
||||
}
|
||||
|
||||
private async void OnLoaded(object sender, RoutedEventArgs e)
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ namespace MapControl
|
|||
|
||||
if (buffer != null && buffer.Length > 0)
|
||||
{
|
||||
await LoadTile(tile, () => ImageLoader.LoadImageAsync(buffer));
|
||||
await LoadTile(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -75,11 +75,7 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
if (!tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, callback))
|
||||
{
|
||||
tile.IsLoaded = false;
|
||||
tcs.TrySetResult();
|
||||
}
|
||||
tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, callback);
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue