Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL
4 : * Purpose: STACTA (Spatio-Temporal Asset Catalog Tiled Assets) driver
5 : * Author: Even Rouault, <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2020, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #ifndef STACTADATASET_H
14 : #define STACTADATASET_H
15 :
16 : #include "cpl_mem_cache.h"
17 : #include "cpl_string.h"
18 : #include "gdal_pam.h"
19 : #include "memdataset.h"
20 : #include "tilematrixset.hpp"
21 :
22 : #include <map>
23 : #include <memory>
24 : #include <vector>
25 :
26 : namespace
27 : {
28 : struct Limits
29 : {
30 : int min_tile_col = 0;
31 : int max_tile_col = 0;
32 : int min_tile_row = 0;
33 : int max_tile_row = 0;
34 : };
35 : } // namespace
36 : /************************************************************************/
37 : /* ==================================================================== */
38 : /* STACTADataset */
39 : /* ==================================================================== */
40 : /************************************************************************/
41 :
42 : class STACTARawDataset;
43 :
44 : class STACTADataset final : public GDALPamDataset
45 : {
46 : friend class STACTARasterBand;
47 : friend class STACTARawDataset;
48 : friend class STACTARawRasterBand;
49 :
50 : GDALGeoTransform m_gt{};
51 : OGRSpatialReference m_oSRS{};
52 : std::unique_ptr<GDALDataset> m_poDS{};
53 : // Array of overview datasets, that are guaranteed to have the same
54 : // georeferenced extent as m_poDS (and this dataset), for compliance
55 : // with the GDAL data model. They are thus possibly VRT subsets of
56 : // the STACTARawDataset stored in m_apoIntermediaryDS
57 : std::vector<std::unique_ptr<GDALDataset>> m_apoOverviewDS{};
58 : std::vector<std::unique_ptr<GDALDataset>> m_apoIntermediaryDS{};
59 :
60 : // Cache of tile datasets
61 : lru11::Cache<std::string, std::unique_ptr<GDALDataset>> m_oCacheTileDS{32};
62 :
63 : bool m_bDownloadWholeMetaTile = false;
64 : bool m_bSkipMissingMetaTile = false;
65 : bool m_bTriedVSICLOUDSubstitution = false;
66 : bool m_bVSICLOUDSubstitutionOK = false;
67 :
68 : bool Open(GDALOpenInfo *poOpenInfo);
69 :
70 : public:
71 : static int Identify(GDALOpenInfo *poOpenInfo);
72 : static GDALDataset *OpenStatic(GDALOpenInfo *poOpenInfo);
73 :
74 : ~STACTADataset() override;
75 :
76 : const OGRSpatialReference *GetSpatialRef() const override;
77 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
78 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
79 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
80 : GDALDataType eBufType, int nBandCount,
81 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
82 : GSpacing nLineSpace, GSpacing nBandSpace,
83 : GDALRasterIOExtraArg *psExtraArg) override;
84 : CPLErr FlushCache(bool bAtClosing) override;
85 : };
86 :
87 : /************************************************************************/
88 : /* ==================================================================== */
89 : /* STACTARasterBand */
90 : /* ==================================================================== */
91 : /************************************************************************/
92 :
93 : class STACTARasterBand final : public GDALRasterBand
94 : {
95 : friend class STACTADataset;
96 : GDALColorInterp m_eColorInterp = GCI_Undefined;
97 : int m_bHasNoDataValue = false;
98 : double m_dfNoData = 0;
99 : double m_dfScale = 1.0;
100 : double m_dfOffset = 0.0;
101 : std::string m_osUnit{};
102 :
103 : public:
104 : STACTARasterBand(STACTADataset *poDSIn, int nBandIn,
105 : GDALRasterBand *poProtoBand);
106 : CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) override;
107 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
108 : GDALDataType, GSpacing, GSpacing,
109 : GDALRasterIOExtraArg *psExtraArg) override;
110 :
111 0 : GDALColorInterp GetColorInterpretation() override
112 : {
113 0 : return m_eColorInterp;
114 : }
115 :
116 : int GetOverviewCount() override;
117 : GDALRasterBand *GetOverview(int nIdx) override;
118 : double GetNoDataValue(int *pbHasNoData = nullptr) override;
119 :
120 6 : const char *GetUnitType() override
121 : {
122 6 : return m_osUnit.c_str();
123 : }
124 :
125 6 : double GetScale(int *pbHasValue = nullptr) override
126 : {
127 6 : if (pbHasValue)
128 6 : *pbHasValue = m_dfScale != 1.0;
129 6 : return m_dfScale;
130 : }
131 :
132 6 : double GetOffset(int *pbHasValue = nullptr) override
133 : {
134 6 : if (pbHasValue)
135 6 : *pbHasValue = m_dfOffset != 0.0;
136 6 : return m_dfOffset;
137 : }
138 : };
139 :
140 : /************************************************************************/
141 : /* ==================================================================== */
142 : /* STACTARawDataset */
143 : /* ==================================================================== */
144 : /************************************************************************/
145 :
146 : class STACTARawDataset final : public GDALDataset
147 : {
148 : friend class STACTADataset;
149 : friend class STACTARawRasterBand;
150 :
151 : CPLString m_osURLTemplate{};
152 : int m_nMinMetaTileCol = 0;
153 : int m_nMinMetaTileRow = 0;
154 : int m_nMetaTileWidth = 0;
155 : int m_nMetaTileHeight = 0;
156 : STACTADataset *m_poMasterDS = nullptr;
157 :
158 : GDALGeoTransform m_gt{};
159 : OGRSpatialReference m_oSRS{};
160 :
161 : public:
162 : bool InitRaster(GDALDataset *poProtoDS,
163 : const std::vector<GDALDataType> &aeDT,
164 : const std::vector<bool> &abSetNoData,
165 : const std::vector<double> &adfNoData,
166 : const gdal::TileMatrixSet *poTMS, const std::string &osTMId,
167 : const gdal::TileMatrixSet::TileMatrix &oTM,
168 : const std::map<CPLString, Limits> &oMapLimits);
169 :
170 2 : const OGRSpatialReference *GetSpatialRef() const override
171 : {
172 2 : return &m_oSRS;
173 : }
174 :
175 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
176 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
177 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
178 : GDALDataType eBufType, int nBandCount,
179 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
180 : GSpacing nLineSpace, GSpacing nBandSpace,
181 : GDALRasterIOExtraArg *psExtraArg) override;
182 : };
183 :
184 : /************************************************************************/
185 : /* ==================================================================== */
186 : /* STACTARawRasterBand */
187 : /* ==================================================================== */
188 : /************************************************************************/
189 :
190 : class STACTARawRasterBand final : public GDALRasterBand
191 : {
192 : GDALColorInterp m_eColorInterp = GCI_Undefined;
193 : int m_bHasNoDataValue = false;
194 : double m_dfNoData = 0;
195 :
196 : public:
197 : STACTARawRasterBand(STACTARawDataset *poDSIn, int nBandIn,
198 : GDALRasterBand *poProtoBand);
199 :
200 : STACTARawRasterBand(STACTARawDataset *poDSIn, int nBandIn, GDALDataType eDT,
201 : bool bSetNoData, double dfNoData);
202 :
203 : CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) override;
204 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
205 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
206 : GDALDataType eBufType, GSpacing nPixelSpace,
207 : GSpacing nLineSpace,
208 : GDALRasterIOExtraArg *psExtraArg) override;
209 :
210 71 : GDALColorInterp GetColorInterpretation() override
211 : {
212 71 : return m_eColorInterp;
213 : }
214 :
215 : double GetNoDataValue(int *pbHasNoData = nullptr) override;
216 : };
217 :
218 : #endif // STACTADATASET_H
|