mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
TileSource/TileImageLoader
This commit is contained in:
parent
cb8fff0dd1
commit
4912fa1e40
|
|
@ -24,8 +24,6 @@ namespace MapControl.MBTiles
|
||||||
|
|
||||||
public IDictionary<string, string> Metadata { get; } = new Dictionary<string, string>();
|
public IDictionary<string, string> Metadata { get; } = new Dictionary<string, string>();
|
||||||
|
|
||||||
public override bool Cacheable => false;
|
|
||||||
|
|
||||||
public async Task OpenAsync(string file)
|
public async Task OpenAsync(string file)
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
|
|
@ -34,14 +32,13 @@ namespace MapControl.MBTiles
|
||||||
|
|
||||||
await connection.OpenAsync();
|
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())
|
var reader = await command.ExecuteReaderAsync();
|
||||||
{
|
|
||||||
Metadata[(string)reader["name"]] = (string)reader["value"];
|
while (await reader.ReadAsync())
|
||||||
}
|
{
|
||||||
|
Metadata[(string)reader["name"]] = (string)reader["value"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,18 +63,17 @@ namespace MapControl.MBTiles
|
||||||
|
|
||||||
try
|
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);
|
||||||
|
|
||||||
|
var buffer = (byte[])await command.ExecuteScalarAsync();
|
||||||
|
|
||||||
|
if (buffer?.Length > 0)
|
||||||
{
|
{
|
||||||
command.Parameters.AddWithValue("@z", zoomLevel);
|
image = await ImageLoader.LoadImageAsync(buffer);
|
||||||
command.Parameters.AddWithValue("@x", x);
|
|
||||||
command.Parameters.AddWithValue("@y", (1 << zoomLevel) - y - 1);
|
|
||||||
|
|
||||||
var buffer = (byte[])await command.ExecuteScalarAsync();
|
|
||||||
|
|
||||||
if (buffer?.Length > 0)
|
|
||||||
{
|
|
||||||
image = await LoadImageAsync(buffer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
@ -87,10 +83,5 @@ namespace MapControl.MBTiles
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Uri GetUri(int zoomLevel, int column, int row)
|
|
||||||
{
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,13 @@ namespace MapControl
|
||||||
public interface ITileImageLoader
|
public interface ITileImageLoader
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads all pending tiles from the tiles collection.
|
/// Loads all pending tiles from the tiles collection. Tile image caching is enabled
|
||||||
/// Tile image caching is enabled when tileSource.UriFormat starts with "http" and cacheName is a non-empty string.
|
/// when cacheName is a non-empty string and tiles are loaded from http or https Uris.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress);
|
void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Cancels a potentially ongoing tile loading task.
|
/// Terminates all running tile loading tasks.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void CancelLoadTiles();
|
void CancelLoadTiles();
|
||||||
}
|
}
|
||||||
|
|
@ -76,7 +76,7 @@ namespace MapControl
|
||||||
|
|
||||||
public void BeginLoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress)
|
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
|
cacheName = null; // disable caching
|
||||||
}
|
}
|
||||||
|
|
@ -142,25 +142,26 @@ namespace MapControl
|
||||||
|
|
||||||
try
|
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.
|
// 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);
|
await tile.LoadImageAsync(() => tileSource.LoadImageAsync(tile.ZoomLevel, tile.Column, tile.Row)).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
else if (uri.Scheme != "http" && uri.Scheme != "https" || string.IsNullOrEmpty(cacheName))
|
||||||
|
{
|
||||||
|
await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(uri)).ConfigureAwait(false);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var uri = tileSource.GetUri(tile.ZoomLevel, tile.Column, tile.Row);
|
var buffer = await LoadCachedBuffer(tile, uri, cacheName).ConfigureAwait(false);
|
||||||
|
|
||||||
if (uri != null)
|
if (buffer?.Length > 0)
|
||||||
{
|
{
|
||||||
var buffer = await LoadCachedBuffer(tile, uri, cacheName).ConfigureAwait(false);
|
await tile.LoadImageAsync(() => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||||
|
|
||||||
if (buffer?.Length > 0)
|
|
||||||
{
|
|
||||||
await tile.LoadImageAsync(() => tileSource.LoadImageAsync(buffer)).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,30 +21,18 @@ namespace MapControl
|
||||||
#else
|
#else
|
||||||
[System.ComponentModel.TypeConverter(typeof(TileSourceConverter))]
|
[System.ComponentModel.TypeConverter(typeof(TileSourceConverter))]
|
||||||
#endif
|
#endif
|
||||||
public abstract class TileSource
|
public class TileSource
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
public abstract bool Cacheable { get; }
|
public virtual Uri GetUri(int zoomLevel, int column, int row) => null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the image Uri for the specified zoom level and tile indices.
|
/// Loads a tile image without an Uri.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract Uri GetUri(int zoomLevel, int column, int row);
|
public virtual Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row) => null;
|
||||||
|
|
||||||
/// <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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a TileSource instance from an Uri template string.
|
/// Creates a TileSource instance from an Uri template string.
|
||||||
|
|
@ -78,8 +66,6 @@ namespace MapControl
|
||||||
|
|
||||||
public string[] Subdomains { get; set; }
|
public string[] Subdomains { get; set; }
|
||||||
|
|
||||||
public override bool Cacheable => UriTemplate != null && UriTemplate.StartsWith("http");
|
|
||||||
|
|
||||||
public override Uri GetUri(int zoomLevel, int column, int row)
|
public override Uri GetUri(int zoomLevel, int column, int row)
|
||||||
{
|
{
|
||||||
Uri uri = null;
|
Uri uri = null;
|
||||||
|
|
@ -103,13 +89,6 @@ namespace MapControl
|
||||||
return uri;
|
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()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return UriTemplate;
|
return UriTemplate;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue