mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Version 4.12.2 Improved TileImageLoader.
This commit is contained in:
parent
4531e620ca
commit
26bf0b5005
|
|
@ -7,9 +7,11 @@ using System.Diagnostics;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
#if WINDOWS_UWP
|
#if WINDOWS_UWP
|
||||||
using Windows.Web.Http;
|
using Windows.Web.Http;
|
||||||
|
using Windows.UI.Xaml.Media;
|
||||||
using Windows.UI.Xaml.Media.Imaging;
|
using Windows.UI.Xaml.Media.Imaging;
|
||||||
#else
|
#else
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -22,9 +24,9 @@ namespace MapControl
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static HttpClient HttpClient { get; set; } = new HttpClient();
|
public static HttpClient HttpClient { get; set; } = new HttpClient();
|
||||||
|
|
||||||
public static async Task<BitmapSource> LoadImageAsync(Uri uri)
|
public static async Task<ImageSource> LoadImageAsync(Uri uri)
|
||||||
{
|
{
|
||||||
BitmapSource image = null;
|
ImageSource image = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -49,9 +51,9 @@ namespace MapControl
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<BitmapSource> LoadHttpImageAsync(Uri uri)
|
private static async Task<ImageSource> LoadHttpImageAsync(Uri uri)
|
||||||
{
|
{
|
||||||
BitmapSource image = null;
|
ImageSource image = null;
|
||||||
|
|
||||||
using (var response = await HttpClient.GetAsync(uri))
|
using (var response = await HttpClient.GetAsync(uri))
|
||||||
{
|
{
|
||||||
|
|
@ -59,7 +61,7 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||||
}
|
}
|
||||||
else if (IsTileAvailable(response.Headers))
|
else if (ImageAvailable(response.Headers))
|
||||||
{
|
{
|
||||||
image = await LoadImageAsync(response.Content);
|
image = await LoadImageAsync(response.Content);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,10 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
public interface ITileImageLoader
|
public interface ITileImageLoader
|
||||||
{
|
{
|
||||||
void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName);
|
TileSource TileSource { get; set; }
|
||||||
|
string SourceName { get; set; }
|
||||||
|
|
||||||
|
void LoadTilesAsync(IEnumerable<Tile> tiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -51,7 +54,8 @@ namespace MapControl
|
||||||
new PropertyMetadata(null, (o, e) => ((MapTileLayer)o).TileSourcePropertyChanged()));
|
new PropertyMetadata(null, (o, e) => ((MapTileLayer)o).TileSourcePropertyChanged()));
|
||||||
|
|
||||||
public static readonly DependencyProperty SourceNameProperty = DependencyProperty.Register(
|
public static readonly DependencyProperty SourceNameProperty = DependencyProperty.Register(
|
||||||
nameof(SourceName), typeof(string), typeof(MapTileLayer), new PropertyMetadata(null));
|
nameof(SourceName), typeof(string), typeof(MapTileLayer),
|
||||||
|
new PropertyMetadata(null, (o, e) => ((MapTileLayer)o).TileImageLoader.SourceName = (string)e.NewValue));
|
||||||
|
|
||||||
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
|
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
|
||||||
nameof(Description), typeof(string), typeof(MapTileLayer), new PropertyMetadata(null));
|
nameof(Description), typeof(string), typeof(MapTileLayer), new PropertyMetadata(null));
|
||||||
|
|
@ -71,7 +75,7 @@ namespace MapControl
|
||||||
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayer)o).updateTimer.Interval = (TimeSpan)e.NewValue));
|
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayer)o).updateTimer.Interval = (TimeSpan)e.NewValue));
|
||||||
|
|
||||||
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
|
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
|
||||||
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayer), new PropertyMetadata(true));
|
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayer), new PropertyMetadata(false));
|
||||||
|
|
||||||
public static readonly DependencyProperty MapBackgroundProperty = DependencyProperty.Register(
|
public static readonly DependencyProperty MapBackgroundProperty = DependencyProperty.Register(
|
||||||
nameof(MapBackground), typeof(Brush), typeof(MapTileLayer), new PropertyMetadata(null));
|
nameof(MapBackground), typeof(Brush), typeof(MapTileLayer), new PropertyMetadata(null));
|
||||||
|
|
@ -99,7 +103,7 @@ namespace MapControl
|
||||||
MapPanel.InitMapElement(this);
|
MapPanel.InitMapElement(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITileImageLoader TileImageLoader { get; private set; }
|
public ITileImageLoader TileImageLoader { get; }
|
||||||
|
|
||||||
public TileGrid TileGrid { get; private set; }
|
public TileGrid TileGrid { get; private set; }
|
||||||
|
|
||||||
|
|
@ -271,6 +275,8 @@ namespace MapControl
|
||||||
|
|
||||||
private void TileSourcePropertyChanged()
|
private void TileSourcePropertyChanged()
|
||||||
{
|
{
|
||||||
|
TileImageLoader.TileSource = TileSource;
|
||||||
|
|
||||||
if (TileGrid != null)
|
if (TileGrid != null)
|
||||||
{
|
{
|
||||||
Tiles = new List<Tile>();
|
Tiles = new List<Tile>();
|
||||||
|
|
@ -391,7 +397,7 @@ namespace MapControl
|
||||||
Children.Add(tile.Image);
|
Children.Add(tile.Image);
|
||||||
}
|
}
|
||||||
|
|
||||||
TileImageLoader.LoadTilesAsync(Tiles, TileSource, SourceName);
|
TileImageLoader.LoadTilesAsync(Tiles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,11 +44,6 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return string.Format("{0}/{1}/{2}", ZoomLevel, XIndex, Y);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void FadeIn()
|
private void FadeIn()
|
||||||
{
|
{
|
||||||
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation { From = 0d, To = 1d, Duration = FadeDuration, FillBehavior = FillBehavior.Stop });
|
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation { From = 0d, To = 1d, Duration = FadeDuration, FillBehavior = FillBehavior.Stop });
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|
@ -38,57 +39,42 @@ namespace MapControl
|
||||||
public static TimeSpan DefaultCacheExpiration { get; set; } = TimeSpan.FromDays(1);
|
public static TimeSpan DefaultCacheExpiration { get; set; } = TimeSpan.FromDays(1);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Format string for creating cache keys from the SourceName property of a TileSource,
|
/// Format string for creating cache keys from the SourceName property and the
|
||||||
/// the ZoomLevel, XIndex, and Y properties of a Tile, and the image file extension.
|
/// ZoomLevel, XIndex, and Y properties of a Tile and the image file extension.
|
||||||
/// The default value is "{0};{1};{2};{3}{4}".
|
/// The default value is "{0}/{1}/{2}/{3}{4}".
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string CacheKeyFormat { get; set; } = "{0};{1};{2};{3}{4}";
|
public static string CacheKeyFormat { get; set; } = "{0}/{1}/{2}/{3}{4}";
|
||||||
|
|
||||||
|
|
||||||
private readonly TileQueue tileQueue = new TileQueue();
|
private readonly TileQueue tileQueue = new TileQueue();
|
||||||
private int taskCount;
|
private int taskCount;
|
||||||
|
|
||||||
|
public TileSource TileSource { get; set; }
|
||||||
|
public string SourceName { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks.
|
/// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks.
|
||||||
/// If the UriFormat of the TileSource starts with "http" and the sourceName string is non-empty,
|
/// If the UriFormat of TileSource starts with "http" and SourceName is a non-empty string,
|
||||||
/// tile images are cached in the TileImageLoader's Cache.
|
/// tile images will be cached in the TileImageLoader's Cache.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName)
|
public async void LoadTilesAsync(IEnumerable<Tile> tiles)
|
||||||
{
|
{
|
||||||
tileQueue.Clear();
|
tileQueue.Clear();
|
||||||
|
|
||||||
if (tileSource != null)
|
|
||||||
{
|
|
||||||
tileQueue.Enqueue(tiles);
|
tileQueue.Enqueue(tiles);
|
||||||
|
|
||||||
var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;
|
var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;
|
||||||
|
|
||||||
if (newTasks > 0)
|
if (newTasks > 0)
|
||||||
{
|
{
|
||||||
Func<Tile, Task> loadTileFunc;
|
|
||||||
|
|
||||||
if (Cache != null &&
|
|
||||||
tileSource.UriFormat != null &&
|
|
||||||
tileSource.UriFormat.StartsWith("http") &&
|
|
||||||
!string.IsNullOrEmpty(sourceName))
|
|
||||||
{
|
|
||||||
loadTileFunc = tile => LoadCachedTileImageAsync(tile, tileSource, sourceName);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
loadTileFunc = tile => LoadTileImageAsync(tile, tileSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
Interlocked.Add(ref taskCount, newTasks);
|
Interlocked.Add(ref taskCount, newTasks);
|
||||||
|
|
||||||
while (--newTasks >= 0)
|
await Task
|
||||||
{
|
.WhenAll(Enumerable.Range(0, newTasks).Select(n => LoadTilesFromQueueAsync()))
|
||||||
Task.Run(() => LoadTilesFromQueueAsync(loadTileFunc));
|
.ConfigureAwait(false);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task LoadTilesFromQueueAsync(Func<Tile, Task> loadTileFunc)
|
private async Task LoadTilesFromQueueAsync()
|
||||||
{
|
{
|
||||||
Tile tile;
|
Tile tile;
|
||||||
|
|
||||||
|
|
@ -96,7 +82,7 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await loadTileFunc(tile);
|
await LoadTileImageAsync(tile, TileSource, SourceName).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
@ -107,7 +93,14 @@ namespace MapControl
|
||||||
Interlocked.Decrement(ref taskCount);
|
Interlocked.Decrement(ref taskCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task LoadCachedTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
|
private async Task LoadTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
|
||||||
|
{
|
||||||
|
if (tileSource != null)
|
||||||
|
{
|
||||||
|
if (Cache != null &&
|
||||||
|
tileSource.UriFormat != null &&
|
||||||
|
tileSource.UriFormat.StartsWith("http") &&
|
||||||
|
!string.IsNullOrEmpty(sourceName))
|
||||||
{
|
{
|
||||||
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
|
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
|
||||||
|
|
||||||
|
|
@ -122,7 +115,13 @@ namespace MapControl
|
||||||
|
|
||||||
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
|
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
|
||||||
|
|
||||||
await LoadCachedTileImageAsync(tile, uri, cacheKey);
|
await LoadCachedTileImageAsync(tile, uri, cacheKey).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await LoadTileImageAsync(tile, tileSource).ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ namespace MapControl.Caching
|
||||||
|
|
||||||
public virtual async Task SetAsync(string key, IBuffer buffer, DateTime expiration)
|
public virtual async Task SetAsync(string key, IBuffer buffer, DateTime expiration)
|
||||||
{
|
{
|
||||||
var paths = key.Split('\\', '/', ':', ';');
|
var paths = key.Split('\\', '/', ',', ':', ';');
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ using System.Runtime.InteropServices.WindowsRuntime;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Windows.Storage;
|
using Windows.Storage;
|
||||||
using Windows.Storage.Streams;
|
using Windows.Storage.Streams;
|
||||||
|
using Windows.UI.Xaml.Media;
|
||||||
using Windows.UI.Xaml.Media.Imaging;
|
using Windows.UI.Xaml.Media.Imaging;
|
||||||
using Windows.Web.Http;
|
using Windows.Web.Http;
|
||||||
using Windows.Web.Http.Headers;
|
using Windows.Web.Http.Headers;
|
||||||
|
|
@ -17,14 +18,14 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
public static partial class ImageLoader
|
public static partial class ImageLoader
|
||||||
{
|
{
|
||||||
public static async Task<BitmapSource> LoadImageAsync(IRandomAccessStream stream)
|
public static async Task<ImageSource> LoadImageAsync(IRandomAccessStream stream)
|
||||||
{
|
{
|
||||||
var image = new BitmapImage();
|
var image = new BitmapImage();
|
||||||
await image.SetSourceAsync(stream);
|
await image.SetSourceAsync(stream);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<BitmapSource> LoadImageAsync(byte[] buffer)
|
public static async Task<ImageSource> LoadImageAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
using (var stream = new InMemoryRandomAccessStream())
|
using (var stream = new InMemoryRandomAccessStream())
|
||||||
{
|
{
|
||||||
|
|
@ -34,7 +35,7 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<BitmapSource> LoadImageAsync(IHttpContent content)
|
private static async Task<ImageSource> LoadImageAsync(IHttpContent content)
|
||||||
{
|
{
|
||||||
using (var stream = new InMemoryRandomAccessStream())
|
using (var stream = new InMemoryRandomAccessStream())
|
||||||
{
|
{
|
||||||
|
|
@ -44,9 +45,9 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<BitmapSource> LoadLocalImageAsync(Uri uri)
|
private static async Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
||||||
{
|
{
|
||||||
BitmapSource image = null;
|
ImageSource image = null;
|
||||||
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
||||||
|
|
||||||
if (File.Exists(path))
|
if (File.Exists(path))
|
||||||
|
|
@ -62,30 +63,42 @@ namespace MapControl
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static async Task<Tuple<IBuffer, TimeSpan?>> LoadHttpBufferAsync(Uri uri)
|
internal class HttpBufferResponse
|
||||||
{
|
{
|
||||||
Tuple<IBuffer, TimeSpan?> result = null;
|
public readonly IBuffer Buffer;
|
||||||
|
public readonly TimeSpan? MaxAge;
|
||||||
|
|
||||||
|
public HttpBufferResponse(IBuffer buffer, TimeSpan? maxAge)
|
||||||
|
{
|
||||||
|
Buffer = buffer;
|
||||||
|
MaxAge = maxAge;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async Task<HttpBufferResponse> LoadHttpBufferAsync(Uri uri)
|
||||||
|
{
|
||||||
|
HttpBufferResponse response = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var response = await HttpClient.GetAsync(uri))
|
using (var responseMessage = await HttpClient.GetAsync(uri))
|
||||||
{
|
{
|
||||||
if (!response.IsSuccessStatusCode)
|
if (responseMessage.IsSuccessStatusCode)
|
||||||
{
|
|
||||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
IBuffer buffer = null;
|
IBuffer buffer = null;
|
||||||
TimeSpan? maxAge = null;
|
TimeSpan? maxAge = null;
|
||||||
|
|
||||||
if (IsTileAvailable(response.Headers))
|
if (ImageAvailable(responseMessage.Headers))
|
||||||
{
|
{
|
||||||
buffer = await response.Content.ReadAsBufferAsync();
|
buffer = await responseMessage.Content.ReadAsBufferAsync();
|
||||||
maxAge = response.Headers.CacheControl?.MaxAge;
|
maxAge = responseMessage.Headers.CacheControl?.MaxAge;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = new Tuple<IBuffer, TimeSpan?>(buffer, maxAge);
|
response = new HttpBufferResponse(buffer, maxAge);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)responseMessage.StatusCode, responseMessage.ReasonPhrase);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -94,10 +107,10 @@ namespace MapControl
|
||||||
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsTileAvailable(HttpResponseHeaderCollection responseHeaders)
|
private static bool ImageAvailable(HttpResponseHeaderCollection responseHeaders)
|
||||||
{
|
{
|
||||||
return !responseHeaders.TryGetValue("X-VE-Tile-Info", out string tileInfo) || tileInfo != "no-tile";
|
return !responseHeaders.TryGetValue("X-VE-Tile-Info", out string tileInfo) || tileInfo != "no-tile";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ using System.Threading.Tasks;
|
||||||
using Windows.Storage;
|
using Windows.Storage;
|
||||||
using Windows.Storage.Streams;
|
using Windows.Storage.Streams;
|
||||||
using Windows.UI.Core;
|
using Windows.UI.Core;
|
||||||
|
using Windows.UI.Xaml.Media;
|
||||||
|
|
||||||
namespace MapControl
|
namespace MapControl
|
||||||
{
|
{
|
||||||
|
|
@ -28,58 +29,48 @@ namespace MapControl
|
||||||
|
|
||||||
private async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
|
private async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
|
||||||
{
|
{
|
||||||
var cacheItem = await Cache.GetAsync(cacheKey);
|
var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
||||||
var cacheBuffer = cacheItem?.Buffer;
|
var cacheBuffer = cacheItem?.Buffer;
|
||||||
|
|
||||||
if (cacheBuffer == null || cacheItem.Expiration < DateTime.UtcNow)
|
if (cacheBuffer == null || cacheItem.Expiration < DateTime.UtcNow)
|
||||||
{
|
{
|
||||||
var result = await ImageLoader.LoadHttpBufferAsync(uri);
|
var response = await ImageLoader.LoadHttpBufferAsync(uri).ConfigureAwait(false);
|
||||||
|
|
||||||
if (result != null) // download succeeded
|
if (response != null) // download succeeded
|
||||||
{
|
{
|
||||||
cacheBuffer = null; // discard cached image
|
cacheBuffer = null; // discard cached image
|
||||||
|
|
||||||
if (result.Item1 != null) // tile image available
|
if (response.Buffer != null) // tile image available
|
||||||
{
|
{
|
||||||
await LoadTileImageAsync(tile, result.Item1);
|
await LoadTileImageAsync(tile, response.Buffer).ConfigureAwait(false);
|
||||||
await Cache.SetAsync(cacheKey, result.Item1, GetExpiration(result.Item2));
|
await Cache.SetAsync(cacheKey, response.Buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cacheBuffer != null) // cached image not expired or download failed
|
if (cacheBuffer != null) // cached image not expired or download failed
|
||||||
{
|
{
|
||||||
await LoadTileImageAsync(tile, cacheBuffer);
|
await LoadTileImageAsync(tile, cacheBuffer).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task LoadTileImageAsync(Tile tile, IBuffer buffer)
|
private async Task LoadTileImageAsync(Tile tile, IBuffer buffer)
|
||||||
{
|
{
|
||||||
var tcs = new TaskCompletionSource<object>();
|
|
||||||
|
|
||||||
using (var stream = new InMemoryRandomAccessStream())
|
using (var stream = new InMemoryRandomAccessStream())
|
||||||
{
|
{
|
||||||
await stream.WriteAsync(buffer);
|
await stream.WriteAsync(buffer);
|
||||||
stream.Seek(0);
|
stream.Seek(0);
|
||||||
|
|
||||||
await tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
|
await SetTileImageAsync(tile, () => ImageLoader.LoadImageAsync(stream)).ConfigureAwait(false);
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
tile.SetImage(await ImageLoader.LoadImageAsync(stream));
|
|
||||||
tcs.SetResult(null);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
tcs.SetException(ex);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await tcs.Task;
|
private Task LoadTileImageAsync(Tile tile, TileSource tileSource)
|
||||||
|
{
|
||||||
|
return SetTileImageAsync(tile, () => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task LoadTileImageAsync(Tile tile, TileSource tileSource)
|
private async Task SetTileImageAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||||
{
|
{
|
||||||
var tcs = new TaskCompletionSource<object>();
|
var tcs = new TaskCompletionSource<object>();
|
||||||
|
|
||||||
|
|
@ -87,7 +78,7 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
tile.SetImage(await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
|
tile.SetImage(await loadImageFunc());
|
||||||
tcs.SetResult(null);
|
tcs.SetResult(null);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
@ -96,7 +87,7 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await tcs.Task;
|
await tcs.Task.ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -261,7 +261,7 @@ namespace MapControl.Caching
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return Path.Combine(rootFolder, Path.Combine(key.Split('\\', '/', ':', ';')));
|
return Path.Combine(rootFolder, Path.Combine(key.Split('\\', '/', ',', ':', ';')));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,14 @@ using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
|
|
||||||
namespace MapControl
|
namespace MapControl
|
||||||
{
|
{
|
||||||
public static partial class ImageLoader
|
public static partial class ImageLoader
|
||||||
{
|
{
|
||||||
public static BitmapSource LoadImage(Stream stream)
|
public static ImageSource LoadImage(Stream stream)
|
||||||
{
|
{
|
||||||
var bitmapImage = new BitmapImage();
|
var bitmapImage = new BitmapImage();
|
||||||
bitmapImage.BeginInit();
|
bitmapImage.BeginInit();
|
||||||
|
|
@ -27,12 +28,12 @@ namespace MapControl
|
||||||
return bitmapImage;
|
return bitmapImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<BitmapSource> LoadImageAsync(Stream stream)
|
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
||||||
{
|
{
|
||||||
return Task.Run(() => LoadImage(stream));
|
return Task.Run(() => LoadImage(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BitmapSource LoadImage(byte[] buffer)
|
public static ImageSource LoadImage(byte[] buffer)
|
||||||
{
|
{
|
||||||
using (var stream = new MemoryStream(buffer))
|
using (var stream = new MemoryStream(buffer))
|
||||||
{
|
{
|
||||||
|
|
@ -40,24 +41,25 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<BitmapSource> LoadImageAsync(byte[] buffer)
|
public static Task<ImageSource> LoadImageAsync(byte[] buffer)
|
||||||
{
|
{
|
||||||
return Task.Run(() => LoadImage(buffer));
|
return Task.Run(() => LoadImage(buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<BitmapSource> LoadImageAsync(HttpContent content)
|
private static async Task<ImageSource> LoadImageAsync(HttpContent content)
|
||||||
{
|
{
|
||||||
using (var stream = new MemoryStream())
|
using (var stream = new MemoryStream())
|
||||||
{
|
{
|
||||||
await content.CopyToAsync(stream);
|
await content.CopyToAsync(stream);
|
||||||
stream.Seek(0, SeekOrigin.Begin);
|
stream.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
return await LoadImageAsync(stream);
|
return await LoadImageAsync(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BitmapSource LoadLocalImage(Uri uri)
|
private static ImageSource LoadLocalImage(Uri uri)
|
||||||
{
|
{
|
||||||
BitmapSource image = null;
|
ImageSource image = null;
|
||||||
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
||||||
|
|
||||||
if (File.Exists(path))
|
if (File.Exists(path))
|
||||||
|
|
@ -71,37 +73,50 @@ namespace MapControl
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Task<BitmapSource> LoadLocalImageAsync(Uri uri)
|
private static Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
||||||
{
|
{
|
||||||
return Task.Run(() => LoadLocalImage(uri));
|
return Task.Run(() => LoadLocalImage(uri));
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static async Task<Tuple<MemoryStream, TimeSpan?>> LoadHttpStreamAsync(Uri uri)
|
internal class HttpStreamResponse
|
||||||
{
|
{
|
||||||
Tuple<MemoryStream, TimeSpan?> result = null;
|
public readonly MemoryStream Stream;
|
||||||
|
public readonly TimeSpan? MaxAge;
|
||||||
|
|
||||||
|
public HttpStreamResponse(MemoryStream stream, TimeSpan? maxAge)
|
||||||
|
{
|
||||||
|
Stream = stream;
|
||||||
|
MaxAge = maxAge;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static async Task<HttpStreamResponse> LoadHttpStreamAsync(Uri uri)
|
||||||
|
{
|
||||||
|
HttpStreamResponse response = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var response = await HttpClient.GetAsync(uri))
|
using (var responseMessage = await HttpClient.GetAsync(uri))
|
||||||
{
|
{
|
||||||
if (!response.IsSuccessStatusCode)
|
if (responseMessage.IsSuccessStatusCode)
|
||||||
{
|
|
||||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
MemoryStream stream = null;
|
MemoryStream stream = null;
|
||||||
TimeSpan? maxAge = null;
|
TimeSpan? maxAge = null;
|
||||||
|
|
||||||
if (IsTileAvailable(response.Headers))
|
if (ImageAvailable(responseMessage.Headers))
|
||||||
{
|
{
|
||||||
stream = new MemoryStream();
|
stream = new MemoryStream();
|
||||||
await response.Content.CopyToAsync(stream);
|
await responseMessage.Content.CopyToAsync(stream);
|
||||||
stream.Seek(0, SeekOrigin.Begin);
|
stream.Seek(0, SeekOrigin.Begin);
|
||||||
maxAge = response.Headers.CacheControl?.MaxAge;
|
|
||||||
|
maxAge = responseMessage.Headers.CacheControl?.MaxAge;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = new Tuple<MemoryStream, TimeSpan?>(stream, maxAge);
|
response = new HttpStreamResponse(stream, maxAge);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)responseMessage.StatusCode, responseMessage.ReasonPhrase);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -110,10 +125,10 @@ namespace MapControl
|
||||||
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsTileAvailable(HttpResponseHeaders responseHeaders)
|
private static bool ImageAvailable(HttpResponseHeaders responseHeaders)
|
||||||
{
|
{
|
||||||
IEnumerable<string> tileInfo;
|
IEnumerable<string> tileInfo;
|
||||||
return !responseHeaders.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile");
|
return !responseHeaders.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile");
|
||||||
|
|
|
||||||
|
|
@ -34,18 +34,18 @@ namespace MapControl
|
||||||
|
|
||||||
if (cacheBuffer == null || expiration < DateTime.UtcNow)
|
if (cacheBuffer == null || expiration < DateTime.UtcNow)
|
||||||
{
|
{
|
||||||
var result = await ImageLoader.LoadHttpStreamAsync(uri);
|
var response = await ImageLoader.LoadHttpStreamAsync(uri).ConfigureAwait(false);
|
||||||
|
|
||||||
if (result != null) // download succeeded
|
if (response != null) // download succeeded
|
||||||
{
|
{
|
||||||
cacheBuffer = null; // discard cached image
|
cacheBuffer = null; // discard cached image
|
||||||
|
|
||||||
if (result.Item1 != null) // tile image available
|
if (response.Stream != null) // tile image available
|
||||||
{
|
{
|
||||||
using (var stream = result.Item1)
|
using (var stream = response.Stream)
|
||||||
{
|
{
|
||||||
LoadTileImage(tile, stream);
|
LoadTileImage(tile, stream);
|
||||||
SetCachedImage(cacheKey, stream, GetExpiration(result.Item2));
|
SetCachedImage(cacheKey, stream, GetExpiration(response.MaxAge));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +62,7 @@ namespace MapControl
|
||||||
|
|
||||||
private async Task LoadTileImageAsync(Tile tile, TileSource tileSource)
|
private async Task LoadTileImageAsync(Tile tile, TileSource tileSource)
|
||||||
{
|
{
|
||||||
SetTileImage(tile, await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
|
SetTileImage(tile, await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel).ConfigureAwait(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LoadTileImage(Tile tile, Stream stream)
|
private void LoadTileImage(Tile tile, Stream stream)
|
||||||
|
|
|
||||||
|
|
@ -80,12 +80,13 @@ namespace MapControl.Images
|
||||||
public static async Task<WorldFileImage> ReadWorldFileImage(string imagePath, string worldFilePath, string projFilePath = null)
|
public static async Task<WorldFileImage> ReadWorldFileImage(string imagePath, string worldFilePath, string projFilePath = null)
|
||||||
{
|
{
|
||||||
BitmapSource bitmap;
|
BitmapSource bitmap;
|
||||||
|
|
||||||
using (var stream = File.OpenRead(imagePath))
|
using (var stream = File.OpenRead(imagePath))
|
||||||
{
|
{
|
||||||
#if WINDOWS_UWP
|
#if WINDOWS_UWP
|
||||||
bitmap = await ImageLoader.LoadImageAsync(stream.AsRandomAccessStream());
|
bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(stream.AsRandomAccessStream());
|
||||||
#else
|
#else
|
||||||
bitmap = await ImageLoader.LoadImageAsync(stream);
|
bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(stream);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue