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

105 lines
3.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
// © 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;
using System.Windows.Media;
2017-08-04 21:38:58 +02:00
namespace MapControl
{
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-10 19:30:47 +02:00
private async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
2017-08-04 21:38:58 +02:00
{
DateTime expiration;
var cacheBuffer = GetCachedImage(cacheKey, out expiration);
2017-08-04 21:38:58 +02:00
if (cacheBuffer == null || expiration < DateTime.UtcNow)
2017-08-04 21:38:58 +02:00
{
var result = await ImageLoader.LoadHttpStreamAsync(uri);
if (result != null) // download succeeded
{
cacheBuffer = null; // discard cached image
if (result.Item1 != null) // tile image available
{
using (var stream = result.Item1)
{
LoadTileImage(tile, stream);
SetCachedImage(cacheKey, stream, GetExpiration(result.Item2));
}
}
2017-08-04 21:38:58 +02:00
}
}
if (cacheBuffer != null) // cached image not expired or download failed
2017-08-04 21:38:58 +02:00
{
using (var stream = new MemoryStream(cacheBuffer))
2017-08-04 21:38:58 +02:00
{
LoadTileImage(tile, stream);
2017-08-04 21:38:58 +02:00
}
}
}
private async Task LoadTileImageAsync(Tile tile, TileSource tileSource)
{
SetTileImage(tile, await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
}
private void LoadTileImage(Tile tile, Stream stream)
2017-08-04 21:38:58 +02:00
{
SetTileImage(tile, ImageLoader.LoadImage(stream));
}
2017-08-04 21:38:58 +02:00
private void SetTileImage(Tile tile, ImageSource imageSource)
{
tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(imageSource));
2017-08-04 21:38:58 +02:00
}
private static byte[] GetCachedImage(string cacheKey, out DateTime expiration)
{
var buffer = Cache.Get(cacheKey) as byte[];
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;
}
return buffer;
}
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 });
}
}
}