QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsmbtiles.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmbtiles.cpp
3 --------------------------------------
4 Date : January 2020
5 Copyright : (C) 2020 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsmbtiles.h"
17
18#include "qgslogger.h"
19#include "qgsrectangle.h"
20
21#include <QFile>
22#include <QImage>
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
27QgsMbTiles::QgsMbTiles( const QString &filename )
28 : mFilename( filename )
29{}
30
32{
33 if ( mDatabase )
34 return true; // already opened
35
36 if ( mFilename.isEmpty() )
37 return false;
38
39 const sqlite3_database_unique_ptr database;
40 const int result = mDatabase.open_v2( mFilename, SQLITE_OPEN_READONLY, nullptr );
41 if ( result != SQLITE_OK )
42 {
43 QgsDebugError( u"Can't open MBTiles database: %1"_s.arg( database.errorMessage() ) );
44 return false;
45 }
46 return true;
47}
48
50{
51 return bool( mDatabase );
52}
53
55{
56 if ( mDatabase )
57 return false;
58
59 if ( QFile::exists( mFilename ) )
60 return false;
61
62 const sqlite3_database_unique_ptr database;
63 int result = mDatabase.open_v2( mFilename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr );
64 if ( result != SQLITE_OK )
65 {
66 QgsDebugError( u"Can't create MBTiles database: %1"_s.arg( database.errorMessage() ) );
67 return false;
68 }
69
70 const QString sql = "CREATE TABLE metadata (name text, value text);"
71 "CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob);"
72 "CREATE UNIQUE INDEX tile_index on tiles (zoom_level, tile_column, tile_row);";
73 QString errorMessage;
74 result = mDatabase.exec( sql, errorMessage );
75 if ( result != SQLITE_OK )
76 {
77 QgsDebugError( u"Failed to initialize MBTiles database: "_s + errorMessage );
78 return false;
79 }
80
81 return true;
82}
83
84QString QgsMbTiles::metadataValue( const QString &key ) const
85{
86 if ( !mDatabase )
87 {
88 QgsDebugError( u"MBTiles database not open: "_s + mFilename );
89 return QString();
90 }
91
92 int result;
93 const QString sql = u"select value from metadata where name='%1'"_s.arg( key );
94 sqlite3_statement_unique_ptr preparedStatement = mDatabase.prepare( sql, result );
95 if ( result != SQLITE_OK )
96 {
97 QgsDebugError( u"MBTile failed to prepare statement: "_s + sql );
98 return QString();
99 }
100
101 if ( preparedStatement.step() != SQLITE_ROW )
102 {
103 QgsDebugError( u"MBTile metadata value not found: "_s + key );
104 return QString();
105 }
106
107 return preparedStatement.columnAsText( 0 );
108}
109
110void QgsMbTiles::setMetadataValue( const QString &key, const QString &value ) const
111{
112 if ( !mDatabase )
113 {
114 QgsDebugError( u"MBTiles database not open: "_s + mFilename );
115 return;
116 }
117
118 int result;
119 const QString sql = u"insert into metadata values (%1, %2)"_s.arg( QgsSqliteUtils::quotedValue( key ), QgsSqliteUtils::quotedValue( value ) );
120 sqlite3_statement_unique_ptr preparedStatement = mDatabase.prepare( sql, result );
121 if ( result != SQLITE_OK )
122 {
123 QgsDebugError( u"MBTile failed to prepare statement: "_s + sql );
124 return;
125 }
126
127 if ( preparedStatement.step() != SQLITE_DONE )
128 {
129 QgsDebugError( u"MBTile metadata value failed to be set: "_s + key );
130 return;
131 }
132}
133
135{
136 const QString boundsStr = metadataValue( "bounds" );
137 if ( boundsStr.isEmpty() )
138 return QgsRectangle();
139 QStringList boundsArray = boundsStr.split( ',' );
140 if ( boundsArray.count() != 4 )
141 return QgsRectangle();
142
143 return QgsRectangle( boundsArray[0].toDouble(), boundsArray[1].toDouble(), boundsArray[2].toDouble(), boundsArray[3].toDouble() );
144}
145
146QByteArray QgsMbTiles::tileData( int z, int x, int y ) const
147{
148 if ( !mDatabase )
149 {
150 QgsDebugError( u"MBTiles database not open: "_s + mFilename );
151 return QByteArray();
152 }
153
154 int result;
155 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 );
156 sqlite3_statement_unique_ptr preparedStatement = mDatabase.prepare( sql, result );
157 if ( result != SQLITE_OK )
158 {
159 QgsDebugError( u"MBTile failed to prepare statement: "_s + sql );
160 return QByteArray();
161 }
162
163 if ( preparedStatement.step() != SQLITE_ROW )
164 {
165 // this is not entirely unexpected -- user may have just requested a tile outside of the extent of the mbtiles package
166 QgsDebugMsgLevel( u"MBTile not found: z=%1 x=%2 y=%3"_s.arg( z ).arg( x ).arg( y ), 2 );
167 return QByteArray();
168 }
169
170 return preparedStatement.columnAsBlob( 0 );
171}
172
173QImage QgsMbTiles::tileDataAsImage( int z, int x, int y ) const
174{
175 QImage tileImage;
176 const QByteArray tileBlob = tileData( z, x, y );
177 if ( !tileImage.loadFromData( tileBlob ) )
178 {
179 QgsDebugError( u"MBTile data failed to load: z=%1 x=%2 y=%3"_s.arg( z ).arg( x ).arg( y ) );
180 return QImage();
181 }
182 return tileImage;
183}
184
185void QgsMbTiles::setTileData( int z, int x, int y, const QByteArray &data ) const
186{
187 if ( !mDatabase )
188 {
189 QgsDebugError( u"MBTiles database not open: "_s + mFilename );
190 return;
191 }
192
193 int result;
194 const QString sql = u"insert into tiles values (%1, %2, %3, ?)"_s.arg( z ).arg( x ).arg( y );
195 sqlite3_statement_unique_ptr preparedStatement = mDatabase.prepare( sql, result );
196 if ( result != SQLITE_OK )
197 {
198 QgsDebugError( u"MBTile failed to prepare statement: "_s + sql );
199 return;
200 }
201
202 sqlite3_bind_blob( preparedStatement.get(), 1, data.constData(), data.size(), SQLITE_TRANSIENT );
203
204 if ( preparedStatement.step() != SQLITE_DONE )
205 {
206 QgsDebugError( u"MBTile tile failed to be set: %1,%2,%3"_s.arg( z ).arg( x ).arg( y ) );
207 return;
208 }
209}
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)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59