mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Version 4.15.1: Simplified MBTiles.
This commit is contained in:
parent
08e1823e06
commit
d0e671585b
|
|
@ -15,11 +15,28 @@ namespace MapControl.MBTiles
|
||||||
{
|
{
|
||||||
private readonly SQLiteConnection connection;
|
private readonly SQLiteConnection connection;
|
||||||
|
|
||||||
|
public IDictionary<string, string> Metadata { get; } = new Dictionary<string, string>();
|
||||||
|
|
||||||
private MBTileData(string file)
|
private MBTileData(string file)
|
||||||
{
|
{
|
||||||
connection = new SQLiteConnection("Data Source=" + Path.GetFullPath(file));
|
connection = new SQLiteConnection("Data Source=" + Path.GetFullPath(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
connection.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<MBTileData> CreateAsync(string file)
|
||||||
|
{
|
||||||
|
var tileData = new MBTileData(file);
|
||||||
|
|
||||||
|
await tileData.OpenAsync();
|
||||||
|
await tileData.ReadMetadataAsync();
|
||||||
|
|
||||||
|
return tileData;
|
||||||
|
}
|
||||||
|
|
||||||
private async Task OpenAsync()
|
private async Task OpenAsync()
|
||||||
{
|
{
|
||||||
await connection.OpenAsync();
|
await connection.OpenAsync();
|
||||||
|
|
@ -35,24 +52,8 @@ namespace MapControl.MBTiles
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<MBTileData> CreateAsync(string file)
|
private async Task ReadMetadataAsync()
|
||||||
{
|
{
|
||||||
var tileData = new MBTileData(file);
|
|
||||||
|
|
||||||
await tileData.OpenAsync();
|
|
||||||
|
|
||||||
return tileData;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
connection.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IDictionary<string, string>> ReadMetadataAsync()
|
|
||||||
{
|
|
||||||
var metadata = new Dictionary<string, string>();
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var command = new SQLiteCommand("select * from metadata", connection))
|
using (var command = new SQLiteCommand("select * from metadata", connection))
|
||||||
|
|
@ -61,7 +62,7 @@ namespace MapControl.MBTiles
|
||||||
|
|
||||||
while (await reader.ReadAsync())
|
while (await reader.ReadAsync())
|
||||||
{
|
{
|
||||||
metadata[(string)reader["name"]] = (string)reader["value"];
|
Metadata[(string)reader["name"]] = (string)reader["value"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -69,17 +70,15 @@ namespace MapControl.MBTiles
|
||||||
{
|
{
|
||||||
Debug.WriteLine("MBTileData: " + ex.Message);
|
Debug.WriteLine("MBTileData: " + ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return metadata;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task WriteMetadataAsync(IDictionary<string, string> metaData)
|
public async Task WriteMetadataAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var command = new SQLiteCommand("insert or replace into metadata (name, value) values (@n, @v)", connection))
|
using (var command = new SQLiteCommand("insert or replace into metadata (name, value) values (@n, @v)", connection))
|
||||||
{
|
{
|
||||||
foreach (var keyValue in metaData)
|
foreach (var keyValue in Metadata)
|
||||||
{
|
{
|
||||||
command.Parameters.AddWithValue("@n", keyValue.Key);
|
command.Parameters.AddWithValue("@n", keyValue.Key);
|
||||||
command.Parameters.AddWithValue("@v", keyValue.Value);
|
command.Parameters.AddWithValue("@v", keyValue.Value);
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,14 @@ namespace MapControl.MBTiles
|
||||||
set { SetValue(FileProperty, value); }
|
set { SetValue(FileProperty, value); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// May be overridden to create a derived MBTileSource that handles other tile formats than png and jpg, e.g. pbf.
|
||||||
|
/// </summary>
|
||||||
|
protected virtual MBTileSource CreateTileSource(MBTileData tileData)
|
||||||
|
{
|
||||||
|
return new MBTileSource(tileData);
|
||||||
|
}
|
||||||
|
|
||||||
private async Task FilePropertyChanged(string file)
|
private async Task FilePropertyChanged(string file)
|
||||||
{
|
{
|
||||||
(TileSource as MBTileSource)?.Dispose();
|
(TileSource as MBTileSource)?.Dispose();
|
||||||
|
|
@ -48,33 +56,32 @@ namespace MapControl.MBTiles
|
||||||
|
|
||||||
if (file != null)
|
if (file != null)
|
||||||
{
|
{
|
||||||
var tiledata = await MBTileData.CreateAsync(file);
|
var tileData = await MBTileData.CreateAsync(file);
|
||||||
var metadata = await tiledata.ReadMetadataAsync();
|
|
||||||
string s;
|
|
||||||
int minZoom;
|
int minZoom;
|
||||||
int maxZoom;
|
int maxZoom;
|
||||||
|
string s;
|
||||||
|
|
||||||
if (metadata.TryGetValue("name", out s))
|
if (tileData.Metadata.TryGetValue("name", out s))
|
||||||
{
|
{
|
||||||
SourceName = s;
|
SourceName = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metadata.TryGetValue("description", out s))
|
if (tileData.Metadata.TryGetValue("description", out s))
|
||||||
{
|
{
|
||||||
Description = s;
|
Description = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metadata.TryGetValue("minzoom", out s) && int.TryParse(s, out minZoom))
|
if (tileData.Metadata.TryGetValue("minzoom", out s) && int.TryParse(s, out minZoom))
|
||||||
{
|
{
|
||||||
MinZoomLevel = minZoom;
|
MinZoomLevel = minZoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metadata.TryGetValue("maxzoom", out s) && int.TryParse(s, out maxZoom))
|
if (tileData.Metadata.TryGetValue("maxzoom", out s) && int.TryParse(s, out maxZoom))
|
||||||
{
|
{
|
||||||
MaxZoomLevel = maxZoom;
|
MaxZoomLevel = maxZoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
TileSource = new MBTileSource(tiledata);
|
TileSource = CreateTileSource(tileData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ using System.Windows.Media;
|
||||||
|
|
||||||
namespace MapControl.MBTiles
|
namespace MapControl.MBTiles
|
||||||
{
|
{
|
||||||
public sealed class MBTileSource : TileSource, IDisposable
|
public class MBTileSource : TileSource, IDisposable
|
||||||
{
|
{
|
||||||
public MBTileData TileData { get; }
|
public MBTileData TileData { get; }
|
||||||
|
|
||||||
|
|
@ -23,7 +23,15 @@ namespace MapControl.MBTiles
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
TileData.Dispose();
|
Dispose(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
TileData.Dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
|
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue