2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2017 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
2017-09-05 20:57:17 +02:00
|
|
|
|
using System.Net;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using Windows.Web.Http;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Loads and optionally caches map tile images for a MapTileLayer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public partial class TileImageLoader : ITileImageLoader
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Minimum expiration time for cached tile images. The default value is one hour.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static TimeSpan MinimumCacheExpiration { get; set; } = TimeSpan.FromHours(1);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maximum expiration time for cached tile images. The default value is one week.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static TimeSpan MaximumCacheExpiration { get; set; } = TimeSpan.FromDays(7);
|
|
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
|
2017-09-05 20:57:17 +02:00
|
|
|
|
public async Task 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())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Cache == null || string.IsNullOrEmpty(sourceName) ||
|
|
|
|
|
|
tileSource.UriFormat == null || !tileSource.UriFormat.StartsWith("http"))
|
|
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
// no caching, load tile images directly
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
foreach (var tile in tiles)
|
|
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
await LoadTileImageAsync(tileSource, tile);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
pendingTiles.PushRange(tiles.Reverse().ToArray());
|
|
|
|
|
|
|
2017-09-05 20:57:17 +02:00
|
|
|
|
while (taskCount < Math.Min(pendingTiles.Count, ServicePointManager.DefaultConnectionLimit))
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
Interlocked.Increment(ref taskCount);
|
|
|
|
|
|
|
|
|
|
|
|
var task = Task.Run(async () => // do not await
|
|
|
|
|
|
{
|
|
|
|
|
|
await LoadPendingTilesAsync(tileSource, sourceName); // run multiple times in parallel
|
|
|
|
|
|
|
|
|
|
|
|
Interlocked.Decrement(ref taskCount);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-05 20:57:17 +02:00
|
|
|
|
private async Task LoadTileImageAsync(TileSource tileSource, Tile tile)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
tile.Pending = false;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
var imageSource = await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
if (imageSource != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
tile.SetImage(imageSource);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task LoadPendingTilesAsync(TileSource tileSource, string sourceName)
|
|
|
|
|
|
{
|
|
|
|
|
|
Tile tile;
|
|
|
|
|
|
|
|
|
|
|
|
while (pendingTiles.TryPop(out tile))
|
|
|
|
|
|
{
|
|
|
|
|
|
tile.Pending = false;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
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 LoadTileImageAsync(tile, uri, cacheKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static DateTime GetExpiration(HttpResponseMessage response)
|
|
|
|
|
|
{
|
|
|
|
|
|
var expiration = DefaultCacheExpiration;
|
|
|
|
|
|
var headers = response.Headers;
|
|
|
|
|
|
|
|
|
|
|
|
if (headers.CacheControl != null && headers.CacheControl.MaxAge.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
expiration = headers.CacheControl.MaxAge.Value;
|
|
|
|
|
|
|
|
|
|
|
|
if (expiration < MinimumCacheExpiration)
|
|
|
|
|
|
{
|
|
|
|
|
|
expiration = MinimumCacheExpiration;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (expiration > MaximumCacheExpiration)
|
|
|
|
|
|
{
|
|
|
|
|
|
expiration = MaximumCacheExpiration;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return DateTime.UtcNow.Add(expiration);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|