I am doing a windows store (8.1) app in c# Xaml to generate maps using the Bing Maps for c# extension with a mbtiles file, I've already used the project Portable Basemap Server to do the work but now I'm trying access the data in the mbtiles file by myself with SQLite.
I managed to get tiles from that kind of files in WPF but I don't know how to do it in a windows store project;
My code in WPF :
SQLiteConnection _sqlcon;
using (_sqlcon = new SQLiteConnection(String.Format("Data Source={0};Version=3;", "PATH_TO_MBTILES")))
{
_sqlcon.Open();
SQLiteCommand cmd = new SQLiteCommand(string.Format("SELECT tile_data FROM tiles WHERE tile_column={0} AND tile_row={1} AND zoom_level={2}", 2, 5, 3), _sqlcon);
object o = cmd.ExecuteScalar();
if (o != null)
{
byte[] c = (byte[])o;
using (MemoryStream stream = new MemoryStream(c))
{
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.StreamSource = stream;
bmp.EndInit();
img.Source = bmp;
}
}
}
With that code I get the tile at column 2 row 5 and zoom level 3.
But in windows store app I get a SQLiteException "Could not open database file" when I try to create a SQLiteConnection
with the same file (I am using the NuGet sqlite-net and the SqLite for Windows Runtime 8.1 extension)
My code in Windows Store App :
SQLiteConnection _sqlcon;
using (_sqlcon = new SQLiteConnection(String.Format("Data Source={0};Version=3;", "PATH_TO_MBTILES"))) //SQLiteExeption here
{
SQLiteCommand cmd = new SQLiteCommand(string.Format("SELECT tile_data FROM tiles WHERE tile_column={0} AND tile_row={1} AND zoom_level={2}", 2, 5, 3), _sqlcon);
byte[] o = cmd.ExecuteScalar<byte[]>();
//etc...
}
The debugger of Visual Studio send me to the SQLite.cs file of the sqlite-net NuGet :
public SQLiteConnection (string databasePath, SQLiteOpenFlags openFlags, bool storeDateTimeAsTicks = false)
{
if (string.IsNullOrEmpty (databasePath))
throw new ArgumentException ("Must be specified", "databasePath");
DatabasePath = databasePath;
#if NETFX_CORE
SQLite3.SetDirectory(/*temp directory type*/2, Windows.Storage.ApplicationData.Current.TemporaryFolder.Path);
#endif
Sqlite3DatabaseHandle handle;
#if SILVERLIGHT || USE_CSHARP_SQLITE
var r = SQLite3.Open (databasePath, out handle, (int)openFlags, IntPtr.Zero);
#else
// open using the byte[]
// in the case where the path may include Unicode
// force open to using UTF-8 using sqlite3_open_v2
var databasePathAsBytes = GetNullTerminatedUtf8 (DatabasePath);
var r = SQLite3.Open (databasePathAsBytes, out handle, (int) openFlags, IntPtr.Zero);
#endif
Handle = handle;
if (r != SQLite3.Result.OK) {
throw SQLiteException.New (r, String.Format ("Could not open database file: {0} ({1})", DatabasePath, r));
}
_open = true; // Debugger Stops here !
StoreDateTimeAsTicks = storeDateTimeAsTicks;
BusyTimeout = TimeSpan.FromSeconds (0.1);
}
I have found many examples of mbtiles files used in WPF but none in a windows store app.
Do the SQLite extensions for Windows store dev support MBTiles files as databases ? If yes what am I doing wrong ?
Windows Store apps can access only their own directories, but not the private files of the admin user.
Read the documentation, but you probably want to install the data as part of your app.