Version 4.10.0: Simplified TileImageLoader.

This commit is contained in:
ClemensF 2018-08-21 23:56:54 +02:00
parent f6135d2bfb
commit be3a247064
6 changed files with 113 additions and 101 deletions

View file

@ -5,7 +5,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
#if WINDOWS_UWP #if WINDOWS_UWP
using Windows.Foundation; using Windows.Foundation;
using Windows.UI.Xaml; using Windows.UI.Xaml;
@ -22,7 +21,7 @@ namespace MapControl
{ {
public interface ITileImageLoader public interface ITileImageLoader
{ {
Task LoadTilesAsync(MapTileLayer tileLayer); void LoadTilesAsync(MapTileLayer tileLayer);
} }
/// <summary> /// <summary>
@ -93,7 +92,6 @@ namespace MapControl
IsHitTestVisible = false; IsHitTestVisible = false;
RenderTransform = new MatrixTransform(); RenderTransform = new MatrixTransform();
TileImageLoader = tileImageLoader; TileImageLoader = tileImageLoader;
Tiles = new List<Tile>();
updateTimer = new DispatcherTimer { Interval = UpdateInterval }; updateTimer = new DispatcherTimer { Interval = UpdateInterval };
updateTimer.Tick += (s, e) => UpdateTileGrid(); updateTimer.Tick += (s, e) => UpdateTileGrid();
@ -102,9 +100,11 @@ namespace MapControl
} }
public ITileImageLoader TileImageLoader { get; private set; } public ITileImageLoader TileImageLoader { get; private set; }
public ICollection<Tile> Tiles { get; private set; }
public TileGrid TileGrid { get; private set; } public TileGrid TileGrid { get; private set; }
public IReadOnlyCollection<Tile> Tiles { get; private set; } = new List<Tile>();
/// <summary> /// <summary>
/// Provides map tile URIs or images. /// Provides map tile URIs or images.
/// </summary> /// </summary>
@ -273,7 +273,7 @@ namespace MapControl
{ {
if (TileGrid != null) if (TileGrid != null)
{ {
Tiles.Clear(); Tiles = new List<Tile>();
UpdateTiles(); UpdateTiles();
} }
} }
@ -344,9 +344,7 @@ namespace MapControl
var maxZoomLevel = Math.Min(TileGrid.ZoomLevel, MaxZoomLevel); var maxZoomLevel = Math.Min(TileGrid.ZoomLevel, MaxZoomLevel);
var minZoomLevel = MinZoomLevel; var minZoomLevel = MinZoomLevel;
if (minZoomLevel < maxZoomLevel && if (minZoomLevel < maxZoomLevel && parentMap.MapLayer != this)
parentMap.MapLayer != this &&
parentMap.Children.Cast<UIElement>().FirstOrDefault() != this)
{ {
minZoomLevel = maxZoomLevel; // do not load lower level tiles if this is note a "base" layer minZoomLevel = maxZoomLevel; // do not load lower level tiles if this is note a "base" layer
} }
@ -393,7 +391,7 @@ namespace MapControl
Children.Add(tile.Image); Children.Add(tile.Image);
} }
var task = TileImageLoader.LoadTilesAsync(this); TileImageLoader.LoadTilesAsync(this);
} }
} }
} }

View file

@ -18,6 +18,12 @@ namespace MapControl
/// </summary> /// </summary>
public class PolygonCollection : ObservableCollection<IEnumerable<Location>>, IWeakEventListener public class PolygonCollection : ObservableCollection<IEnumerable<Location>>, IWeakEventListener
{ {
public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, sender, sender));
return true;
}
protected override void InsertItem(int index, IEnumerable<Location> polygon) protected override void InsertItem(int index, IEnumerable<Location> polygon)
{ {
var observablePolygon = polygon as INotifyCollectionChanged; var observablePolygon = polygon as INotifyCollectionChanged;
@ -63,12 +69,5 @@ namespace MapControl
base.ClearItems(); base.ClearItems();
} }
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, sender, sender));
return true;
}
} }
} }

View file

@ -18,20 +18,25 @@ namespace MapControl
public partial class TileImageLoader : ITileImageLoader public partial class TileImageLoader : ITileImageLoader
{ {
/// <summary> /// <summary>
/// Default expiration time for cached tile images. Used when no expiration time /// Maximum number of parallel tile loading tasks. The default value is 4.
/// was transmitted on download. The default value is one day.
/// </summary> /// </summary>
public static TimeSpan DefaultCacheExpiration { get; set; } = TimeSpan.FromDays(1); public static int MaxLoadTasks { get; set; } = 4;
/// <summary> /// <summary>
/// Minimum expiration time for cached tile images. The default value is one hour. /// Minimum expiration time for cached tile images. The default value is one hour.
/// </summary> /// </summary>
public static TimeSpan MinimumCacheExpiration { get; set; } = TimeSpan.FromHours(1); public static TimeSpan MinCacheExpiration { get; set; } = TimeSpan.FromHours(1);
/// <summary> /// <summary>
/// Maximum expiration time for cached tile images. The default value is one week. /// Maximum expiration time for cached tile images. The default value is one week.
/// </summary> /// </summary>
public static TimeSpan MaximumCacheExpiration { get; set; } = TimeSpan.FromDays(7); public static TimeSpan MaxCacheExpiration { get; set; } = TimeSpan.FromDays(7);
/// <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> /// <summary>
/// Format string for creating cache keys from the SourceName property of a TileSource, /// Format string for creating cache keys from the SourceName property of a TileSource,
@ -43,7 +48,12 @@ namespace MapControl
private readonly ConcurrentStack<Tile> pendingTiles = new ConcurrentStack<Tile>(); private readonly ConcurrentStack<Tile> pendingTiles = new ConcurrentStack<Tile>();
private int taskCount; private int taskCount;
public async Task LoadTilesAsync(MapTileLayer tileLayer) /// <summary>
/// Loads all pending tiles from the Tiles collection of a MapTileLayer by running up to MaxLoadTasks parallel Tasks.
/// If the TileSource's SourceName is non-empty and its UriFormat starts with "http", tile images are cached in the
/// TileImageLoader's Cache.
/// </summary>
public void LoadTilesAsync(MapTileLayer tileLayer)
{ {
pendingTiles.Clear(); pendingTiles.Clear();
@ -53,55 +63,34 @@ namespace MapControl
if (tileSource != null && tiles.Any()) if (tileSource != null && tiles.Any())
{ {
if (Cache == null || string.IsNullOrEmpty(sourceName) || pendingTiles.PushRange(tiles.Reverse().ToArray());
tileSource.UriFormat == null || !tileSource.UriFormat.StartsWith("http"))
{
// no caching, load tile images directly
foreach (var tile in tiles) Func<Tile, Task> loadFunc;
{
await LoadTileImageAsync(tileSource, tile); if (Cache != null && !string.IsNullOrEmpty(sourceName) &&
} tileSource.UriFormat != null && tileSource.UriFormat.StartsWith("http"))
{
loadFunc = tile => LoadCachedTileImageAsync(tile, tileSource, sourceName);
} }
else else
{ {
pendingTiles.PushRange(tiles.Reverse().ToArray()); loadFunc = tile => LoadTileImageAsync(tile, tileSource);
while (taskCount < Math.Min(pendingTiles.Count, DefaultConnectionLimit))
{
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);
});
}
} }
}
}
private async Task LoadTileImageAsync(TileSource tileSource, Tile tile) var maxTasks = Math.Min(pendingTiles.Count, MaxLoadTasks);
{
tile.Pending = false;
try while (taskCount < maxTasks)
{
var imageSource = await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel);
if (imageSource != null)
{ {
tile.SetImage(imageSource); Interlocked.Increment(ref taskCount);
var task = Task.Run(() => LoadTilesAsync(loadFunc)); // do not await
} }
}
catch (Exception ex) //Debug.WriteLine("{0}: {1} tasks", Environment.CurrentManagedThreadId, taskCount);
{
Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message);
} }
} }
private async Task LoadPendingTilesAsync(TileSource tileSource, string sourceName) private async Task LoadTilesAsync(Func<Tile, Task> loadTileImageFunc)
{ {
Tile tile; Tile tile;
@ -111,27 +100,35 @@ namespace MapControl
try try
{ {
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel); await loadTileImageFunc(tile);
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) catch (Exception ex)
{ {
Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message); Debug.WriteLine("TileImageLoader: {0}/{1}/{2}: {3}", tile.ZoomLevel, tile.XIndex, tile.Y, ex.Message);
} }
} }
Interlocked.Decrement(ref taskCount);
//Debug.WriteLine("{0}: {1} tasks", Environment.CurrentManagedThreadId, taskCount);
}
private async Task LoadCachedTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
{
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 LoadCachedTileImageAsync(tile, uri, cacheKey);
}
} }
private static DateTime GetExpiration(TimeSpan? maxAge) private static DateTime GetExpiration(TimeSpan? maxAge)
@ -142,13 +139,13 @@ namespace MapControl
{ {
expiration = maxAge.Value; expiration = maxAge.Value;
if (expiration < MinimumCacheExpiration) if (expiration < MinCacheExpiration)
{ {
expiration = MinimumCacheExpiration; expiration = MinCacheExpiration;
} }
else if (expiration > MaximumCacheExpiration) else if (expiration > MaxCacheExpiration)
{ {
expiration = MaximumCacheExpiration; expiration = MaxCacheExpiration;
} }
} }

View file

@ -26,12 +26,7 @@ namespace MapControl
/// </summary> /// </summary>
public static Caching.IImageCache Cache { get; set; } public static Caching.IImageCache Cache { get; set; }
/// <summary> private async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
/// Gets or sets the maximum number of concurrent connections. The default value is 2.
/// </summary>
public static int DefaultConnectionLimit { get; set; } = 2;
private async Task LoadTileImageAsync(Tile tile, Uri uri, string cacheKey)
{ {
var cacheItem = await Cache.GetAsync(cacheKey); var cacheItem = await Cache.GetAsync(cacheKey);
var cacheBuffer = cacheItem?.Buffer; var cacheBuffer = cacheItem?.Buffer;
@ -46,7 +41,7 @@ namespace MapControl
if (result.Item1 != null) // tile image available if (result.Item1 != null) // tile image available
{ {
await SetTileImageAsync(tile, result.Item1); // show before caching await LoadTileImageAsync(tile, result.Item1);
await Cache.SetAsync(cacheKey, result.Item1, GetExpiration(result.Item2)); await Cache.SetAsync(cacheKey, result.Item1, GetExpiration(result.Item2));
} }
} }
@ -54,21 +49,20 @@ namespace MapControl
if (cacheBuffer != null) if (cacheBuffer != null)
{ {
await SetTileImageAsync(tile, cacheBuffer); await LoadTileImageAsync(tile, cacheBuffer);
} }
} }
private async Task SetTileImageAsync(Tile tile, IBuffer buffer) private async Task LoadTileImageAsync(Tile tile, IBuffer buffer)
{ {
var tcs = new TaskCompletionSource<object>(); var tcs = new TaskCompletionSource<object>();
using (var stream = new InMemoryRandomAccessStream()) using (var stream = new InMemoryRandomAccessStream())
{ {
await stream.WriteAsync(buffer); await stream.WriteAsync(buffer);
await stream.FlushAsync(); // necessary?
stream.Seek(0); stream.Seek(0);
await tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => await tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
{ {
try try
{ {
@ -84,5 +78,25 @@ namespace MapControl
await tcs.Task; await tcs.Task;
} }
private async Task LoadTileImageAsync(Tile tile, TileSource tileSource)
{
var tcs = new TaskCompletionSource<object>();
await tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
{
try
{
tile.SetImage(await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
tcs.SetResult(null);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
});
await tcs.Task;
}
} }
} }

View file

@ -21,7 +21,7 @@ namespace MapControl
get { return Data; } get { return Data; }
} }
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e) public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{ {
UpdateData(); UpdateData();
return true; return true;

View file

@ -4,10 +4,11 @@
using System; using System;
using System.IO; using System.IO;
using System.Net;
using System.Runtime.Caching; using System.Runtime.Caching;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Threading;
namespace MapControl namespace MapControl
{ {
@ -27,12 +28,7 @@ namespace MapControl
/// </summary> /// </summary>
public static ObjectCache Cache { get; set; } = MemoryCache.Default; public static ObjectCache Cache { get; set; } = MemoryCache.Default;
private static int DefaultConnectionLimit private async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
{
get { return ServicePointManager.DefaultConnectionLimit; }
}
private async Task LoadTileImageAsync(Tile tile, Uri uri, string cacheKey)
{ {
DateTime expiration; DateTime expiration;
var cacheBuffer = GetCachedImage(cacheKey, out expiration); var cacheBuffer = GetCachedImage(cacheKey, out expiration);
@ -49,7 +45,7 @@ namespace MapControl
{ {
using (var stream = result.Item1) using (var stream = result.Item1)
{ {
SetTileImage(tile, stream); // show before caching LoadTileImage(tile, stream);
SetCachedImage(cacheKey, stream, GetExpiration(result.Item2)); SetCachedImage(cacheKey, stream, GetExpiration(result.Item2));
} }
} }
@ -60,15 +56,23 @@ namespace MapControl
{ {
using (var stream = new MemoryStream(cacheBuffer)) using (var stream = new MemoryStream(cacheBuffer))
{ {
SetTileImage(tile, stream); LoadTileImage(tile, stream);
} }
} }
} }
private void SetTileImage(Tile tile, Stream stream) private async Task LoadTileImageAsync(Tile tile, TileSource tileSource)
{ {
var imageSource = ImageLoader.LoadImage(stream); SetTileImage(tile, await tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
}
private void LoadTileImage(Tile tile, Stream stream)
{
SetTileImage(tile, ImageLoader.LoadImage(stream));
}
private void SetTileImage(Tile tile, ImageSource imageSource)
{
tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(imageSource)); tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(imageSource));
} }