2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2021-01-13 21:19:27 +01:00
|
|
|
|
// © 2021 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.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public partial class TileImageLoader
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2019-07-18 21:09:54 +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>
|
2021-07-02 21:30:38 +02:00
|
|
|
|
/// An ObjectCache instance used to cache tile image data. The default value is MemoryCache.Default.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static ObjectCache Cache { get; set; } = MemoryCache.Default;
|
|
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
|
2020-10-21 22:18:16 +02:00
|
|
|
|
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-07-02 15:57:01 +02:00
|
|
|
|
var cacheItem = Cache.Get(cacheKey) as Tuple<byte[], DateTime>;
|
|
|
|
|
|
var buffer = cacheItem?.Item1;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2021-07-02 15:57:01 +02:00
|
|
|
|
if (cacheItem == null || cacheItem.Item2 < DateTime.UtcNow)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2020-09-25 15:02:41 +02:00
|
|
|
|
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
|
2019-07-13 08:49:49 +02:00
|
|
|
|
|
|
|
|
|
|
if (response != null) // download succeeded
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2021-07-02 15:57:01 +02:00
|
|
|
|
buffer = response.Buffer; // may be null or empty when no tile available, but still be cached
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2021-07-02 15:57:01 +02:00
|
|
|
|
cacheItem = Tuple.Create(buffer, GetExpiration(response.MaxAge));
|
2021-06-30 17:56:02 +02:00
|
|
|
|
|
2021-07-02 15:57:01 +02:00
|
|
|
|
Cache.Set(cacheKey, cacheItem, new CacheItemPolicy { AbsoluteExpiration = cacheItem.Item2 });
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-07-06 18:49:48 +02:00
|
|
|
|
//else System.Diagnostics.Debug.WriteLine($"Cached: {cacheKey}");
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
if (buffer != null && buffer.Length > 0)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2020-09-24 23:33:23 +02:00
|
|
|
|
var image = await ImageLoader.LoadImageAsync(buffer).ConfigureAwait(false);
|
|
|
|
|
|
|
2021-06-28 23:35:03 +02:00
|
|
|
|
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-21 22:18:16 +02:00
|
|
|
|
private static async Task LoadTileAsync(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
|
|
|
|
|
2021-06-28 23:35:03 +02:00
|
|
|
|
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|