25using namespace Qt::StringLiterals;
28 : mFilename( filename )
36 if ( mFilename.isEmpty() )
39 const int result = mDatabase.open_v2( mFilename, SQLITE_OPEN_READONLY,
nullptr );
40 if ( result != SQLITE_OK )
42 mLastError = mDatabase.errorMessage();
43 QgsDebugError( u
"Can't open MBTiles database: %1"_s.arg( mLastError ) );
51 return bool( mDatabase );
59 if ( !mDeferredIndexCreation )
63 mDatabase.exec( u
"PRAGMA journal_mode = DELETE;"_s, errorMessage );
65 const int result = mDatabase.exec( u
"CREATE UNIQUE INDEX IF NOT EXISTS tile_index ON tiles (zoom_level, tile_column, tile_row);"_s, mLastError );
67 return result == SQLITE_OK;
75 if ( QFile::exists( mFilename ) )
78 int result = mDatabase.open_v2( mFilename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr );
79 if ( result != SQLITE_OK )
81 mLastError = mDatabase.errorMessage();
82 QgsDebugError( u
"Can't create MBTiles database: %1"_s.arg( mLastError ) );
89 mDatabase.exec( u
"PRAGMA journal_mode = WAL;"_s, errorMessage );
90 mDatabase.exec( u
"PRAGMA synchronous = OFF;"_s, errorMessage );
91 mDatabase.exec( u
"PRAGMA temp_store = MEMORY;"_s, errorMessage );
92 mDatabase.exec( u
"PRAGMA cache_size = -64000;"_s, errorMessage );
94 QString sql =
"CREATE TABLE metadata (name text, value text);"
95 "CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob);";
96 if ( !deferIndexCreation )
98 sql +=
"CREATE UNIQUE INDEX tile_index on tiles (zoom_level, tile_column, tile_row);";
102 mDeferredIndexCreation =
true;
105 result = mDatabase.exec( sql, mLastError );
106 if ( result != SQLITE_OK )
108 QgsDebugError( u
"Failed to initialize MBTiles database: "_s + mLastError );
119 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
124 const QString sql = u
"select value from metadata where name='%1'"_s.arg( key );
126 if ( result != SQLITE_OK )
128 mLastError = mDatabase.errorMessage();
129 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
133 if ( preparedStatement.
step() != SQLITE_ROW )
135 mLastError = mDatabase.errorMessage();
136 QgsDebugError( u
"MBTile metadata value not found: "_s + key );
147 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
154 if ( result != SQLITE_OK )
156 mLastError = mDatabase.errorMessage();
157 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
161 if ( preparedStatement.
step() != SQLITE_DONE )
163 mLastError = mDatabase.errorMessage();
164 QgsDebugError( u
"MBTile metadata value failed to be set: "_s + key );
172 if ( boundsStr.isEmpty() )
174 QStringList boundsArray = boundsStr.split(
',' );
175 if ( boundsArray.count() != 4 )
178 return QgsRectangle( boundsArray[0].toDouble(), boundsArray[1].toDouble(), boundsArray[2].toDouble(), boundsArray[3].toDouble() );
185 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
190 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 );
192 if ( result != SQLITE_OK )
194 mLastError = mDatabase.errorMessage();
195 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
199 if ( preparedStatement.
step() != SQLITE_ROW )
202 QgsDebugMsgLevel( u
"MBTile not found: z=%1 x=%2 y=%3"_s.arg( z ).arg( x ).arg( y ), 2 );
212 const QByteArray tileBlob =
tileData( z, x, y );
213 if ( !tileImage.loadFromData( tileBlob ) )
215 QgsDebugError( u
"MBTile data failed to load: z=%1 x=%2 y=%3"_s.arg( z ).arg( x ).arg( y ) );
225 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
230 const QString sql = u
"insert into tiles values (%1, %2, %3, ?)"_s.arg( z ).arg( x ).arg( y );
232 if ( result != SQLITE_OK )
234 mLastError = mDatabase.errorMessage();
235 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
239 sqlite3_bind_blob( preparedStatement.get(), 1, data.constData(), data.size(), SQLITE_TRANSIENT );
241 if ( preparedStatement.
step() != SQLITE_DONE )
243 mLastError = mDatabase.errorMessage();
244 QgsDebugError( u
"MBTile tile failed to be set: %1,%2,%3"_s.arg( z ).arg( x ).arg( y ) );
251 if ( tiles.isEmpty() )
258 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
262 int result = mDatabase.exec( u
"BEGIN TRANSACTION;"_s, mLastError );
263 if ( result != SQLITE_OK )
265 QgsDebugError( u
"Failed to begin transaction: %1"_s.arg( mLastError ) );
269 const QString sql = u
"INSERT OR REPLACE INTO tiles (zoom_level, tile_column, tile_row, tile_data) VALUES (?, ?, ?, ?)"_s;
271 if ( result != SQLITE_OK )
273 mLastError = mDatabase.errorMessage();
274 QgsDebugError( u
"MBTile failed to prepare statement: %1"_s.arg( sql ) );
275 QString errorMessage;
276 mDatabase.exec( u
"ROLLBACK TRANSACTION;"_s, errorMessage );
280 for (
const TileData &tile : tiles )
282 sqlite3_reset( preparedStatement.get() );
283 sqlite3_clear_bindings( preparedStatement.get() );
285 sqlite3_bind_int( preparedStatement.get(), 1, tile.z );
286 sqlite3_bind_int( preparedStatement.get(), 2, tile.x );
287 sqlite3_bind_int( preparedStatement.get(), 3, tile.y );
288 sqlite3_bind_blob( preparedStatement.get(), 4, tile.data.constData(), tile.data.size(), SQLITE_TRANSIENT );
290 if ( preparedStatement.
step() != SQLITE_DONE )
292 mLastError = mDatabase.errorMessage();
293 QgsDebugError( u
"MBTile tile failed to be set: %1,%2,%3"_s.arg( tile.z ).arg( tile.x ).arg( tile.y ) );
297 result = mDatabase.exec( u
"COMMIT TRANSACTION;"_s, mLastError );
298 if ( result != SQLITE_OK )
300 QgsDebugError( u
"Failed to commit transaction: %1"_s.arg( mLastError ) );
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 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.