Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsmbtiles.h 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 : : #ifndef QGSMBTILES_H 17 : : #define QGSMBTILES_H 18 : : 19 : : #include "qgis_core.h" 20 : : 21 : : #include "sqlite3.h" 22 : : #include "qgssqliteutils.h" 23 : : 24 : : #define SIP_NO_FILE 25 : : 26 : : class QImage; 27 : : class QgsRectangle; 28 : : 29 : : /** 30 : : * \ingroup core 31 : : * \brief Utility class for reading and writing MBTiles files (which are SQLite3 databases). 32 : : * 33 : : * See the specification for more details: 34 : : * https://github.com/mapbox/mbtiles-spec/blob/master/1.3/spec.md 35 : : * 36 : : * \since QGIS 3.14 37 : : */ 38 : 0 : class CORE_EXPORT QgsMbTiles 39 : : { 40 : : public: 41 : : //! Constructs MBTiles reader (but it does not open the file yet) 42 : : explicit QgsMbTiles( const QString &filename ); 43 : : 44 : : //! Tries to open the file, returns true on success 45 : : bool open(); 46 : : 47 : : //! Returns whether the MBTiles file is currently opened 48 : : bool isOpen() const; 49 : : 50 : : /** 51 : : * Creates a new MBTiles file and initializes it with metadata and tiles tables. 52 : : * It is up to the caller to set appropriate metadata entries and add tiles afterwards. 53 : : * Returns TRUE on success. If the file exists already, returns FALSE. 54 : : */ 55 : : bool create(); 56 : : 57 : : //! Requests metadata value for the given key 58 : : QString metadataValue( const QString &key ); 59 : : 60 : : /** 61 : : * Sets metadata value for the given key. Does not overwrite existing entries. 62 : : * \note the database has to be opened in read-write mode (currently only when opened with create() 63 : : */ 64 : : void setMetadataValue( const QString &key, const QString &value ); 65 : : 66 : : //! Returns bounding box from metadata, given in WGS 84 (if available) 67 : : QgsRectangle extent(); 68 : : 69 : : //! Returns raw tile data for given tile 70 : : QByteArray tileData( int z, int x, int y ); 71 : : 72 : : //! Returns tile decoded as a raster image (if stored in a known format like JPG or PNG) 73 : : QImage tileDataAsImage( int z, int x, int y ); 74 : : 75 : : /** 76 : : * Adds tile data for the given tile coordinates. Does not overwrite existing entries. 77 : : * \note the database has to be opened in read-write mode (currently only when opened with create() 78 : : */ 79 : : void setTileData( int z, int x, int y, const QByteArray &data ); 80 : : 81 : : //! Decodes gzip byte stream, returns true on success. Useful for reading vector tiles. 82 : : static bool decodeGzip( const QByteArray &bytesIn, QByteArray &bytesOut ); 83 : : //! Encodes gzip byte stream, returns true on success. Useful for writing vector tiles. 84 : : static bool encodeGzip( const QByteArray &bytesIn, QByteArray &bytesOut ); 85 : : 86 : : private: 87 : : QString mFilename; 88 : : sqlite3_database_unique_ptr mDatabase; 89 : : }; 90 : : 91 : : 92 : : #endif // QGSMBTILES_H