2018-08-15 23:29:03 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2024-02-03 21:01:53 +01:00
|
|
|
|
// Copyright © 2024 Clemens Fischer
|
2018-08-15 23:29:03 +02:00
|
|
|
|
// 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;
|
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
|
|
|
|
{
|
2019-11-17 00:10:08 +01:00
|
|
|
|
public class MBTileSource : TileSource, IDisposable
|
2018-08-15 23:29:03 +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();
|
|
|
|
|
|
|
|
|
|
|
|
connection = new SQLiteConnection("Data Source=" + Path.GetFullPath(file));
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-07-18 21:09:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-15 23:29:03 +02: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
|
|
|
|
}
|
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
|
|
|
|
{
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|