2025-04-01 16:11:53 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
2024-08-20 18:00:04 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Data.SQLite;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
|
|
|
|
|
using System.Windows.Media;
|
2021-11-17 23:46:48 +01:00
|
|
|
|
#elif UWP
|
2018-08-15 23:29:03 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#elif WINUI
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
2024-05-27 17:30:58 +02:00
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using ImageSource = Avalonia.Media.IImage;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2018-12-09 17:14:32 +01:00
|
|
|
|
namespace MapControl.MBTiles
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2025-11-13 15:32:01 +01:00
|
|
|
|
public sealed class MBTileSource : TileSource, IDisposable
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2025-04-01 16:11:53 +02:00
|
|
|
|
private static ILogger logger;
|
2025-09-14 21:57:19 +02:00
|
|
|
|
private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger<MBTileSource>();
|
2025-04-01 16:11:53 +02:00
|
|
|
|
|
2024-08-20 18:00:04 +02:00
|
|
|
|
private SQLiteConnection connection;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
2024-08-20 18:00:04 +02:00
|
|
|
|
public IDictionary<string, string> Metadata { get; } = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
|
|
public async Task OpenAsync(string file)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2024-08-20 18:00:04 +02:00
|
|
|
|
Close();
|
|
|
|
|
|
|
2025-08-31 14:47:47 +02:00
|
|
|
|
connection = new SQLiteConnection("Data Source=" + FilePath.GetFullPath(file) + ";Read Only=True");
|
2024-08-20 18:00:04 +02:00
|
|
|
|
|
|
|
|
|
|
await connection.OpenAsync();
|
|
|
|
|
|
|
2025-11-13 17:06:33 +01:00
|
|
|
|
using var command = new SQLiteCommand("select * from metadata", connection);
|
2024-08-20 18:00:04 +02:00
|
|
|
|
|
2025-11-13 17:06:33 +01:00
|
|
|
|
var reader = await command.ExecuteReaderAsync();
|
|
|
|
|
|
|
|
|
|
|
|
while (await reader.ReadAsync())
|
|
|
|
|
|
{
|
|
|
|
|
|
Metadata[(string)reader["name"]] = (string)reader["value"];
|
2024-08-20 18:00:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (connection != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Metadata.Clear();
|
|
|
|
|
|
connection.Dispose();
|
|
|
|
|
|
connection = null;
|
2021-11-10 22:31:06 +01:00
|
|
|
|
}
|
2019-07-18 21:09:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-15 23:29:03 +02:00
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
2025-02-23 18:49:07 +01:00
|
|
|
|
Close();
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
|
|
|
|
|
|
{
|
2021-11-10 22:31:06 +01:00
|
|
|
|
ImageSource image = null;
|
|
|
|
|
|
|
2024-08-20 18:00:04 +02:00
|
|
|
|
try
|
2021-11-10 22:31:06 +01:00
|
|
|
|
{
|
2025-11-13 17:06:33 +01:00
|
|
|
|
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);
|
2025-02-23 18:49:07 +01:00
|
|
|
|
|
2025-11-13 17:06:33 +01:00
|
|
|
|
var buffer = (byte[])await command.ExecuteScalarAsync();
|
2025-03-09 21:40:37 +01:00
|
|
|
|
|
2025-11-13 17:06:33 +01:00
|
|
|
|
if (buffer?.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
image = await ImageLoader.LoadImageAsync(buffer);
|
2021-11-10 22:31:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-20 18:00:04 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-04-01 16:11:53 +02:00
|
|
|
|
Logger?.LogError(ex, "LoadImageAsync");
|
2024-08-20 18:00:04 +02:00
|
|
|
|
}
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
2021-11-10 22:31:06 +01:00
|
|
|
|
return image;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|