-
Notifications
You must be signed in to change notification settings - Fork 59
/
MBTileSource.UWP.cs
85 lines (71 loc) · 2.65 KB
/
MBTileSource.UWP.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media;
namespace MapControl
{
public class MBTileSource : TileSource, IDisposable
{
private readonly SqliteConnection connection;
public IDictionary<string, string> Metadata { get; } = new Dictionary<string, string>();
public MBTileSource(string file)
{
connection = new SqliteConnection("Data Source=" + file);
connection.Open();
using (var command = new SqliteCommand("select * from metadata", connection))
{
var reader = command.ExecuteReader();
while (reader.Read())
{
Metadata[(string)reader["name"]] = (string)reader["value"];
}
}
}
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
{
ImageSource imageSource = 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);
var buffer = await command.ExecuteScalarAsync() as byte[];
if (buffer != null)
{
using (var stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(buffer.AsBuffer());
stream.Seek(0);
imageSource = await ImageLoader.CreateImageSourceAsync(stream);
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine("MBTileSource: {0}/{1}/{2}: {3}", zoomLevel, x, y, ex.Message);
}
return imageSource;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing && connection != null)
{
connection.Close();
}
}
}
}