25using namespace Qt::StringLiterals;
28 : mFilename( filename )
37 if ( mFilename.isEmpty() )
41 const int result = mDatabase.
open_v2( mFilename, SQLITE_OPEN_READONLY,
nullptr );
42 if ( result != SQLITE_OK )
52 return bool( mDatabase );
60 if ( QFile::exists( mFilename ) )
64 int result = mDatabase.
open_v2( mFilename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr );
65 if ( result != SQLITE_OK )
72 "CREATE TABLE metadata (name text, value text);" \
73 "CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob);" \
74 "CREATE UNIQUE INDEX tile_index on tiles (zoom_level, tile_column, tile_row);";
76 result = mDatabase.exec( sql, errorMessage );
77 if ( result != SQLITE_OK )
79 QgsDebugError( u
"Failed to initialize MBTiles database: "_s + errorMessage );
90 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
95 const QString sql = u
"select value from metadata where name='%1'"_s.arg( key );
97 if ( result != SQLITE_OK )
99 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
103 if ( preparedStatement.
step() != SQLITE_ROW )
105 QgsDebugError( u
"MBTile metadata value not found: "_s + key );
116 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
123 if ( result != SQLITE_OK )
125 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
129 if ( preparedStatement.
step() != SQLITE_DONE )
131 QgsDebugError( u
"MBTile metadata value failed to be set: "_s + key );
139 if ( boundsStr.isEmpty() )
141 QStringList boundsArray = boundsStr.split(
',' );
142 if ( boundsArray.count() != 4 )
145 return QgsRectangle( boundsArray[0].toDouble(), boundsArray[1].toDouble(),
146 boundsArray[2].toDouble(), boundsArray[3].toDouble() );
153 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
158 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 );
160 if ( result != SQLITE_OK )
162 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
166 if ( preparedStatement.
step() != SQLITE_ROW )
169 QgsDebugMsgLevel( u
"MBTile not found: z=%1 x=%2 y=%3"_s.arg( z ).arg( x ).arg( y ), 2 );
179 const QByteArray tileBlob =
tileData( z, x, y );
180 if ( !tileImage.loadFromData( tileBlob ) )
182 QgsDebugError( u
"MBTile data failed to load: z=%1 x=%2 y=%3"_s.arg( z ).arg( x ).arg( y ) );
192 QgsDebugError( u
"MBTiles database not open: "_s + mFilename );
197 const QString sql = u
"insert into tiles values (%1, %2, %3, ?)"_s.arg( z ).arg( x ).arg( y );
199 if ( result != SQLITE_OK )
201 QgsDebugError( u
"MBTile failed to prepare statement: "_s + sql );
205 sqlite3_bind_blob( preparedStatement.get(), 1, data.constData(), data.size(), SQLITE_TRANSIENT );
207 if ( preparedStatement.
step() != SQLITE_DONE )
209 QgsDebugError( u
"MBTile tile failed to be set: %1,%2,%3"_s.arg( z ).arg( x ).arg( y ) );
bool create()
Creates a new MBTiles file and initializes it with metadata and tiles tables.
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).
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.
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)