2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2023-01-03 15:12:53 +01:00
|
|
|
|
// Copyright © 2023 Clemens Fischer
|
2017-08-04 21:38:58 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
2019-06-10 17:20:21 +02:00
|
|
|
|
using System.Collections.Generic;
|
2019-06-10 22:18:02 +02:00
|
|
|
|
using System.Diagnostics;
|
2021-07-05 00:03:44 +02:00
|
|
|
|
using System.Globalization;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
using System.IO;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
using System.Linq;
|
2022-08-05 18:54:19 +02:00
|
|
|
|
using System.Threading;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Loads and optionally caches map tile images for a MapTileLayer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public partial class TileImageLoader : ITileImageLoader
|
|
|
|
|
|
{
|
2022-08-05 18:54:19 +02:00
|
|
|
|
private class TileQueue : ConcurrentStack<Tile>
|
|
|
|
|
|
{
|
|
|
|
|
|
public TileQueue(IEnumerable<Tile> tiles)
|
2023-08-12 17:36:37 +02:00
|
|
|
|
: base(tiles.Where(tile => tile.IsPending).Reverse())
|
2022-08-05 18:54:19 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsCanceled { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public bool TryDequeue(out Tile tile)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (IsCanceled || !TryPop(out tile))
|
|
|
|
|
|
{
|
2023-08-12 17:36:37 +02:00
|
|
|
|
tile = null;
|
2022-08-05 18:54:19 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
tile.IsPending = false;
|
2022-08-05 18:54:19 +02:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Cancel()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsCanceled = true;
|
|
|
|
|
|
Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// <summary>
|
2020-04-13 09:12:21 +02:00
|
|
|
|
/// Maximum number of parallel tile loading tasks. The default value is 4.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2020-04-13 09:12:21 +02:00
|
|
|
|
public static int MaxLoadTasks { get; set; } = 4;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-07-16 20:13:52 +02:00
|
|
|
|
/// Default expiration time for cached tile images. Used when no expiration time
|
|
|
|
|
|
/// was transmitted on download. The default value is one day.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2019-07-16 20:13:52 +02:00
|
|
|
|
public static TimeSpan DefaultCacheExpiration { get; set; } = TimeSpan.FromDays(1);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-01-07 22:52:41 +01:00
|
|
|
|
/// Maximum expiration time for cached tile images. A transmitted expiration time
|
|
|
|
|
|
/// that exceeds this value is ignored. The default value is ten days.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2021-01-07 22:52:41 +01:00
|
|
|
|
public static TimeSpan MaxCacheExpiration { get; set; } = TimeSpan.FromDays(10);
|
2019-06-13 21:38:01 +02:00
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
private TileQueue pendingTiles;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2018-08-21 23:56:54 +02:00
|
|
|
|
/// <summary>
|
2022-11-23 17:14:12 +01:00
|
|
|
|
/// Loads all unloaded tiles from the tiles collection.
|
2021-07-06 01:01:22 +02:00
|
|
|
|
/// If tileSource.UriFormat starts with "http" and cacheName is a non-empty string,
|
|
|
|
|
|
/// tile images will be cached in the TileImageLoader's Cache - if that is not null.
|
2018-08-21 23:56:54 +02:00
|
|
|
|
/// </summary>
|
2023-08-12 17:36:37 +02:00
|
|
|
|
public Task LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2023-08-12 17:36:37 +02:00
|
|
|
|
pendingTiles?.Cancel();
|
2021-11-22 23:07:07 +01:00
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
if (tiles != null && tileSource != null)
|
2021-11-22 23:07:07 +01:00
|
|
|
|
{
|
2023-08-12 17:36:37 +02:00
|
|
|
|
pendingTiles = new TileQueue(tiles);
|
2021-07-05 14:51:28 +02:00
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
var numTasks = Math.Min(pendingTiles.Count, MaxLoadTasks);
|
2021-07-05 14:51:28 +02:00
|
|
|
|
|
2021-11-28 23:50:22 +01:00
|
|
|
|
if (numTasks > 0)
|
|
|
|
|
|
{
|
2022-08-23 17:20:16 +02:00
|
|
|
|
if (Cache == null || tileSource.UriTemplate == null || !tileSource.UriTemplate.StartsWith("http"))
|
2021-11-28 23:50:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
cacheName = null; // no tile caching
|
|
|
|
|
|
}
|
2019-06-10 22:18:02 +02:00
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
var progressLoaded = 0;
|
|
|
|
|
|
var progressTotal = 0;
|
2019-06-10 22:18:02 +02:00
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
if (progress != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
progressTotal = pendingTiles.Count;
|
|
|
|
|
|
progress.Report(0d);
|
|
|
|
|
|
}
|
2022-08-05 18:54:19 +02:00
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
async Task LoadPendingTiles()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (pendingTiles.TryDequeue(out var tile))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await LoadTile(tile, tileSource, cacheName).ConfigureAwait(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine($"TileImageLoader: {tile.ZoomLevel}/{tile.Column}/{tile.Row}: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (progress != null && !pendingTiles.IsCanceled)
|
|
|
|
|
|
{
|
|
|
|
|
|
Interlocked.Increment(ref progressLoaded);
|
|
|
|
|
|
|
|
|
|
|
|
progress.Report((double)progressLoaded / progressTotal);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-05 18:54:19 +02:00
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
return Task.WhenAll(Enumerable.Range(0, numTasks).Select(_ => Task.Run(LoadPendingTiles)));
|
2022-08-05 18:54:19 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
2023-08-12 17:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-22 23:07:07 +01:00
|
|
|
|
private static Task LoadTile(Tile tile, TileSource tileSource, string cacheName)
|
2019-07-13 00:08:56 +02:00
|
|
|
|
{
|
2021-07-06 22:58:03 +02:00
|
|
|
|
if (string.IsNullOrEmpty(cacheName))
|
2019-07-13 00:08:56 +02:00
|
|
|
|
{
|
2022-11-22 19:15:34 +01:00
|
|
|
|
return LoadTile(tile, () => tileSource.LoadImageAsync(tile.Column, tile.Row, tile.ZoomLevel));
|
2021-01-23 17:41:52 +01:00
|
|
|
|
}
|
2019-06-13 21:38:01 +02:00
|
|
|
|
|
2022-11-22 19:15:34 +01:00
|
|
|
|
var uri = tileSource.GetUri(tile.Column, tile.Row, tile.ZoomLevel);
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2021-11-28 23:50:22 +01:00
|
|
|
|
if (uri != null)
|
2021-06-28 23:35:03 +02:00
|
|
|
|
{
|
2021-11-28 23:50:22 +01:00
|
|
|
|
var extension = Path.GetExtension(uri.LocalPath);
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
|
|
|
|
|
|
{
|
|
|
|
|
|
extension = ".jpg";
|
|
|
|
|
|
}
|
2021-01-23 17:41:52 +01:00
|
|
|
|
|
2021-11-28 23:50:22 +01:00
|
|
|
|
var cacheKey = string.Format(CultureInfo.InvariantCulture,
|
2022-11-22 19:15:34 +01:00
|
|
|
|
"{0}/{1}/{2}/{3}{4}", cacheName, tile.ZoomLevel, tile.Column, tile.Row, extension);
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2021-11-28 23:50:22 +01:00
|
|
|
|
return LoadCachedTile(tile, uri, cacheKey);
|
2019-07-10 18:00:25 +02:00
|
|
|
|
}
|
2021-06-28 23:35:03 +02:00
|
|
|
|
|
2021-11-28 23:50:22 +01:00
|
|
|
|
return Task.CompletedTask;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-27 17:15:18 +02:00
|
|
|
|
private static DateTime GetExpiration(TimeSpan? maxAge)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-01-07 22:52:41 +01:00
|
|
|
|
if (!maxAge.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
maxAge = DefaultCacheExpiration;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (maxAge.Value > MaxCacheExpiration)
|
|
|
|
|
|
{
|
|
|
|
|
|
maxAge = MaxCacheExpiration;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return DateTime.UtcNow.Add(maxAge.Value);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|