Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: Hierarchical Data Format Release 5 (HDF5)
4 : * Purpose: RAT utility
5 : * Author: Even Rouault <even dot rouault at spatialys dot com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2023, Even Rouault <even dot rouault at spatialys dot com>
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : ****************************************************************************/
28 :
29 : #include "rat.h"
30 :
31 : /************************************************************************/
32 : /* CreateRAT() */
33 : /************************************************************************/
34 :
35 : std::unique_ptr<GDALRasterAttributeTable>
36 8 : HDF5CreateRAT(const std::shared_ptr<GDALMDArray> &poValues,
37 : bool bFirstColIsMinMax)
38 : {
39 16 : auto poRAT = std::make_unique<GDALDefaultRasterAttributeTable>();
40 8 : const auto &poComponents = poValues->GetDataType().GetComponents();
41 32 : for (const auto &poComponent : poComponents)
42 : {
43 : GDALRATFieldType eType;
44 24 : if (poComponent->GetType().GetClass() == GEDTC_NUMERIC)
45 : {
46 16 : if (GDALDataTypeIsInteger(
47 32 : poComponent->GetType().GetNumericDataType()))
48 8 : eType = GFT_Integer;
49 : else
50 8 : eType = GFT_Real;
51 : }
52 : else
53 : {
54 8 : eType = GFT_String;
55 : }
56 48 : poRAT->CreateColumn(poComponent->GetName().c_str(), eType,
57 6 : bFirstColIsMinMax && poRAT->GetColumnCount() == 0
58 : ? GFU_MinMax
59 24 : : GFU_Generic);
60 : }
61 :
62 8 : const auto &oValuesDT = poValues->GetDataType();
63 16 : std::vector<GByte> abyRow(oValuesDT.GetSize());
64 8 : const int nRows = static_cast<int>(poValues->GetDimensions()[0]->GetSize());
65 42 : for (int iRow = 0; iRow < nRows; iRow++)
66 : {
67 34 : const GUInt64 arrayStartIdx = static_cast<GUInt64>(iRow);
68 34 : const size_t count = 1;
69 34 : const GInt64 arrayStep = 0;
70 34 : const GPtrDiff_t bufferStride = 0;
71 68 : poValues->Read(&arrayStartIdx, &count, &arrayStep, &bufferStride,
72 34 : oValuesDT, &abyRow[0]);
73 34 : int iCol = 0;
74 136 : for (const auto &poComponent : poComponents)
75 : {
76 102 : const auto eRATType = poRAT->GetTypeOfCol(iCol);
77 102 : if (eRATType == GFT_Integer)
78 : {
79 34 : int nValue = 0;
80 34 : GDALCopyWords(&abyRow[poComponent->GetOffset()],
81 34 : poComponent->GetType().GetNumericDataType(), 0,
82 : &nValue, GDT_Int32, 0, 1);
83 34 : poRAT->SetValue(iRow, iCol, nValue);
84 : }
85 68 : else if (eRATType == GFT_Real)
86 : {
87 34 : double dfValue = 0;
88 34 : GDALCopyWords(&abyRow[poComponent->GetOffset()],
89 34 : poComponent->GetType().GetNumericDataType(), 0,
90 : &dfValue, GDT_Float64, 0, 1);
91 34 : poRAT->SetValue(iRow, iCol, dfValue);
92 : }
93 : else
94 : {
95 34 : char *pszStr = nullptr;
96 68 : GDALExtendedDataType::CopyValue(
97 34 : &abyRow[poComponent->GetOffset()], poComponent->GetType(),
98 68 : &pszStr, GDALExtendedDataType::CreateString());
99 34 : if (pszStr)
100 : {
101 34 : poRAT->SetValue(iRow, iCol, pszStr);
102 : }
103 34 : CPLFree(pszStr);
104 : }
105 102 : iCol++;
106 : }
107 34 : oValuesDT.FreeDynamicMemory(&abyRow[0]);
108 : }
109 16 : return poRAT;
110 : }
|