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
34 class 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
45 class GDALDefaultRasterAttributeTable;
46
47 class CPL_DLL GDALRasterAttributeTable
48{
49 public:
50 virtual ~GDALRasterAttributeTable();
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
425 virtual CPLErr InitializeFromColorTable(const GDALColorTable *);
426 virtual GDALColorTable *TranslateToColorTable(int nEntryCount = -1);
427
428 virtual void DumpReadable(FILE * = nullptr);
429
432 static inline GDALRasterAttributeTableH
433 ToHandle(GDALRasterAttributeTable *poRAT)
434 {
435 return static_cast<GDALRasterAttributeTableH>(poRAT);
436 }
437
440 static inline GDALRasterAttributeTable *
441 FromHandle(GDALRasterAttributeTableH hRAT)
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;
464 GDALRasterAttributeTable(const GDALRasterAttributeTable &) = default;
465 GDALRasterAttributeTable &
466 operator=(const GDALRasterAttributeTable &) = default;
467 GDALRasterAttributeTable(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
488 class CPL_DLL GDALDefaultRasterAttributeTable : public GDALRasterAttributeTable
489{
490 private:
491 struct GDALRasterAttributeField
492 {
493 CPLString sName{};
494
495 GDALRATFieldType eType = GFT_Integer;
496
497 GDALRATFieldUsage eUsage = GFU_Generic;
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:
526 GDALDefaultRasterAttributeTable();
527 ~GDALDefaultRasterAttributeTable() override;
528
530 GDALDefaultRasterAttributeTable(const GDALDefaultRasterAttributeTable &) =
531 default;
532 GDALDefaultRasterAttributeTable &
533 operator=(const GDALDefaultRasterAttributeTable &) = default;
534 GDALDefaultRasterAttributeTable(GDALDefaultRasterAttributeTable &&) =
535 default;
536 GDALDefaultRasterAttributeTable &
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 */
CPLString
Convenient string class based on std::string.
Definition cpl_string.h:320
GDALColorTable
A color table / palette.
Definition gdal_colortable.h:32
GDALDefaultRasterAttributeTable
Raster Attribute Table container.
Definition gdal_rat.h:489
GDALDefaultRasterAttributeTable::GDALDefaultRasterAttributeTable
GDALDefaultRasterAttributeTable()
Construct empty table.
GDALRasterAttributeTable
The GDALRasterAttributeTable (or RAT) class is used to encapsulate a table used to provide attribute ...
Definition gdal_rat.h:48
GDALRasterAttributeTable::SetValue
virtual CPLErr SetValue(int iRow, int iField, bool bValue)=0
Set field value from boolean.
GDALRasterAttributeTable::GetValueAsString
virtual const char * GetValueAsString(int iRow, int iField) const =0
Fetch field value as a string.
GDALRasterAttributeTable::GetValueAsDouble
virtual double GetValueAsDouble(int iRow, int iField) const =0
Fetch field value as a double.
GDALRasterAttributeTable::ToHandle
static GDALRasterAttributeTableH ToHandle(GDALRasterAttributeTable *poRAT)
Convert a GDALRasterAttributeTable* to a GDALRasterAttributeTableH.
Definition gdal_rat.h:433
GDALRasterAttributeTable::RemoveStatistics
virtual void RemoveStatistics()=0
Remove statistics from the RAT.
GDALRasterAttributeTable::GetValueAsBoolean
virtual bool GetValueAsBoolean(int iRow, int iField) const =0
Fetch field value as a boolean.
GDALRasterAttributeTable::GetRowCount
virtual int GetRowCount() const =0
Fetch row count.
GDALRasterAttributeTable::GetValueAsDateTime
virtual GDALRATDateTime GetValueAsDateTime(int iRow, int iField) const =0
Fetch field value as a datetime.
GDALRasterAttributeTable::ChangesAreWrittenToFile
virtual int ChangesAreWrittenToFile()=0
Determine whether changes made to this RAT are reflected directly in the dataset.
GDALRasterAttributeTable::SetTableType
virtual CPLErr SetTableType(const GDALRATTableType eInTableType)=0
Set the RAT table type.
GDALRasterAttributeTable::SetValue
virtual CPLErr SetValue(int iRow, int iField, double dfValue)=0
Set field value from double.
GDALRasterAttributeTable::FromHandle
static GDALRasterAttributeTable * FromHandle(GDALRasterAttributeTableH hRAT)
Convert a GDALRasterAttributeTableH to a GDALRasterAttributeTable*.
Definition gdal_rat.h:441
GDALRasterAttributeTable::GetColumnCount
virtual int GetColumnCount() const =0
Fetch table column count.
GDALRasterAttributeTable::SetValue
virtual CPLErr SetValue(int iRow, int iField, const GDALRATDateTime &sDateTime)=0
Set field value from datetime.
GDALRasterAttributeTable::GetUsageOfCol
virtual GDALRATFieldUsage GetUsageOfCol(int iCol) const =0
Fetch column usage value.
GDALRasterAttributeTable::SetValue
virtual CPLErr SetValue(int iRow, int iField, int nValue)=0
Set field value from integer.
GDALRasterAttributeTable::Clone
virtual GDALRasterAttributeTable * Clone() const =0
Copy Raster Attribute Table.
GDALRasterAttributeTable::SetValue
virtual CPLErr SetValue(int iRow, int iField, const void *pabyWKB, size_t nWKBSize)=0
Set field value from a WKB geometry.
GDALRasterAttributeTable::GetTableType
virtual GDALRATTableType GetTableType() const =0
Get the RAT table type.
GDALRasterAttributeTable::GetNameOfCol
virtual const char * GetNameOfCol(int iCol) const =0
Fetch name of indicated column.
GDALRasterAttributeTable::GetValueAsInt
virtual int GetValueAsInt(int iRow, int iField) const =0
Fetch field value as a integer.
GDALRasterAttributeTable::GetValueAsWKBGeometry
virtual const GByte * GetValueAsWKBGeometry(int iRow, int iField, size_t &nWKBSize) const =0
Fetch field value as a WKB geometry.
GDALRasterAttributeTable::GetColOfUsage
virtual int GetColOfUsage(GDALRATFieldUsage eUsage) const =0
Fetch column index for given usage.
GDALRasterAttributeTable::SetValue
virtual CPLErr SetValue(int iRow, int iField, const char *pszValue)=0
Set field value from string.
GDALRasterAttributeTable::GetTypeOfCol
virtual GDALRATFieldType GetTypeOfCol(int iCol) const =0
Fetch column type.
cpl_error.h
CPL error handling services.
CPLErr
CPLErr
Error category.
Definition cpl_error.h:37
cpl_minixml.h
Definitions for CPL mini XML Parser/Serializer.
cpl_port.h
Core portability definitions for CPL.
GByte
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:175
cpl_string.h
Various convenience functions for working with strings and string lists.
gdal.h
Public (C callable) GDAL entry points.
GDALRATTableType
GDALRATTableType
RAT table type (thematic or athematic)
Definition gdal.h:2263
GRTT_THEMATIC
@ GRTT_THEMATIC
Definition gdal.h:2264
GDALRATFieldUsage
GDALRATFieldUsage
Field usage of raster attribute table.
Definition gdal.h:2238
GFU_Generic
@ GFU_Generic
Definition gdal.h:2239
GDALRATFieldType
GDALRATFieldType
Field type of raster attribute table.
Definition gdal.h:2224
GFT_Integer
@ GFT_Integer
Definition gdal.h:2225
GDALRWFlag
GDALRWFlag
Definition gdal.h:125
GDALRasterAttributeTableH
void * GDALRasterAttributeTableH
Opaque type used for the C bindings of the C++ GDALRasterAttributeTable class.
Definition gdal_fwd.h:58
gdal_priv.h
This file is legacy since GDAL 3.12, but will be kept at least in the whole GDAL 3....
CPLXMLNode
Document node structure.
Definition cpl_minixml.h:54
GDALRATDateTime
Structure encoding a DateTime field for a GDAL Raster Attribute Table.
Definition gdal.h:2309