GDAL
gdal_rat.h
1/******************************************************************************
2 *
3 * Project: GDAL Core
4 * Purpose: GDALRasterAttributeTable class declarations.
5 * Author: Frank Warmerdam, warmerdam@pobox.com
6 *
7 ******************************************************************************
8 * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13#ifndef GDAL_RAT_H_INCLUDED
14#define GDAL_RAT_H_INCLUDED
15
16#if !defined(GDAL_COMPILATION) && \
17 !defined(GDAL_RAT_SKIP_OTHER_GDAL_HEADERS) && !defined(GDAL_4_0_COMPAT)
18
19#include "cpl_minixml.h"
20#include "gdal_priv.h"
21
22#else
23
24#include "cpl_port.h"
25#include "cpl_error.h"
26#include "cpl_string.h"
27#include "gdal.h"
28
29#endif
30
31#include <memory>
32#include <vector>
33
34class GDALColorTable;
35
36// Clone and Serialize are allowed to fail if GetRowCount()*GetColCount()
37// greater than this number
38#define RAT_MAX_ELEM_FOR_CLONE 1000000
39
40/************************************************************************/
41/* GDALRasterAttributeTable */
42/************************************************************************/
43
46
48{
49 public:
63 virtual GDALRasterAttributeTable *Clone() const = 0;
64
72 virtual int GetColumnCount() const = 0;
73
83 virtual const char *GetNameOfCol(int iCol) const = 0;
84
94 virtual GDALRATFieldUsage GetUsageOfCol(int iCol) const = 0;
95
105 virtual GDALRATFieldType GetTypeOfCol(int iCol) const = 0;
106
119 virtual int GetColOfUsage(GDALRATFieldUsage eUsage) const = 0;
120
128 virtual int GetRowCount() const = 0;
129
149 virtual const char *GetValueAsString(int iRow, int iField) const = 0;
150
167 virtual int GetValueAsInt(int iRow, int iField) const = 0;
168
185 virtual double GetValueAsDouble(int iRow, int iField) const = 0;
186
204 virtual bool GetValueAsBoolean(int iRow, int iField) const = 0;
205
222 virtual GDALRATDateTime GetValueAsDateTime(int iRow, int iField) const = 0;
223
243 virtual const GByte *GetValueAsWKBGeometry(int iRow, int iField,
244 size_t &nWKBSize) const = 0;
245
262 virtual CPLErr SetValue(int iRow, int iField, const char *pszValue) = 0;
263
280 virtual CPLErr SetValue(int iRow, int iField, int nValue) = 0;
281
298 virtual CPLErr SetValue(int iRow, int iField, double dfValue) = 0;
299
317 virtual CPLErr SetValue(int iRow, int iField, bool bValue) = 0;
318
338 virtual CPLErr SetValue(int iRow, int iField,
339 const GDALRATDateTime &sDateTime) = 0;
340
358 virtual CPLErr SetValue(int iRow, int iField, const void *pabyWKB,
359 size_t nWKBSize) = 0;
360
373 virtual int ChangesAreWrittenToFile() = 0;
374
381 virtual CPLErr SetTableType(const GDALRATTableType eInTableType) = 0;
382
390 virtual GDALRATTableType GetTableType() const = 0;
391
392 virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow,
393 int iLength, double *pdfData);
394 virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow,
395 int iLength, int *pnData);
396 virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow,
397 int iLength, char **papszStrList);
398 virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow,
399 int iLength, bool *pbData);
400 virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow,
401 int iLength, GDALRATDateTime *pasDateTime);
402 virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow,
403 int iLength, GByte **ppabyWKB, size_t *pnWKBSize);
404
405 virtual void SetRowCount(int iCount);
406 virtual int GetRowOfValue(double dfValue) const;
407 virtual int GetRowOfValue(int nValue) const;
408
409 virtual CPLErr CreateColumn(const char *pszFieldName,
410 GDALRATFieldType eFieldType,
411 GDALRATFieldUsage eFieldUsage);
412 virtual CPLErr SetLinearBinning(double dfRow0Min, double dfBinSize);
413 virtual int GetLinearBinning(double *pdfRow0Min, double *pdfBinSize) const;
414
421 virtual CPLXMLNode *Serialize() const;
422 virtual void *SerializeJSON() const;
423 virtual CPLErr XMLInit(const CPLXMLNode *, const char *);
424
426 virtual GDALColorTable *TranslateToColorTable(int nEntryCount = -1);
427
428 virtual void DumpReadable(FILE * = nullptr);
429
432 static inline GDALRasterAttributeTableH
434 {
435 return static_cast<GDALRasterAttributeTableH>(poRAT);
436 }
437
440 static inline GDALRasterAttributeTable *
442 {
443 return static_cast<GDALRasterAttributeTable *>(hRAT);
444 }
445
450 virtual void RemoveStatistics() = 0;
451
453 static std::string DateTimeToString(const GDALRATDateTime &sDateTime);
454 static bool StringToDateTime(const char *pszStr,
455 GDALRATDateTime &sDateTime);
456
457 static std::string WKBGeometryToWKT(const void *pabyWKB, size_t nWKBSize);
458 static std::vector<GByte> WKTGeometryToWKB(const char *pszWKT);
460
461 protected:
463 GDALRasterAttributeTable() = default;
466 operator=(const GDALRasterAttributeTable &) = default;
468 GDALRasterAttributeTable &operator=(GDALRasterAttributeTable &&) = default;
469
470 CPLErr ValuesIOBooleanFromIntoInt(GDALRWFlag eRWFlag, int iField,
471 int iStartRow, int iLength, bool *pbData);
472 CPLErr ValuesIODateTimeFromIntoString(GDALRWFlag eRWFlag, int iField,
473 int iStartRow, int iLength,
474 GDALRATDateTime *psDateTime);
475 CPLErr ValuesIOWKBGeometryFromIntoString(GDALRWFlag eRWFlag, int iField,
476 int iStartRow, int iLength,
477 GByte **ppabyWKB,
478 size_t *pnWKBSize);
480};
481
482/************************************************************************/
483/* GDALDefaultRasterAttributeTable */
484/************************************************************************/
485
487
489{
490 private:
491 struct GDALRasterAttributeField
492 {
493 CPLString sName{};
494
496
498
499 std::vector<GInt32> anValues{};
500 std::vector<double> adfValues{};
501 std::vector<CPLString> aosValues{};
502 std::vector<bool> abValues{};
503 std::vector<GDALRATDateTime> asDateTimeValues{};
504 std::vector<std::vector<GByte>> aabyWKBGeometryValues{};
505 };
506
507 std::vector<GDALRasterAttributeField> aoFields{};
508
509 int bLinearBinning = false; // TODO(schwehr): Can this be a bool?
510 double dfRow0Min = -0.5;
511 double dfBinSize = 1.0;
512
513 GDALRATTableType eTableType = GRTT_THEMATIC;
514
515 void AnalyseColumns();
516 int bColumnsAnalysed = false; // TODO(schwehr): Can this be a bool?
517 int nMinCol = -1;
518 int nMaxCol = -1;
519
520 int nRowCount = 0;
521
522 CPLString osWorkingResult{};
523 mutable std::vector<GByte> m_abyWKB{};
524
525 public:
528
531 default;
533 operator=(const GDALDefaultRasterAttributeTable &) = default;
535 default;
537 operator=(GDALDefaultRasterAttributeTable &&) = default;
539
540 GDALDefaultRasterAttributeTable *Clone() const override;
541
542 int GetColumnCount() const override;
543
544 const char *GetNameOfCol(int) const override;
545 GDALRATFieldUsage GetUsageOfCol(int) const override;
546 GDALRATFieldType GetTypeOfCol(int) const override;
547
548 int GetColOfUsage(GDALRATFieldUsage) const override;
549
550 int GetRowCount() const override;
551
552 const char *GetValueAsString(int iRow, int iField) const override;
553 int GetValueAsInt(int iRow, int iField) const override;
554 double GetValueAsDouble(int iRow, int iField) const override;
555 bool GetValueAsBoolean(int iRow, int iField) const override;
556 GDALRATDateTime GetValueAsDateTime(int iRow, int iField) const override;
557 const GByte *GetValueAsWKBGeometry(int iRow, int iField,
558 size_t &nWKBSize) const override;
559
560 CPLErr SetValue(int iRow, int iField, const char *pszValue) override;
561 CPLErr SetValue(int iRow, int iField, double dfValue) override;
562 CPLErr SetValue(int iRow, int iField, int nValue) override;
563 CPLErr SetValue(int iRow, int iField, bool bValue) override;
564 CPLErr SetValue(int iRow, int iField,
565 const GDALRATDateTime &sDateTime) override;
566 CPLErr SetValue(int iRow, int iField, const void *pabyWKB,
567 size_t nWKBSize) override;
568
569 int ChangesAreWrittenToFile() override;
570 void SetRowCount(int iCount) override;
571
572 int GetRowOfValue(double dfValue) const override;
573 int GetRowOfValue(int nValue) const override;
574
575 CPLErr CreateColumn(const char *pszFieldName, GDALRATFieldType eFieldType,
576 GDALRATFieldUsage eFieldUsage) override;
577 CPLErr SetLinearBinning(double dfRow0Min, double dfBinSize) override;
578 int GetLinearBinning(double *pdfRow0Min, double *pdfBinSize) const override;
579
580 CPLErr SetTableType(const GDALRATTableType eInTableType) override;
581 GDALRATTableType GetTableType() const override;
582
583 void RemoveStatistics() override;
584};
585
586std::unique_ptr<GDALRasterAttributeTable>
587 CPL_DLL GDALLoadVATDBF(const char *pszFilename);
588
589#endif /* ndef GDAL_RAT_H_INCLUDED */
Convenient string class based on std::string.
Definition cpl_string.h:320
A color table / palette.
Definition gdal_colortable.h:32
Raster Attribute Table container.
Definition gdal_rat.h:489
GDALDefaultRasterAttributeTable()
Construct empty table.
int GetRowOfValue(double dfValue) const override
Get row for pixel value.
Definition gdal_rat.cpp:3094
int ChangesAreWrittenToFile() override
Determine whether changes made to this RAT are reflected directly in the dataset.
Definition gdal_rat.cpp:3064
int GetColumnCount() const override
Fetch table column count.
Definition gdal_rat.cpp:1830
GDALRATFieldUsage GetUsageOfCol(int) const override
Fetch column usage value.
Definition gdal_rat.cpp:1903
void SetRowCount(int iCount) override
Set row count.
Definition gdal_rat.cpp:2531
GDALRATFieldType GetTypeOfCol(int) const override
Fetch column type.
Definition gdal_rat.cpp:1944
CPLErr SetValue(int iRow, int iField, const char *pszValue) override
Set value.
Definition gdal_rat.cpp:2579
double GetValueAsDouble(int iRow, int iField) const override
Fetch field value as a double.
Definition gdal_rat.cpp:2215
void RemoveStatistics() override
Remove Statistics from RAT.
Definition gdal_rat.cpp:3324
GDALRATDateTime GetValueAsDateTime(int iRow, int iField) const override
Fetch field value as a datetime.
Definition gdal_rat.cpp:2352
bool GetValueAsBoolean(int iRow, int iField) const override
Fetch field value as a boolean.
Definition gdal_rat.cpp:2282
GDALRATTableType GetTableType() const override
Get RAT Table Type.
Definition gdal_rat.cpp:3227
CPLErr CreateColumn(const char *pszFieldName, GDALRATFieldType eFieldType, GDALRATFieldUsage eFieldUsage) override
If the table already has rows, all row values for the new column will be initialized to the default v...
Definition gdal_rat.cpp:3261
CPLErr SetLinearBinning(double dfRow0Min, double dfBinSize) override
Set linear binning information.
Definition gdal_rat.cpp:3184
GDALDefaultRasterAttributeTable * Clone() const override
Copy Raster Attribute Table.
Definition gdal_rat.cpp:3364
const char * GetValueAsString(int iRow, int iField) const override
Fetch field value as a string.
Definition gdal_rat.cpp:2046
int GetRowCount() const override
Fetch row count.
Definition gdal_rat.cpp:2019
int GetColOfUsage(GDALRATFieldUsage) const override
Return the index of the column that corresponds to the passed usage.
Definition gdal_rat.cpp:1983
int GetLinearBinning(double *pdfRow0Min, double *pdfBinSize) const override
Get linear binning information.
Definition gdal_rat.cpp:3199
const char * GetNameOfCol(int) const override
Fetch name of indicated column.
Definition gdal_rat.cpp:1862
CPLErr SetTableType(const GDALRATTableType eInTableType) override
Set RAT Table Type.
Definition gdal_rat.cpp:3249
const GByte * GetValueAsWKBGeometry(int iRow, int iField, size_t &nWKBSize) const override
Fetch field value as a WKB geometry.
Definition gdal_rat.cpp:2436
int GetValueAsInt(int iRow, int iField) const override
Fetch field value as a integer.
Definition gdal_rat.cpp:2149
The GDALRasterAttributeTable (or RAT) class is used to encapsulate a table used to provide attribute ...
Definition gdal_rat.h:48
virtual CPLErr SetValue(int iRow, int iField, bool bValue)=0
Set field value from boolean.
virtual void DumpReadable(FILE *=nullptr)
Dump RAT in readable form.
Definition gdal_rat.cpp:1713
virtual const char * GetValueAsString(int iRow, int iField) const =0
Fetch field value as a string.
virtual CPLErr XMLInit(const CPLXMLNode *, const char *)
Deserialize from XML.
Definition gdal_rat.cpp:1414
virtual double GetValueAsDouble(int iRow, int iField) const =0
Fetch field value as a double.
static GDALRasterAttributeTableH ToHandle(GDALRasterAttributeTable *poRAT)
Convert a GDALRasterAttributeTable* to a GDALRasterAttributeTableH.
Definition gdal_rat.h:433
virtual void RemoveStatistics()=0
Remove statistics from the RAT.
virtual bool GetValueAsBoolean(int iRow, int iField) const =0
Fetch field value as a boolean.
virtual int GetRowCount() const =0
Fetch row count.
virtual GDALRATDateTime GetValueAsDateTime(int iRow, int iField) const =0
Fetch field value as a datetime.
virtual CPLErr CreateColumn(const char *pszFieldName, GDALRATFieldType eFieldType, GDALRATFieldUsage eFieldUsage)
If the table already has rows, all row values for the new column will be initialized to the default v...
Definition gdal_rat.cpp:968
virtual int ChangesAreWrittenToFile()=0
Determine whether changes made to this RAT are reflected directly in the dataset.
virtual int GetRowOfValue(double dfValue) const
Get row for pixel value.
Definition gdal_rat.cpp:891
virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength, double *pdfData)
Read or Write a block of doubles to/from the Attribute Table.
Definition gdal_rat.cpp:183
virtual CPLErr SetTableType(const GDALRATTableType eInTableType)=0
Set the RAT table type.
virtual CPLErr SetValue(int iRow, int iField, double dfValue)=0
Set field value from double.
static GDALRasterAttributeTable * FromHandle(GDALRasterAttributeTableH hRAT)
Convert a GDALRasterAttributeTableH to a GDALRasterAttributeTable*.
Definition gdal_rat.h:441
virtual int GetColumnCount() const =0
Fetch table column count.
virtual void SetRowCount(int iCount)
Set row count.
Definition gdal_rat.cpp:843
virtual CPLErr SetValue(int iRow, int iField, const GDALRATDateTime &sDateTime)=0
Set field value from datetime.
virtual GDALRATFieldUsage GetUsageOfCol(int iCol) const =0
Fetch column usage value.
virtual void * SerializeJSON() const
Serialize as a JSON object.
Definition gdal_rat.cpp:1279
virtual GDALColorTable * TranslateToColorTable(int nEntryCount=-1)
Translate to a color table.
Definition gdal_rat.cpp:1610
virtual CPLErr SetValue(int iRow, int iField, int nValue)=0
Set field value from integer.
virtual GDALRasterAttributeTable * Clone() const =0
Copy Raster Attribute Table.
virtual CPLErr SetValue(int iRow, int iField, const void *pabyWKB, size_t nWKBSize)=0
Set field value from a WKB geometry.
virtual GDALRATTableType GetTableType() const =0
Get the RAT table type.
virtual const char * GetNameOfCol(int iCol) const =0
Fetch name of indicated column.
virtual CPLErr SetLinearBinning(double dfRow0Min, double dfBinSize)
Set linear binning information.
Definition gdal_rat.cpp:1016
virtual int GetValueAsInt(int iRow, int iField) const =0
Fetch field value as a integer.
virtual CPLErr InitializeFromColorTable(const GDALColorTable *)
Initialize from color table.
Definition gdal_rat.cpp:1535
virtual const GByte * GetValueAsWKBGeometry(int iRow, int iField, size_t &nWKBSize) const =0
Fetch field value as a WKB geometry.
virtual int GetColOfUsage(GDALRATFieldUsage eUsage) const =0
Fetch column index for given usage.
virtual CPLXMLNode * Serialize() const
Serialize.
Definition gdal_rat.cpp:1131
virtual CPLErr SetValue(int iRow, int iField, const char *pszValue)=0
Set field value from string.
virtual int GetLinearBinning(double *pdfRow0Min, double *pdfBinSize) const
Get linear binning information.
Definition gdal_rat.cpp:1059
virtual GDALRATFieldType GetTypeOfCol(int iCol) const =0
Fetch column type.
CPL error handling services.
CPLErr
Error category.
Definition cpl_error.h:37
Definitions for CPL mini XML Parser/Serializer.
Core portability definitions for CPL.
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:175
Various convenience functions for working with strings and string lists.
Public (C callable) GDAL entry points.
GDALRATTableType
RAT table type (thematic or athematic).
Definition gdal.h:2263
@ GRTT_THEMATIC
Definition gdal.h:2264
GDALRATFieldUsage
Field usage of raster attribute table.
Definition gdal.h:2238
@ GFU_Generic
Definition gdal.h:2239
GDALRATFieldType
Field type of raster attribute table.
Definition gdal.h:2224
@ GFT_Integer
Definition gdal.h:2225
GDALRWFlag
Definition gdal.h:125
void * GDALRasterAttributeTableH
Opaque type used for the C bindings of the C++ GDALRasterAttributeTable class.
Definition gdal_fwd.h:58
This file is legacy since GDAL 3.12, but will be kept at least in the whole GDAL 3....
Document node structure.
Definition cpl_minixml.h:54
Structure encoding a DateTime field for a GDAL Raster Attribute Table.
Definition gdal.h:2309