GDAL
gdal_pam.h
1 /******************************************************************************
2  * $Id$
3  *
4  * Project: GDAL Core
5  * Purpose: Declaration for Peristable Auxiliary Metadata classes.
6  * Author: Frank Warmerdam, warmerdam@pobox.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.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 GDAL_PAM_H_INCLUDED
31 #define GDAL_PAM_H_INCLUDED
32 
34 
35 #include "cpl_minixml.h"
36 #include "gdal_priv.h"
37 #include <limits>
38 #include <map>
39 #include <vector>
40 
41 class GDALPamRasterBand;
42 
43 /* Clone Info Flags */
44 
45 #define GCIF_GEOTRANSFORM 0x01
46 #define GCIF_PROJECTION 0x02
47 #define GCIF_METADATA 0x04
48 #define GCIF_GCPS 0x08
49 
50 #define GCIF_NODATA 0x001000
51 #define GCIF_CATEGORYNAMES 0x002000
52 #define GCIF_MINMAX 0x004000
53 #define GCIF_SCALEOFFSET 0x008000
54 #define GCIF_UNITTYPE 0x010000
55 #define GCIF_COLORTABLE 0x020000
56 #define GCIF_COLORINTERP 0x020000
57 #define GCIF_BAND_METADATA 0x040000
58 #define GCIF_RAT 0x080000
59 #define GCIF_MASK 0x100000
60 #define GCIF_BAND_DESCRIPTION 0x200000
61 
62 #define GCIF_ONLY_IF_MISSING 0x10000000
63 #define GCIF_PROCESS_BANDS 0x20000000
64 
65 #define GCIF_PAM_DEFAULT (GCIF_GEOTRANSFORM | GCIF_PROJECTION | \
66  GCIF_METADATA | GCIF_GCPS | \
67  GCIF_NODATA | GCIF_CATEGORYNAMES | \
68  GCIF_MINMAX | GCIF_SCALEOFFSET | \
69  GCIF_UNITTYPE | GCIF_COLORTABLE | \
70  GCIF_COLORINTERP | GCIF_BAND_METADATA | \
71  GCIF_RAT | GCIF_MASK | \
72  GCIF_ONLY_IF_MISSING | GCIF_PROCESS_BANDS|\
73  GCIF_BAND_DESCRIPTION)
74 
75 /* GDAL PAM Flags */
76 /* ERO 2011/04/13 : GPF_AUXMODE seems to be unimplemented */
77 #define GPF_DIRTY 0x01 // .pam file needs to be written on close
78 #define GPF_TRIED_READ_FAILED 0x02 // no need to keep trying to read .pam.
79 #define GPF_DISABLED 0x04 // do not try any PAM stuff.
80 #define GPF_AUXMODE 0x08 // store info in .aux (HFA) file.
81 #define GPF_NOSAVE 0x10 // do not try to save pam info.
82 
83 /* ==================================================================== */
84 /* GDALDatasetPamInfo */
85 /* */
86 /* We make these things a separate structure of information */
87 /* primarily so we can modify it without altering the size of */
88 /* the GDALPamDataset. It is an effort to reduce ABI churn for */
89 /* driver plugins. */
90 /* ==================================================================== */
91 class GDALDatasetPamInfo
92 {
93 public:
94  char *pszPamFilename = nullptr;
95 
96  std::vector<CPLXMLTreeCloser> m_apoOtherNodes{};
97 
98  OGRSpatialReference* poSRS = nullptr;
99 
100  int bHaveGeoTransform = false;
101  double adfGeoTransform[6]{0,0,0,0,0,0};
102 
103  int nGCPCount = 0;
104  GDAL_GCP *pasGCPList = nullptr;
105  OGRSpatialReference* poGCP_SRS = nullptr;
106 
107  CPLString osPhysicalFilename{};
108  CPLString osSubdatasetName{};
109  CPLString osAuxFilename{};
110 
111  int bHasMetadata = false;
112 };
114 
115 /* ******************************************************************** */
116 /* GDALPamDataset */
117 /* ******************************************************************** */
118 
120 class CPL_DLL GDALPamDataset : public GDALDataset
121 {
122  friend class GDALPamRasterBand;
123 
124  private:
125  int IsPamFilenameAPotentialSiblingFile();
126 
127  protected:
128 
129  GDALPamDataset(void);
131  int nPamFlags = 0;
132  GDALDatasetPamInfo *psPam = nullptr;
133 
134  virtual const char *_GetProjectionRef() override;
135  virtual const char *_GetGCPProjection() override;
136  virtual CPLErr _SetProjection( const char * pszProjection ) override;
137  virtual CPLErr _SetGCPs( int nGCPCount, const GDAL_GCP *pasGCPList,
138  const char *pszGCPProjection ) override;
139 
140  virtual CPLXMLNode *SerializeToXML( const char *);
141  virtual CPLErr XMLInit( CPLXMLNode *, const char * );
142 
143  virtual CPLErr TryLoadXML(char **papszSiblingFiles = nullptr);
144  virtual CPLErr TrySaveXML();
145 
146  CPLErr TryLoadAux(char **papszSiblingFiles = nullptr);
147  CPLErr TrySaveAux();
148 
149  virtual const char *BuildPamFilename();
150 
151  void PamInitialize();
152  void PamClear();
153 
154  void SetPhysicalFilename( const char * );
155  const char *GetPhysicalFilename();
156  void SetSubdatasetName( const char *);
157  const char *GetSubdatasetName();
159 
160  public:
161  ~GDALPamDataset() override;
162 
163  void FlushCache(bool bAtClosing) override;
164 
165  const OGRSpatialReference* GetSpatialRef() const override;
166  CPLErr SetSpatialRef(const OGRSpatialReference* poSRS) override;
167 
168  CPLErr GetGeoTransform( double * ) override;
169  CPLErr SetGeoTransform( double * ) override;
170  void DeleteGeoTransform();
171 
172  int GetGCPCount() override;
173  const OGRSpatialReference* GetGCPSpatialRef() const override;
174  const GDAL_GCP *GetGCPs() override;
175  using GDALDataset::SetGCPs;
176  CPLErr SetGCPs( int nGCPCount, const GDAL_GCP *pasGCPList,
177  const OGRSpatialReference* poSRS ) override;
178 
179  CPLErr SetMetadata( char ** papszMetadata,
180  const char * pszDomain = "" ) override;
181  CPLErr SetMetadataItem( const char * pszName,
182  const char * pszValue,
183  const char * pszDomain = "" ) override;
184  char **GetMetadata( const char * pszDomain = "" ) override;
185  const char *GetMetadataItem( const char * pszName,
186  const char * pszDomain = "" ) override;
187 
188  char **GetFileList(void) override;
189 
190  void ClearStatistics() override;
191 
193  virtual CPLErr CloneInfo( GDALDataset *poSrcDS, int nCloneInfoFlags );
194 
195  CPLErr IBuildOverviews( const char *pszResampling,
196  int nOverviews, int *panOverviewList,
197  int nListBands, int *panBandList,
198  GDALProgressFunc pfnProgress,
199  void * pProgressData ) override;
200 
201  // "semi private" methods.
202  void MarkPamDirty() { nPamFlags |= GPF_DIRTY; }
203  GDALDatasetPamInfo *GetPamInfo() { return psPam; }
204  int GetPamFlags() { return nPamFlags; }
205  void SetPamFlags(int nValue ) { nPamFlags = nValue; }
207 
208  private:
210 };
211 
213 
214 constexpr double GDAL_PAM_DEFAULT_NODATA_VALUE = 0;
215 constexpr int64_t GDAL_PAM_DEFAULT_NODATA_VALUE_INT64 = std::numeric_limits<int64_t>::min();
216 constexpr uint64_t GDAL_PAM_DEFAULT_NODATA_VALUE_UINT64 = std::numeric_limits<uint64_t>::max();
217 
218 /* ==================================================================== */
219 /* GDALRasterBandPamInfo */
220 /* */
221 /* We make these things a separate structure of information */
222 /* primarily so we can modify it without altering the size of */
223 /* the GDALPamDataset. It is an effort to reduce ABI churn for */
224 /* driver plugins. */
225 /* ==================================================================== */
226 struct GDALRasterBandPamInfo {
227  GDALPamDataset *poParentDS = nullptr;
228 
229  bool bNoDataValueSet = false;
230  bool bNoDataValueSetAsInt64 = false;
231  bool bNoDataValueSetAsUInt64 = false;
232 
233  double dfNoDataValue = GDAL_PAM_DEFAULT_NODATA_VALUE;
234  int64_t nNoDataValueInt64 = GDAL_PAM_DEFAULT_NODATA_VALUE_INT64;
235  uint64_t nNoDataValueUInt64 = GDAL_PAM_DEFAULT_NODATA_VALUE_UINT64;
236 
237  GDALColorTable *poColorTable = nullptr;
238 
239  GDALColorInterp eColorInterp = GCI_Undefined;
240 
241  char *pszUnitType = nullptr;
242  char **papszCategoryNames = nullptr;
243 
244  double dfOffset = 0.0;
245  double dfScale = 1.0;
246 
247  int bHaveMinMax = FALSE;
248  double dfMin = 0;
249  double dfMax = 0;
250 
251  int bHaveStats = FALSE;
252  double dfMean = 0;
253  double dfStdDev = 0;
254 
255  CPLXMLNode *psSavedHistograms = nullptr;
256 
257  GDALRasterAttributeTable *poDefaultRAT = nullptr;
258 
259  bool bOffsetSet = false;
260  bool bScaleSet = false;
261 };
263 /* ******************************************************************** */
264 /* GDALPamRasterBand */
265 /* ******************************************************************** */
266 
268 class CPL_DLL GDALPamRasterBand : public GDALRasterBand
269 {
270  friend class GDALPamDataset;
271 
272  protected:
274  virtual CPLXMLNode *SerializeToXML( const char *pszVRTPath );
275  virtual CPLErr XMLInit( CPLXMLNode *, const char * );
276 
277  void PamInitialize();
278  void PamClear();
279  void PamInitializeNoParent();
280  void MarkPamDirty();
281 
282  GDALRasterBandPamInfo *psPam = nullptr;
284 
285  public:
288  explicit GDALPamRasterBand(int bForceCachedIO);
290  ~GDALPamRasterBand() override;
291 
292  void SetDescription( const char * ) override;
293 
294  CPLErr SetNoDataValue( double ) override;
295  CPLErr SetNoDataValueAsInt64( int64_t nNoData ) override;
296  CPLErr SetNoDataValueAsUInt64( uint64_t nNoData ) override;
297  double GetNoDataValue( int *pbSuccess = nullptr ) override;
298  int64_t GetNoDataValueAsInt64( int *pbSuccess = nullptr ) override;
299  uint64_t GetNoDataValueAsUInt64( int *pbSuccess = nullptr ) override;
300  CPLErr DeleteNoDataValue() override;
301 
302  CPLErr SetColorTable( GDALColorTable * ) override;
303  GDALColorTable *GetColorTable() override;
304 
305  CPLErr SetColorInterpretation( GDALColorInterp ) override;
306  GDALColorInterp GetColorInterpretation() override;
307 
308  const char *GetUnitType() override;
309  CPLErr SetUnitType( const char * ) override;
310 
311  char **GetCategoryNames() override;
312  CPLErr SetCategoryNames( char ** ) override;
313 
314  double GetOffset( int *pbSuccess = nullptr ) override;
315  CPLErr SetOffset( double ) override;
316  double GetScale( int *pbSuccess = nullptr ) override;
317  CPLErr SetScale( double ) override;
318 
319  CPLErr GetHistogram( double dfMin, double dfMax,
320  int nBuckets, GUIntBig * panHistogram,
321  int bIncludeOutOfRange, int bApproxOK,
322  GDALProgressFunc, void *pProgressData ) override;
323 
324  CPLErr GetDefaultHistogram( double *pdfMin, double *pdfMax,
325  int *pnBuckets, GUIntBig ** ppanHistogram,
326  int bForce,
327  GDALProgressFunc, void *pProgressData) override;
328 
329  CPLErr SetDefaultHistogram( double dfMin, double dfMax,
330  int nBuckets, GUIntBig *panHistogram ) override;
331 
332  CPLErr SetMetadata( char ** papszMetadata,
333  const char * pszDomain = "" ) override;
334  CPLErr SetMetadataItem( const char * pszName,
335  const char * pszValue,
336  const char * pszDomain = "" ) override;
337 
338  GDALRasterAttributeTable *GetDefaultRAT() override;
339  CPLErr SetDefaultRAT( const GDALRasterAttributeTable * ) override;
340 
342  // new in GDALPamRasterBand.
343  virtual CPLErr CloneInfo( GDALRasterBand *poSrcBand, int nCloneInfoFlags );
344 
345  // "semi private" methods.
346  GDALRasterBandPamInfo *GetPamInfo() { return psPam; }
348  private:
350 
351  void ResetNoDataValues();
352 };
353 
355 
356 /* ******************************************************************** */
357 /* GDALPamMultiDim */
358 /* ******************************************************************** */
359 
363 class CPL_DLL GDALPamMultiDim
364 {
365  struct Private;
366  std::unique_ptr<Private> d;
367 
368  void Load();
369  void Save();
370 
371 public:
372  explicit GDALPamMultiDim(const std::string& osFilename);
373  virtual ~GDALPamMultiDim();
374 
375  std::shared_ptr<OGRSpatialReference> GetSpatialRef(const std::string& osArrayFullName);
376 
377  void SetSpatialRef(const std::string& osArrayFullName,
378  const OGRSpatialReference* poSRS);
379 
380  CPLErr GetStatistics( const std::string& osArrayFullName,
381  bool bApproxOK,
382  double *pdfMin, double *pdfMax,
383  double *pdfMean, double *pdfStdDev,
384  GUInt64* pnValidCount);
385 
386  void SetStatistics( const std::string& osArrayFullName,
387  bool bApproxStats,
388  double dfMin, double dfMax,
389  double dfMean, double dfStdDev,
390  GUInt64 nValidCount );
391 
392  void ClearStatistics();
393 
394  void ClearStatistics( const std::string& osArrayFullName );
395 };
396 
397 /* ******************************************************************** */
398 /* GDALPamMDArray */
399 /* ******************************************************************** */
400 
402 class CPL_DLL GDALPamMDArray: public GDALMDArray
403 {
404  std::shared_ptr<GDALPamMultiDim> m_poPam;
405 
406 protected:
407  GDALPamMDArray(const std::string& osParentName,
408  const std::string& osName,
409  const std::shared_ptr<GDALPamMultiDim>& poPam);
410 
411  bool SetStatistics( bool bApproxStats,
412  double dfMin, double dfMax,
413  double dfMean, double dfStdDev,
414  GUInt64 nValidCount ) override;
415 
416 public:
417  const std::shared_ptr<GDALPamMultiDim>& GetPAM() const { return m_poPam; }
418 
419  CPLErr GetStatistics( bool bApproxOK, bool bForce,
420  double *pdfMin, double *pdfMax,
421  double *pdfMean, double *padfStdDev,
422  GUInt64* pnValidCount,
423  GDALProgressFunc pfnProgress, void *pProgressData ) override;
424 
425  void ClearStatistics() override;
426 
427  bool SetSpatialRef(const OGRSpatialReference* poSRS) override;
428 
429  std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override;
430 };
431 
432 // These are mainly helper functions for internal use.
433 int CPL_DLL PamParseHistogram( CPLXMLNode *psHistItem,
434  double *pdfMin, double *pdfMax,
435  int *pnBuckets, GUIntBig **ppanHistogram,
436  int *pbIncludeOutOfRange, int *pbApproxOK );
437 CPLXMLNode CPL_DLL *
438 PamFindMatchingHistogram( CPLXMLNode *psSavedHistograms,
439  double dfMin, double dfMax, int nBuckets,
440  int bIncludeOutOfRange, int bApproxOK );
441 CPLXMLNode CPL_DLL *
442 PamHistogramToXMLTree( double dfMin, double dfMax,
443  int nBuckets, GUIntBig * panHistogram,
444  int bIncludeOutOfRange, int bApprox );
445 
446 // For managing the proxy file database.
447 const char CPL_DLL * PamGetProxy( const char * );
448 const char CPL_DLL * PamAllocateProxy( const char * );
449 const char CPL_DLL * PamDeallocateProxy( const char * );
450 void CPL_DLL PamCleanProxyDB( void );
451 
453 
454 #endif /* ndef GDAL_PAM_H_INCLUDED */
Convenient string class based on std::string.
Definition: cpl_string.h:320
A color table / palette.
Definition: gdal_priv.h:1033
A set of associated raster bands, usually from one file.
Definition: gdal_priv.h:340
virtual void FlushCache(bool bAtClosing=false)
Flush all write cached data to disk.
Definition: gdaldataset.cpp:435
virtual int GetGCPCount()
Get number of GCPs.
Definition: gdaldataset.cpp:1549
virtual CPLErr GetGeoTransform(double *padfTransform)
Fetch the affine transformation coefficients.
Definition: gdaldataset.cpp:1177
virtual const OGRSpatialReference * GetGCPSpatialRef() const
Get output spatial reference system for GCPs.
Definition: gdaldataset.cpp:1646
virtual void ClearStatistics()
Clear statistics.
Definition: gdaldataset.cpp:8433
CPLErr SetMetadata(char **papszMetadata, const char *pszDomain) override
Set metadata.
virtual CPLErr SetSpatialRef(const OGRSpatialReference *poSRS)
Set the spatial reference system for this dataset.
Definition: gdaldataset.cpp:1059
virtual CPLErr SetGeoTransform(double *padfTransform)
Set the affine transformation coefficients.
Definition: gdaldataset.cpp:1232
virtual CPLErr SetGCPs(int nGCPCount, const GDAL_GCP *pasGCPList, const OGRSpatialReference *poGCP_SRS)
Assign GCPs.
Definition: gdaldataset.cpp:1834
virtual const OGRSpatialReference * GetSpatialRef() const
Fetch the spatial reference for this dataset.
Definition: gdaldataset.cpp:928
CPLErr SetMetadataItem(const char *pszName, const char *pszValue, const char *pszDomain) override
Set single metadata item.
void static void char ** GetMetadata(const char *pszDomain="") override
Fetch metadata.
Definition: gdaldataset.cpp:4229
virtual const GDAL_GCP * GetGCPs()
Fetch GCPs.
Definition: gdaldataset.cpp:1729
virtual char ** GetFileList(void)
Fetch files forming dataset.
Definition: gdaldataset.cpp:3026
Class modeling a multi-dimensional array.
Definition: gdal_priv.h:2416
virtual const char * GetMetadataItem(const char *pszName, const char *pszDomain="")
Fetch single metadata item.
Definition: gdalmajorobject.cpp:344
virtual void SetDescription(const char *)
Set object description.
Definition: gdalmajorobject.cpp:120
PAM dataset.
Definition: gdal_pam.h:121
PAM raster band.
Definition: gdal_pam.h:269
The GDALRasterAttributeTable (or RAT) class is used to encapsulate a table used to provide attribute ...
Definition: gdal_rat.h:48
A single raster band (or channel).
Definition: gdal_priv.h:1151
This class represents an OpenGIS Spatial Reference System, and contains methods for converting betwee...
Definition: ogr_spatialref.h:158
CPLErr
Error category.
Definition: cpl_error.h:53
Definitions for CPL mini XML Parser/Serializer.
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition: cpl_port.h:233
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition: cpl_port.h:927
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:251
GDALColorInterp
Definition: gdal.h:205
@ GCI_Undefined
Definition: gdal.h:206
C++ GDAL entry points.
Document node structure.
Definition: cpl_minixml.h:70
Ground Control Point.
Definition: gdal.h:733