2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 Clemens Fischer
|
2017-08-04 21:38:58 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Runtime.Caching;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2018-08-21 23:56:54 +02:00
|
|
|
|
using System.Windows.Media;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public partial class TileImageLoader
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2017-09-06 20:43:46 +02:00
|
|
|
|
/// Default folder path where an ObjectCache instance may save cached data,
|
|
|
|
|
|
/// i.e. C:\ProgramData\MapControl\TileCache
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2017-09-06 20:43:46 +02:00
|
|
|
|
public static string DefaultCacheFolder
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache"); }
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The ObjectCache used to cache tile images. The default is MemoryCache.Default.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static ObjectCache Cache { get; set; } = MemoryCache.Default;
|
|
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
|
2019-07-11 18:00:21 +02:00
|
|
|
|
private static async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
ImageSource image = null;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
DateTime expiration;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
byte[] cacheBuffer;
|
|
|
|
|
|
|
|
|
|
|
|
GetCachedImage(cacheKey, out cacheBuffer, out expiration);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2018-08-09 18:21:16 +02:00
|
|
|
|
if (cacheBuffer == null || expiration < DateTime.UtcNow)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-13 08:49:49 +02:00
|
|
|
|
var response = await ImageLoader.GetHttpResponseAsync(uri, false).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
if (response != null) // download succeeded
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-07-13 08:49:49 +02:00
|
|
|
|
cacheBuffer = null; // discard cached image
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2019-07-13 08:49:49 +02:00
|
|
|
|
if (response.Stream != null) // tile image available
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var stream = response.Stream)
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
image = ImageLoader.LoadImage(stream);
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2019-07-13 08:49:49 +02:00
|
|
|
|
SetCachedImage(cacheKey, stream, GetExpiration(response.MaxAge));
|
2018-08-09 18:21:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-08 23:18:33 +02:00
|
|
|
|
if (cacheBuffer != null) // cached image not expired or download failed
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
image = ImageLoader.LoadImage(cacheBuffer);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetTileImage(tile, image);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-11 18:00:21 +02:00
|
|
|
|
private static async Task LoadTileImageAsync(Tile tile, TileSource tileSource)
|
2018-08-21 23:56:54 +02:00
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
var image = await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel).ConfigureAwait(false);
|
2018-08-21 23:56:54 +02:00
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
if (image != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetTileImage(tile, image);
|
|
|
|
|
|
}
|
2018-08-21 23:56:54 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-11 18:00:21 +02:00
|
|
|
|
private static void SetTileImage(Tile tile, ImageSource image)
|
2018-08-21 23:56:54 +02:00
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-13 00:08:56 +02:00
|
|
|
|
private static void GetCachedImage(string cacheKey, out byte[] buffer, out DateTime expiration)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-13 00:08:56 +02:00
|
|
|
|
buffer = Cache.Get(cacheKey) as byte[];
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
if (buffer != null && buffer.Length >= 16 &&
|
|
|
|
|
|
Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == "EXPIRES:")
|
|
|
|
|
|
{
|
|
|
|
|
|
expiration = new DateTime(BitConverter.ToInt64(buffer, buffer.Length - 8), DateTimeKind.Utc);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
expiration = DateTime.MinValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void SetCachedImage(string cacheKey, MemoryStream stream, DateTime expiration)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.End);
|
|
|
|
|
|
stream.Write(Encoding.ASCII.GetBytes("EXPIRES:"), 0, 8);
|
|
|
|
|
|
stream.Write(BitConverter.GetBytes(expiration.Ticks), 0, 8);
|
|
|
|
|
|
|
|
|
|
|
|
Cache.Set(cacheKey, stream.ToArray(), new CacheItemPolicy { AbsoluteExpiration = expiration });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|