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

150 lines
5.1 KiB
C#
Raw Normal View History

2017-08-04 21:38:58 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2019 Clemens Fischer
2017-08-04 21:38:58 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
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;
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
{
/// <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 and the
/// ZoomLevel, XIndex, and Y properties of a Tile and the image file extension.
/// The default value is "{0}/{1}/{2}/{3}{4}".
2017-08-04 21:38:58 +02:00
/// </summary>
public static string CacheKeyFormat { get; set; } = "{0}/{1}/{2}/{3}{4}";
2017-08-04 21:38:58 +02:00
2019-06-10 18:08:34 +02:00
private readonly TileQueue tileQueue = new TileQueue();
2019-06-10 22:18:02 +02:00
private int taskCount;
2017-08-04 21:38:58 +02:00
public TileSource TileSource { get; set; }
public string SourceName { get; set; }
/// <summary>
2019-06-10 17:20:21 +02:00
/// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks.
/// If the UriFormat of TileSource starts with "http" and SourceName is a non-empty string,
/// tile images will be cached in the TileImageLoader's Cache.
/// </summary>
public void LoadTilesAsync(IEnumerable<Tile> tiles)
2017-08-04 21:38:58 +02:00
{
2019-06-10 18:08:34 +02:00
tileQueue.Clear();
tileQueue.Enqueue(tiles);
2017-08-04 21:38:58 +02:00
var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;
2019-06-10 22:18:02 +02:00
if (newTasks > 0)
{
Interlocked.Add(ref taskCount, newTasks);
2019-06-10 22:18:02 +02:00
while (--newTasks >= 0)
{
Task.Run(() => LoadTilesFromQueueAsync());
}
2019-06-10 22:18:02 +02:00
}
}
private async Task LoadTilesFromQueueAsync()
2019-06-10 22:18:02 +02:00
{
Tile tile;
while (tileQueue.TryDequeue(out tile))
{
try
{
await LoadTileImageAsync(tile, TileSource, SourceName).ConfigureAwait(false);
2017-08-04 21:38:58 +02:00
}
2019-06-10 22:18:02 +02:00
catch (Exception ex)
2017-08-04 21:38:58 +02:00
{
2019-06-11 22:14:58 +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
}
private async Task LoadTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
2017-08-04 21:38:58 +02:00
{
if (tileSource != null)
{
if (Cache != null &&
tileSource.UriFormat != null &&
tileSource.UriFormat.StartsWith("http") &&
!string.IsNullOrEmpty(sourceName))
2017-08-04 21:38:58 +02:00
{
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
if (uri != null)
{
var extension = Path.GetExtension(uri.LocalPath);
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
{
extension = ".jpg";
}
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
await LoadCachedTileImageAsync(tile, uri, cacheKey).ConfigureAwait(false);
}
}
else
{
await LoadTileImageAsync(tile, tileSource).ConfigureAwait(false);
}
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);
}
}
}