2018-08-15 23:29:03 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 Clemens Fischer
|
2018-08-15 23:29:03 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
2019-06-15 01:39:07 +02:00
|
|
|
|
using System.IO;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using SQLiteCommand = Microsoft.Data.Sqlite.SqliteCommand;
|
|
|
|
|
|
using SQLiteConnection = Microsoft.Data.Sqlite.SqliteConnection;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Data.SQLite;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2018-12-09 17:14:32 +01:00
|
|
|
|
namespace MapControl.MBTiles
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
public class MBTileData : IDisposable
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly SQLiteConnection connection;
|
|
|
|
|
|
|
|
|
|
|
|
public MBTileData(string file)
|
|
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
connection = new SQLiteConnection("Data Source=" + Path.GetFullPath(file));
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task OpenAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
return connection.OpenAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
|
|
{
|
|
|
|
|
|
connection.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IDictionary<string, string>> ReadMetadataAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var metadata = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var command = new SQLiteCommand("select * from metadata", connection))
|
|
|
|
|
|
{
|
|
|
|
|
|
var reader = await command.ExecuteReaderAsync();
|
|
|
|
|
|
|
|
|
|
|
|
while (await reader.ReadAsync())
|
|
|
|
|
|
{
|
|
|
|
|
|
metadata[(string)reader["name"]] = (string)reader["value"];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("MBTileData: " + ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-17 16:49:16 +02:00
|
|
|
|
public async Task WriteMetadataAsync(IDictionary<string, string> metadata)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var command = new SQLiteCommand("create table if not exists metadata (name string, value string)", connection))
|
|
|
|
|
|
{
|
|
|
|
|
|
await command.ExecuteNonQueryAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-17 16:49:16 +02:00
|
|
|
|
using (var command = new SQLiteCommand("insert or replace into metadata (name, value) values (@n, @v)", connection))
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2018-08-17 16:49:16 +02:00
|
|
|
|
foreach (var keyValue in metadata)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
command.Parameters.AddWithValue("@n", keyValue.Key);
|
2018-08-17 16:49:16 +02:00
|
|
|
|
command.Parameters.AddWithValue("@v", keyValue.Value);
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
|
|
|
|
|
await command.ExecuteNonQueryAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("MBTileData: " + ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<byte[]> ReadImageBufferAsync(int x, int y, int zoomLevel)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] imageBuffer = null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
imageBuffer = await command.ExecuteScalarAsync() as byte[];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("MBTileData: {0}/{1}/{2}: {3}", zoomLevel, x, y, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return imageBuffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task WriteImageBufferAsync(int x, int y, int zoomLevel, byte[] imageBuffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var command = new SQLiteCommand("create table if not exists tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob)", connection))
|
|
|
|
|
|
{
|
|
|
|
|
|
await command.ExecuteNonQueryAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using (var command = new SQLiteCommand("insert or replace into tiles (zoom_level, tile_column, tile_row, tile_data) values (@z, @x, @y, @b)", connection))
|
|
|
|
|
|
{
|
|
|
|
|
|
command.Parameters.AddWithValue("@z", zoomLevel);
|
|
|
|
|
|
command.Parameters.AddWithValue("@x", x);
|
|
|
|
|
|
command.Parameters.AddWithValue("@y", (1 << zoomLevel) - y - 1);
|
|
|
|
|
|
command.Parameters.AddWithValue("@b", imageBuffer);
|
|
|
|
|
|
|
|
|
|
|
|
await command.ExecuteNonQueryAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("MBTileData: {0}/{1}/{2}: {3}", zoomLevel, x, y, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|