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

156 lines
5.4 KiB
C#
Raw Normal View History

2017-08-04 21:38:58 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
2017-08-04 21:38:58 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MapControl
{
/// <summary>
/// Loads and optionally caches map tile images for a MapTileLayer.
/// </summary>
public partial class TileImageLoader : ITileImageLoader
{
/// <summary>
/// Maximum number of parallel tile loading tasks. The default value is 4.
2017-08-04 21:38:58 +02:00
/// </summary>
public static int MaxLoadTasks { get; set; } = 4;
2017-08-04 21:38:58 +02:00
/// <summary>
/// Minimum expiration time for cached tile images. The default value is one hour.
/// </summary>
public static TimeSpan MinCacheExpiration { get; set; } = TimeSpan.FromHours(1);
2017-08-04 21:38:58 +02:00
/// <summary>
/// Maximum expiration time for cached tile images. The default value is one week.
/// </summary>
public static TimeSpan MaxCacheExpiration { get; set; } = TimeSpan.FromDays(7);
/// <summary>
/// Default expiration time for cached tile images. Used when no expiration time
/// was transmitted on download. The default value is one day.
/// </summary>
public static TimeSpan DefaultCacheExpiration { get; set; } = TimeSpan.FromDays(1);
2017-08-04 21:38:58 +02:00
/// <summary>
/// Format string for creating cache keys from the SourceName property of a TileSource,
/// the ZoomLevel, XIndex, and Y properties of a Tile, and the image file extension.
/// The default value is "{0};{1};{2};{3}{4}".
/// </summary>
public static string CacheKeyFormat { get; set; } = "{0};{1};{2};{3}{4}";
private readonly ConcurrentStack<Tile> pendingTiles = new ConcurrentStack<Tile>();
private int taskCount;
/// <summary>
/// Loads all pending tiles from the Tiles collection of a MapTileLayer by running up to MaxLoadTasks parallel Tasks.
/// If the TileSource's SourceName is non-empty and its UriFormat starts with "http", tile images are cached in the
/// TileImageLoader's Cache.
/// </summary>
public void LoadTilesAsync(MapTileLayer tileLayer)
2017-08-04 21:38:58 +02:00
{
pendingTiles.Clear();
var tileSource = tileLayer.TileSource;
var sourceName = tileLayer.SourceName;
var tiles = tileLayer.Tiles.Where(t => t.Pending);
if (tileSource != null && tiles.Any())
{
pendingTiles.PushRange(tiles.Reverse().ToArray());
Func<Tile, Task> loadFunc;
2017-08-04 21:38:58 +02:00
if (Cache != null && !string.IsNullOrEmpty(sourceName) &&
tileSource.UriFormat != null && tileSource.UriFormat.StartsWith("http"))
{
loadFunc = tile => LoadCachedTileImageAsync(tile, tileSource, sourceName);
2017-08-04 21:38:58 +02:00
}
else
{
loadFunc = tile => LoadTileImageAsync(tile, tileSource);
2017-08-04 21:38:58 +02:00
}
var maxTasks = Math.Min(pendingTiles.Count, MaxLoadTasks);
2017-08-04 21:38:58 +02:00
while (taskCount < maxTasks)
2017-08-04 21:38:58 +02:00
{
Interlocked.Increment(ref taskCount);
var task = Task.Run(() => LoadTilesAsync(loadFunc)); // do not await
2017-08-04 21:38:58 +02:00
}
//Debug.WriteLine("{0}: {1} tasks", Environment.CurrentManagedThreadId, taskCount);
2017-08-04 21:38:58 +02:00
}
}
private async Task LoadTilesAsync(Func<Tile, Task> loadTileImageFunc)
2017-08-04 21:38:58 +02:00
{
Tile tile;
while (pendingTiles.TryPop(out tile))
{
tile.Pending = false;
try
{
await loadTileImageFunc(tile);
}
catch (Exception ex)
{
Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message);
}
}
2017-08-04 21:38:58 +02:00
Interlocked.Decrement(ref taskCount);
//Debug.WriteLine("{0}: {1} tasks", Environment.CurrentManagedThreadId, taskCount);
}
2017-08-04 21:38:58 +02:00
private async Task LoadCachedTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
{
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
2017-08-04 21:38:58 +02:00
if (uri != null)
{
var extension = Path.GetExtension(uri.LocalPath);
2017-08-04 21:38:58 +02:00
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
2017-08-04 21:38:58 +02:00
{
extension = ".jpg";
2017-08-04 21:38:58 +02:00
}
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
await LoadCachedTileImageAsync(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
{
var expiration = DefaultCacheExpiration;
if (maxAge.HasValue)
2017-08-04 21:38:58 +02:00
{
expiration = maxAge.Value;
2017-08-04 21:38:58 +02:00
if (expiration < MinCacheExpiration)
2017-08-04 21:38:58 +02:00
{
expiration = MinCacheExpiration;
2017-08-04 21:38:58 +02:00
}
else if (expiration > MaxCacheExpiration)
2017-08-04 21:38:58 +02:00
{
expiration = MaxCacheExpiration;
2017-08-04 21:38:58 +02:00
}
}
return DateTime.UtcNow.Add(expiration);
}
}
}