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

155 lines
4.9 KiB
C#
Raw Normal View History

2017-08-04 21:38:58 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2021-01-13 21:19:27 +01:00
// © 2021 Clemens Fischer
2017-08-04 21:38:58 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Concurrent;
2019-06-10 17:20:21 +02:00
using System.Collections.Generic;
2019-06-10 22:18:02 +02:00
using System.Diagnostics;
2017-08-04 21:38:58 +02:00
using System.IO;
using System.Linq;
2019-06-10 22:18:02 +02:00
using System.Threading;
2017-08-04 21:38:58 +02:00
using System.Threading.Tasks;
namespace MapControl
{
2021-01-23 17:41:52 +01:00
#if NETFRAMEWORK
static class ConcurrentQueueEx
{
public static void Clear<T>(this ConcurrentQueue<T> tileQueue)
{
while (tileQueue.TryDequeue(out _)) ;
}
}
#endif
2017-08-04 21:38:58 +02:00
/// <summary>
/// Loads and optionally caches map tile images for a MapTileLayer.
/// </summary>
public partial class TileImageLoader : ITileImageLoader
{
/// <summary>
2020-04-13 09:12:21 +02:00
/// Maximum number of parallel tile loading tasks. The default value is 4.
2017-08-04 21:38:58 +02:00
/// </summary>
2020-04-13 09:12:21 +02:00
public static int MaxLoadTasks { get; set; } = 4;
2017-08-04 21:38:58 +02:00
/// <summary>
/// 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>
public static TimeSpan DefaultCacheExpiration { get; set; } = TimeSpan.FromDays(1);
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);
2021-01-23 17:41:52 +01:00
private readonly ConcurrentQueue<Tile> tileQueue = new ConcurrentQueue<Tile>();
2021-01-22 23:00:58 +01:00
private TileSource tileSource;
private string cacheName;
2019-06-10 22:18:02 +02:00
private int taskCount;
2017-08-04 21:38:58 +02:00
/// <summary>
/// Loads all pending tiles from the tiles collection.
2021-01-22 23:00:58 +01:00
/// If source.UriFormat starts with "http" and cache is a non-empty string,
2020-10-21 22:18:16 +02:00
/// tile images will be cached in the TileImageLoader's Cache (if that is not null).
/// </summary>
2021-01-22 23:00:58 +01:00
public void LoadTiles(IEnumerable<Tile> tiles, TileSource source, string cache)
2017-08-04 21:38:58 +02:00
{
2019-06-10 18:08:34 +02:00
tileQueue.Clear();
2019-06-10 22:18:02 +02:00
tiles = tiles.Where(tile => tile.Pending);
2019-06-10 22:18:02 +02:00
2021-01-23 09:52:45 +01:00
tileSource = source;
cacheName = null;
if (tiles.Any() && tileSource != null)
{
2021-01-23 17:41:52 +01:00
if (!string.IsNullOrEmpty(cache) &&
Cache != null &&
tileSource.UriFormat != null &&
tileSource.UriFormat.StartsWith("http"))
2021-01-23 09:52:45 +01:00
{
cacheName = cache;
}
2021-01-23 17:41:52 +01:00
foreach (var tile in tiles)
{
tileQueue.Enqueue(tile);
}
2020-10-21 22:18:16 +02:00
while (taskCount < Math.Min(tileQueue.Count, MaxLoadTasks))
{
Interlocked.Increment(ref taskCount);
2020-11-21 14:51:57 +01:00
Task.Run(LoadTilesFromQueueAsync);
2020-10-21 22:18:16 +02:00
}
2019-06-10 22:18:02 +02:00
}
}
private async Task LoadTilesFromQueueAsync()
2019-06-10 22:18:02 +02:00
{
2021-01-22 23:00:58 +01:00
// tileSource or cacheName may change after dequeuing a tile
var source = tileSource;
var cache = cacheName;
2020-10-21 22:18:16 +02:00
while (tileQueue.TryDequeue(out Tile tile))
2019-06-10 22:18:02 +02:00
{
2020-10-21 22:18:16 +02:00
tile.Pending = false;
try
{
2021-01-23 17:41:52 +01:00
await LoadTileAsync(tile, source, cache).ConfigureAwait(false);
2020-10-21 22:18:16 +02:00
}
catch (Exception ex)
2017-08-04 21:38:58 +02:00
{
2020-10-21 22:18:16 +02:00
Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message);
2017-08-04 21:38:58 +02:00
}
}
2019-06-10 22:18:02 +02:00
Interlocked.Decrement(ref taskCount);
2017-08-04 21:38:58 +02:00
}
2021-01-23 17:41:52 +01:00
private static async Task LoadTileAsync(Tile tile, TileSource tileSource, string cacheName)
{
2021-01-23 17:41:52 +01:00
if (cacheName == null)
{
2021-01-23 17:41:52 +01:00
await LoadTileAsync(tile, tileSource).ConfigureAwait(false);
}
else
{
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
2021-01-23 17:41:52 +01:00
if (uri != null)
{
2021-01-23 17:41:52 +01:00
var extension = Path.GetExtension(uri.LocalPath);
2021-01-23 17:41:52 +01:00
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
{
extension = ".jpg";
}
var cacheKey = string.Format("{0}/{1}/{2}/{3}{4}", cacheName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
2021-01-23 17:41:52 +01:00
await LoadCachedTileAsync(tile, uri, cacheKey).ConfigureAwait(false);
}
}
2017-08-04 21:38:58 +02:00
}
private static DateTime GetExpiration(TimeSpan? maxAge)
2017-08-04 21:38:58 +02:00
{
2021-01-07 22:52:41 +01:00
if (!maxAge.HasValue)
{
maxAge = DefaultCacheExpiration;
}
else if (maxAge.Value > MaxCacheExpiration)
{
maxAge = MaxCacheExpiration;
}
return DateTime.UtcNow.Add(maxAge.Value);
2017-08-04 21:38:58 +02:00
}
}
}