25using namespace Qt::StringLiterals;
28 : mFilename( filename )
36 if ( mFilename.isEmpty() )
40 const int result = mDatabase.
open_v2( mFilename, SQLITE_OPEN_READONLY,
nullptr );
41 if ( result != SQLITE_OK )
51 return bool( mDatabase );
59 if ( !mDeferredIndexCreation )
63 const int result = mDatabase.exec( u
"CREATE UNIQUE INDEX IF NOT EXISTS tile_index ON tiles (zoom_level, tile_column, tile_row);"_s, errorMessage );
65 return result == SQLITE_OK;
73 if ( QFile::exists( mFilename ) )
77 int result = mDatabase.
open_v2( mFilename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr );
78 if ( result != SQLITE_OK )
87 mDatabase.exec( u
"PRAGMA journal_mode = WAL;"_s, errorMessage );
88 mDatabase.exec( u
"PRAGMA synchronous = OFF;"_s, errorMessage );
89 mDatabase.exec( u
"PRAGMA temp_store = MEMORY;"_s, errorMessage );
90 mDatabase.exec( u
"PRAGMA cache_size = -64000;"_s, errorMessage );
92 QString sql =
"CREATE TABLE metadata (name text, value text);"
93 "CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob);";
94 if ( !deferIndexCreation )
96 sql +=
"CREATE UNIQUE INDEX tile_index on tiles (zoom_level, tile_column, tile_row);";
100 mDeferredIndexCreation =
true;
103 result = mDatabase.exec( sql, errorMessage );
104 if ( result != SQLITE_OK )
106 QgsDebugError( u
"Failed to initialize MBTiles database: "_s + errorMessage );
117 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
122 const QString sql = u
"select value from metadata where name='%1'"_s.arg( key );
124 if ( result != SQLITE_OK )
126 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
130 if ( preparedStatement.
step() != SQLITE_ROW )
132 QgsDebugError( u
"MBTile metadata value not found: "_s + key );
143 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
150 if ( result != SQLITE_OK )
152 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
156 if ( preparedStatement.
step() != SQLITE_DONE )
158 QgsDebugError( u
"MBTile metadata value failed to be set: "_s + key );
166 if ( boundsStr.isEmpty() )
168 QStringList boundsArray = boundsStr.split(
',' );
169 if ( boundsArray.count() != 4 )
172 return QgsRectangle( boundsArray[0].toDouble(), boundsArray[1].toDouble(), boundsArray[2].toDouble(), boundsArray[3].toDouble() );
179 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
184 const QString sql = u
"select tile_data from tiles where zoom_level=%1 and tile_column=%2 and tile_row=%3"_s.arg( z ).arg( x ).arg( y );
186 if ( result != SQLITE_OK )
188 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
192 if ( preparedStatement.
step() != SQLITE_ROW )
195 QgsDebugMsgLevel( u
"MBTile not found: z=%1 x=%2 y=%3"_s.arg( z ).arg( x ).arg( y ), 2 );
205 const QByteArray tileBlob =
tileData( z, x, y );
206 if ( !tileImage.loadFromData( tileBlob ) )
208 QgsDebugError( u
"MBTile data failed to load: z=%1 x=%2 y=%3"_s.arg( z ).arg( x ).arg( y ) );
218 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
223 const QString sql = u
"insert into tiles values (%1, %2, %3, ?)"_s.arg( z ).arg( x ).arg( y );
225 if ( result != SQLITE_OK )
227 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
231 sqlite3_bind_blob( preparedStatement.get(), 1, data.constData(), data.size(), SQLITE_TRANSIENT );
233 if ( preparedStatement.
step() != SQLITE_DONE )
235 QgsDebugError( u
"MBTile tile failed to be set: %1,%2,%3"_s.arg( z ).arg( x ).arg( y ) );
242 if ( tiles.isEmpty() )
249 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
253 QString errorMessage;
254 int result = mDatabase.exec( u
"BEGIN TRANSACTION;"_s, errorMessage );
255 if ( result != SQLITE_OK )
257 QgsDebugError( u
"Failed to begin transaction: %1"_s.arg( errorMessage ) );
261 const QString sql = u
"INSERT OR REPLACE INTO tiles (zoom_level, tile_column, tile_row, tile_data) VALUES (?, ?, ?, ?)"_s;
263 if ( result != SQLITE_OK )
265 QgsDebugError( u
"MBTile failed to prepare statement: %1"_s.arg( sql ) );
266 mDatabase.exec( u
"ROLLBACK TRANSACTION;"_s, errorMessage );
270 for (
const TileData &tile : tiles )
272 sqlite3_reset( preparedStatement.get() );
273 sqlite3_clear_bindings( preparedStatement.get() );
275 sqlite3_bind_int( preparedStatement.get(), 1, tile.z );
276 sqlite3_bind_int( preparedStatement.get(), 2, tile.x );
277 sqlite3_bind_int( preparedStatement.get(), 3, tile.y );
278 sqlite3_bind_blob( preparedStatement.get(), 4, tile.data.constData(), tile.data.size(), SQLITE_TRANSIENT );
280 if ( preparedStatement.
step() != SQLITE_DONE )
282 QgsDebugError( u
"MBTile tile failed to be set: %1,%2,%3"_s.arg( tile.z ).arg( tile.x ).arg( tile.y ) );
286 result = mDatabase.exec( u
"COMMIT TRANSACTION;"_s, errorMessage );
287 if ( result != SQLITE_OK )
289 QgsDebugError( u
"Failed to commit transaction: %1"_s.arg( errorMessage ) );
QgsMbTiles(const QString &filename)
Constructs MBTiles reader (but it does not open the file yet).
QString metadataValue(const QString &key) const
Requests metadata value for the given key.
bool open()
Tries to open the file, returns true on success.
QImage tileDataAsImage(int z, int x, int y) const
Returns tile decoded as a raster image (if stored in a known format like JPG or PNG).
bool finalize()
Finalizes the database after writing all tiles.
QByteArray tileData(int z, int x, int y) const
Returns raw tile data for given tile.
void setMetadataValue(const QString &key, const QString &value) const
Sets metadata value for the given key.
bool isOpen() const
Returns whether the MBTiles file is currently opened.
void setTileData(int z, int x, int y, const QByteArray &data) const
Adds tile data for the given tile coordinates.
bool create(bool deferIndexCreation=false)
Creates a new MBTiles file and initializes it with metadata and tiles tables.
QgsRectangle extent() const
Returns bounding box from metadata, given in WGS 84 (if available).
A rectangle specified with double values.
static QString quotedValue(const QVariant &value)
Returns a properly quoted and escaped version of value for use in SQL strings.
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
QString errorMessage() const
Returns the most recent error message encountered by the database.
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
int step()
Steps to the next record in the statement, returning the sqlite3 result code.
QByteArray columnAsBlob(int column) const
Returns the column value from the current statement row as raw byte array.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
Struct representing a raw image tile.