Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GeoPackage/MBTiles Translator 4 : * Purpose: Definition of common classes for GeoPackage and MBTiles drivers. 5 : * Author: Even Rouault <even dot rouault at spatialys dot com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2014-2016, Even Rouault <even dot rouault at spatialys dot com> 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #ifndef GPKGMBTILESCOMMON_H_INCLUDED 14 : #define GPKGMBTILESCOMMON_H_INCLUDED 15 : 16 : #include "cpl_string.h" 17 : #include "gdal_pam.h" 18 : #include <sqlite3.h> 19 : 20 : typedef struct 21 : { 22 : int nRow; 23 : int nCol; 24 : int nIdxWithinTileData; 25 : bool abBandDirty[4]; 26 : } CachedTileDesc; 27 : 28 : typedef enum 29 : { 30 : GPKG_TF_PNG_JPEG, 31 : GPKG_TF_PNG, 32 : GPKG_TF_PNG8, 33 : GPKG_TF_JPEG, 34 : GPKG_TF_WEBP, 35 : GPKG_TF_PNG_16BIT, // For GPKG elevation data 36 : GPKG_TF_TIFF_32BIT_FLOAT // For GPKG elevation data 37 : } GPKGTileFormat; 38 : 39 : GPKGTileFormat GDALGPKGMBTilesGetTileFormat(const char *pszTF); 40 : const char *GDALMBTilesGetTileFormatName(GPKGTileFormat); 41 : 42 : class GDALGPKGMBTilesLikePseudoDataset /* non final */ 43 : { 44 : friend class GDALGPKGMBTilesLikeRasterBand; 45 : 46 : GDALGPKGMBTilesLikePseudoDataset(const GDALGPKGMBTilesLikePseudoDataset &) = 47 : delete; 48 : GDALGPKGMBTilesLikePseudoDataset & 49 : operator=(const GDALGPKGMBTilesLikePseudoDataset &) = delete; 50 : 51 : protected: 52 : bool m_bNew = false; 53 : bool m_bHasModifiedTiles = false; 54 : 55 : CPLString m_osRasterTable{}; 56 : GDALDataType m_eDT = GDT_Byte; 57 : int m_nDTSize = 1; 58 : double m_dfOffset = 0.0; 59 : double m_dfScale = 1.0; 60 : double m_dfPrecision = 1.0; 61 : GUInt16 m_usGPKGNull = 0; 62 : int m_nZoomLevel = -1; 63 : GByte *m_pabyCachedTiles = nullptr; 64 : CachedTileDesc m_asCachedTilesDesc[4]; 65 : int m_nShiftXTiles = 0; 66 : int m_nShiftXPixelsMod = 0; 67 : int m_nShiftYTiles = 0; 68 : int m_nShiftYPixelsMod = 0; 69 : int m_nTileMatrixWidth = 0; 70 : int m_nTileMatrixHeight = 0; 71 : 72 : GPKGTileFormat m_eTF = GPKG_TF_PNG_JPEG; 73 : bool m_bPNGSupports2Bands = 74 : true; // for test/debug purposes only. true is the nominal value 75 : bool m_bPNGSupportsCT = 76 : true; // for test/debug purposes only. true is the nominal value 77 : int m_nZLevel = 6; 78 : int m_nQuality = 75; 79 : bool m_bDither = false; 80 : 81 : GDALColorTable *m_poCT = nullptr; 82 : bool m_bTriedEstablishingCT = false; 83 : void *m_pabyHugeColorArray = nullptr; 84 : 85 : CPLString m_osWHERE{}; 86 : 87 : sqlite3_vfs *m_pMyVFS = nullptr; 88 : sqlite3 *m_hTempDB = nullptr; 89 : CPLString m_osTempDBFilename{}; 90 : time_t m_nLastSpaceCheckTimestamp = 0; 91 : bool m_bForceTempDBCompaction = false; 92 : GIntBig m_nAge = 0; 93 : 94 : int m_nTileInsertionCount = 0; 95 : 96 : GDALGPKGMBTilesLikePseudoDataset *m_poParentDS = nullptr; 97 : 98 : private: 99 : bool m_bInWriteTile = false; 100 : CPLErr WriteTileInternal(); /* should only be called by WriteTile() */ 101 : GIntBig GetTileId(int nRow, int nCol); 102 : bool DeleteTile(int nRow, int nCol); 103 : bool DeleteFromGriddedTileAncillary(GIntBig nTileId); 104 : void GetTileOffsetAndScale(GIntBig nTileId, double &dfTileOffset, 105 : double &dfTileScale); 106 : void FillBuffer(GByte *pabyData, size_t nPixels); 107 : void FillEmptyTile(GByte *pabyData); 108 : void FillEmptyTileSingleBand(GByte *pabyData); 109 : 110 : public: 111 : GDALGPKGMBTilesLikePseudoDataset(); 112 : virtual ~GDALGPKGMBTilesLikePseudoDataset(); 113 : 114 : void SetDataType(GDALDataType eDT); 115 : void SetGlobalOffsetScale(double dfOffset, double dfScale); 116 : 117 : CPLErr ReadTile(const CPLString &osMemFileName, GByte *pabyTileData, 118 : double dfTileOffset, double dfTileScale, 119 : bool *pbIsLossyFormat = nullptr); 120 : GByte *ReadTile(int nRow, int nCol); 121 : GByte *ReadTile(int nRow, int nCol, GByte *pabyData, 122 : bool *pbIsLossyFormat = nullptr); 123 : 124 : CPLErr WriteTile(); 125 : 126 : CPLErr FlushTiles(); 127 : CPLErr FlushRemainingShiftedTiles(bool bPartialFlush); 128 : CPLErr WriteShiftedTile(int nRow, int nCol, int iBand, int nDstXOffset, 129 : int nDstYOffset, int nDstXSize, int nDstYSize); 130 : CPLErr DoPartialFlushOfPartialTilesIfNecessary(); 131 : 132 : virtual CPLErr IFlushCacheWithErrCode(bool bAtClosing) = 0; 133 : virtual int IGetRasterCount() = 0; 134 : virtual GDALRasterBand *IGetRasterBand(int nBand) = 0; 135 : virtual sqlite3 *IGetDB() = 0; 136 : virtual bool IGetUpdate() = 0; 137 : virtual bool ICanIWriteBlock() = 0; 138 : virtual OGRErr IStartTransaction() = 0; 139 : virtual OGRErr ICommitTransaction() = 0; 140 : virtual const char *IGetFilename() = 0; 141 : virtual int GetRowFromIntoTopConvention(int nRow) = 0; 142 : }; 143 : 144 : class GDALGPKGMBTilesLikeRasterBand /* non final*/ : public GDALPamRasterBand 145 : { 146 : GDALGPKGMBTilesLikeRasterBand(const GDALGPKGMBTilesLikeRasterBand &) = 147 : delete; 148 : GDALGPKGMBTilesLikeRasterBand & 149 : operator=(const GDALGPKGMBTilesLikeRasterBand &) = delete; 150 : 151 : protected: 152 : GDALGPKGMBTilesLikePseudoDataset *m_poTPD = nullptr; 153 : int m_nDTSize = 0; 154 : bool m_bHasNoData = false; 155 : double m_dfNoDataValue = 0; 156 : CPLString m_osUom{}; 157 : 158 : public: 159 : GDALGPKGMBTilesLikeRasterBand(GDALGPKGMBTilesLikePseudoDataset *poTPD, 160 : int nTileWidth, int nTileHeight); 161 : 162 : CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pData) override; 163 : virtual CPLErr IWriteBlock(int nBlockXOff, int nBlockYOff, 164 : void *pData) override; 165 : CPLErr FlushCache(bool bAtClosing) override; 166 : 167 : int IGetDataCoverageStatus(int nXOff, int nYOff, int nXSize, int nYSize, 168 : int nMaskFlagStop, double *pdfDataPct) override; 169 : 170 : GDALColorTable *GetColorTable() override; 171 : CPLErr SetColorTable(GDALColorTable *poCT) override; 172 : 173 : void AssignColorTable(const GDALColorTable *poCT); 174 : 175 : GDALColorInterp GetColorInterpretation() override; 176 : CPLErr SetColorInterpretation(GDALColorInterp) override; 177 : 178 : double GetNoDataValue(int *pbSuccess = nullptr) override; 179 : 180 1 : const char *GetUnitType() override 181 : { 182 1 : return m_osUom.c_str(); 183 : } 184 : 185 : void SetNoDataValueInternal(double dfNoDataValue); 186 : 187 2 : void SetUnitTypeInternal(const CPLString &osUom) 188 : { 189 2 : m_osUom = osUom; 190 2 : } 191 : 192 4263 : GDALRasterBlock *AccessibleTryGetLockedBlockRef(int nBlockXOff, 193 : int nBlockYOff) 194 : { 195 4263 : return TryGetLockedBlockRef(nBlockXOff, nBlockYOff); 196 : } 197 : }; 198 : 199 : #endif // GPKGMBTILESCOMMON_H_INCLUDED