25 : mFilename( filename )
35 const int result = mDatabase.
open_v2( mFilename, SQLITE_OPEN_READONLY,
nullptr );
36 if ( result != SQLITE_OK )
46 return bool( mDatabase );
54 if ( QFile::exists( mFilename ) )
58 int result = mDatabase.
open_v2( mFilename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr );
59 if ( result != SQLITE_OK )
66 "CREATE TABLE metadata (name text, value text);" \
67 "CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob);" \
68 "CREATE UNIQUE INDEX tile_index on tiles (zoom_level, tile_column, tile_row);";
70 result = mDatabase.
exec( sql, errorMessage );
71 if ( result != SQLITE_OK )
73 QgsDebugMsg( QStringLiteral(
"Failed to initialize MBTiles database: " ) + errorMessage );
84 QgsDebugMsg( QStringLiteral(
"MBTiles database not open: " ) + mFilename );
89 const QString sql = QStringLiteral(
"select value from metadata where name='%1'" ).arg( key );
91 if ( result != SQLITE_OK )
93 QgsDebugMsg( QStringLiteral(
"MBTile failed to prepare statement: " ) + sql );
97 if ( preparedStatement.
step() != SQLITE_ROW )
99 QgsDebugMsg( QStringLiteral(
"MBTile metadata value not found: " ) + key );
110 QgsDebugMsg( QStringLiteral(
"MBTiles database not open: " ) + mFilename );
117 if ( result != SQLITE_OK )
119 QgsDebugMsg( QStringLiteral(
"MBTile failed to prepare statement: " ) + sql );
123 if ( preparedStatement.
step() != SQLITE_DONE )
125 QgsDebugMsg( QStringLiteral(
"MBTile metadata value failed to be set: " ) + key );
133 if ( boundsStr.isEmpty() )
135 QStringList boundsArray = boundsStr.split(
',' );
136 if ( boundsArray.count() != 4 )
139 return QgsRectangle( boundsArray[0].toDouble(), boundsArray[1].toDouble(),
140 boundsArray[2].toDouble(), boundsArray[3].toDouble() );
147 QgsDebugMsg( QStringLiteral(
"MBTiles database not open: " ) + mFilename );
152 const QString sql = QStringLiteral(
"select tile_data from tiles where zoom_level=%1 and tile_column=%2 and tile_row=%3" ).arg( z ).arg( x ).arg( y );
154 if ( result != SQLITE_OK )
156 QgsDebugMsg( QStringLiteral(
"MBTile failed to prepare statement: " ) + sql );
160 if ( preparedStatement.
step() != SQLITE_ROW )
163 QgsDebugMsgLevel( QStringLiteral(
"MBTile not found: z=%1 x=%2 y=%3" ).arg( z ).arg( x ).arg( y ), 2 );
173 const QByteArray tileBlob =
tileData( z, x, y );
174 if ( !tileImage.loadFromData( tileBlob ) )
176 QgsDebugMsg( QStringLiteral(
"MBTile data failed to load: z=%1 x=%2 y=%3" ).arg( z ).arg( x ).arg( y ) );
186 QgsDebugMsg( QStringLiteral(
"MBTiles database not open: " ) + mFilename );
191 const QString sql = QStringLiteral(
"insert into tiles values (%1, %2, %3, ?)" ).arg( z ).arg( x ).arg( y );
193 if ( result != SQLITE_OK )
195 QgsDebugMsg( QStringLiteral(
"MBTile failed to prepare statement: " ) + sql );
199 sqlite3_bind_blob( preparedStatement.get(), 1, data.constData(), data.size(), SQLITE_TRANSIENT );
201 if ( preparedStatement.
step() != SQLITE_DONE )
203 QgsDebugMsg( QStringLiteral(
"MBTile tile failed to be set: %1,%2,%3" ).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...
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
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.
int exec(const QString &sql, QString &errorMessage) const
Executes the sql command in the database.
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)