2025-02-27 18:46:32 +01:00
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
2024-02-04 00:26:59 +01:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2025-03-31 21:33:52 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-02-04 00:26:59 +01:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
using System;
|
2019-06-10 17:20:21 +02:00
|
|
|
|
using System.Collections.Generic;
|
2024-08-30 16:37:40 +02:00
|
|
|
|
using System.IO;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
using System.Linq;
|
2025-08-20 16:44:48 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2024-05-19 17:24:18 +02:00
|
|
|
|
public interface ITileImageLoader
|
|
|
|
|
|
{
|
2025-11-13 13:36:28 +01:00
|
|
|
|
/// <summary>
|
2025-11-13 17:06:33 +01:00
|
|
|
|
/// Loads all pending tiles from the tiles collection. Tile image caching is enabled
|
|
|
|
|
|
/// when cacheName is a non-empty string and tiles are loaded from http or https Uris.
|
2025-11-13 13:36:28 +01:00
|
|
|
|
/// </summary>
|
2025-11-13 15:32:01 +01:00
|
|
|
|
void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress);
|
2025-08-21 21:25:04 +02:00
|
|
|
|
|
2025-11-13 13:36:28 +01:00
|
|
|
|
/// <summary>
|
2025-11-13 17:06:33 +01:00
|
|
|
|
/// Terminates all running tile loading tasks.
|
2025-11-13 13:36:28 +01:00
|
|
|
|
/// </summary>
|
2025-08-21 21:25:04 +02:00
|
|
|
|
void CancelLoadTiles();
|
2024-05-19 17:24:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-13 13:36:28 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Loads and optionally caches map tile images for a MapTilePyramidLayer.
|
|
|
|
|
|
/// </summary>
|
2025-09-10 20:09:12 +02:00
|
|
|
|
public class TileImageLoader : ITileImageLoader
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2025-08-22 11:06:37 +02:00
|
|
|
|
private static ILogger logger;
|
2025-09-19 18:48:05 +02:00
|
|
|
|
private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(TileImageLoader));
|
2025-08-22 11:06:37 +02:00
|
|
|
|
|
2024-08-15 19:46:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Default folder path where a persistent cache implementation may save data, i.e. "C:\ProgramData\MapControl\TileCache".
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static string DefaultCacheFolder =>
|
2025-02-20 11:45:45 +01:00
|
|
|
|
#if UWP
|
|
|
|
|
|
Path.Combine(Windows.Storage.ApplicationData.Current.LocalCacheFolder.Path, "TileCache");
|
|
|
|
|
|
#else
|
2024-08-30 16:37:40 +02:00
|
|
|
|
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache");
|
2025-02-20 11:45:45 +01:00
|
|
|
|
#endif
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// <summary>
|
2024-02-03 20:53:32 +01:00
|
|
|
|
/// An IDistributedCache implementation used to cache tile images.
|
2024-02-04 00:26:59 +01:00
|
|
|
|
/// The default value is a MemoryDistributedCache instance.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2024-02-04 00:26:59 +01:00
|
|
|
|
public static IDistributedCache Cache { get; set; } = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-07-16 20:13:52 +02:00
|
|
|
|
/// Default expiration time for cached tile images. Used when no expiration time
|
|
|
|
|
|
/// was transmitted on download. The default value is one day.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2019-07-16 20:13:52 +02:00
|
|
|
|
public static TimeSpan DefaultCacheExpiration { get; set; } = TimeSpan.FromDays(1);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2025-02-18 17:22:14 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Minimum expiration time for cached tile images. A transmitted expiration time
|
|
|
|
|
|
/// that falls below this value is ignored. The default value is TimeSpan.Zero.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static TimeSpan MinCacheExpiration { get; set; } = TimeSpan.Zero;
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// <summary>
|
2021-01-07 22:52:41 +01:00
|
|
|
|
/// Maximum expiration time for cached tile images. A transmitted expiration time
|
|
|
|
|
|
/// that exceeds this value is ignored. The default value is ten days.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2021-01-07 22:52:41 +01:00
|
|
|
|
public static TimeSpan MaxCacheExpiration { get; set; } = TimeSpan.FromDays(10);
|
2019-06-13 21:38:01 +02:00
|
|
|
|
|
2024-02-03 20:53:32 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maximum number of parallel tile loading tasks. The default value is 4.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static int MaxLoadTasks { get; set; } = 4;
|
|
|
|
|
|
|
2025-11-13 15:32:01 +01:00
|
|
|
|
private readonly Queue<Tile> tileQueue = new();
|
2025-08-26 11:40:20 +02:00
|
|
|
|
private int tileCount;
|
2025-08-21 21:25:04 +02:00
|
|
|
|
private int taskCount;
|
|
|
|
|
|
|
2025-11-13 15:32:01 +01:00
|
|
|
|
public void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2025-11-13 17:06:33 +01:00
|
|
|
|
if (Cache == null)
|
2025-08-21 21:25:04 +02:00
|
|
|
|
{
|
|
|
|
|
|
cacheName = null; // disable caching
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-25 20:41:33 +02:00
|
|
|
|
lock (tileQueue)
|
|
|
|
|
|
{
|
|
|
|
|
|
tileQueue.Clear();
|
2025-08-21 21:25:04 +02:00
|
|
|
|
|
2025-08-25 20:41:33 +02:00
|
|
|
|
foreach (var tile in tiles.Where(tile => tile.IsPending))
|
|
|
|
|
|
{
|
|
|
|
|
|
tileQueue.Enqueue(tile);
|
|
|
|
|
|
}
|
2025-08-20 22:46:03 +02:00
|
|
|
|
|
2025-08-26 11:40:20 +02:00
|
|
|
|
tileCount = tileQueue.Count;
|
|
|
|
|
|
|
2025-08-25 20:41:33 +02:00
|
|
|
|
var maxTasks = Math.Min(tileCount, MaxLoadTasks);
|
2025-08-22 22:41:57 +02:00
|
|
|
|
|
2025-08-25 20:41:33 +02:00
|
|
|
|
while (taskCount < maxTasks)
|
|
|
|
|
|
{
|
|
|
|
|
|
taskCount++;
|
|
|
|
|
|
Logger?.LogDebug("Task count: {count}", taskCount);
|
2025-08-21 21:25:04 +02:00
|
|
|
|
|
2025-09-08 23:56:57 +02:00
|
|
|
|
_ = Task.Run(() => LoadTilesFromQueue(tileSource, cacheName, progress));
|
2025-08-25 20:41:33 +02:00
|
|
|
|
}
|
2025-08-21 21:25:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void CancelLoadTiles()
|
|
|
|
|
|
{
|
2025-08-25 20:41:33 +02:00
|
|
|
|
lock (tileQueue)
|
|
|
|
|
|
{
|
|
|
|
|
|
tileQueue.Clear();
|
2025-08-26 11:40:20 +02:00
|
|
|
|
tileCount = 0;
|
2025-08-25 20:41:33 +02:00
|
|
|
|
}
|
2025-08-21 21:25:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-13 15:32:01 +01:00
|
|
|
|
private async Task LoadTilesFromQueue(TileSource tileSource, string cacheName, IProgress<double> progress)
|
2025-08-21 21:25:04 +02:00
|
|
|
|
{
|
2025-11-13 15:32:01 +01:00
|
|
|
|
bool TryDequeueTile(out Tile tile)
|
2025-08-21 21:25:04 +02:00
|
|
|
|
{
|
2025-11-05 09:35:52 +01:00
|
|
|
|
lock (tileQueue)
|
2025-08-25 20:41:33 +02:00
|
|
|
|
{
|
2025-11-05 09:35:52 +01:00
|
|
|
|
if (tileQueue.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
tile = tileQueue.Dequeue();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
taskCount--;
|
|
|
|
|
|
Logger?.LogDebug("Task count: {count}", taskCount);
|
2025-08-25 20:41:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 09:35:52 +01:00
|
|
|
|
tile = null;
|
2025-11-04 21:51:03 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-13 15:32:01 +01:00
|
|
|
|
while (TryDequeueTile(out Tile tile))
|
2025-11-04 21:51:03 +01:00
|
|
|
|
{
|
2025-08-25 20:41:33 +02:00
|
|
|
|
tile.IsPending = false;
|
2025-11-04 21:51:03 +01:00
|
|
|
|
|
|
|
|
|
|
Logger?.LogDebug("Thread {thread,2}: Loading tile ({zoom}/{column}/{row})",
|
|
|
|
|
|
Environment.CurrentManagedThreadId, tile.ZoomLevel, tile.Column, tile.Row);
|
2025-08-21 21:25:04 +02:00
|
|
|
|
|
2025-08-21 08:23:55 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-11-13 17:06:33 +01:00
|
|
|
|
// Pass image loading callbacks to platform-specific method
|
2025-09-10 23:10:32 +02:00
|
|
|
|
// tile.LoadImageAsync(Func<Task<ImageSource>>) for completion in the UI thread.
|
2024-08-31 11:55:59 +02:00
|
|
|
|
|
2025-11-13 17:06:33 +01:00
|
|
|
|
var uri = tileSource.GetUri(tile.ZoomLevel, tile.Column, tile.Row);
|
|
|
|
|
|
|
|
|
|
|
|
if (uri == null)
|
2025-09-10 23:10:32 +02:00
|
|
|
|
{
|
|
|
|
|
|
await tile.LoadImageAsync(() => tileSource.LoadImageAsync(tile.ZoomLevel, tile.Column, tile.Row)).ConfigureAwait(false);
|
|
|
|
|
|
}
|
2025-11-13 17:06:33 +01:00
|
|
|
|
else if (uri.Scheme != "http" && uri.Scheme != "https" || string.IsNullOrEmpty(cacheName))
|
|
|
|
|
|
{
|
|
|
|
|
|
await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(uri)).ConfigureAwait(false);
|
|
|
|
|
|
}
|
2025-09-10 23:10:32 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-11-13 17:06:33 +01:00
|
|
|
|
var buffer = await LoadCachedBuffer(tile, uri, cacheName).ConfigureAwait(false);
|
2025-01-20 19:28:59 +01:00
|
|
|
|
|
2025-11-13 17:06:33 +01:00
|
|
|
|
if (buffer?.Length > 0)
|
2025-09-10 23:10:32 +02:00
|
|
|
|
{
|
2025-11-13 17:06:33 +01:00
|
|
|
|
await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
2025-09-10 23:10:32 +02:00
|
|
|
|
}
|
2025-01-17 17:21:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-10 23:10:32 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "Failed loading tile {zoom}/{column}/{row}", tile.ZoomLevel, tile.Column, tile.Row);
|
|
|
|
|
|
}
|
2025-11-04 21:51:03 +01:00
|
|
|
|
|
|
|
|
|
|
progress?.Report(1d - (double)tileQueue.Count / tileCount);
|
2025-01-17 17:21:05 +01:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-13 15:32:01 +01:00
|
|
|
|
private static async Task<byte[]> LoadCachedBuffer(Tile tile, Uri uri, string cacheName)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2025-10-23 18:59:02 +02:00
|
|
|
|
var extension = Path.GetExtension(uri.LocalPath).ToLower();
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
2025-10-23 18:59:02 +02:00
|
|
|
|
if (string.IsNullOrEmpty(extension) || extension.Equals(".jpeg"))
|
2021-01-07 22:52:41 +01:00
|
|
|
|
{
|
2024-02-03 20:53:32 +01:00
|
|
|
|
extension = ".jpg";
|
2021-01-07 22:52:41 +01:00
|
|
|
|
}
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
2024-04-16 19:55:59 +02:00
|
|
|
|
var cacheKey = $"{cacheName}/{tile.ZoomLevel}/{tile.Column}/{tile.Row}{extension}";
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
2025-01-16 19:57:00 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-09-10 23:10:32 +02:00
|
|
|
|
var cachedBuffer = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
if (cachedBuffer != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return cachedBuffer;
|
|
|
|
|
|
}
|
2025-08-20 16:44:48 +02:00
|
|
|
|
}
|
2025-01-16 19:57:00 +01:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-08-20 16:44:48 +02:00
|
|
|
|
Logger?.LogError(ex, "Cache.GetAsync({cacheKey})", cacheKey);
|
2025-01-16 19:57:00 +01:00
|
|
|
|
}
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
2025-09-10 23:10:32 +02:00
|
|
|
|
(var buffer, var maxAge) = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
|
2024-02-03 20:53:32 +01:00
|
|
|
|
|
2025-09-10 23:10:32 +02:00
|
|
|
|
if (buffer != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2024-02-03 20:53:32 +01:00
|
|
|
|
{
|
2025-09-10 23:10:32 +02:00
|
|
|
|
var options = new DistributedCacheEntryOptions
|
2025-01-16 19:57:00 +01:00
|
|
|
|
{
|
2025-09-10 23:10:32 +02:00
|
|
|
|
AbsoluteExpirationRelativeToNow =
|
|
|
|
|
|
!maxAge.HasValue ? DefaultCacheExpiration
|
|
|
|
|
|
: maxAge.Value < MinCacheExpiration ? MinCacheExpiration
|
|
|
|
|
|
: maxAge.Value > MaxCacheExpiration ? MaxCacheExpiration
|
|
|
|
|
|
: maxAge.Value
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await Cache.SetAsync(cacheKey, buffer, options).ConfigureAwait(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "Cache.SetAsync({cacheKey})", cacheKey);
|
2025-01-16 19:57:00 +01:00
|
|
|
|
}
|
2024-02-09 16:10:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 19:57:00 +01:00
|
|
|
|
return buffer;
|
2024-02-09 16:10:55 +01:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|