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

140 lines
4.6 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;
2021-07-05 00:03:44 +02:00
using System.Globalization;
2017-08-04 21:38:58 +02:00
using System.IO;
using System.Linq;
2017-08-04 21:38:58 +02:00
using System.Threading.Tasks;
namespace MapControl
{
/// <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-07-06 01:01:22 +02:00
/// <summary>
/// The current TileSource, passed to the most recent LoadTiles call.
/// </summary>
2021-07-05 00:03:44 +02:00
public TileSource TileSource { get; private set; }
2021-07-06 18:44:41 +02:00
private ConcurrentStack<Tile> pendingTiles;
2017-08-04 21:38:58 +02:00
/// <summary>
/// Loads all pending tiles from the tiles collection.
2021-07-06 01:01:22 +02:00
/// 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>
2021-07-05 14:51:28 +02:00
public Task LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName)
2017-08-04 21:38:58 +02:00
{
2021-11-22 23:07:07 +01:00
pendingTiles?.Clear(); // stop processing the current queue
TileSource = tileSource;
if (tileSource == null)
{
return Task.CompletedTask;
}
2021-07-05 14:51:28 +02:00
2021-07-06 18:44:41 +02:00
pendingTiles = new ConcurrentStack<Tile>(tiles.Where(tile => tile.Pending).Reverse());
2021-07-05 14:51:28 +02:00
2021-11-22 23:07:07 +01:00
var numTasks = Math.Min(pendingTiles.Count, MaxLoadTasks);
2019-06-10 22:18:02 +02:00
2021-11-22 23:07:07 +01:00
if (numTasks < 1)
{
2021-07-06 01:01:22 +02:00
return Task.CompletedTask;
}
2021-07-06 22:58:03 +02:00
if (Cache == null || tileSource.UriFormat == null || !tileSource.UriFormat.StartsWith("http"))
2021-07-06 01:01:22 +02:00
{
cacheName = null; // no tile caching
2019-06-10 22:18:02 +02:00
}
2021-07-05 14:51:28 +02:00
2021-11-22 23:07:07 +01:00
var tasks = Enumerable.Range(0, numTasks)
.Select(_ => Task.Run(() => LoadPendingTiles(pendingTiles, tileSource, cacheName)));
2021-07-06 01:01:22 +02:00
return Task.WhenAll(tasks);
2019-06-10 22:18:02 +02:00
}
2021-11-22 23:07:07 +01:00
private static async Task LoadPendingTiles(ConcurrentStack<Tile> pendingTiles, TileSource tileSource, string cacheName)
2019-06-10 22:18:02 +02:00
{
2021-07-06 18:44:41 +02:00
while (pendingTiles.TryPop(out var tile))
2019-06-10 22:18:02 +02:00
{
2020-10-21 22:18:16 +02:00
tile.Pending = false;
try
{
2021-11-22 23:07:07 +01:00
await LoadTile(tile, tileSource, cacheName).ConfigureAwait(false);
2020-10-21 22:18:16 +02:00
}
catch (Exception ex)
2017-08-04 21:38:58 +02:00
{
2021-07-06 18:44:41 +02:00
Debug.WriteLine($"TileImageLoader: {tile.ZoomLevel}/{tile.XIndex}/{tile.Y}: {ex.Message}");
2017-08-04 21:38:58 +02:00
}
}
}
2021-11-22 23:07:07 +01:00
private static Task LoadTile(Tile tile, TileSource tileSource, string cacheName)
{
2021-07-06 22:58:03 +02:00
if (string.IsNullOrEmpty(cacheName))
{
2021-11-22 23:07:07 +01:00
return LoadTile(tile, tileSource);
2021-01-23 17:41:52 +01:00
}
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
if (uri == null)
{
return Task.CompletedTask;
}
2021-01-23 17:41:52 +01:00
var extension = Path.GetExtension(uri.LocalPath);
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
{
extension = ".jpg";
}
2021-07-05 00:03:44 +02:00
var cacheKey = string.Format(CultureInfo.InvariantCulture,
"{0}/{1}/{2}/{3}{4}", cacheName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
2021-11-22 23:07:07 +01:00
return LoadCachedTile(tile, uri, cacheKey);
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
}
}
}