XAML-Map-Control/MBTiles/Shared/MBTileSource.cs

106 lines
2.9 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2024-08-20 18:00:04 +02:00
using System.Collections.Generic;
using System.Data.SQLite;
2021-11-10 22:31:06 +01:00
using System.Diagnostics;
2024-08-20 18:00:04 +02:00
using System.IO;
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
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml.Media;
#elif AVALONIA
using ImageSource = Avalonia.Media.IImage;
#endif
namespace MapControl.MBTiles
{
2019-11-17 00:10:08 +01:00
public class MBTileSource : TileSource, IDisposable
{
2024-08-20 18:00:04 +02:00
private SQLiteConnection connection;
2024-08-20 18:00:04 +02:00
public IDictionary<string, string> Metadata { get; } = new Dictionary<string, string>();
public async Task OpenAsync(string file)
{
2024-08-20 18:00:04 +02:00
Close();
2024-09-05 20:38:49 +02:00
connection = new SQLiteConnection("Data Source=" + Path.GetFullPath(file) + ";Read Only=True");
2024-08-20 18:00:04 +02:00
await connection.OpenAsync();
using (var command = new SQLiteCommand("select * from metadata", connection))
2021-11-10 22:31:06 +01:00
{
2024-08-20 18:00:04 +02:00
var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
Metadata[(string)reader["name"]] = (string)reader["value"];
}
}
}
public void Close()
{
if (connection != null)
{
Metadata.Clear();
connection.Dispose();
connection = null;
2021-11-10 22:31:06 +01:00
}
}
public void Dispose()
{
2019-11-17 00:10:08 +01:00
Dispose(true);
2021-11-10 22:31:06 +01:00
GC.SuppressFinalize(this);
2019-11-17 00:10:08 +01:00
}
protected virtual void Dispose(bool disposing)
{
2024-08-20 18:00:04 +02:00
if (disposing)
2019-11-17 00:10:08 +01:00
{
2024-08-20 18:00:04 +02:00
Close();
}
}
public async Task<byte[]> ReadImageBufferAsync(int x, int y, int zoomLevel)
{
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);
return await command.ExecuteScalarAsync() as byte[];
2019-11-17 00:10:08 +01: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
{
2024-08-20 18:00:04 +02:00
var buffer = await ReadImageBufferAsync(x, y, zoomLevel);
2021-11-10 22:31:06 +01:00
if (buffer != null)
{
2024-08-20 18:00:04 +02:00
image = await ImageLoader.LoadImageAsync(buffer);
2021-11-10 22:31:06 +01:00
}
}
2024-08-20 18:00:04 +02:00
catch (Exception ex)
{
2024-08-31 16:39:49 +02:00
Debug.WriteLine($"{nameof(MBTileSource)}: {ex.Message}");
2024-08-20 18:00:04 +02:00
}
2021-11-10 22:31:06 +01:00
return image;
}
}
}