Added MapTileLayerBase.LoadTiles method

This commit is contained in:
ClemensF 2020-11-19 18:53:20 +01:00
parent f38ac5bec3
commit ab69894f13
5 changed files with 22 additions and 14 deletions

View file

@ -146,7 +146,7 @@ namespace MapControl
Children.Add(tile.Image); Children.Add(tile.Image);
} }
TileImageLoader.LoadTiles(Tiles, TileSource, SourceName); LoadTiles(Tiles, SourceName);
} }
} }

View file

@ -19,7 +19,7 @@ namespace MapControl
{ {
public interface ITileImageLoader public interface ITileImageLoader
{ {
void LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName); void LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName);
} }
public abstract class MapTileLayerBase : Panel, IMapLayer public abstract class MapTileLayerBase : Panel, IMapLayer
@ -189,5 +189,10 @@ namespace MapControl
protected abstract void UpdateTileLayer(bool tileSourceChanged); protected abstract void UpdateTileLayer(bool tileSourceChanged);
protected abstract void SetRenderTransform(); protected abstract void SetRenderTransform();
protected virtual void LoadTiles(IEnumerable<Tile> tiles, string cacheName)
{
TileImageLoader.LoadTiles(tiles, TileSource, cacheName);
}
} }
} }

View file

@ -30,7 +30,7 @@ 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 argument passed to LoadTilesAsync, /// Format string for creating cache keys from the cacheName argument passed to LoadTilesAsync,
/// the ZoomLevel, XIndex, and Y properties of a Tile, and the image file extension. /// the 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>
@ -56,10 +56,10 @@ namespace MapControl
/// <summary> /// <summary>
/// Loads all pending tiles from the tiles collection. /// Loads all pending tiles from the tiles collection.
/// If tileSource.UriFormat starts with "http" and sourceName is a non-empty string, /// 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). /// tile images will be cached in the TileImageLoader's Cache (if that is not null).
/// </summary> /// </summary>
public void LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName) public void LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName)
{ {
tileQueue.Clear(); tileQueue.Clear();
@ -70,9 +70,9 @@ namespace MapControl
if (Cache != null && if (Cache != null &&
tileSource.UriFormat != null && tileSource.UriFormat != null &&
tileSource.UriFormat.StartsWith("http") && tileSource.UriFormat.StartsWith("http") &&
!string.IsNullOrEmpty(sourceName)) !string.IsNullOrEmpty(cacheName))
{ {
loadTile = tile => LoadCachedTileAsync(tile, tileSource, sourceName); loadTile = tile => LoadCachedTileAsync(tile, tileSource, cacheName);
} }
else else
{ {
@ -109,7 +109,7 @@ namespace MapControl
Interlocked.Decrement(ref taskCount); Interlocked.Decrement(ref taskCount);
} }
private static async Task LoadCachedTileAsync(Tile tile, TileSource tileSource, string sourceName) private static async Task LoadCachedTileAsync(Tile tile, TileSource tileSource, string cacheName)
{ {
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel); var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
@ -122,7 +122,7 @@ namespace MapControl
extension = ".jpg"; extension = ".jpg";
} }
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension); var cacheKey = string.Format(CacheKeyFormat, cacheName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
await LoadCachedTileAsync(tile, uri, cacheKey).ConfigureAwait(false); await LoadCachedTileAsync(tile, uri, cacheKey).ConfigureAwait(false);
} }

View file

@ -162,15 +162,15 @@ namespace MapControl
} }
var tileSource = TileSource as WmtsTileSource; var tileSource = TileSource as WmtsTileSource;
var sourceName = SourceName; var cacheName = SourceName;
if (tileSource != null && tileMatrixSet != null) if (tileSource != null && tileMatrixSet != null)
{ {
tileSource.TileMatrixSet = tileMatrixSet; tileSource.TileMatrixSet = tileMatrixSet;
if (sourceName != null) if (cacheName != null)
{ {
sourceName += "/" + tileMatrixSet.Identifier cacheName += "/" + tileMatrixSet.Identifier
.Replace(':', '_') .Replace(':', '_')
.Replace(';', '_') .Replace(';', '_')
.Replace(',', '_') .Replace(',', '_')
@ -179,7 +179,7 @@ namespace MapControl
} }
} }
TileImageLoader.LoadTiles(tiles, tileSource, sourceName); LoadTiles(tiles, cacheName);
} }
private async void OnLoaded(object sender, RoutedEventArgs e) private async void OnLoaded(object sender, RoutedEventArgs e)

View file

@ -14,7 +14,10 @@ namespace MapControl
{ {
Uri uri = null; Uri uri = null;
if (UriFormat != null && TileMatrixSet != null && zoomLevel >= 0 && zoomLevel < TileMatrixSet.TileMatrixes.Count) if (UriFormat != null &&
TileMatrixSet != null &&
zoomLevel >= 0 &&
zoomLevel < TileMatrixSet.TileMatrixes.Count)
{ {
uri = new Uri(UriFormat uri = new Uri(UriFormat
.Replace("{TileMatrixSet}", TileMatrixSet.Identifier) .Replace("{TileMatrixSet}", TileMatrixSet.Identifier)