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 GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize,
75 : int nBands, GDALDataType eType,
76 : char **papszParamList);
77 : static GDALDataset *CreateCopy(const char *pszFilename,
78 : GDALDataset *poSrcDS, int bStrict,
79 : char **papszOptions,
80 : GDALProgressFunc pfnProgress,
81 : void *pProgressData);
82 : static CPLErr Delete(const char *pszFilename);
83 :
84 : char **GetFileList() override;
85 :
86 : const OGRSpatialReference *GetSpatialRef() const override;
87 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
88 :
89 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
90 : CPLErr SetGeoTransform(const GDALGeoTransform >) override;
91 :
92 : int GetGCPCount() override;
93 : const OGRSpatialReference *GetGCPSpatialRef() const override;
94 : const GDAL_GCP *GetGCPs() override;
95 :
96 : CPLErr SetMetadata(char **, const char * = "") override;
97 : CPLErr SetMetadataItem(const char *, const char *,
98 : const char * = "") override;
99 :
100 : CPLErr FlushCache(bool bAtClosing) override;
101 : CPLErr IBuildOverviews(const char *pszResampling, int nOverviews,
102 : const int *panOverviewList, int nListBands,
103 : const int *panBandList, GDALProgressFunc pfnProgress,
104 : void *pProgressData,
105 : CSLConstList papszOptions) override;
106 : };
107 :
108 : /************************************************************************/
109 : /* ==================================================================== */
110 : /* HFARasterBand */
111 : /* ==================================================================== */
112 : /************************************************************************/
113 :
114 : class HFARasterBand final : public GDALPamRasterBand
115 : {
116 : friend class HFADataset;
117 : friend class HFARasterAttributeTable;
118 :
119 : GDALColorTable *poCT;
120 :
121 : EPTType eHFADataType;
122 :
123 : int nOverviews;
124 : int nThisOverview;
125 : HFARasterBand **papoOverviewBands;
126 :
127 : CPLErr CleanOverviews();
128 :
129 : HFAHandle hHFA;
130 :
131 : bool bMetadataDirty;
132 :
133 : GDALRasterAttributeTable *poDefaultRAT;
134 :
135 : void ReadAuxMetadata();
136 : void ReadHistogramMetadata();
137 : void EstablishOverviews();
138 : CPLErr WriteNamedRAT(const char *pszName,
139 : const GDALRasterAttributeTable *poRAT);
140 :
141 : public:
142 : HFARasterBand(HFADataset *, int, int);
143 : ~HFARasterBand() override;
144 :
145 : CPLErr IReadBlock(int, int, void *) override;
146 : CPLErr IWriteBlock(int, int, void *) override;
147 :
148 : const char *GetDescription() const override;
149 : void SetDescription(const char *) override;
150 :
151 : GDALColorInterp GetColorInterpretation() override;
152 : GDALColorTable *GetColorTable() override;
153 : CPLErr SetColorTable(GDALColorTable *) override;
154 : int GetOverviewCount() override;
155 : GDALRasterBand *GetOverview(int) override;
156 :
157 : double GetMinimum(int *pbSuccess = nullptr) override;
158 : double GetMaximum(int *pbSuccess = nullptr) override;
159 : double GetNoDataValue(int *pbSuccess = nullptr) override;
160 : CPLErr SetNoDataValue(double dfValue) override;
161 :
162 : CPLErr SetMetadata(char **, const char * = "") override;
163 : CPLErr SetMetadataItem(const char *, const char *,
164 : const char * = "") override;
165 : virtual CPLErr BuildOverviews(const char *, int, const int *,
166 : GDALProgressFunc, void *,
167 : CSLConstList papszOptions) override;
168 :
169 : CPLErr GetDefaultHistogram(double *pdfMin, double *pdfMax, int *pnBuckets,
170 : GUIntBig **ppanHistogram, int bForce,
171 : GDALProgressFunc, void *pProgressData) override;
172 :
173 : GDALRasterAttributeTable *GetDefaultRAT() override;
174 : CPLErr SetDefaultRAT(const GDALRasterAttributeTable *) override;
175 : };
176 :
177 : class HFAAttributeField
178 : {
179 : public:
180 : CPLString sName;
181 : GDALRATFieldType eType;
182 : GDALRATFieldUsage eUsage;
183 : int nDataOffset;
184 : int nElementSize;
185 : HFAEntry *poColumn;
186 : bool bIsBinValues; // Handled differently.
187 : bool bConvertColors; // Map 0-1 floats to 0-255 ints.
188 : };
189 :
190 : class HFARasterAttributeTable final : public GDALRasterAttributeTable
191 : {
192 : private:
193 : HFAHandle hHFA;
194 : HFAEntry *poDT;
195 : CPLString osName;
196 : int nBand;
197 : GDALAccess eAccess;
198 :
199 : std::vector<HFAAttributeField> aoFields;
200 : int nRows;
201 : mutable std::vector<GByte> m_abyWKB{};
202 :
203 : bool bLinearBinning;
204 : double dfRow0Min;
205 : double dfBinSize;
206 : GDALRATTableType eTableType;
207 :
208 : CPLString osWorkingResult;
209 :
210 287 : void AddColumn(const char *pszName, GDALRATFieldType eType,
211 : GDALRATFieldUsage eUsage, int nDataOffset, int nElementSize,
212 : HFAEntry *poColumn, bool bIsBinValues = false,
213 : bool bConvertColors = false)
214 : {
215 574 : HFAAttributeField aField;
216 287 : aField.sName = pszName;
217 287 : aField.eType = eType;
218 287 : aField.eUsage = eUsage;
219 287 : aField.nDataOffset = nDataOffset;
220 287 : aField.nElementSize = nElementSize;
221 287 : aField.poColumn = poColumn;
222 287 : aField.bIsBinValues = bIsBinValues;
223 287 : aField.bConvertColors = bConvertColors;
224 :
225 287 : aoFields.push_back(std::move(aField));
226 287 : }
227 :
228 1 : void CreateDT()
229 : {
230 1 : poDT = HFAEntry::New(hHFA->papoBand[nBand - 1]->psInfo, osName,
231 1 : "Edsc_Table", hHFA->papoBand[nBand - 1]->poNode);
232 1 : poDT->SetIntField("numrows", nRows);
233 1 : }
234 :
235 : public:
236 : HFARasterAttributeTable(HFARasterBand *poBand, const char *pszName);
237 : ~HFARasterAttributeTable() override;
238 :
239 : GDALRasterAttributeTable *Clone() const override;
240 :
241 : int GetColumnCount() const override;
242 :
243 : const char *GetNameOfCol(int) const override;
244 : GDALRATFieldUsage GetUsageOfCol(int) const override;
245 : GDALRATFieldType GetTypeOfCol(int) const override;
246 :
247 : int GetColOfUsage(GDALRATFieldUsage) const override;
248 :
249 : int GetRowCount() const override;
250 :
251 : const char *GetValueAsString(int iRow, int iField) const override;
252 : int GetValueAsInt(int iRow, int iField) const override;
253 : double GetValueAsDouble(int iRow, int iField) const override;
254 : bool GetValueAsBoolean(int iRow, int iField) const override;
255 : GDALRATDateTime GetValueAsDateTime(int iRow, int iField) const override;
256 : const GByte *GetValueAsWKBGeometry(int iRow, int iField,
257 : size_t &nWKBSize) const override;
258 :
259 : CPLErr SetValue(int iRow, int iField, const char *pszValue) override;
260 : CPLErr SetValue(int iRow, int iField, double dfValue) override;
261 : CPLErr SetValue(int iRow, int iField, int nValue) override;
262 : CPLErr SetValue(int iRow, int iField, bool bValue) override;
263 : CPLErr SetValue(int iRow, int iField,
264 : const GDALRATDateTime &sDateTime) override;
265 : CPLErr SetValue(int iRow, int iField, const void *pabyWKB,
266 : size_t nWKBSize) override;
267 :
268 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
269 : double *pdfData) override;
270 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
271 : int *pnData) override;
272 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
273 : char **papszStrList) override;
274 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
275 : bool *pbData) override;
276 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
277 : GDALRATDateTime *psDateTime) override;
278 : CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
279 : GByte **ppabyWKB, size_t *pnWKBSize) override;
280 :
281 : int ChangesAreWrittenToFile() override;
282 : void SetRowCount(int iCount) override;
283 :
284 : int GetRowOfValue(double dfValue) const override;
285 : int GetRowOfValue(int nValue) const override;
286 :
287 : virtual CPLErr CreateColumn(const char *pszFieldName,
288 : GDALRATFieldType eFieldType,
289 : GDALRATFieldUsage eFieldUsage) override;
290 : virtual CPLErr SetLinearBinning(double dfRow0Min,
291 : double dfBinSize) override;
292 : virtual int GetLinearBinning(double *pdfRow0Min,
293 : double *pdfBinSize) const override;
294 :
295 : CPLXMLNode *Serialize() const override;
296 :
297 : CPLErr SetTableType(const GDALRATTableType eInTableType) override;
298 : GDALRATTableType GetTableType() const override;
299 : void RemoveStatistics() override;
300 :
301 : protected:
302 : CPLErr ColorsIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
303 : int *pnData);
304 : };
305 :
306 : #endif // HFADATASET_H_INCLUDED
|