Line data Source code
1 : /******************************************************************************
2 : * $Id$
3 : *
4 : * Project: GeoPackage/MBTiles Translator
5 : * Purpose: Definition of common classes for GeoPackage and MBTiles drivers.
6 : * Author: Even Rouault <even dot rouault at spatialys dot com>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2014-2016, Even Rouault <even dot rouault at spatialys dot com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef GPKGMBTILESCOMMON_H_INCLUDED
31 : #define GPKGMBTILESCOMMON_H_INCLUDED
32 :
33 : #include "cpl_string.h"
34 : #include "gdal_pam.h"
35 : #include <sqlite3.h>
36 :
37 : typedef struct
38 : {
39 : int nRow;
40 : int nCol;
41 : int nIdxWithinTileData;
42 : bool abBandDirty[4];
43 : } CachedTileDesc;
44 :
45 : typedef enum
46 : {
47 : GPKG_TF_PNG_JPEG,
48 : GPKG_TF_PNG,
49 : GPKG_TF_PNG8,
50 : GPKG_TF_JPEG,
51 : GPKG_TF_WEBP,
52 : GPKG_TF_PNG_16BIT, // For GPKG elevation data
53 : GPKG_TF_TIFF_32BIT_FLOAT // For GPKG elevation data
54 : } GPKGTileFormat;
55 :
56 : GPKGTileFormat GDALGPKGMBTilesGetTileFormat(const char *pszTF);
57 : const char *GDALMBTilesGetTileFormatName(GPKGTileFormat);
58 :
59 : class GDALGPKGMBTilesLikePseudoDataset
60 : {
61 : friend class GDALGPKGMBTilesLikeRasterBand;
62 :
63 : GDALGPKGMBTilesLikePseudoDataset(const GDALGPKGMBTilesLikePseudoDataset &) =
64 : delete;
65 : GDALGPKGMBTilesLikePseudoDataset &
66 : operator=(const GDALGPKGMBTilesLikePseudoDataset &) = delete;
67 :
68 : protected:
69 : bool m_bNew = false;
70 : bool m_bHasModifiedTiles = false;
71 :
72 : CPLString m_osRasterTable{};
73 : GDALDataType m_eDT = GDT_Byte;
74 : int m_nDTSize = 1;
75 : double m_dfOffset = 0.0;
76 : double m_dfScale = 1.0;
77 : double m_dfPrecision = 1.0;
78 : GUInt16 m_usGPKGNull = 0;
79 : int m_nZoomLevel = -1;
80 : GByte *m_pabyCachedTiles = nullptr;
81 : CachedTileDesc m_asCachedTilesDesc[4];
82 : int m_nShiftXTiles = 0;
83 : int m_nShiftXPixelsMod = 0;
84 : int m_nShiftYTiles = 0;
85 : int m_nShiftYPixelsMod = 0;
86 : int m_nTileMatrixWidth = 0;
87 : int m_nTileMatrixHeight = 0;
88 :
89 : GPKGTileFormat m_eTF = GPKG_TF_PNG_JPEG;
90 : bool m_bPNGSupports2Bands =
91 : true; // for test/debug purposes only. true is the nominal value
92 : bool m_bPNGSupportsCT =
93 : true; // for test/debug purposes only. true is the nominal value
94 : int m_nZLevel = 6;
95 : int m_nQuality = 75;
96 : bool m_bDither = false;
97 :
98 : GDALColorTable *m_poCT = nullptr;
99 : bool m_bTriedEstablishingCT = false;
100 : void *m_pabyHugeColorArray = nullptr;
101 :
102 : CPLString m_osWHERE{};
103 :
104 : sqlite3_vfs *m_pMyVFS = nullptr;
105 : sqlite3 *m_hTempDB = nullptr;
106 : CPLString m_osTempDBFilename{};
107 : time_t m_nLastSpaceCheckTimestamp = 0;
108 : bool m_bForceTempDBCompaction = false;
109 : GIntBig m_nAge = 0;
110 :
111 : int m_nTileInsertionCount = 0;
112 :
113 : GDALGPKGMBTilesLikePseudoDataset *m_poParentDS = nullptr;
114 :
115 : private:
116 : bool m_bInWriteTile = false;
117 : CPLErr WriteTileInternal(); /* should only be called by WriteTile() */
118 : GIntBig GetTileId(int nRow, int nCol);
119 : bool DeleteTile(int nRow, int nCol);
120 : bool DeleteFromGriddedTileAncillary(GIntBig nTileId);
121 : void GetTileOffsetAndScale(GIntBig nTileId, double &dfTileOffset,
122 : double &dfTileScale);
123 : void FillBuffer(GByte *pabyData, size_t nPixels);
124 : void FillEmptyTile(GByte *pabyData);
125 : void FillEmptyTileSingleBand(GByte *pabyData);
126 :
127 : public:
128 : GDALGPKGMBTilesLikePseudoDataset();
129 : virtual ~GDALGPKGMBTilesLikePseudoDataset();
130 :
131 : void SetDataType(GDALDataType eDT);
132 : void SetGlobalOffsetScale(double dfOffset, double dfScale);
133 :
134 : CPLErr ReadTile(const CPLString &osMemFileName, GByte *pabyTileData,
135 : double dfTileOffset, double dfTileScale,
136 : bool *pbIsLossyFormat = nullptr);
137 : GByte *ReadTile(int nRow, int nCol);
138 : GByte *ReadTile(int nRow, int nCol, GByte *pabyData,
139 : bool *pbIsLossyFormat = nullptr);
140 :
141 : CPLErr WriteTile();
142 :
143 : CPLErr FlushTiles();
144 : CPLErr FlushRemainingShiftedTiles(bool bPartialFlush);
145 : CPLErr WriteShiftedTile(int nRow, int nCol, int iBand, int nDstXOffset,
146 : int nDstYOffset, int nDstXSize, int nDstYSize);
147 : CPLErr DoPartialFlushOfPartialTilesIfNecessary();
148 :
149 : virtual CPLErr IFlushCacheWithErrCode(bool bAtClosing) = 0;
150 : virtual int IGetRasterCount() = 0;
151 : virtual GDALRasterBand *IGetRasterBand(int nBand) = 0;
152 : virtual sqlite3 *IGetDB() = 0;
153 : virtual bool IGetUpdate() = 0;
154 : virtual bool ICanIWriteBlock() = 0;
155 : virtual OGRErr IStartTransaction() = 0;
156 : virtual OGRErr ICommitTransaction() = 0;
157 : virtual const char *IGetFilename() = 0;
158 : virtual int GetRowFromIntoTopConvention(int nRow) = 0;
159 : };
160 :
161 : class GDALGPKGMBTilesLikeRasterBand : public GDALPamRasterBand
162 : {
163 : GDALGPKGMBTilesLikeRasterBand(const GDALGPKGMBTilesLikeRasterBand &) =
164 : delete;
165 : GDALGPKGMBTilesLikeRasterBand &
166 : operator=(const GDALGPKGMBTilesLikeRasterBand &) = delete;
167 :
168 : protected:
169 : GDALGPKGMBTilesLikePseudoDataset *m_poTPD = nullptr;
170 : int m_nDTSize = 0;
171 : bool m_bHasNoData = false;
172 : double m_dfNoDataValue = 0;
173 : CPLString m_osUom{};
174 :
175 : public:
176 : GDALGPKGMBTilesLikeRasterBand(GDALGPKGMBTilesLikePseudoDataset *poTPD,
177 : int nTileWidth, int nTileHeight);
178 :
179 : virtual CPLErr IReadBlock(int nBlockXOff, int nBlockYOff,
180 : void *pData) override;
181 : virtual CPLErr IWriteBlock(int nBlockXOff, int nBlockYOff,
182 : void *pData) override;
183 : virtual CPLErr FlushCache(bool bAtClosing) override;
184 :
185 : virtual GDALColorTable *GetColorTable() override;
186 : virtual CPLErr SetColorTable(GDALColorTable *poCT) override;
187 :
188 : void AssignColorTable(const GDALColorTable *poCT);
189 :
190 : virtual GDALColorInterp GetColorInterpretation() override;
191 : virtual CPLErr SetColorInterpretation(GDALColorInterp) override;
192 :
193 : virtual double GetNoDataValue(int *pbSuccess = nullptr) override;
194 :
195 1 : virtual const char *GetUnitType() override
196 : {
197 1 : return m_osUom.c_str();
198 : }
199 :
200 : void SetNoDataValueInternal(double dfNoDataValue);
201 :
202 2 : void SetUnitTypeInternal(const CPLString &osUom)
203 : {
204 2 : m_osUom = osUom;
205 2 : }
206 :
207 4255 : GDALRasterBlock *AccessibleTryGetLockedBlockRef(int nBlockXOff,
208 : int nBlockYOff)
209 : {
210 4255 : return TryGetLockedBlockRef(nBlockXOff, nBlockYOff);
211 : }
212 : };
213 :
214 : #endif // GPKGMBTILESCOMMON_H_INCLUDED
|