Line data Source code
1 : /******************************************************************************
2 : *
3 : * Name: hfadataset.cpp
4 : * Project: Erdas Imagine Driver
5 : * Purpose: Main driver for Erdas Imagine format.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Frank Warmerdam
10 : * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * SPDX-License-Identifier: MIT
13 : ****************************************************************************/
14 :
15 : #ifndef HFADATASET_H_INCLUDED
16 : #define HFADATASET_H_INCLUDED
17 :
18 : #include <cstddef>
19 : #include <vector>
20 :
21 : #include "gdal_frmts.h"
22 : #include "gdal_pam.h"
23 : #include "gdal_rat.h"
24 : #include "hfa_p.h"
25 : #include "ogr_spatialref.h"
26 : #include "ogr_srs_api.h"
27 :
28 : /************************************************************************/
29 : /* ==================================================================== */
30 : /* HFADataset */
31 : /* ==================================================================== */
32 : /************************************************************************/
33 :
34 : class HFARasterBand;
35 :
36 : class HFADataset final : public GDALPamDataset
37 : {
38 : friend class HFARasterBand;
39 :
40 : HFAHandle hHFA = nullptr;
41 :
42 : bool bMetadataDirty = false;
43 :
44 : bool bGeoDirty = false;
45 : GDALGeoTransform m_gt{};
46 : OGRSpatialReference m_oSRS{};
47 :
48 : bool bIgnoreUTM = false;
49 :
50 : CPLErr ReadProjection();
51 : CPLErr WriteProjection();
52 : bool bForceToPEString = false;
53 : bool bDisablePEString = false;
54 :
55 : std::vector<gdal::GCP> m_aoGCPs{};
56 :
57 : void UseXFormStack(int nStepCount, Efga_Polynomial *pasPolyListForward,
58 : Efga_Polynomial *pasPolyListReverse);
59 :
60 : protected:
61 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
62 : GDALDataType, int, BANDMAP_TYPE, GSpacing nPixelSpace,
63 : GSpacing nLineSpace, GSpacing nBandSpace,
64 : GDALRasterIOExtraArg *psExtraArg) override;
65 :
66 : public:
67 : HFADataset();
68 : ~HFADataset() override;
69 :
70 : static int Identify(GDALOpenInfo *);
71 : static CPLErr Rename(const char *pszNewName, const char *pszOldName);
72 : static CPLErr CopyFiles(const char *pszNewName, const char *pszOldName);
73 : static GDALDataset *Open(GDALOpenInfo *);
74 : static HFADataset *OpenHFA(GDALOpenInfo *);
75 : static GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize,
76 : int nBands, GDALDataType eType,
77 : CSLConstList papszParamList);
78 : static GDALDataset *CreateCopy(const char *pszFilename,
79 : GDALDataset *poSrcDS, int bStrict,
80 : CSLConstList papszOptions,
81 : GDALProgressFunc pfnProgress,
82 : void *pProgressData);
83 : static CPLErr Delete(const char *pszFilename);
84 :
85 : char **GetFileList() override;
86 :
87 : const OGRSpatialReference *GetSpatialRef() const override;
88 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
89 :
90 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
91 : CPLErr SetGeoTransform(const GDALGeoTransform >) override;
92 :
93 : int GetGCPCount() override;
94 : const OGRSpatialReference *GetGCPSpatialRef() const override;
95 : const GDAL_GCP *GetGCPs() override;
96 :
97 : CPLErr SetMetadata(CSLConstList, const char * = "") override;
98 : CPLErr SetMetadataItem(const char *, const char *,
99 : const char * = "") override;
100 :
101 : CPLErr FlushCache(bool bAtClosing) override;
102 : CPLErr IBuildOverviews(const char *pszResampling, int nOverviews,
103 : const int *panOverviewList, int nListBands,
104 : const int *panBandList, GDALProgressFunc pfnProgress,
105 : void *pProgressData,
106 : CSLConstList papszOptions) override;
107 : };
108 :
109 : /************************************************************************/
110 : /* ==================================================================== */
111 : /* HFARasterBand */
112 : /* ==================================================================== */
113 : /************************************************************************/
114 :
115 : class HFARasterBand final : public GDALPamRasterBand
116 : {
117 : friend class HFADataset;
118 : friend class HFARasterAttributeTable;
119 :
120 : GDALColorTable *poCT;
121 :
122 : EPTType eHFADataType;
123 :
124 : int nOverviews;
125 : int nThisOverview;
126 : HFARasterBand **papoOverviewBands;
127 :
128 : CPLErr CleanOverviews();
129 :
130 : HFAHandle hHFA;
131 :
132 : bool bMetadataDirty;
133 :
134 : GDALRasterAttributeTable *poDefaultRAT;
135 :
136 : void ReadAuxMetadata();
137 : void ReadHistogramMetadata();
138 : void EstablishOverviews();
139 : CPLErr WriteNamedRAT(const char *pszName,
140 : const GDALRasterAttributeTable *poRAT);
141 :
142 : public:
143 : HFARasterBand(HFADataset *, int, int);
144 : ~HFARasterBand() override;
145 :
146 : CPLErr IReadBlock(int, int, void *) override;
147 : CPLErr IWriteBlock(int, int, void *) override;
148 :
149 : const char *GetDescription() const override;
150 : void SetDescription(const char *) override;
151 :
152 : GDALColorInterp GetColorInterpretation() override;
153 : GDALColorTable *GetColorTable() override;
154 : CPLErr SetColorTable(GDALColorTable *) override;
155 : int GetOverviewCount() override;
156 : GDALRasterBand *GetOverview(int) override;
157 :
158 : double GetMinimum(int *pbSuccess = nullptr) override;
159 : double GetMaximum(int *pbSuccess = nullptr) override;
160 : double GetNoDataValue(int *pbSuccess = nullptr) override;
161 : CPLErr SetNoDataValue(double dfValue) override;
162 :
163 : CPLErr SetMetadata(CSLConstList, const char * = "") override;
164 : CPLErr SetMetadataItem(const char *, const char *,
165 : const char * = "") override;
166 : virtual CPLErr BuildOverviews(const char *, int, const int *,
167 : GDALProgressFunc, void *,
168 : CSLConstList papszOptions) override;
169 :
170 : CPLErr GetDefaultHistogram(double *pdfMin, double *pdfMax, int *pnBuckets,
171 : GUIntBig **ppanHistogram, int bForce,
172 : GDALProgressFunc, void *pProgressData) override;
173 :
174 : GDALRasterAttributeTable *GetDefaultRAT() override;
175 : CPLErr SetDefaultRAT(const GDALRasterAttributeTable *) override;
176 : };
177 :
178 : class HFAAttributeField
179 : {
180 : public:
181 : CPLString sName;
182 : GDALRATFieldType eType;
183 : GDALRATFieldUsage eUsage;
184 : int nDataOffset;
185 : int nElementSize;
186 : HFAEntry *poColumn;
187 : bool bIsBinValues; // Handled differently.
188 : bool bConvertColors; // Map 0-1 floats to 0-255 ints.
189 : };
190 :
191 : class HFARasterAttributeTable final : public GDALRasterAttributeTable
192 : {
193 : private:
194 : HFAHandle hHFA;
195 : HFAEntry *poDT;
196 : CPLString osName;
197 : int nBand;
198 : GDALAccess eAccess;
199 :
200 : std::vector<HFAAttributeField> aoFields;
201 : int nRows;
202 : mutable std::vector<GByte> m_abyWKB{};
203 :
204 : bool bLinearBinning;
205 : double dfRow0Min;
206 : double dfBinSize;
207 : GDALRATTableType eTableType;
208 :
209 : CPLString osWorkingResult;
210 :
211 287 : void AddColumn(const char *pszName, GDALRATFieldType eType,
212 : GDALRATFieldUsage eUsage, int nDataOffset, int nElementSize,
213 : HFAEntry *poColumn, bool bIsBinValues = false,
214 : bool bConvertColors = false)
215 : {
216 574 : HFAAttributeField aField;
217 287 : aField.sName = pszName;
218 287 : aField.eType = eType;
219 287 : aField.eUsage = eUsage;
220 287 : aField.nDataOffset = nDataOffset;
221 287 : aField.nElementSize = nElementSize;
222 287 : aField.poColumn = poColumn;
223 287 : aField.bIsBinValues = bIsBinValues;
224 287 : aField.bConvertColors = bConvertColors;
225 :
226 287 : aoFields.push_back(std::move(aField));
227 287 : }
228 :
229 1 : void CreateDT()
230 : {
231 1 : poDT = HFAEntry::New(hHFA->papoBand[nBand - 1]->psInfo, osName,
232 1 : "Edsc_Table", hHFA->papoBand[nBand - 1]->poNode);
233 1 : poDT->SetIntField("numrows", nRows);
234 1 : }
235 :
236 : public:
237 : HFARasterAttributeTable(HFARasterBand *poBand, const char *pszName);
238 : ~HFARasterAttributeTable() override;
239 :
240 : GDALRasterAttributeTable *Clone() const override;
241 :
242 : int GetColumnCount() const override;
243 :
244 : const char *GetNameOfCol(int) const override;
245 : GDALRATFieldUsage GetUsageOfCol(int) const override;
246 : GDALRATFieldType GetTypeOfCol(int) const override;
247 :
248 : int GetColOfUsage(GDALRATFieldUsage) const override;
249 :
250 : int GetRowCount() const override;
251 :
252 : const char *GetValueAsString(int iRow, int iField) const override;
253 : int GetValueAsInt(int iRow, int iField) const override;
254 : double GetValueAsDouble(int iRow, int iField) const override;
255 : bool GetValueAsBoolean(int iRow, int iField) const override;
256 : GDALRATDateTime GetValueAsDateTime(int iRow, int iField) const override;
257 : const GByte *GetValueAsWKBGeometry(int iRow, int iField,
258 : size_t &nWKBSize) const override;
259 :
260 : CPLErr SetValue(int iRow, int iField, const char *pszValue) override;
261 : CPLErr SetValue(int iRow, int iField, double dfValue) override;
262 : CPLErr SetValue(int iRow, int iField, int nValue) override;
263 : CPLErr SetValue(int iRow, int iField, bool bValue) override;
264 : CPLErr SetValue(int iRow, int iField,
265 : const GDALRATDateTime &sDateTime) override;
266 : CPLErr SetValue(int iRow, int iField, const void *pabyWKB,
267 : size_t nWKBSize) override;
268 :
269 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
270 : double *pdfData) override;
271 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
272 : int *pnData) override;
273 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
274 : char **papszStrList) override;
275 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
276 : bool *pbData) override;
277 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
278 : GDALRATDateTime *psDateTime) override;
279 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
280 : GByte **ppabyWKB, size_t *pnWKBSize) override;
281 :
282 : int ChangesAreWrittenToFile() override;
283 : void SetRowCount(int iCount) override;
284 :
285 : int GetRowOfValue(double dfValue) const override;
286 : int GetRowOfValue(int nValue) const override;
287 :
288 : virtual CPLErr CreateColumn(const char *pszFieldName,
289 : GDALRATFieldType eFieldType,
290 : GDALRATFieldUsage eFieldUsage) override;
291 : virtual CPLErr SetLinearBinning(double dfRow0Min,
292 : double dfBinSize) override;
293 : virtual int GetLinearBinning(double *pdfRow0Min,
294 : double *pdfBinSize) const override;
295 :
296 : CPLXMLNode *Serialize() const override;
297 :
298 : CPLErr SetTableType(const GDALRATTableType eInTableType) override;
299 : GDALRATTableType GetTableType() const override;
300 : void RemoveStatistics() override;
301 :
302 : protected:
303 : CPLErr ColorsIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
304 : int *pnData);
305 : };
306 :
307 : #endif // HFADATASET_H_INCLUDED
|