TileSource/TileImageLoader

This commit is contained in:
ClemensFischer 2025-11-13 17:06:33 +01:00
parent cb8fff0dd1
commit 4912fa1e40
3 changed files with 37 additions and 66 deletions

View file

@ -24,8 +24,6 @@ namespace MapControl.MBTiles
public IDictionary<string, string> Metadata { get; } = new Dictionary<string, string>();
public override bool Cacheable => false;
public async Task OpenAsync(string file)
{
Close();
@ -34,8 +32,8 @@ namespace MapControl.MBTiles
await connection.OpenAsync();
using (var command = new SQLiteCommand("select * from metadata", connection))
{
using var command = new SQLiteCommand("select * from metadata", connection);
var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
@ -43,7 +41,6 @@ namespace MapControl.MBTiles
Metadata[(string)reader["name"]] = (string)reader["value"];
}
}
}
public void Close()
{
@ -66,8 +63,8 @@ namespace MapControl.MBTiles
try
{
using (var command = new SQLiteCommand("select tile_data from tiles where zoom_level=@z and tile_column=@x and tile_row=@y", connection))
{
using var command = new SQLiteCommand("select tile_data from tiles where zoom_level=@z and tile_column=@x and tile_row=@y", connection);
command.Parameters.AddWithValue("@z", zoomLevel);
command.Parameters.AddWithValue("@x", x);
command.Parameters.AddWithValue("@y", (1 << zoomLevel) - y - 1);
@ -76,8 +73,7 @@ namespace MapControl.MBTiles
if (buffer?.Length > 0)
{
image = await LoadImageAsync(buffer);
}
image = await ImageLoader.LoadImageAsync(buffer);
}
}
catch (Exception ex)
@ -87,10 +83,5 @@ namespace MapControl.MBTiles
return image;
}
public override Uri GetUri(int zoomLevel, int column, int row)
{
throw new NotSupportedException();
}
}
}

View file

@ -13,13 +13,13 @@ namespace MapControl
public interface ITileImageLoader
{
/// <summary>
/// Loads all pending tiles from the tiles collection.
/// Tile image caching is enabled when tileSource.UriFormat starts with "http" and cacheName is a non-empty string.
/// Loads all pending tiles from the tiles collection. Tile image caching is enabled
/// when cacheName is a non-empty string and tiles are loaded from http or https Uris.
/// </summary>
void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress);
/// <summary>
/// Cancels a potentially ongoing tile loading task.
/// Terminates all running tile loading tasks.
/// </summary>
void CancelLoadTiles();
}
@ -76,7 +76,7 @@ namespace MapControl
public void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress)
{
if (Cache == null || !tileSource.Cacheable)
if (Cache == null)
{
cacheName = null; // disable caching
}
@ -142,25 +142,26 @@ namespace MapControl
try
{
// Pass tileSource.LoadImageAsync calls to platform-specific method
// Pass image loading callbacks to platform-specific method
// tile.LoadImageAsync(Func<Task<ImageSource>>) for completion in the UI thread.
if (string.IsNullOrEmpty(cacheName))
var uri = tileSource.GetUri(tile.ZoomLevel, tile.Column, tile.Row);
if (uri == null)
{
await tile.LoadImageAsync(() => tileSource.LoadImageAsync(tile.ZoomLevel, tile.Column, tile.Row)).ConfigureAwait(false);
}
else
else if (uri.Scheme != "http" && uri.Scheme != "https" || string.IsNullOrEmpty(cacheName))
{
var uri = tileSource.GetUri(tile.ZoomLevel, tile.Column, tile.Row);
if (uri != null)
await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(uri)).ConfigureAwait(false);
}
else
{
var buffer = await LoadCachedBuffer(tile, uri, cacheName).ConfigureAwait(false);
if (buffer?.Length > 0)
{
await tile.LoadImageAsync(() => tileSource.LoadImageAsync(buffer)).ConfigureAwait(false);
}
await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
}
}
}

View file

@ -21,30 +21,18 @@ namespace MapControl
#else
[System.ComponentModel.TypeConverter(typeof(TileSourceConverter))]
#endif
public abstract class TileSource
public class TileSource
{
/// <summary>
/// Indicates whether tile images from this source should be cached.
/// Gets the image request Uri for the specified zoom level and tile indices.
/// May return null when the image shall be loaded by the LoadImageAsync method.
/// </summary>
public abstract bool Cacheable { get; }
public virtual Uri GetUri(int zoomLevel, int column, int row) => null;
/// <summary>
/// Gets the image Uri for the specified zoom level and tile indices.
/// Loads a tile image without an Uri.
/// </summary>
public abstract Uri GetUri(int zoomLevel, int column, int row);
/// <summary>
/// Loads a tile image whithout caching.
/// </summary>
public abstract Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row);
/// <summary>
/// Loads a cacheable tile image from an encoded frame buffer.
/// </summary>
public virtual Task<ImageSource> LoadImageAsync(byte[] buffer)
{
return ImageLoader.LoadImageAsync(buffer);
}
public virtual Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row) => null;
/// <summary>
/// Creates a TileSource instance from an Uri template string.
@ -78,8 +66,6 @@ namespace MapControl
public string[] Subdomains { get; set; }
public override bool Cacheable => UriTemplate != null && UriTemplate.StartsWith("http");
public override Uri GetUri(int zoomLevel, int column, int row)
{
Uri uri = null;
@ -103,13 +89,6 @@ namespace MapControl
return uri;
}
public override Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
{
var uri = GetUri(zoomLevel, column, row);
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
}
public override string ToString()
{
return UriTemplate;