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 :
66 : bool Open(GDALOpenInfo *poOpenInfo);
67 :
68 : public:
69 : static int Identify(GDALOpenInfo *poOpenInfo);
70 : static GDALDataset *OpenStatic(GDALOpenInfo *poOpenInfo);
71 :
72 : ~STACTADataset() override;
73 :
74 : const OGRSpatialReference *GetSpatialRef() const override;
75 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
76 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
77 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
78 : GDALDataType eBufType, int nBandCount,
79 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
80 : GSpacing nLineSpace, GSpacing nBandSpace,
81 : GDALRasterIOExtraArg *psExtraArg) override;
82 : CPLErr FlushCache(bool bAtClosing) override;
83 : };
84 :
85 : /************************************************************************/
86 : /* ==================================================================== */
87 : /* STACTARasterBand */
88 : /* ==================================================================== */
89 : /************************************************************************/
90 :
91 : class STACTARasterBand final : public GDALRasterBand
92 : {
93 : friend class STACTADataset;
94 : GDALColorInterp m_eColorInterp = GCI_Undefined;
95 : int m_bHasNoDataValue = false;
96 : double m_dfNoData = 0;
97 : double m_dfScale = 1.0;
98 : double m_dfOffset = 0.0;
99 : std::string m_osUnit{};
100 :
101 : public:
102 : STACTARasterBand(STACTADataset *poDSIn, int nBandIn,
103 : GDALRasterBand *poProtoBand);
104 : CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) override;
105 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
106 : GDALDataType, GSpacing, GSpacing,
107 : GDALRasterIOExtraArg *psExtraArg) override;
108 :
109 0 : GDALColorInterp GetColorInterpretation() override
110 : {
111 0 : return m_eColorInterp;
112 : }
113 :
114 : int GetOverviewCount() override;
115 : GDALRasterBand *GetOverview(int nIdx) override;
116 : double GetNoDataValue(int *pbHasNoData = nullptr) override;
117 :
118 6 : const char *GetUnitType() override
119 : {
120 6 : return m_osUnit.c_str();
121 : }
122 :
123 6 : double GetScale(int *pbHasValue = nullptr) override
124 : {
125 6 : if (pbHasValue)
126 6 : *pbHasValue = m_dfScale != 1.0;
127 6 : return m_dfScale;
128 : }
129 :
130 6 : double GetOffset(int *pbHasValue = nullptr) override
131 : {
132 6 : if (pbHasValue)
133 6 : *pbHasValue = m_dfOffset != 0.0;
134 6 : return m_dfOffset;
135 : }
136 : };
137 :
138 : /************************************************************************/
139 : /* ==================================================================== */
140 : /* STACTARawDataset */
141 : /* ==================================================================== */
142 : /************************************************************************/
143 :
144 : class STACTARawDataset final : public GDALDataset
145 : {
146 : friend class STACTADataset;
147 : friend class STACTARawRasterBand;
148 :
149 : CPLString m_osURLTemplate{};
150 : int m_nMinMetaTileCol = 0;
151 : int m_nMinMetaTileRow = 0;
152 : int m_nMetaTileWidth = 0;
153 : int m_nMetaTileHeight = 0;
154 : STACTADataset *m_poMasterDS = nullptr;
155 :
156 : GDALGeoTransform m_gt{};
157 : OGRSpatialReference m_oSRS{};
158 :
159 : public:
160 : bool InitRaster(GDALDataset *poProtoDS,
161 : const std::vector<GDALDataType> &aeDT,
162 : const std::vector<bool> &abSetNoData,
163 : const std::vector<double> &adfNoData,
164 : const gdal::TileMatrixSet *poTMS, const std::string &osTMId,
165 : const gdal::TileMatrixSet::TileMatrix &oTM,
166 : const std::map<CPLString, Limits> &oMapLimits);
167 :
168 2 : const OGRSpatialReference *GetSpatialRef() const override
169 : {
170 2 : return &m_oSRS;
171 : }
172 :
173 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
174 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
175 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
176 : GDALDataType eBufType, int nBandCount,
177 : BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
178 : GSpacing nLineSpace, GSpacing nBandSpace,
179 : GDALRasterIOExtraArg *psExtraArg) override;
180 : };
181 :
182 : /************************************************************************/
183 : /* ==================================================================== */
184 : /* STACTARawRasterBand */
185 : /* ==================================================================== */
186 : /************************************************************************/
187 :
188 : class STACTARawRasterBand final : public GDALRasterBand
189 : {
190 : GDALColorInterp m_eColorInterp = GCI_Undefined;
191 : int m_bHasNoDataValue = false;
192 : double m_dfNoData = 0;
193 :
194 : public:
195 : STACTARawRasterBand(STACTARawDataset *poDSIn, int nBandIn,
196 : GDALRasterBand *poProtoBand);
197 :
198 : STACTARawRasterBand(STACTARawDataset *poDSIn, int nBandIn, GDALDataType eDT,
199 : bool bSetNoData, double dfNoData);
200 :
201 : CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) override;
202 : CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
203 : int nYSize, void *pData, int nBufXSize, int nBufYSize,
204 : GDALDataType eBufType, GSpacing nPixelSpace,
205 : GSpacing nLineSpace,
206 : GDALRasterIOExtraArg *psExtraArg) override;
207 :
208 69 : GDALColorInterp GetColorInterpretation() override
209 : {
210 69 : return m_eColorInterp;
211 : }
212 :
213 : double GetNoDataValue(int *pbHasNoData = nullptr) override;
214 : };
215 :
216 : #endif // STACTADATASET_H
|