Coverage Report

Created: 2025-06-13 06:18

/src/gdal/gcore/gdalmultidim.cpp
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Name:     gdalmultidim.cpp
4
 * Project:  GDAL Core
5
 * Purpose:  GDAL Core C++/Private implementation for multidimensional support
6
 * Author:   Even Rouault <even.rouault at spatialys.com>
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 2019, Even Rouault <even.rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include <assert.h>
15
#include <algorithm>
16
#include <limits>
17
#include <list>
18
#include <queue>
19
#include <set>
20
#include <utility>
21
#include <time.h>
22
23
#include <cmath>
24
#include <ctype.h>  // isalnum
25
26
#include "cpl_error_internal.h"
27
#include "cpl_float.h"
28
#include "gdal_priv.h"
29
#include "gdal_pam.h"
30
#include "gdal_rat.h"
31
#include "gdal_utils.h"
32
#include "cpl_safemaths.hpp"
33
#include "memmultidim.h"
34
#include "ogrsf_frmts.h"
35
#include "gdalmultidim_priv.h"
36
37
#if defined(__clang__) || defined(_MSC_VER)
38
#define COMPILER_WARNS_ABOUT_ABSTRACT_VBASE_INIT
39
#endif
40
41
/************************************************************************/
42
/*                       GDALMDArrayUnscaled                            */
43
/************************************************************************/
44
45
class GDALMDArrayUnscaled final : public GDALPamMDArray
46
{
47
  private:
48
    std::shared_ptr<GDALMDArray> m_poParent{};
49
    const GDALExtendedDataType m_dt;
50
    bool m_bHasNoData;
51
    const double m_dfScale;
52
    const double m_dfOffset;
53
    std::vector<GByte> m_abyRawNoData{};
54
55
  protected:
56
    explicit GDALMDArrayUnscaled(const std::shared_ptr<GDALMDArray> &poParent,
57
                                 double dfScale, double dfOffset,
58
                                 double dfOverriddenDstNodata, GDALDataType eDT)
59
0
        : GDALAbstractMDArray(std::string(),
60
0
                              "Unscaled view of " + poParent->GetFullName()),
61
0
          GDALPamMDArray(
62
0
              std::string(), "Unscaled view of " + poParent->GetFullName(),
63
0
              GDALPamMultiDim::GetPAM(poParent), poParent->GetContext()),
64
0
          m_poParent(std::move(poParent)),
65
0
          m_dt(GDALExtendedDataType::Create(eDT)),
66
0
          m_bHasNoData(m_poParent->GetRawNoDataValue() != nullptr),
67
0
          m_dfScale(dfScale), m_dfOffset(dfOffset)
68
0
    {
69
0
        m_abyRawNoData.resize(m_dt.GetSize());
70
0
        const auto eNonComplexDT =
71
0
            GDALGetNonComplexDataType(m_dt.GetNumericDataType());
72
0
        GDALCopyWords64(
73
0
            &dfOverriddenDstNodata, GDT_Float64, 0, m_abyRawNoData.data(),
74
0
            eNonComplexDT, GDALGetDataTypeSizeBytes(eNonComplexDT),
75
0
            GDALDataTypeIsComplex(m_dt.GetNumericDataType()) ? 2 : 1);
76
0
    }
77
78
    bool IRead(const GUInt64 *arrayStartIdx, const size_t *count,
79
               const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
80
               const GDALExtendedDataType &bufferDataType,
81
               void *pDstBuffer) const override;
82
83
    bool IWrite(const GUInt64 *arrayStartIdx, const size_t *count,
84
                const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
85
                const GDALExtendedDataType &bufferDataType,
86
                const void *pSrcBuffer) override;
87
88
    bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count,
89
                     CSLConstList papszOptions) const override
90
0
    {
91
0
        return m_poParent->AdviseRead(arrayStartIdx, count, papszOptions);
92
0
    }
93
94
  public:
95
    static std::shared_ptr<GDALMDArrayUnscaled>
96
    Create(const std::shared_ptr<GDALMDArray> &poParent, double dfScale,
97
           double dfOffset, double dfDstNodata, GDALDataType eDT)
98
0
    {
99
0
        auto newAr(std::shared_ptr<GDALMDArrayUnscaled>(new GDALMDArrayUnscaled(
100
0
            poParent, dfScale, dfOffset, dfDstNodata, eDT)));
101
0
        newAr->SetSelf(newAr);
102
0
        return newAr;
103
0
    }
104
105
    bool IsWritable() const override
106
0
    {
107
0
        return m_poParent->IsWritable();
108
0
    }
109
110
    const std::string &GetFilename() const override
111
0
    {
112
0
        return m_poParent->GetFilename();
113
0
    }
114
115
    const std::vector<std::shared_ptr<GDALDimension>> &
116
    GetDimensions() const override
117
0
    {
118
0
        return m_poParent->GetDimensions();
119
0
    }
120
121
    const GDALExtendedDataType &GetDataType() const override
122
0
    {
123
0
        return m_dt;
124
0
    }
125
126
    const std::string &GetUnit() const override
127
0
    {
128
0
        return m_poParent->GetUnit();
129
0
    }
130
131
    std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override
132
0
    {
133
0
        return m_poParent->GetSpatialRef();
134
0
    }
135
136
    const void *GetRawNoDataValue() const override
137
0
    {
138
0
        return m_bHasNoData ? m_abyRawNoData.data() : nullptr;
139
0
    }
140
141
    bool SetRawNoDataValue(const void *pRawNoData) override
142
0
    {
143
0
        m_bHasNoData = true;
144
0
        memcpy(m_abyRawNoData.data(), pRawNoData, m_dt.GetSize());
145
0
        return true;
146
0
    }
147
148
    std::vector<GUInt64> GetBlockSize() const override
149
0
    {
150
0
        return m_poParent->GetBlockSize();
151
0
    }
152
153
    std::shared_ptr<GDALAttribute>
154
    GetAttribute(const std::string &osName) const override
155
0
    {
156
0
        return m_poParent->GetAttribute(osName);
157
0
    }
158
159
    std::vector<std::shared_ptr<GDALAttribute>>
160
    GetAttributes(CSLConstList papszOptions = nullptr) const override
161
0
    {
162
0
        return m_poParent->GetAttributes(papszOptions);
163
0
    }
164
165
    bool SetUnit(const std::string &osUnit) override
166
0
    {
167
0
        return m_poParent->SetUnit(osUnit);
168
0
    }
169
170
    bool SetSpatialRef(const OGRSpatialReference *poSRS) override
171
0
    {
172
0
        return m_poParent->SetSpatialRef(poSRS);
173
0
    }
174
175
    std::shared_ptr<GDALAttribute>
176
    CreateAttribute(const std::string &osName,
177
                    const std::vector<GUInt64> &anDimensions,
178
                    const GDALExtendedDataType &oDataType,
179
                    CSLConstList papszOptions = nullptr) override
180
0
    {
181
0
        return m_poParent->CreateAttribute(osName, anDimensions, oDataType,
182
0
                                           papszOptions);
183
0
    }
184
};
185
186
/************************************************************************/
187
/*                         ~GDALIHasAttribute()                         */
188
/************************************************************************/
189
190
0
GDALIHasAttribute::~GDALIHasAttribute() = default;
191
192
/************************************************************************/
193
/*                            GetAttribute()                            */
194
/************************************************************************/
195
196
/** Return an attribute by its name.
197
 *
198
 * If the attribute does not exist, nullptr should be silently returned.
199
 *
200
 * @note Driver implementation: this method will fallback to
201
 * GetAttributeFromAttributes() is not explicitly implemented
202
 *
203
 * Drivers known to implement it for groups and arrays: MEM, netCDF.
204
 *
205
 * This is the same as the C function GDALGroupGetAttribute() or
206
 * GDALMDArrayGetAttribute().
207
 *
208
 * @param osName Attribute name
209
 * @return the attribute, or nullptr if it does not exist or an error occurred.
210
 */
211
std::shared_ptr<GDALAttribute>
212
GDALIHasAttribute::GetAttribute(const std::string &osName) const
213
0
{
214
0
    return GetAttributeFromAttributes(osName);
215
0
}
216
217
/************************************************************************/
218
/*                       GetAttributeFromAttributes()                   */
219
/************************************************************************/
220
221
/** Possible fallback implementation for GetAttribute() using GetAttributes().
222
 */
223
std::shared_ptr<GDALAttribute>
224
GDALIHasAttribute::GetAttributeFromAttributes(const std::string &osName) const
225
0
{
226
0
    auto attrs(GetAttributes());
227
0
    for (const auto &attr : attrs)
228
0
    {
229
0
        if (attr->GetName() == osName)
230
0
            return attr;
231
0
    }
232
0
    return nullptr;
233
0
}
234
235
/************************************************************************/
236
/*                           GetAttributes()                            */
237
/************************************************************************/
238
239
/** Return the list of attributes contained in a GDALMDArray or GDALGroup.
240
 *
241
 * If the attribute does not exist, nullptr should be silently returned.
242
 *
243
 * @note Driver implementation: optionally implemented. If implemented,
244
 * GetAttribute() should also be implemented.
245
 *
246
 * Drivers known to implement it for groups and arrays: MEM, netCDF.
247
 *
248
 * This is the same as the C function GDALGroupGetAttributes() or
249
 * GDALMDArrayGetAttributes().
250
251
 * @param papszOptions Driver specific options determining how attributes
252
 * should be retrieved. Pass nullptr for default behavior.
253
 *
254
 * @return the attributes.
255
 */
256
std::vector<std::shared_ptr<GDALAttribute>>
257
GDALIHasAttribute::GetAttributes(CPL_UNUSED CSLConstList papszOptions) const
258
0
{
259
0
    return {};
260
0
}
261
262
/************************************************************************/
263
/*                             CreateAttribute()                         */
264
/************************************************************************/
265
266
/** Create an attribute within a GDALMDArray or GDALGroup.
267
 *
268
 * The attribute might not be "physically" created until a value is written
269
 * into it.
270
 *
271
 * Optionally implemented.
272
 *
273
 * Drivers known to implement it: MEM, netCDF
274
 *
275
 * This is the same as the C function GDALGroupCreateAttribute() or
276
 * GDALMDArrayCreateAttribute()
277
 *
278
 * @param osName Attribute name.
279
 * @param anDimensions List of dimension sizes, ordered from the slowest varying
280
 *                     dimension first to the fastest varying dimension last.
281
 *                     Empty for a scalar attribute (common case)
282
 * @param oDataType  Attribute data type.
283
 * @param papszOptions Driver specific options determining how the attribute.
284
 * should be created.
285
 *
286
 * @return the new attribute, or nullptr if case of error
287
 */
288
std::shared_ptr<GDALAttribute> GDALIHasAttribute::CreateAttribute(
289
    CPL_UNUSED const std::string &osName,
290
    CPL_UNUSED const std::vector<GUInt64> &anDimensions,
291
    CPL_UNUSED const GDALExtendedDataType &oDataType,
292
    CPL_UNUSED CSLConstList papszOptions)
293
0
{
294
0
    CPLError(CE_Failure, CPLE_NotSupported,
295
0
             "CreateAttribute() not implemented");
296
0
    return nullptr;
297
0
}
298
299
/************************************************************************/
300
/*                          DeleteAttribute()                           */
301
/************************************************************************/
302
303
/** Delete an attribute from a GDALMDArray or GDALGroup.
304
 *
305
 * Optionally implemented.
306
 *
307
 * After this call, if a previously obtained instance of the deleted object
308
 * is still alive, no method other than for freeing it should be invoked.
309
 *
310
 * Drivers known to implement it: MEM, netCDF
311
 *
312
 * This is the same as the C function GDALGroupDeleteAttribute() or
313
 * GDALMDArrayDeleteAttribute()
314
 *
315
 * @param osName Attribute name.
316
 * @param papszOptions Driver specific options determining how the attribute.
317
 * should be deleted.
318
 *
319
 * @return true in case of success
320
 * @since GDAL 3.8
321
 */
322
bool GDALIHasAttribute::DeleteAttribute(CPL_UNUSED const std::string &osName,
323
                                        CPL_UNUSED CSLConstList papszOptions)
324
0
{
325
0
    CPLError(CE_Failure, CPLE_NotSupported,
326
0
             "DeleteAttribute() not implemented");
327
0
    return false;
328
0
}
329
330
/************************************************************************/
331
/*                            GDALGroup()                               */
332
/************************************************************************/
333
334
//! @cond Doxygen_Suppress
335
GDALGroup::GDALGroup(const std::string &osParentName, const std::string &osName,
336
                     const std::string &osContext)
337
0
    : m_osName(osParentName.empty() ? "/" : osName),
338
      m_osFullName(
339
0
          !osParentName.empty()
340
0
              ? ((osParentName == "/" ? "/" : osParentName + "/") + osName)
341
0
              : "/"),
342
0
      m_osContext(osContext)
343
0
{
344
0
}
345
346
//! @endcond
347
348
/************************************************************************/
349
/*                            ~GDALGroup()                              */
350
/************************************************************************/
351
352
0
GDALGroup::~GDALGroup() = default;
353
354
/************************************************************************/
355
/*                          GetMDArrayNames()                           */
356
/************************************************************************/
357
358
/** Return the list of multidimensional array names contained in this group.
359
 *
360
 * @note Driver implementation: optionally implemented. If implemented,
361
 * OpenMDArray() should also be implemented.
362
 *
363
 * Drivers known to implement it: MEM, netCDF.
364
 *
365
 * This is the same as the C function GDALGroupGetMDArrayNames().
366
 *
367
 * @param papszOptions Driver specific options determining how arrays
368
 * should be retrieved. Pass nullptr for default behavior.
369
 *
370
 * @return the array names.
371
 */
372
std::vector<std::string>
373
GDALGroup::GetMDArrayNames(CPL_UNUSED CSLConstList papszOptions) const
374
0
{
375
0
    return {};
376
0
}
377
378
/************************************************************************/
379
/*                     GetMDArrayFullNamesRecursive()                   */
380
/************************************************************************/
381
382
/** Return the list of multidimensional array full names contained in this
383
 * group and its subgroups.
384
 *
385
 * This is the same as the C function GDALGroupGetMDArrayFullNamesRecursive().
386
 *
387
 * @param papszGroupOptions Driver specific options determining how groups
388
 * should be retrieved. Pass nullptr for default behavior.
389
 * @param papszArrayOptions Driver specific options determining how arrays
390
 * should be retrieved. Pass nullptr for default behavior.
391
 *
392
 * @return the array full names.
393
 *
394
 * @since 3.11
395
 */
396
std::vector<std::string>
397
GDALGroup::GetMDArrayFullNamesRecursive(CSLConstList papszGroupOptions,
398
                                        CSLConstList papszArrayOptions) const
399
0
{
400
0
    std::vector<std::string> ret;
401
0
    std::list<std::shared_ptr<GDALGroup>> stackGroups;
402
0
    stackGroups.push_back(nullptr);  // nullptr means this
403
0
    while (!stackGroups.empty())
404
0
    {
405
0
        std::shared_ptr<GDALGroup> groupPtr = std::move(stackGroups.front());
406
0
        stackGroups.erase(stackGroups.begin());
407
0
        const GDALGroup *poCurGroup = groupPtr ? groupPtr.get() : this;
408
0
        for (const std::string &arrayName :
409
0
             poCurGroup->GetMDArrayNames(papszArrayOptions))
410
0
        {
411
0
            std::string osFullName = poCurGroup->GetFullName();
412
0
            if (!osFullName.empty() && osFullName.back() != '/')
413
0
                osFullName += '/';
414
0
            osFullName += arrayName;
415
0
            ret.push_back(std::move(osFullName));
416
0
        }
417
0
        auto insertionPoint = stackGroups.begin();
418
0
        for (const auto &osSubGroup :
419
0
             poCurGroup->GetGroupNames(papszGroupOptions))
420
0
        {
421
0
            auto poSubGroup = poCurGroup->OpenGroup(osSubGroup);
422
0
            if (poSubGroup)
423
0
                stackGroups.insert(insertionPoint, std::move(poSubGroup));
424
0
        }
425
0
    }
426
427
0
    return ret;
428
0
}
429
430
/************************************************************************/
431
/*                            OpenMDArray()                             */
432
/************************************************************************/
433
434
/** Open and return a multidimensional array.
435
 *
436
 * @note Driver implementation: optionally implemented. If implemented,
437
 * GetMDArrayNames() should also be implemented.
438
 *
439
 * Drivers known to implement it: MEM, netCDF.
440
 *
441
 * This is the same as the C function GDALGroupOpenMDArray().
442
 *
443
 * @param osName Array name.
444
 * @param papszOptions Driver specific options determining how the array should
445
 * be opened.  Pass nullptr for default behavior.
446
 *
447
 * @return the array, or nullptr.
448
 */
449
std::shared_ptr<GDALMDArray>
450
GDALGroup::OpenMDArray(CPL_UNUSED const std::string &osName,
451
                       CPL_UNUSED CSLConstList papszOptions) const
452
0
{
453
0
    return nullptr;
454
0
}
455
456
/************************************************************************/
457
/*                           GetGroupNames()                            */
458
/************************************************************************/
459
460
/** Return the list of sub-groups contained in this group.
461
 *
462
 * @note Driver implementation: optionally implemented. If implemented,
463
 * OpenGroup() should also be implemented.
464
 *
465
 * Drivers known to implement it: MEM, netCDF.
466
 *
467
 * This is the same as the C function GDALGroupGetGroupNames().
468
 *
469
 * @param papszOptions Driver specific options determining how groups
470
 * should be retrieved. Pass nullptr for default behavior.
471
 *
472
 * @return the group names.
473
 */
474
std::vector<std::string>
475
GDALGroup::GetGroupNames(CPL_UNUSED CSLConstList papszOptions) const
476
0
{
477
0
    return {};
478
0
}
479
480
/************************************************************************/
481
/*                             OpenGroup()                              */
482
/************************************************************************/
483
484
/** Open and return a sub-group.
485
 *
486
 * @note Driver implementation: optionally implemented. If implemented,
487
 * GetGroupNames() should also be implemented.
488
 *
489
 * Drivers known to implement it: MEM, netCDF.
490
 *
491
 * This is the same as the C function GDALGroupOpenGroup().
492
 *
493
 * @param osName Sub-group name.
494
 * @param papszOptions Driver specific options determining how the sub-group
495
 * should be opened.  Pass nullptr for default behavior.
496
 *
497
 * @return the group, or nullptr.
498
 */
499
std::shared_ptr<GDALGroup>
500
GDALGroup::OpenGroup(CPL_UNUSED const std::string &osName,
501
                     CPL_UNUSED CSLConstList papszOptions) const
502
0
{
503
0
    return nullptr;
504
0
}
505
506
/************************************************************************/
507
/*                        GetVectorLayerNames()                         */
508
/************************************************************************/
509
510
/** Return the list of layer names contained in this group.
511
 *
512
 * @note Driver implementation: optionally implemented. If implemented,
513
 * OpenVectorLayer() should also be implemented.
514
 *
515
 * Drivers known to implement it: OpenFileGDB, FileGDB
516
 *
517
 * Other drivers will return an empty list. GDALDataset::GetLayerCount() and
518
 * GDALDataset::GetLayer() should then be used.
519
 *
520
 * This is the same as the C function GDALGroupGetVectorLayerNames().
521
 *
522
 * @param papszOptions Driver specific options determining how layers
523
 * should be retrieved. Pass nullptr for default behavior.
524
 *
525
 * @return the vector layer names.
526
 * @since GDAL 3.4
527
 */
528
std::vector<std::string>
529
GDALGroup::GetVectorLayerNames(CPL_UNUSED CSLConstList papszOptions) const
530
0
{
531
0
    return {};
532
0
}
533
534
/************************************************************************/
535
/*                           OpenVectorLayer()                          */
536
/************************************************************************/
537
538
/** Open and return a vector layer.
539
 *
540
 * Due to the historical ownership of OGRLayer* by GDALDataset*, the
541
 * lifetime of the returned OGRLayer* is linked to the one of the owner
542
 * dataset (contrary to the general design of this class where objects can be
543
 * used independently of the object that returned them)
544
 *
545
 * @note Driver implementation: optionally implemented. If implemented,
546
 * GetVectorLayerNames() should also be implemented.
547
 *
548
 * Drivers known to implement it: MEM, netCDF.
549
 *
550
 * This is the same as the C function GDALGroupOpenVectorLayer().
551
 *
552
 * @param osName Vector layer name.
553
 * @param papszOptions Driver specific options determining how the layer should
554
 * be opened.  Pass nullptr for default behavior.
555
 *
556
 * @return the group, or nullptr.
557
 */
558
OGRLayer *GDALGroup::OpenVectorLayer(CPL_UNUSED const std::string &osName,
559
                                     CPL_UNUSED CSLConstList papszOptions) const
560
0
{
561
0
    return nullptr;
562
0
}
563
564
/************************************************************************/
565
/*                             GetDimensions()                          */
566
/************************************************************************/
567
568
/** Return the list of dimensions contained in this group and used by its
569
 * arrays.
570
 *
571
 * This is for dimensions that can potentially be used by several arrays.
572
 * Not all drivers might implement this. To retrieve the dimensions used by
573
 * a specific array, use GDALMDArray::GetDimensions().
574
 *
575
 * Drivers known to implement it: MEM, netCDF
576
 *
577
 * This is the same as the C function GDALGroupGetDimensions().
578
 *
579
 * @param papszOptions Driver specific options determining how groups
580
 * should be retrieved. Pass nullptr for default behavior.
581
 *
582
 * @return the dimensions.
583
 */
584
std::vector<std::shared_ptr<GDALDimension>>
585
GDALGroup::GetDimensions(CPL_UNUSED CSLConstList papszOptions) const
586
0
{
587
0
    return {};
588
0
}
589
590
/************************************************************************/
591
/*                         GetStructuralInfo()                          */
592
/************************************************************************/
593
594
/** Return structural information on the group.
595
 *
596
 * This may be the compression, etc..
597
 *
598
 * The return value should not be freed and is valid until GDALGroup is
599
 * released or this function called again.
600
 *
601
 * This is the same as the C function GDALGroupGetStructuralInfo().
602
 */
603
CSLConstList GDALGroup::GetStructuralInfo() const
604
0
{
605
0
    return nullptr;
606
0
}
607
608
/************************************************************************/
609
/*                              CreateGroup()                           */
610
/************************************************************************/
611
612
/** Create a sub-group within a group.
613
 *
614
 * Optionally implemented by drivers.
615
 *
616
 * Drivers known to implement it: MEM, netCDF
617
 *
618
 * This is the same as the C function GDALGroupCreateGroup().
619
 *
620
 * @param osName Sub-group name.
621
 * @param papszOptions Driver specific options determining how the sub-group
622
 * should be created.
623
 *
624
 * @return the new sub-group, or nullptr in case of error.
625
 */
626
std::shared_ptr<GDALGroup>
627
GDALGroup::CreateGroup(CPL_UNUSED const std::string &osName,
628
                       CPL_UNUSED CSLConstList papszOptions)
629
0
{
630
0
    CPLError(CE_Failure, CPLE_NotSupported, "CreateGroup() not implemented");
631
0
    return nullptr;
632
0
}
633
634
/************************************************************************/
635
/*                          DeleteGroup()                               */
636
/************************************************************************/
637
638
/** Delete a sub-group from a group.
639
 *
640
 * Optionally implemented.
641
 *
642
 * After this call, if a previously obtained instance of the deleted object
643
 * is still alive, no method other than for freeing it should be invoked.
644
 *
645
 * Drivers known to implement it: MEM, Zarr
646
 *
647
 * This is the same as the C function GDALGroupDeleteGroup().
648
 *
649
 * @param osName Sub-group name.
650
 * @param papszOptions Driver specific options determining how the group.
651
 * should be deleted.
652
 *
653
 * @return true in case of success
654
 * @since GDAL 3.8
655
 */
656
bool GDALGroup::DeleteGroup(CPL_UNUSED const std::string &osName,
657
                            CPL_UNUSED CSLConstList papszOptions)
658
0
{
659
0
    CPLError(CE_Failure, CPLE_NotSupported, "DeleteGroup() not implemented");
660
0
    return false;
661
0
}
662
663
/************************************************************************/
664
/*                            CreateDimension()                         */
665
/************************************************************************/
666
667
/** Create a dimension within a group.
668
 *
669
 * @note Driver implementation: drivers supporting CreateDimension() should
670
 * implement this method, but do not have necessarily to implement
671
 * GDALGroup::GetDimensions().
672
 *
673
 * Drivers known to implement it: MEM, netCDF
674
 *
675
 * This is the same as the C function GDALGroupCreateDimension().
676
 *
677
 * @param osName Dimension name.
678
 * @param osType Dimension type (might be empty, and ignored by drivers)
679
 * @param osDirection Dimension direction (might be empty, and ignored by
680
 * drivers)
681
 * @param nSize  Number of values indexed by this dimension. Should be > 0.
682
 * @param papszOptions Driver specific options determining how the dimension
683
 * should be created.
684
 *
685
 * @return the new dimension, or nullptr if case of error
686
 */
687
std::shared_ptr<GDALDimension> GDALGroup::CreateDimension(
688
    CPL_UNUSED const std::string &osName, CPL_UNUSED const std::string &osType,
689
    CPL_UNUSED const std::string &osDirection, CPL_UNUSED GUInt64 nSize,
690
    CPL_UNUSED CSLConstList papszOptions)
691
0
{
692
0
    CPLError(CE_Failure, CPLE_NotSupported,
693
0
             "CreateDimension() not implemented");
694
0
    return nullptr;
695
0
}
696
697
/************************************************************************/
698
/*                             CreateMDArray()                          */
699
/************************************************************************/
700
701
/** Create a multidimensional array within a group.
702
 *
703
 * It is recommended that the GDALDimension objects passed in aoDimensions
704
 * belong to this group, either by retrieving them with GetDimensions()
705
 * or creating a new one with CreateDimension().
706
 *
707
 * Optionally implemented.
708
 *
709
 * Drivers known to implement it: MEM, netCDF
710
 *
711
 * This is the same as the C function GDALGroupCreateMDArray().
712
 *
713
 * @note Driver implementation: drivers should take into account the possibility
714
 * that GDALDimension object passed in aoDimensions might belong to a different
715
 * group / dataset / driver and act accordingly.
716
 *
717
 * @param osName Array name.
718
 * @param aoDimensions List of dimensions, ordered from the slowest varying
719
 *                     dimension first to the fastest varying dimension last.
720
 *                     Might be empty for a scalar array (if supported by
721
 * driver)
722
 * @param oDataType  Array data type.
723
 * @param papszOptions Driver specific options determining how the array
724
 * should be created.
725
 *
726
 * @return the new array, or nullptr in case of error
727
 */
728
std::shared_ptr<GDALMDArray> GDALGroup::CreateMDArray(
729
    CPL_UNUSED const std::string &osName,
730
    CPL_UNUSED const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
731
    CPL_UNUSED const GDALExtendedDataType &oDataType,
732
    CPL_UNUSED CSLConstList papszOptions)
733
0
{
734
0
    CPLError(CE_Failure, CPLE_NotSupported, "CreateMDArray() not implemented");
735
0
    return nullptr;
736
0
}
737
738
/************************************************************************/
739
/*                          DeleteMDArray()                             */
740
/************************************************************************/
741
742
/** Delete an array from a group.
743
 *
744
 * Optionally implemented.
745
 *
746
 * After this call, if a previously obtained instance of the deleted object
747
 * is still alive, no method other than for freeing it should be invoked.
748
 *
749
 * Drivers known to implement it: MEM, Zarr
750
 *
751
 * This is the same as the C function GDALGroupDeleteMDArray().
752
 *
753
 * @param osName Arrayname.
754
 * @param papszOptions Driver specific options determining how the array.
755
 * should be deleted.
756
 *
757
 * @return true in case of success
758
 * @since GDAL 3.8
759
 */
760
bool GDALGroup::DeleteMDArray(CPL_UNUSED const std::string &osName,
761
                              CPL_UNUSED CSLConstList papszOptions)
762
0
{
763
0
    CPLError(CE_Failure, CPLE_NotSupported, "DeleteMDArray() not implemented");
764
0
    return false;
765
0
}
766
767
/************************************************************************/
768
/*                           GetTotalCopyCost()                         */
769
/************************************************************************/
770
771
/** Return a total "cost" to copy the group.
772
 *
773
 * Used as a parameter for CopFrom()
774
 */
775
GUInt64 GDALGroup::GetTotalCopyCost() const
776
0
{
777
0
    GUInt64 nCost = COPY_COST;
778
0
    nCost += GetAttributes().size() * GDALAttribute::COPY_COST;
779
780
0
    auto groupNames = GetGroupNames();
781
0
    for (const auto &name : groupNames)
782
0
    {
783
0
        auto subGroup = OpenGroup(name);
784
0
        if (subGroup)
785
0
        {
786
0
            nCost += subGroup->GetTotalCopyCost();
787
0
        }
788
0
    }
789
790
0
    auto arrayNames = GetMDArrayNames();
791
0
    for (const auto &name : arrayNames)
792
0
    {
793
0
        auto array = OpenMDArray(name);
794
0
        if (array)
795
0
        {
796
0
            nCost += array->GetTotalCopyCost();
797
0
        }
798
0
    }
799
0
    return nCost;
800
0
}
801
802
/************************************************************************/
803
/*                               CopyFrom()                             */
804
/************************************************************************/
805
806
/** Copy the content of a group into a new (generally empty) group.
807
 *
808
 * @param poDstRootGroup Destination root group. Must NOT be nullptr.
809
 * @param poSrcDS    Source dataset. Might be nullptr (but for correct behavior
810
 *                   of some output drivers this is not recommended)
811
 * @param poSrcGroup Source group. Must NOT be nullptr.
812
 * @param bStrict Whether to enable strict mode. In strict mode, any error will
813
 *                stop the copy. In relaxed mode, the copy will be attempted to
814
 *                be pursued.
815
 * @param nCurCost  Should be provided as a variable initially set to 0.
816
 * @param nTotalCost Total cost from GetTotalCopyCost().
817
 * @param pfnProgress Progress callback, or nullptr.
818
 * @param pProgressData Progress user data, or nulptr.
819
 * @param papszOptions Creation options. Currently, only array creation
820
 *                     options are supported. They must be prefixed with
821
 * "ARRAY:" . The scope may be further restricted to arrays of a certain
822
 *                     dimension by adding "IF(DIM={ndims}):" after "ARRAY:".
823
 *                     For example, "ARRAY:IF(DIM=2):BLOCKSIZE=256,256" will
824
 *                     restrict BLOCKSIZE=256,256 to arrays of dimension 2.
825
 *                     Restriction to arrays of a given name is done with adding
826
 *                     "IF(NAME={name}):" after "ARRAY:". {name} can also be
827
 *                     a full qualified name.
828
 *                     A non-driver specific ARRAY option, "AUTOSCALE=YES" can
829
 * be used to ask (non indexing) variables of type Float32 or Float64 to be
830
 * scaled to UInt16 with scale and offset values being computed from the minimum
831
 * and maximum of the source array. The integer data type used can be set with
832
 *                     AUTOSCALE_DATA_TYPE=Byte/UInt16/Int16/UInt32/Int32.
833
 *
834
 * @return true in case of success (or partial success if bStrict == false).
835
 */
836
bool GDALGroup::CopyFrom(const std::shared_ptr<GDALGroup> &poDstRootGroup,
837
                         GDALDataset *poSrcDS,
838
                         const std::shared_ptr<GDALGroup> &poSrcGroup,
839
                         bool bStrict, GUInt64 &nCurCost,
840
                         const GUInt64 nTotalCost, GDALProgressFunc pfnProgress,
841
                         void *pProgressData, CSLConstList papszOptions)
842
0
{
843
0
    if (pfnProgress == nullptr)
844
0
        pfnProgress = GDALDummyProgress;
845
846
0
#define EXIT_OR_CONTINUE_IF_NULL(x)                                            \
847
0
    if (!(x))                                                                  \
848
0
    {                                                                          \
849
0
        if (bStrict)                                                           \
850
0
            return false;                                                      \
851
0
        continue;                                                              \
852
0
    }                                                                          \
853
0
    (void)0
854
855
0
    try
856
0
    {
857
0
        nCurCost += GDALGroup::COPY_COST;
858
859
0
        const auto srcDims = poSrcGroup->GetDimensions();
860
0
        std::map<std::string, std::shared_ptr<GDALDimension>>
861
0
            mapExistingDstDims;
862
0
        std::map<std::string, std::string> mapSrcVariableNameToIndexedDimName;
863
0
        for (const auto &dim : srcDims)
864
0
        {
865
0
            auto dstDim = CreateDimension(dim->GetName(), dim->GetType(),
866
0
                                          dim->GetDirection(), dim->GetSize());
867
0
            EXIT_OR_CONTINUE_IF_NULL(dstDim);
868
0
            mapExistingDstDims[dim->GetName()] = std::move(dstDim);
869
0
            auto poIndexingVarSrc(dim->GetIndexingVariable());
870
0
            if (poIndexingVarSrc)
871
0
            {
872
0
                mapSrcVariableNameToIndexedDimName[poIndexingVarSrc
873
0
                                                       ->GetName()] =
874
0
                    dim->GetName();
875
0
            }
876
0
        }
877
878
0
        auto attrs = poSrcGroup->GetAttributes();
879
0
        for (const auto &attr : attrs)
880
0
        {
881
0
            auto dstAttr =
882
0
                CreateAttribute(attr->GetName(), attr->GetDimensionsSize(),
883
0
                                attr->GetDataType());
884
0
            EXIT_OR_CONTINUE_IF_NULL(dstAttr);
885
0
            auto raw(attr->ReadAsRaw());
886
0
            if (!dstAttr->Write(raw.data(), raw.size()) && bStrict)
887
0
                return false;
888
0
        }
889
0
        if (!attrs.empty())
890
0
        {
891
0
            nCurCost += attrs.size() * GDALAttribute::COPY_COST;
892
0
            if (!pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData))
893
0
                return false;
894
0
        }
895
896
0
        const auto CopyArray =
897
0
            [this, &poSrcDS, &poDstRootGroup, &mapExistingDstDims,
898
0
             &mapSrcVariableNameToIndexedDimName, pfnProgress, pProgressData,
899
0
             papszOptions, bStrict, &nCurCost,
900
0
             nTotalCost](const std::shared_ptr<GDALMDArray> &srcArray)
901
0
        {
902
            // Map source dimensions to target dimensions
903
0
            std::vector<std::shared_ptr<GDALDimension>> dstArrayDims;
904
0
            const auto &srcArrayDims(srcArray->GetDimensions());
905
0
            for (const auto &dim : srcArrayDims)
906
0
            {
907
0
                auto dstDim = poDstRootGroup->OpenDimensionFromFullname(
908
0
                    dim->GetFullName());
909
0
                if (dstDim && dstDim->GetSize() == dim->GetSize())
910
0
                {
911
0
                    dstArrayDims.emplace_back(dstDim);
912
0
                }
913
0
                else
914
0
                {
915
0
                    auto oIter = mapExistingDstDims.find(dim->GetName());
916
0
                    if (oIter != mapExistingDstDims.end() &&
917
0
                        oIter->second->GetSize() == dim->GetSize())
918
0
                    {
919
0
                        dstArrayDims.emplace_back(oIter->second);
920
0
                    }
921
0
                    else
922
0
                    {
923
0
                        std::string newDimName;
924
0
                        if (oIter == mapExistingDstDims.end())
925
0
                        {
926
0
                            newDimName = dim->GetName();
927
0
                        }
928
0
                        else
929
0
                        {
930
0
                            std::string newDimNamePrefix(srcArray->GetName() +
931
0
                                                         '_' + dim->GetName());
932
0
                            newDimName = newDimNamePrefix;
933
0
                            int nIterCount = 2;
934
0
                            while (
935
0
                                cpl::contains(mapExistingDstDims, newDimName))
936
0
                            {
937
0
                                newDimName = newDimNamePrefix +
938
0
                                             CPLSPrintf("_%d", nIterCount);
939
0
                                nIterCount++;
940
0
                            }
941
0
                        }
942
0
                        dstDim = CreateDimension(newDimName, dim->GetType(),
943
0
                                                 dim->GetDirection(),
944
0
                                                 dim->GetSize());
945
0
                        if (!dstDim)
946
0
                            return false;
947
0
                        mapExistingDstDims[newDimName] = dstDim;
948
0
                        dstArrayDims.emplace_back(dstDim);
949
0
                    }
950
0
                }
951
0
            }
952
953
0
            CPLStringList aosArrayCO;
954
0
            bool bAutoScale = false;
955
0
            GDALDataType eAutoScaleType = GDT_UInt16;
956
0
            for (const char *pszItem : cpl::Iterate(papszOptions))
957
0
            {
958
0
                if (STARTS_WITH_CI(pszItem, "ARRAY:"))
959
0
                {
960
0
                    const char *pszOption = pszItem + strlen("ARRAY:");
961
0
                    if (STARTS_WITH_CI(pszOption, "IF(DIM="))
962
0
                    {
963
0
                        const char *pszNext = strchr(pszOption, ':');
964
0
                        if (pszNext != nullptr)
965
0
                        {
966
0
                            int nDim = atoi(pszOption + strlen("IF(DIM="));
967
0
                            if (static_cast<size_t>(nDim) ==
968
0
                                dstArrayDims.size())
969
0
                            {
970
0
                                pszOption = pszNext + 1;
971
0
                            }
972
0
                            else
973
0
                            {
974
0
                                pszOption = nullptr;
975
0
                            }
976
0
                        }
977
0
                    }
978
0
                    else if (STARTS_WITH_CI(pszOption, "IF(NAME="))
979
0
                    {
980
0
                        const char *pszName = pszOption + strlen("IF(NAME=");
981
0
                        const char *pszNext = strchr(pszName, ':');
982
0
                        if (pszNext != nullptr && pszNext > pszName &&
983
0
                            pszNext[-1] == ')')
984
0
                        {
985
0
                            CPLString osName;
986
0
                            osName.assign(pszName, pszNext - pszName - 1);
987
0
                            if (osName == srcArray->GetName() ||
988
0
                                osName == srcArray->GetFullName())
989
0
                            {
990
0
                                pszOption = pszNext + 1;
991
0
                            }
992
0
                            else
993
0
                            {
994
0
                                pszOption = nullptr;
995
0
                            }
996
0
                        }
997
0
                    }
998
0
                    if (pszOption)
999
0
                    {
1000
0
                        if (STARTS_WITH_CI(pszOption, "AUTOSCALE="))
1001
0
                        {
1002
0
                            bAutoScale =
1003
0
                                CPLTestBool(pszOption + strlen("AUTOSCALE="));
1004
0
                        }
1005
0
                        else if (STARTS_WITH_CI(pszOption,
1006
0
                                                "AUTOSCALE_DATA_TYPE="))
1007
0
                        {
1008
0
                            const char *pszDataType =
1009
0
                                pszOption + strlen("AUTOSCALE_DATA_TYPE=");
1010
0
                            eAutoScaleType = GDALGetDataTypeByName(pszDataType);
1011
0
                            if (GDALDataTypeIsComplex(eAutoScaleType) ||
1012
0
                                GDALDataTypeIsFloating(eAutoScaleType))
1013
0
                            {
1014
0
                                CPLError(CE_Failure, CPLE_NotSupported,
1015
0
                                         "Unsupported value for "
1016
0
                                         "AUTOSCALE_DATA_TYPE");
1017
0
                                return false;
1018
0
                            }
1019
0
                        }
1020
0
                        else
1021
0
                        {
1022
0
                            aosArrayCO.AddString(pszOption);
1023
0
                        }
1024
0
                    }
1025
0
                }
1026
0
            }
1027
1028
0
            auto oIterDimName =
1029
0
                mapSrcVariableNameToIndexedDimName.find(srcArray->GetName());
1030
0
            const auto &srcArrayType = srcArray->GetDataType();
1031
1032
0
            std::shared_ptr<GDALMDArray> dstArray;
1033
1034
            // Only autoscale non-indexing variables
1035
0
            bool bHasOffset = false;
1036
0
            bool bHasScale = false;
1037
0
            if (bAutoScale && srcArrayType.GetClass() == GEDTC_NUMERIC &&
1038
0
                (srcArrayType.GetNumericDataType() == GDT_Float16 ||
1039
0
                 srcArrayType.GetNumericDataType() == GDT_Float32 ||
1040
0
                 srcArrayType.GetNumericDataType() == GDT_Float64) &&
1041
0
                srcArray->GetOffset(&bHasOffset) == 0.0 && !bHasOffset &&
1042
0
                srcArray->GetScale(&bHasScale) == 1.0 && !bHasScale &&
1043
0
                oIterDimName == mapSrcVariableNameToIndexedDimName.end())
1044
0
            {
1045
0
                constexpr bool bApproxOK = false;
1046
0
                constexpr bool bForce = true;
1047
0
                double dfMin = 0.0;
1048
0
                double dfMax = 0.0;
1049
0
                if (srcArray->GetStatistics(bApproxOK, bForce, &dfMin, &dfMax,
1050
0
                                            nullptr, nullptr, nullptr, nullptr,
1051
0
                                            nullptr) != CE_None)
1052
0
                {
1053
0
                    CPLError(CE_Failure, CPLE_AppDefined,
1054
0
                             "Could not retrieve statistics for array %s",
1055
0
                             srcArray->GetName().c_str());
1056
0
                    return false;
1057
0
                }
1058
0
                double dfDTMin = 0;
1059
0
                double dfDTMax = 0;
1060
0
#define setDTMinMax(ctype)                                                     \
1061
0
    do                                                                         \
1062
0
    {                                                                          \
1063
0
        dfDTMin = static_cast<double>(cpl::NumericLimits<ctype>::lowest());    \
1064
0
        dfDTMax = static_cast<double>(cpl::NumericLimits<ctype>::max());       \
1065
0
    } while (0)
1066
1067
0
                switch (eAutoScaleType)
1068
0
                {
1069
0
                    case GDT_Byte:
1070
0
                        setDTMinMax(GByte);
1071
0
                        break;
1072
0
                    case GDT_Int8:
1073
0
                        setDTMinMax(GInt8);
1074
0
                        break;
1075
0
                    case GDT_UInt16:
1076
0
                        setDTMinMax(GUInt16);
1077
0
                        break;
1078
0
                    case GDT_Int16:
1079
0
                        setDTMinMax(GInt16);
1080
0
                        break;
1081
0
                    case GDT_UInt32:
1082
0
                        setDTMinMax(GUInt32);
1083
0
                        break;
1084
0
                    case GDT_Int32:
1085
0
                        setDTMinMax(GInt32);
1086
0
                        break;
1087
0
                    case GDT_UInt64:
1088
0
                        setDTMinMax(std::uint64_t);
1089
0
                        break;
1090
0
                    case GDT_Int64:
1091
0
                        setDTMinMax(std::int64_t);
1092
0
                        break;
1093
0
                    case GDT_Float16:
1094
0
                    case GDT_Float32:
1095
0
                    case GDT_Float64:
1096
0
                    case GDT_Unknown:
1097
0
                    case GDT_CInt16:
1098
0
                    case GDT_CInt32:
1099
0
                    case GDT_CFloat16:
1100
0
                    case GDT_CFloat32:
1101
0
                    case GDT_CFloat64:
1102
0
                    case GDT_TypeCount:
1103
0
                        CPLAssert(false);
1104
0
                }
1105
1106
0
                dstArray =
1107
0
                    CreateMDArray(srcArray->GetName(), dstArrayDims,
1108
0
                                  GDALExtendedDataType::Create(eAutoScaleType),
1109
0
                                  aosArrayCO.List());
1110
0
                if (!dstArray)
1111
0
                    return !bStrict;
1112
1113
0
                if (srcArray->GetRawNoDataValue() != nullptr)
1114
0
                {
1115
                    // If there's a nodata value in the source array, reserve
1116
                    // DTMax for that purpose in the target scaled array
1117
0
                    if (!dstArray->SetNoDataValue(dfDTMax))
1118
0
                    {
1119
0
                        CPLError(CE_Failure, CPLE_AppDefined,
1120
0
                                 "Cannot set nodata value");
1121
0
                        return false;
1122
0
                    }
1123
0
                    dfDTMax--;
1124
0
                }
1125
0
                const double dfScale =
1126
0
                    dfMax > dfMin ? (dfMax - dfMin) / (dfDTMax - dfDTMin) : 1.0;
1127
0
                const double dfOffset = dfMin - dfDTMin * dfScale;
1128
1129
0
                if (!dstArray->SetOffset(dfOffset) ||
1130
0
                    !dstArray->SetScale(dfScale))
1131
0
                {
1132
0
                    CPLError(CE_Failure, CPLE_AppDefined,
1133
0
                             "Cannot set scale/offset");
1134
0
                    return false;
1135
0
                }
1136
1137
0
                auto poUnscaled = dstArray->GetUnscaled();
1138
0
                if (srcArray->GetRawNoDataValue() != nullptr)
1139
0
                {
1140
0
                    poUnscaled->SetNoDataValue(
1141
0
                        srcArray->GetNoDataValueAsDouble());
1142
0
                }
1143
1144
                // Copy source array into unscaled array
1145
0
                if (!poUnscaled->CopyFrom(poSrcDS, srcArray.get(), bStrict,
1146
0
                                          nCurCost, nTotalCost, pfnProgress,
1147
0
                                          pProgressData))
1148
0
                {
1149
0
                    return false;
1150
0
                }
1151
0
            }
1152
0
            else
1153
0
            {
1154
0
                dstArray = CreateMDArray(srcArray->GetName(), dstArrayDims,
1155
0
                                         srcArrayType, aosArrayCO.List());
1156
0
                if (!dstArray)
1157
0
                    return !bStrict;
1158
1159
0
                if (!dstArray->CopyFrom(poSrcDS, srcArray.get(), bStrict,
1160
0
                                        nCurCost, nTotalCost, pfnProgress,
1161
0
                                        pProgressData))
1162
0
                {
1163
0
                    return false;
1164
0
                }
1165
0
            }
1166
1167
            // If this array is the indexing variable of a dimension, link them
1168
            // together.
1169
0
            if (oIterDimName != mapSrcVariableNameToIndexedDimName.end())
1170
0
            {
1171
0
                auto oCorrespondingDimIter =
1172
0
                    mapExistingDstDims.find(oIterDimName->second);
1173
0
                if (oCorrespondingDimIter != mapExistingDstDims.end())
1174
0
                {
1175
0
                    CPLErrorStateBackuper oErrorStateBackuper(
1176
0
                        CPLQuietErrorHandler);
1177
0
                    oCorrespondingDimIter->second->SetIndexingVariable(
1178
0
                        std::move(dstArray));
1179
0
                }
1180
0
            }
1181
1182
0
            return true;
1183
0
        };
1184
1185
0
        const auto arrayNames = poSrcGroup->GetMDArrayNames();
1186
1187
        // Start by copying arrays that are indexing variables of dimensions
1188
0
        for (const auto &name : arrayNames)
1189
0
        {
1190
0
            auto srcArray = poSrcGroup->OpenMDArray(name);
1191
0
            EXIT_OR_CONTINUE_IF_NULL(srcArray);
1192
1193
0
            if (cpl::contains(mapSrcVariableNameToIndexedDimName,
1194
0
                              srcArray->GetName()))
1195
0
            {
1196
0
                if (!CopyArray(srcArray))
1197
0
                    return false;
1198
0
            }
1199
0
        }
1200
1201
        // Then copy regular arrays
1202
0
        for (const auto &name : arrayNames)
1203
0
        {
1204
0
            auto srcArray = poSrcGroup->OpenMDArray(name);
1205
0
            EXIT_OR_CONTINUE_IF_NULL(srcArray);
1206
1207
0
            if (!cpl::contains(mapSrcVariableNameToIndexedDimName,
1208
0
                               srcArray->GetName()))
1209
0
            {
1210
0
                if (!CopyArray(srcArray))
1211
0
                    return false;
1212
0
            }
1213
0
        }
1214
1215
0
        const auto groupNames = poSrcGroup->GetGroupNames();
1216
0
        for (const auto &name : groupNames)
1217
0
        {
1218
0
            auto srcSubGroup = poSrcGroup->OpenGroup(name);
1219
0
            EXIT_OR_CONTINUE_IF_NULL(srcSubGroup);
1220
0
            auto dstSubGroup = CreateGroup(name);
1221
0
            EXIT_OR_CONTINUE_IF_NULL(dstSubGroup);
1222
0
            if (!dstSubGroup->CopyFrom(
1223
0
                    poDstRootGroup, poSrcDS, srcSubGroup, bStrict, nCurCost,
1224
0
                    nTotalCost, pfnProgress, pProgressData, papszOptions))
1225
0
                return false;
1226
0
        }
1227
1228
0
        if (!pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData))
1229
0
            return false;
1230
1231
0
        return true;
1232
0
    }
1233
0
    catch (const std::exception &e)
1234
0
    {
1235
0
        CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
1236
0
        return false;
1237
0
    }
1238
0
}
1239
1240
/************************************************************************/
1241
/*                         GetInnerMostGroup()                          */
1242
/************************************************************************/
1243
1244
//! @cond Doxygen_Suppress
1245
const GDALGroup *
1246
GDALGroup::GetInnerMostGroup(const std::string &osPathOrArrayOrDim,
1247
                             std::shared_ptr<GDALGroup> &curGroupHolder,
1248
                             std::string &osLastPart) const
1249
0
{
1250
0
    if (osPathOrArrayOrDim.empty() || osPathOrArrayOrDim[0] != '/')
1251
0
        return nullptr;
1252
0
    const GDALGroup *poCurGroup = this;
1253
0
    CPLStringList aosTokens(
1254
0
        CSLTokenizeString2(osPathOrArrayOrDim.c_str(), "/", 0));
1255
0
    if (aosTokens.size() == 0)
1256
0
    {
1257
0
        return nullptr;
1258
0
    }
1259
1260
0
    for (int i = 0; i < aosTokens.size() - 1; i++)
1261
0
    {
1262
0
        curGroupHolder = poCurGroup->OpenGroup(aosTokens[i], nullptr);
1263
0
        if (!curGroupHolder)
1264
0
        {
1265
0
            CPLError(CE_Failure, CPLE_AppDefined, "Cannot find group %s",
1266
0
                     aosTokens[i]);
1267
0
            return nullptr;
1268
0
        }
1269
0
        poCurGroup = curGroupHolder.get();
1270
0
    }
1271
0
    osLastPart = aosTokens[aosTokens.size() - 1];
1272
0
    return poCurGroup;
1273
0
}
1274
1275
//! @endcond
1276
1277
/************************************************************************/
1278
/*                      OpenMDArrayFromFullname()                       */
1279
/************************************************************************/
1280
1281
/** Get an array from its fully qualified name */
1282
std::shared_ptr<GDALMDArray>
1283
GDALGroup::OpenMDArrayFromFullname(const std::string &osFullName,
1284
                                   CSLConstList papszOptions) const
1285
0
{
1286
0
    std::string osName;
1287
0
    std::shared_ptr<GDALGroup> curGroupHolder;
1288
0
    auto poGroup(GetInnerMostGroup(osFullName, curGroupHolder, osName));
1289
0
    if (poGroup == nullptr)
1290
0
        return nullptr;
1291
0
    return poGroup->OpenMDArray(osName, papszOptions);
1292
0
}
1293
1294
/************************************************************************/
1295
/*                      OpenAttributeFromFullname()                     */
1296
/************************************************************************/
1297
1298
/** Get an attribute from its fully qualified name */
1299
std::shared_ptr<GDALAttribute>
1300
GDALGroup::OpenAttributeFromFullname(const std::string &osFullName,
1301
                                     CSLConstList papszOptions) const
1302
0
{
1303
0
    const auto pos = osFullName.rfind('/');
1304
0
    if (pos == std::string::npos)
1305
0
        return nullptr;
1306
0
    const std::string attrName = osFullName.substr(pos + 1);
1307
0
    if (pos == 0)
1308
0
        return GetAttribute(attrName);
1309
0
    const std::string container = osFullName.substr(0, pos);
1310
0
    auto poArray = OpenMDArrayFromFullname(container, papszOptions);
1311
0
    if (poArray)
1312
0
        return poArray->GetAttribute(attrName);
1313
0
    auto poGroup = OpenGroupFromFullname(container, papszOptions);
1314
0
    if (poGroup)
1315
0
        return poGroup->GetAttribute(attrName);
1316
0
    return nullptr;
1317
0
}
1318
1319
/************************************************************************/
1320
/*                          ResolveMDArray()                            */
1321
/************************************************************************/
1322
1323
/** Locate an array in a group and its subgroups by name.
1324
 *
1325
 * If osName is a fully qualified name, then OpenMDArrayFromFullname() is first
1326
 * used
1327
 * Otherwise the search will start from the group identified by osStartingPath,
1328
 * and an array whose name is osName will be looked for in this group (if
1329
 * osStartingPath is empty or "/", then the current group is used). If there
1330
 * is no match, then a recursive descendent search will be made in its
1331
 * subgroups. If there is no match in the subgroups, then the parent (if
1332
 * existing) of the group pointed by osStartingPath will be used as the new
1333
 * starting point for the search.
1334
 *
1335
 * @param osName name, qualified or not
1336
 * @param osStartingPath fully qualified name of the (sub-)group from which
1337
 *                       the search should be started. If this is a non-empty
1338
 *                       string, the group on which this method is called should
1339
 *                       nominally be the root group (otherwise the path will
1340
 *                       be interpreted as from the current group)
1341
 * @param papszOptions options to pass to OpenMDArray()
1342
 * @since GDAL 3.2
1343
 */
1344
std::shared_ptr<GDALMDArray>
1345
GDALGroup::ResolveMDArray(const std::string &osName,
1346
                          const std::string &osStartingPath,
1347
                          CSLConstList papszOptions) const
1348
0
{
1349
0
    if (!osName.empty() && osName[0] == '/')
1350
0
    {
1351
0
        auto poArray = OpenMDArrayFromFullname(osName, papszOptions);
1352
0
        if (poArray)
1353
0
            return poArray;
1354
0
    }
1355
0
    std::string osPath(osStartingPath);
1356
0
    std::set<std::string> oSetAlreadyVisited;
1357
1358
0
    while (true)
1359
0
    {
1360
0
        std::shared_ptr<GDALGroup> curGroupHolder;
1361
0
        std::shared_ptr<GDALGroup> poGroup;
1362
1363
0
        std::queue<std::shared_ptr<GDALGroup>> oQueue;
1364
0
        bool goOn = false;
1365
0
        if (osPath.empty() || osPath == "/")
1366
0
        {
1367
0
            goOn = true;
1368
0
        }
1369
0
        else
1370
0
        {
1371
0
            std::string osLastPart;
1372
0
            const GDALGroup *poGroupPtr =
1373
0
                GetInnerMostGroup(osPath, curGroupHolder, osLastPart);
1374
0
            if (poGroupPtr)
1375
0
                poGroup = poGroupPtr->OpenGroup(osLastPart);
1376
0
            if (poGroup &&
1377
0
                !cpl::contains(oSetAlreadyVisited, poGroup->GetFullName()))
1378
0
            {
1379
0
                oQueue.push(poGroup);
1380
0
                goOn = true;
1381
0
            }
1382
0
        }
1383
1384
0
        if (goOn)
1385
0
        {
1386
0
            do
1387
0
            {
1388
0
                const GDALGroup *groupPtr;
1389
0
                if (!oQueue.empty())
1390
0
                {
1391
0
                    poGroup = oQueue.front();
1392
0
                    oQueue.pop();
1393
0
                    groupPtr = poGroup.get();
1394
0
                }
1395
0
                else
1396
0
                {
1397
0
                    groupPtr = this;
1398
0
                }
1399
1400
0
                auto poArray = groupPtr->OpenMDArray(osName, papszOptions);
1401
0
                if (poArray)
1402
0
                    return poArray;
1403
1404
0
                const auto aosGroupNames = groupPtr->GetGroupNames();
1405
0
                for (const auto &osGroupName : aosGroupNames)
1406
0
                {
1407
0
                    auto poSubGroup = groupPtr->OpenGroup(osGroupName);
1408
0
                    if (poSubGroup && !cpl::contains(oSetAlreadyVisited,
1409
0
                                                     poSubGroup->GetFullName()))
1410
0
                    {
1411
0
                        oQueue.push(poSubGroup);
1412
0
                        oSetAlreadyVisited.insert(poSubGroup->GetFullName());
1413
0
                    }
1414
0
                }
1415
0
            } while (!oQueue.empty());
1416
0
        }
1417
1418
0
        if (osPath.empty() || osPath == "/")
1419
0
            break;
1420
1421
0
        const auto nPos = osPath.rfind('/');
1422
0
        if (nPos == 0)
1423
0
            osPath = "/";
1424
0
        else
1425
0
        {
1426
0
            if (nPos == std::string::npos)
1427
0
                break;
1428
0
            osPath.resize(nPos);
1429
0
        }
1430
0
    }
1431
0
    return nullptr;
1432
0
}
1433
1434
/************************************************************************/
1435
/*                       OpenGroupFromFullname()                        */
1436
/************************************************************************/
1437
1438
/** Get a group from its fully qualified name.
1439
 * @since GDAL 3.2
1440
 */
1441
std::shared_ptr<GDALGroup>
1442
GDALGroup::OpenGroupFromFullname(const std::string &osFullName,
1443
                                 CSLConstList papszOptions) const
1444
0
{
1445
0
    std::string osName;
1446
0
    std::shared_ptr<GDALGroup> curGroupHolder;
1447
0
    auto poGroup(GetInnerMostGroup(osFullName, curGroupHolder, osName));
1448
0
    if (poGroup == nullptr)
1449
0
        return nullptr;
1450
0
    return poGroup->OpenGroup(osName, papszOptions);
1451
0
}
1452
1453
/************************************************************************/
1454
/*                      OpenDimensionFromFullname()                     */
1455
/************************************************************************/
1456
1457
/** Get a dimension from its fully qualified name */
1458
std::shared_ptr<GDALDimension>
1459
GDALGroup::OpenDimensionFromFullname(const std::string &osFullName) const
1460
0
{
1461
0
    std::string osName;
1462
0
    std::shared_ptr<GDALGroup> curGroupHolder;
1463
0
    auto poGroup(GetInnerMostGroup(osFullName, curGroupHolder, osName));
1464
0
    if (poGroup == nullptr)
1465
0
        return nullptr;
1466
0
    auto dims(poGroup->GetDimensions());
1467
0
    for (auto &dim : dims)
1468
0
    {
1469
0
        if (dim->GetName() == osName)
1470
0
            return dim;
1471
0
    }
1472
0
    return nullptr;
1473
0
}
1474
1475
/************************************************************************/
1476
/*                           ClearStatistics()                          */
1477
/************************************************************************/
1478
1479
/**
1480
 * \brief Clear statistics.
1481
 *
1482
 * @since GDAL 3.4
1483
 */
1484
void GDALGroup::ClearStatistics()
1485
0
{
1486
0
    auto groupNames = GetGroupNames();
1487
0
    for (const auto &name : groupNames)
1488
0
    {
1489
0
        auto subGroup = OpenGroup(name);
1490
0
        if (subGroup)
1491
0
        {
1492
0
            subGroup->ClearStatistics();
1493
0
        }
1494
0
    }
1495
1496
0
    auto arrayNames = GetMDArrayNames();
1497
0
    for (const auto &name : arrayNames)
1498
0
    {
1499
0
        auto array = OpenMDArray(name);
1500
0
        if (array)
1501
0
        {
1502
0
            array->ClearStatistics();
1503
0
        }
1504
0
    }
1505
0
}
1506
1507
/************************************************************************/
1508
/*                            Rename()                                  */
1509
/************************************************************************/
1510
1511
/** Rename the group.
1512
 *
1513
 * This is not implemented by all drivers.
1514
 *
1515
 * Drivers known to implement it: MEM, netCDF, ZARR.
1516
 *
1517
 * This is the same as the C function GDALGroupRename().
1518
 *
1519
 * @param osNewName New name.
1520
 *
1521
 * @return true in case of success
1522
 * @since GDAL 3.8
1523
 */
1524
bool GDALGroup::Rename(CPL_UNUSED const std::string &osNewName)
1525
0
{
1526
0
    CPLError(CE_Failure, CPLE_NotSupported, "Rename() not implemented");
1527
0
    return false;
1528
0
}
1529
1530
/************************************************************************/
1531
/*                         BaseRename()                                 */
1532
/************************************************************************/
1533
1534
//! @cond Doxygen_Suppress
1535
void GDALGroup::BaseRename(const std::string &osNewName)
1536
0
{
1537
0
    m_osFullName.resize(m_osFullName.size() - m_osName.size());
1538
0
    m_osFullName += osNewName;
1539
0
    m_osName = osNewName;
1540
1541
0
    NotifyChildrenOfRenaming();
1542
0
}
1543
1544
//! @endcond
1545
1546
/************************************************************************/
1547
/*                        ParentRenamed()                               */
1548
/************************************************************************/
1549
1550
//! @cond Doxygen_Suppress
1551
void GDALGroup::ParentRenamed(const std::string &osNewParentFullName)
1552
0
{
1553
0
    m_osFullName = osNewParentFullName;
1554
0
    m_osFullName += "/";
1555
0
    m_osFullName += m_osName;
1556
1557
0
    NotifyChildrenOfRenaming();
1558
0
}
1559
1560
//! @endcond
1561
1562
/************************************************************************/
1563
/*                             Deleted()                                */
1564
/************************************************************************/
1565
1566
//! @cond Doxygen_Suppress
1567
void GDALGroup::Deleted()
1568
0
{
1569
0
    m_bValid = false;
1570
1571
0
    NotifyChildrenOfDeletion();
1572
0
}
1573
1574
//! @endcond
1575
1576
/************************************************************************/
1577
/*                        ParentDeleted()                               */
1578
/************************************************************************/
1579
1580
//! @cond Doxygen_Suppress
1581
void GDALGroup::ParentDeleted()
1582
0
{
1583
0
    Deleted();
1584
0
}
1585
1586
//! @endcond
1587
1588
/************************************************************************/
1589
/*                     CheckValidAndErrorOutIfNot()                     */
1590
/************************************************************************/
1591
1592
//! @cond Doxygen_Suppress
1593
bool GDALGroup::CheckValidAndErrorOutIfNot() const
1594
0
{
1595
0
    if (!m_bValid)
1596
0
    {
1597
0
        CPLError(CE_Failure, CPLE_AppDefined,
1598
0
                 "This object has been deleted. No action on it is possible");
1599
0
    }
1600
0
    return m_bValid;
1601
0
}
1602
1603
//! @endcond
1604
1605
/************************************************************************/
1606
/*                       ~GDALAbstractMDArray()                         */
1607
/************************************************************************/
1608
1609
0
GDALAbstractMDArray::~GDALAbstractMDArray() = default;
1610
1611
/************************************************************************/
1612
/*                        GDALAbstractMDArray()                         */
1613
/************************************************************************/
1614
1615
//! @cond Doxygen_Suppress
1616
GDALAbstractMDArray::GDALAbstractMDArray(const std::string &osParentName,
1617
                                         const std::string &osName)
1618
0
    : m_osName(osName),
1619
      m_osFullName(
1620
0
          !osParentName.empty()
1621
0
              ? ((osParentName == "/" ? "/" : osParentName + "/") + osName)
1622
0
              : osName)
1623
0
{
1624
0
}
1625
1626
//! @endcond
1627
1628
/************************************************************************/
1629
/*                           GetDimensions()                            */
1630
/************************************************************************/
1631
1632
/** \fn GDALAbstractMDArray::GetDimensions() const
1633
 * \brief Return the dimensions of an attribute/array.
1634
 *
1635
 * This is the same as the C functions GDALMDArrayGetDimensions() and
1636
 * similar to GDALAttributeGetDimensionsSize().
1637
 */
1638
1639
/************************************************************************/
1640
/*                           GetDataType()                              */
1641
/************************************************************************/
1642
1643
/** \fn GDALAbstractMDArray::GetDataType() const
1644
 * \brief Return the data type of an attribute/array.
1645
 *
1646
 * This is the same as the C functions GDALMDArrayGetDataType() and
1647
 * GDALAttributeGetDataType()
1648
 */
1649
1650
/************************************************************************/
1651
/*                        GetDimensionCount()                           */
1652
/************************************************************************/
1653
1654
/** Return the number of dimensions.
1655
 *
1656
 * Default implementation is GetDimensions().size(), and may be overridden by
1657
 * drivers if they have a faster / less expensive implementations.
1658
 *
1659
 * This is the same as the C function GDALMDArrayGetDimensionCount() or
1660
 * GDALAttributeGetDimensionCount().
1661
 *
1662
 */
1663
size_t GDALAbstractMDArray::GetDimensionCount() const
1664
0
{
1665
0
    return GetDimensions().size();
1666
0
}
1667
1668
/************************************************************************/
1669
/*                            Rename()                                  */
1670
/************************************************************************/
1671
1672
/** Rename the attribute/array.
1673
 *
1674
 * This is not implemented by all drivers.
1675
 *
1676
 * Drivers known to implement it: MEM, netCDF, Zarr.
1677
 *
1678
 * This is the same as the C functions GDALMDArrayRename() or
1679
 * GDALAttributeRename().
1680
 *
1681
 * @param osNewName New name.
1682
 *
1683
 * @return true in case of success
1684
 * @since GDAL 3.8
1685
 */
1686
bool GDALAbstractMDArray::Rename(CPL_UNUSED const std::string &osNewName)
1687
0
{
1688
0
    CPLError(CE_Failure, CPLE_NotSupported, "Rename() not implemented");
1689
0
    return false;
1690
0
}
1691
1692
/************************************************************************/
1693
/*                             CopyValue()                              */
1694
/************************************************************************/
1695
1696
/** Convert a value from a source type to a destination type.
1697
 *
1698
 * If dstType is GEDTC_STRING, the written value will be a pointer to a char*,
1699
 * that must be freed with CPLFree().
1700
 */
1701
bool GDALExtendedDataType::CopyValue(const void *pSrc,
1702
                                     const GDALExtendedDataType &srcType,
1703
                                     void *pDst,
1704
                                     const GDALExtendedDataType &dstType)
1705
0
{
1706
0
    if (srcType.GetClass() == GEDTC_NUMERIC &&
1707
0
        dstType.GetClass() == GEDTC_NUMERIC)
1708
0
    {
1709
0
        GDALCopyWords64(pSrc, srcType.GetNumericDataType(), 0, pDst,
1710
0
                        dstType.GetNumericDataType(), 0, 1);
1711
0
        return true;
1712
0
    }
1713
0
    if (srcType.GetClass() == GEDTC_STRING &&
1714
0
        dstType.GetClass() == GEDTC_STRING)
1715
0
    {
1716
0
        const char *srcStrPtr;
1717
0
        memcpy(&srcStrPtr, pSrc, sizeof(const char *));
1718
0
        char *pszDup = srcStrPtr ? CPLStrdup(srcStrPtr) : nullptr;
1719
0
        *reinterpret_cast<void **>(pDst) = pszDup;
1720
0
        return true;
1721
0
    }
1722
0
    if (srcType.GetClass() == GEDTC_NUMERIC &&
1723
0
        dstType.GetClass() == GEDTC_STRING)
1724
0
    {
1725
0
        const char *str = nullptr;
1726
0
        switch (srcType.GetNumericDataType())
1727
0
        {
1728
0
            case GDT_Unknown:
1729
0
                break;
1730
0
            case GDT_Byte:
1731
0
                str = CPLSPrintf("%d", *static_cast<const GByte *>(pSrc));
1732
0
                break;
1733
0
            case GDT_Int8:
1734
0
                str = CPLSPrintf("%d", *static_cast<const GInt8 *>(pSrc));
1735
0
                break;
1736
0
            case GDT_UInt16:
1737
0
                str = CPLSPrintf("%d", *static_cast<const GUInt16 *>(pSrc));
1738
0
                break;
1739
0
            case GDT_Int16:
1740
0
                str = CPLSPrintf("%d", *static_cast<const GInt16 *>(pSrc));
1741
0
                break;
1742
0
            case GDT_UInt32:
1743
0
                str = CPLSPrintf("%u", *static_cast<const GUInt32 *>(pSrc));
1744
0
                break;
1745
0
            case GDT_Int32:
1746
0
                str = CPLSPrintf("%d", *static_cast<const GInt32 *>(pSrc));
1747
0
                break;
1748
0
            case GDT_UInt64:
1749
0
                str =
1750
0
                    CPLSPrintf(CPL_FRMT_GUIB,
1751
0
                               static_cast<GUIntBig>(
1752
0
                                   *static_cast<const std::uint64_t *>(pSrc)));
1753
0
                break;
1754
0
            case GDT_Int64:
1755
0
                str = CPLSPrintf(CPL_FRMT_GIB,
1756
0
                                 static_cast<GIntBig>(
1757
0
                                     *static_cast<const std::int64_t *>(pSrc)));
1758
0
                break;
1759
0
            case GDT_Float16:
1760
0
                str = CPLSPrintf("%.5g",
1761
0
                                 double(*static_cast<const GFloat16 *>(pSrc)));
1762
0
                break;
1763
0
            case GDT_Float32:
1764
0
                str = CPLSPrintf("%.9g", *static_cast<const float *>(pSrc));
1765
0
                break;
1766
0
            case GDT_Float64:
1767
0
                str = CPLSPrintf("%.17g", *static_cast<const double *>(pSrc));
1768
0
                break;
1769
0
            case GDT_CInt16:
1770
0
            {
1771
0
                const GInt16 *src = static_cast<const GInt16 *>(pSrc);
1772
0
                str = CPLSPrintf("%d+%dj", src[0], src[1]);
1773
0
                break;
1774
0
            }
1775
0
            case GDT_CInt32:
1776
0
            {
1777
0
                const GInt32 *src = static_cast<const GInt32 *>(pSrc);
1778
0
                str = CPLSPrintf("%d+%dj", src[0], src[1]);
1779
0
                break;
1780
0
            }
1781
0
            case GDT_CFloat16:
1782
0
            {
1783
0
                const GFloat16 *src = static_cast<const GFloat16 *>(pSrc);
1784
0
                str = CPLSPrintf("%.5g+%.5gj", double(src[0]), double(src[1]));
1785
0
                break;
1786
0
            }
1787
0
            case GDT_CFloat32:
1788
0
            {
1789
0
                const float *src = static_cast<const float *>(pSrc);
1790
0
                str = CPLSPrintf("%.9g+%.9gj", src[0], src[1]);
1791
0
                break;
1792
0
            }
1793
0
            case GDT_CFloat64:
1794
0
            {
1795
0
                const double *src = static_cast<const double *>(pSrc);
1796
0
                str = CPLSPrintf("%.17g+%.17gj", src[0], src[1]);
1797
0
                break;
1798
0
            }
1799
0
            case GDT_TypeCount:
1800
0
                CPLAssert(false);
1801
0
                break;
1802
0
        }
1803
0
        char *pszDup = str ? CPLStrdup(str) : nullptr;
1804
0
        *reinterpret_cast<void **>(pDst) = pszDup;
1805
0
        return true;
1806
0
    }
1807
0
    if (srcType.GetClass() == GEDTC_STRING &&
1808
0
        dstType.GetClass() == GEDTC_NUMERIC)
1809
0
    {
1810
0
        const char *srcStrPtr;
1811
0
        memcpy(&srcStrPtr, pSrc, sizeof(const char *));
1812
0
        if (dstType.GetNumericDataType() == GDT_Int64)
1813
0
        {
1814
0
            *(static_cast<int64_t *>(pDst)) =
1815
0
                srcStrPtr == nullptr ? 0
1816
0
                                     : static_cast<int64_t>(atoll(srcStrPtr));
1817
0
        }
1818
0
        else if (dstType.GetNumericDataType() == GDT_UInt64)
1819
0
        {
1820
0
            *(static_cast<uint64_t *>(pDst)) =
1821
0
                srcStrPtr == nullptr
1822
0
                    ? 0
1823
0
                    : static_cast<uint64_t>(strtoull(srcStrPtr, nullptr, 10));
1824
0
        }
1825
0
        else
1826
0
        {
1827
            // FIXME GDT_UInt64
1828
0
            const double dfVal = srcStrPtr == nullptr ? 0 : CPLAtof(srcStrPtr);
1829
0
            GDALCopyWords64(&dfVal, GDT_Float64, 0, pDst,
1830
0
                            dstType.GetNumericDataType(), 0, 1);
1831
0
        }
1832
0
        return true;
1833
0
    }
1834
0
    if (srcType.GetClass() == GEDTC_COMPOUND &&
1835
0
        dstType.GetClass() == GEDTC_COMPOUND)
1836
0
    {
1837
0
        const auto &srcComponents = srcType.GetComponents();
1838
0
        const auto &dstComponents = dstType.GetComponents();
1839
0
        const GByte *pabySrc = static_cast<const GByte *>(pSrc);
1840
0
        GByte *pabyDst = static_cast<GByte *>(pDst);
1841
1842
0
        std::map<std::string, const std::unique_ptr<GDALEDTComponent> *>
1843
0
            srcComponentMap;
1844
0
        for (const auto &srcComp : srcComponents)
1845
0
        {
1846
0
            srcComponentMap[srcComp->GetName()] = &srcComp;
1847
0
        }
1848
0
        for (const auto &dstComp : dstComponents)
1849
0
        {
1850
0
            auto oIter = srcComponentMap.find(dstComp->GetName());
1851
0
            if (oIter == srcComponentMap.end())
1852
0
                return false;
1853
0
            const auto &srcComp = *(oIter->second);
1854
0
            if (!GDALExtendedDataType::CopyValue(
1855
0
                    pabySrc + srcComp->GetOffset(), srcComp->GetType(),
1856
0
                    pabyDst + dstComp->GetOffset(), dstComp->GetType()))
1857
0
            {
1858
0
                return false;
1859
0
            }
1860
0
        }
1861
0
        return true;
1862
0
    }
1863
1864
0
    return false;
1865
0
}
1866
1867
/************************************************************************/
1868
/*                             CopyValues()                             */
1869
/************************************************************************/
1870
1871
/** Convert severals value from a source type to a destination type.
1872
 *
1873
 * If dstType is GEDTC_STRING, the written value will be a pointer to a char*,
1874
 * that must be freed with CPLFree().
1875
 */
1876
bool GDALExtendedDataType::CopyValues(const void *pSrc,
1877
                                      const GDALExtendedDataType &srcType,
1878
                                      GPtrDiff_t nSrcStrideInElts, void *pDst,
1879
                                      const GDALExtendedDataType &dstType,
1880
                                      GPtrDiff_t nDstStrideInElts,
1881
                                      size_t nValues)
1882
0
{
1883
0
    const auto nSrcStrideInBytes =
1884
0
        nSrcStrideInElts * static_cast<GPtrDiff_t>(srcType.GetSize());
1885
0
    const auto nDstStrideInBytes =
1886
0
        nDstStrideInElts * static_cast<GPtrDiff_t>(dstType.GetSize());
1887
0
    if (srcType.GetClass() == GEDTC_NUMERIC &&
1888
0
        dstType.GetClass() == GEDTC_NUMERIC &&
1889
0
        nSrcStrideInBytes >= std::numeric_limits<int>::min() &&
1890
0
        nSrcStrideInBytes <= std::numeric_limits<int>::max() &&
1891
0
        nDstStrideInBytes >= std::numeric_limits<int>::min() &&
1892
0
        nDstStrideInBytes <= std::numeric_limits<int>::max())
1893
0
    {
1894
0
        GDALCopyWords64(pSrc, srcType.GetNumericDataType(),
1895
0
                        static_cast<int>(nSrcStrideInBytes), pDst,
1896
0
                        dstType.GetNumericDataType(),
1897
0
                        static_cast<int>(nDstStrideInBytes), nValues);
1898
0
    }
1899
0
    else
1900
0
    {
1901
0
        const GByte *pabySrc = static_cast<const GByte *>(pSrc);
1902
0
        GByte *pabyDst = static_cast<GByte *>(pDst);
1903
0
        for (size_t i = 0; i < nValues; ++i)
1904
0
        {
1905
0
            if (!CopyValue(pabySrc, srcType, pabyDst, dstType))
1906
0
                return false;
1907
0
            pabySrc += nSrcStrideInBytes;
1908
0
            pabyDst += nDstStrideInBytes;
1909
0
        }
1910
0
    }
1911
0
    return true;
1912
0
}
1913
1914
/************************************************************************/
1915
/*                       CheckReadWriteParams()                         */
1916
/************************************************************************/
1917
//! @cond Doxygen_Suppress
1918
bool GDALAbstractMDArray::CheckReadWriteParams(
1919
    const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *&arrayStep,
1920
    const GPtrDiff_t *&bufferStride, const GDALExtendedDataType &bufferDataType,
1921
    const void *buffer, const void *buffer_alloc_start,
1922
    size_t buffer_alloc_size, std::vector<GInt64> &tmp_arrayStep,
1923
    std::vector<GPtrDiff_t> &tmp_bufferStride) const
1924
0
{
1925
0
    const auto lamda_error = []()
1926
0
    {
1927
0
        CPLError(CE_Failure, CPLE_AppDefined,
1928
0
                 "Not all elements pointed by buffer will fit in "
1929
0
                 "[buffer_alloc_start, "
1930
0
                 "buffer_alloc_start + buffer_alloc_size]");
1931
0
    };
1932
1933
0
    const auto &dims = GetDimensions();
1934
0
    if (dims.empty())
1935
0
    {
1936
0
        if (buffer_alloc_start)
1937
0
        {
1938
0
            const size_t elementSize = bufferDataType.GetSize();
1939
0
            const GByte *paby_buffer = static_cast<const GByte *>(buffer);
1940
0
            const GByte *paby_buffer_alloc_start =
1941
0
                static_cast<const GByte *>(buffer_alloc_start);
1942
0
            const GByte *paby_buffer_alloc_end =
1943
0
                paby_buffer_alloc_start + buffer_alloc_size;
1944
1945
0
            if (paby_buffer < paby_buffer_alloc_start ||
1946
0
                paby_buffer + elementSize > paby_buffer_alloc_end)
1947
0
            {
1948
0
                lamda_error();
1949
0
                return false;
1950
0
            }
1951
0
        }
1952
0
        return true;
1953
0
    }
1954
1955
0
    if (arrayStep == nullptr)
1956
0
    {
1957
0
        tmp_arrayStep.resize(dims.size(), 1);
1958
0
        arrayStep = tmp_arrayStep.data();
1959
0
    }
1960
0
    for (size_t i = 0; i < dims.size(); i++)
1961
0
    {
1962
0
        assert(count);
1963
0
        if (count[i] == 0)
1964
0
        {
1965
0
            CPLError(CE_Failure, CPLE_AppDefined, "count[%u] = 0 is invalid",
1966
0
                     static_cast<unsigned>(i));
1967
0
            return false;
1968
0
        }
1969
0
    }
1970
0
    bool bufferStride_all_positive = true;
1971
0
    if (bufferStride == nullptr)
1972
0
    {
1973
0
        GPtrDiff_t stride = 1;
1974
0
        assert(dims.empty() || count != nullptr);
1975
        // To compute strides we must proceed from the fastest varying dimension
1976
        // (the last one), and then reverse the result
1977
0
        for (size_t i = dims.size(); i != 0;)
1978
0
        {
1979
0
            --i;
1980
0
            tmp_bufferStride.push_back(stride);
1981
0
            GUInt64 newStride = 0;
1982
0
            bool bOK;
1983
0
            try
1984
0
            {
1985
0
                newStride = (CPLSM(static_cast<uint64_t>(stride)) *
1986
0
                             CPLSM(static_cast<uint64_t>(count[i])))
1987
0
                                .v();
1988
0
                bOK = static_cast<size_t>(newStride) == newStride &&
1989
0
                      newStride < std::numeric_limits<size_t>::max() / 2;
1990
0
            }
1991
0
            catch (...)
1992
0
            {
1993
0
                bOK = false;
1994
0
            }
1995
0
            if (!bOK)
1996
0
            {
1997
0
                CPLError(CE_Failure, CPLE_OutOfMemory, "Too big count values");
1998
0
                return false;
1999
0
            }
2000
0
            stride = static_cast<GPtrDiff_t>(newStride);
2001
0
        }
2002
0
        std::reverse(tmp_bufferStride.begin(), tmp_bufferStride.end());
2003
0
        bufferStride = tmp_bufferStride.data();
2004
0
    }
2005
0
    else
2006
0
    {
2007
0
        for (size_t i = 0; i < dims.size(); i++)
2008
0
        {
2009
0
            if (bufferStride[i] < 0)
2010
0
            {
2011
0
                bufferStride_all_positive = false;
2012
0
                break;
2013
0
            }
2014
0
        }
2015
0
    }
2016
0
    for (size_t i = 0; i < dims.size(); i++)
2017
0
    {
2018
0
        assert(arrayStartIdx);
2019
0
        if (arrayStartIdx[i] >= dims[i]->GetSize())
2020
0
        {
2021
0
            CPLError(CE_Failure, CPLE_AppDefined,
2022
0
                     "arrayStartIdx[%u] = " CPL_FRMT_GUIB " >= " CPL_FRMT_GUIB,
2023
0
                     static_cast<unsigned>(i),
2024
0
                     static_cast<GUInt64>(arrayStartIdx[i]),
2025
0
                     static_cast<GUInt64>(dims[i]->GetSize()));
2026
0
            return false;
2027
0
        }
2028
0
        bool bOverflow;
2029
0
        if (arrayStep[i] >= 0)
2030
0
        {
2031
0
            try
2032
0
            {
2033
0
                bOverflow = (CPLSM(static_cast<uint64_t>(arrayStartIdx[i])) +
2034
0
                             CPLSM(static_cast<uint64_t>(count[i] - 1)) *
2035
0
                                 CPLSM(static_cast<uint64_t>(arrayStep[i])))
2036
0
                                .v() >= dims[i]->GetSize();
2037
0
            }
2038
0
            catch (...)
2039
0
            {
2040
0
                bOverflow = true;
2041
0
            }
2042
0
            if (bOverflow)
2043
0
            {
2044
0
                CPLError(CE_Failure, CPLE_AppDefined,
2045
0
                         "arrayStartIdx[%u] + (count[%u]-1) * arrayStep[%u] "
2046
0
                         ">= " CPL_FRMT_GUIB,
2047
0
                         static_cast<unsigned>(i), static_cast<unsigned>(i),
2048
0
                         static_cast<unsigned>(i),
2049
0
                         static_cast<GUInt64>(dims[i]->GetSize()));
2050
0
                return false;
2051
0
            }
2052
0
        }
2053
0
        else
2054
0
        {
2055
0
            try
2056
0
            {
2057
0
                bOverflow =
2058
0
                    arrayStartIdx[i] <
2059
0
                    (CPLSM(static_cast<uint64_t>(count[i] - 1)) *
2060
0
                     CPLSM(arrayStep[i] == std::numeric_limits<GInt64>::min()
2061
0
                               ? (static_cast<uint64_t>(1) << 63)
2062
0
                               : static_cast<uint64_t>(-arrayStep[i])))
2063
0
                        .v();
2064
0
            }
2065
0
            catch (...)
2066
0
            {
2067
0
                bOverflow = true;
2068
0
            }
2069
0
            if (bOverflow)
2070
0
            {
2071
0
                CPLError(
2072
0
                    CE_Failure, CPLE_AppDefined,
2073
0
                    "arrayStartIdx[%u] + (count[%u]-1) * arrayStep[%u] < 0",
2074
0
                    static_cast<unsigned>(i), static_cast<unsigned>(i),
2075
0
                    static_cast<unsigned>(i));
2076
0
                return false;
2077
0
            }
2078
0
        }
2079
0
    }
2080
2081
0
    if (buffer_alloc_start)
2082
0
    {
2083
0
        const size_t elementSize = bufferDataType.GetSize();
2084
0
        const GByte *paby_buffer = static_cast<const GByte *>(buffer);
2085
0
        const GByte *paby_buffer_alloc_start =
2086
0
            static_cast<const GByte *>(buffer_alloc_start);
2087
0
        const GByte *paby_buffer_alloc_end =
2088
0
            paby_buffer_alloc_start + buffer_alloc_size;
2089
0
        if (bufferStride_all_positive)
2090
0
        {
2091
0
            if (paby_buffer < paby_buffer_alloc_start)
2092
0
            {
2093
0
                lamda_error();
2094
0
                return false;
2095
0
            }
2096
0
            GUInt64 nOffset = elementSize;
2097
0
            for (size_t i = 0; i < dims.size(); i++)
2098
0
            {
2099
0
                try
2100
0
                {
2101
0
                    nOffset = (CPLSM(static_cast<uint64_t>(nOffset)) +
2102
0
                               CPLSM(static_cast<uint64_t>(bufferStride[i])) *
2103
0
                                   CPLSM(static_cast<uint64_t>(count[i] - 1)) *
2104
0
                                   CPLSM(static_cast<uint64_t>(elementSize)))
2105
0
                                  .v();
2106
0
                }
2107
0
                catch (...)
2108
0
                {
2109
0
                    lamda_error();
2110
0
                    return false;
2111
0
                }
2112
0
            }
2113
#if SIZEOF_VOIDP == 4
2114
            if (static_cast<size_t>(nOffset) != nOffset)
2115
            {
2116
                lamda_error();
2117
                return false;
2118
            }
2119
#endif
2120
0
            if (paby_buffer + nOffset > paby_buffer_alloc_end)
2121
0
            {
2122
0
                lamda_error();
2123
0
                return false;
2124
0
            }
2125
0
        }
2126
0
        else if (dims.size() < 31)
2127
0
        {
2128
            // Check all corners of the hypercube
2129
0
            const unsigned nLoops = 1U << static_cast<unsigned>(dims.size());
2130
0
            for (unsigned iCornerCode = 0; iCornerCode < nLoops; iCornerCode++)
2131
0
            {
2132
0
                const GByte *paby = paby_buffer;
2133
0
                for (unsigned i = 0; i < static_cast<unsigned>(dims.size());
2134
0
                     i++)
2135
0
                {
2136
0
                    if (iCornerCode & (1U << i))
2137
0
                    {
2138
                        // We should check for integer overflows
2139
0
                        paby += bufferStride[i] * (count[i] - 1) * elementSize;
2140
0
                    }
2141
0
                }
2142
0
                if (paby < paby_buffer_alloc_start ||
2143
0
                    paby + elementSize > paby_buffer_alloc_end)
2144
0
                {
2145
0
                    lamda_error();
2146
0
                    return false;
2147
0
                }
2148
0
            }
2149
0
        }
2150
0
    }
2151
2152
0
    return true;
2153
0
}
2154
2155
//! @endcond
2156
2157
/************************************************************************/
2158
/*                               Read()                                 */
2159
/************************************************************************/
2160
2161
/** Read part or totality of a multidimensional array or attribute.
2162
 *
2163
 * This will extract the content of a hyper-rectangle from the array into
2164
 * a user supplied buffer.
2165
 *
2166
 * If bufferDataType is of type string, the values written in pDstBuffer
2167
 * will be char* pointers and the strings should be freed with CPLFree().
2168
 *
2169
 * This is the same as the C function GDALMDArrayRead().
2170
 *
2171
 * @param arrayStartIdx Values representing the starting index to read
2172
 *                      in each dimension (in [0, aoDims[i].GetSize()-1] range).
2173
 *                      Array of GetDimensionCount() values. Must not be
2174
 *                      nullptr, unless for a zero-dimensional array.
2175
 *
2176
 * @param count         Values representing the number of values to extract in
2177
 *                      each dimension.
2178
 *                      Array of GetDimensionCount() values. Must not be
2179
 *                      nullptr, unless for a zero-dimensional array.
2180
 *
2181
 * @param arrayStep     Spacing between values to extract in each dimension.
2182
 *                      The spacing is in number of array elements, not bytes.
2183
 *                      If provided, must contain GetDimensionCount() values.
2184
 *                      If set to nullptr, [1, 1, ... 1] will be used as a
2185
 * default to indicate consecutive elements.
2186
 *
2187
 * @param bufferStride  Spacing between values to store in pDstBuffer.
2188
 *                      The spacing is in number of array elements, not bytes.
2189
 *                      If provided, must contain GetDimensionCount() values.
2190
 *                      Negative values are possible (for example to reorder
2191
 *                      from bottom-to-top to top-to-bottom).
2192
 *                      If set to nullptr, will be set so that pDstBuffer is
2193
 *                      written in a compact way, with elements of the last /
2194
 *                      fastest varying dimension being consecutive.
2195
 *
2196
 * @param bufferDataType Data type of values in pDstBuffer.
2197
 *
2198
 * @param pDstBuffer    User buffer to store the values read. Should be big
2199
 *                      enough to store the number of values indicated by
2200
 * count[] and with the spacing of bufferStride[].
2201
 *
2202
 * @param pDstBufferAllocStart Optional pointer that can be used to validate the
2203
 *                             validity of pDstBuffer. pDstBufferAllocStart
2204
 * should be the pointer returned by the malloc() or equivalent call used to
2205
 * allocate the buffer. It will generally be equal to pDstBuffer (when
2206
 * bufferStride[] values are all positive), but not necessarily. If specified,
2207
 * nDstBufferAllocSize should be also set to the appropriate value. If no
2208
 * validation is needed, nullptr can be passed.
2209
 *
2210
 * @param nDstBufferAllocSize  Optional buffer size, that can be used to
2211
 * validate the validity of pDstBuffer. This is the size of the buffer starting
2212
 * at pDstBufferAllocStart. If specified, pDstBufferAllocStart should be also
2213
 *                             set to the appropriate value.
2214
 *                             If no validation is needed, 0 can be passed.
2215
 *
2216
 * @return true in case of success.
2217
 */
2218
bool GDALAbstractMDArray::Read(
2219
    const GUInt64 *arrayStartIdx, const size_t *count,
2220
    const GInt64 *arrayStep,         // step in elements
2221
    const GPtrDiff_t *bufferStride,  // stride in elements
2222
    const GDALExtendedDataType &bufferDataType, void *pDstBuffer,
2223
    const void *pDstBufferAllocStart, size_t nDstBufferAllocSize) const
2224
0
{
2225
0
    if (!GetDataType().CanConvertTo(bufferDataType))
2226
0
    {
2227
0
        CPLError(CE_Failure, CPLE_AppDefined,
2228
0
                 "Array data type is not convertible to buffer data type");
2229
0
        return false;
2230
0
    }
2231
2232
0
    std::vector<GInt64> tmp_arrayStep;
2233
0
    std::vector<GPtrDiff_t> tmp_bufferStride;
2234
0
    if (!CheckReadWriteParams(arrayStartIdx, count, arrayStep, bufferStride,
2235
0
                              bufferDataType, pDstBuffer, pDstBufferAllocStart,
2236
0
                              nDstBufferAllocSize, tmp_arrayStep,
2237
0
                              tmp_bufferStride))
2238
0
    {
2239
0
        return false;
2240
0
    }
2241
2242
0
    return IRead(arrayStartIdx, count, arrayStep, bufferStride, bufferDataType,
2243
0
                 pDstBuffer);
2244
0
}
2245
2246
/************************************************************************/
2247
/*                                IWrite()                              */
2248
/************************************************************************/
2249
2250
//! @cond Doxygen_Suppress
2251
bool GDALAbstractMDArray::IWrite(const GUInt64 *, const size_t *,
2252
                                 const GInt64 *, const GPtrDiff_t *,
2253
                                 const GDALExtendedDataType &, const void *)
2254
0
{
2255
0
    CPLError(CE_Failure, CPLE_AppDefined, "IWrite() not implemented");
2256
0
    return false;
2257
0
}
2258
2259
//! @endcond
2260
2261
/************************************************************************/
2262
/*                               Write()                                 */
2263
/************************************************************************/
2264
2265
/** Write part or totality of a multidimensional array or attribute.
2266
 *
2267
 * This will set the content of a hyper-rectangle into the array from
2268
 * a user supplied buffer.
2269
 *
2270
 * If bufferDataType is of type string, the values read from pSrcBuffer
2271
 * will be char* pointers.
2272
 *
2273
 * This is the same as the C function GDALMDArrayWrite().
2274
 *
2275
 * @param arrayStartIdx Values representing the starting index to write
2276
 *                      in each dimension (in [0, aoDims[i].GetSize()-1] range).
2277
 *                      Array of GetDimensionCount() values. Must not be
2278
 *                      nullptr, unless for a zero-dimensional array.
2279
 *
2280
 * @param count         Values representing the number of values to write in
2281
 *                      each dimension.
2282
 *                      Array of GetDimensionCount() values. Must not be
2283
 *                      nullptr, unless for a zero-dimensional array.
2284
 *
2285
 * @param arrayStep     Spacing between values to write in each dimension.
2286
 *                      The spacing is in number of array elements, not bytes.
2287
 *                      If provided, must contain GetDimensionCount() values.
2288
 *                      If set to nullptr, [1, 1, ... 1] will be used as a
2289
 * default to indicate consecutive elements.
2290
 *
2291
 * @param bufferStride  Spacing between values to read from pSrcBuffer.
2292
 *                      The spacing is in number of array elements, not bytes.
2293
 *                      If provided, must contain GetDimensionCount() values.
2294
 *                      Negative values are possible (for example to reorder
2295
 *                      from bottom-to-top to top-to-bottom).
2296
 *                      If set to nullptr, will be set so that pSrcBuffer is
2297
 *                      written in a compact way, with elements of the last /
2298
 *                      fastest varying dimension being consecutive.
2299
 *
2300
 * @param bufferDataType Data type of values in pSrcBuffer.
2301
 *
2302
 * @param pSrcBuffer    User buffer to read the values from. Should be big
2303
 *                      enough to store the number of values indicated by
2304
 * count[] and with the spacing of bufferStride[].
2305
 *
2306
 * @param pSrcBufferAllocStart Optional pointer that can be used to validate the
2307
 *                             validity of pSrcBuffer. pSrcBufferAllocStart
2308
 * should be the pointer returned by the malloc() or equivalent call used to
2309
 * allocate the buffer. It will generally be equal to pSrcBuffer (when
2310
 * bufferStride[] values are all positive), but not necessarily. If specified,
2311
 * nSrcBufferAllocSize should be also set to the appropriate value. If no
2312
 * validation is needed, nullptr can be passed.
2313
 *
2314
 * @param nSrcBufferAllocSize  Optional buffer size, that can be used to
2315
 * validate the validity of pSrcBuffer. This is the size of the buffer starting
2316
 * at pSrcBufferAllocStart. If specified, pDstBufferAllocStart should be also
2317
 *                             set to the appropriate value.
2318
 *                             If no validation is needed, 0 can be passed.
2319
 *
2320
 * @return true in case of success.
2321
 */
2322
bool GDALAbstractMDArray::Write(const GUInt64 *arrayStartIdx,
2323
                                const size_t *count, const GInt64 *arrayStep,
2324
                                const GPtrDiff_t *bufferStride,
2325
                                const GDALExtendedDataType &bufferDataType,
2326
                                const void *pSrcBuffer,
2327
                                const void *pSrcBufferAllocStart,
2328
                                size_t nSrcBufferAllocSize)
2329
0
{
2330
0
    if (!bufferDataType.CanConvertTo(GetDataType()))
2331
0
    {
2332
0
        CPLError(CE_Failure, CPLE_AppDefined,
2333
0
                 "Buffer data type is not convertible to array data type");
2334
0
        return false;
2335
0
    }
2336
2337
0
    std::vector<GInt64> tmp_arrayStep;
2338
0
    std::vector<GPtrDiff_t> tmp_bufferStride;
2339
0
    if (!CheckReadWriteParams(arrayStartIdx, count, arrayStep, bufferStride,
2340
0
                              bufferDataType, pSrcBuffer, pSrcBufferAllocStart,
2341
0
                              nSrcBufferAllocSize, tmp_arrayStep,
2342
0
                              tmp_bufferStride))
2343
0
    {
2344
0
        return false;
2345
0
    }
2346
2347
0
    return IWrite(arrayStartIdx, count, arrayStep, bufferStride, bufferDataType,
2348
0
                  pSrcBuffer);
2349
0
}
2350
2351
/************************************************************************/
2352
/*                          GetTotalElementsCount()                     */
2353
/************************************************************************/
2354
2355
/** Return the total number of values in the array.
2356
 *
2357
 * This is the same as the C functions GDALMDArrayGetTotalElementsCount()
2358
 * and GDALAttributeGetTotalElementsCount().
2359
 *
2360
 */
2361
GUInt64 GDALAbstractMDArray::GetTotalElementsCount() const
2362
0
{
2363
0
    const auto &dims = GetDimensions();
2364
0
    if (dims.empty())
2365
0
        return 1;
2366
0
    GUInt64 nElts = 1;
2367
0
    for (const auto &dim : dims)
2368
0
    {
2369
0
        try
2370
0
        {
2371
0
            nElts = (CPLSM(static_cast<uint64_t>(nElts)) *
2372
0
                     CPLSM(static_cast<uint64_t>(dim->GetSize())))
2373
0
                        .v();
2374
0
        }
2375
0
        catch (...)
2376
0
        {
2377
0
            return 0;
2378
0
        }
2379
0
    }
2380
0
    return nElts;
2381
0
}
2382
2383
/************************************************************************/
2384
/*                           GetBlockSize()                             */
2385
/************************************************************************/
2386
2387
/** Return the "natural" block size of the array along all dimensions.
2388
 *
2389
 * Some drivers might organize the array in tiles/blocks and reading/writing
2390
 * aligned on those tile/block boundaries will be more efficient.
2391
 *
2392
 * The returned number of elements in the vector is the same as
2393
 * GetDimensionCount(). A value of 0 should be interpreted as no hint regarding
2394
 * the natural block size along the considered dimension.
2395
 * "Flat" arrays will typically return a vector of values set to 0.
2396
 *
2397
 * The default implementation will return a vector of values set to 0.
2398
 *
2399
 * This method is used by GetProcessingChunkSize().
2400
 *
2401
 * Pedantic note: the returned type is GUInt64, so in the highly unlikeley
2402
 * theoretical case of a 32-bit platform, this might exceed its size_t
2403
 * allocation capabilities.
2404
 *
2405
 * This is the same as the C function GDALMDArrayGetBlockSize().
2406
 *
2407
 * @return the block size, in number of elements along each dimension.
2408
 */
2409
std::vector<GUInt64> GDALAbstractMDArray::GetBlockSize() const
2410
0
{
2411
0
    return std::vector<GUInt64>(GetDimensionCount());
2412
0
}
2413
2414
/************************************************************************/
2415
/*                       GetProcessingChunkSize()                       */
2416
/************************************************************************/
2417
2418
/** \brief Return an optimal chunk size for read/write operations, given the
2419
 * natural block size and memory constraints specified.
2420
 *
2421
 * This method will use GetBlockSize() to define a chunk whose dimensions are
2422
 * multiple of those returned by GetBlockSize() (unless the block define by
2423
 * GetBlockSize() is larger than nMaxChunkMemory, in which case it will be
2424
 * returned by this method).
2425
 *
2426
 * This is the same as the C function GDALMDArrayGetProcessingChunkSize().
2427
 *
2428
 * @param nMaxChunkMemory Maximum amount of memory, in bytes, to use for the
2429
 * chunk.
2430
 *
2431
 * @return the chunk size, in number of elements along each dimension.
2432
 */
2433
std::vector<size_t>
2434
GDALAbstractMDArray::GetProcessingChunkSize(size_t nMaxChunkMemory) const
2435
0
{
2436
0
    const auto &dims = GetDimensions();
2437
0
    const auto &nDTSize = GetDataType().GetSize();
2438
0
    std::vector<size_t> anChunkSize;
2439
0
    auto blockSize = GetBlockSize();
2440
0
    CPLAssert(blockSize.size() == dims.size());
2441
0
    size_t nChunkSize = nDTSize;
2442
0
    bool bOverflow = false;
2443
0
    constexpr auto kSIZE_T_MAX = std::numeric_limits<size_t>::max();
2444
    // Initialize anChunkSize[i] with blockSize[i] by properly clamping in
2445
    // [1, min(sizet_max, dim_size[i])]
2446
    // Also make sure that the product of all anChunkSize[i]) fits on size_t
2447
0
    for (size_t i = 0; i < dims.size(); i++)
2448
0
    {
2449
0
        const auto sizeDimI =
2450
0
            std::max(static_cast<size_t>(1),
2451
0
                     static_cast<size_t>(
2452
0
                         std::min(static_cast<GUInt64>(kSIZE_T_MAX),
2453
0
                                  std::min(blockSize[i], dims[i]->GetSize()))));
2454
0
        anChunkSize.push_back(sizeDimI);
2455
0
        if (nChunkSize > kSIZE_T_MAX / sizeDimI)
2456
0
        {
2457
0
            bOverflow = true;
2458
0
        }
2459
0
        else
2460
0
        {
2461
0
            nChunkSize *= sizeDimI;
2462
0
        }
2463
0
    }
2464
0
    if (nChunkSize == 0)
2465
0
        return anChunkSize;
2466
2467
    // If the product of all anChunkSize[i] does not fit on size_t, then
2468
    // set lowest anChunkSize[i] to 1.
2469
0
    if (bOverflow)
2470
0
    {
2471
0
        nChunkSize = nDTSize;
2472
0
        bOverflow = false;
2473
0
        for (size_t i = dims.size(); i > 0;)
2474
0
        {
2475
0
            --i;
2476
0
            if (bOverflow || nChunkSize > kSIZE_T_MAX / anChunkSize[i])
2477
0
            {
2478
0
                bOverflow = true;
2479
0
                anChunkSize[i] = 1;
2480
0
            }
2481
0
            else
2482
0
            {
2483
0
                nChunkSize *= anChunkSize[i];
2484
0
            }
2485
0
        }
2486
0
    }
2487
2488
0
    nChunkSize = nDTSize;
2489
0
    std::vector<size_t> anAccBlockSizeFromStart;
2490
0
    for (size_t i = 0; i < dims.size(); i++)
2491
0
    {
2492
0
        nChunkSize *= anChunkSize[i];
2493
0
        anAccBlockSizeFromStart.push_back(nChunkSize);
2494
0
    }
2495
0
    if (nChunkSize <= nMaxChunkMemory / 2)
2496
0
    {
2497
0
        size_t nVoxelsFromEnd = 1;
2498
0
        for (size_t i = dims.size(); i > 0;)
2499
0
        {
2500
0
            --i;
2501
0
            const auto nCurBlockSize =
2502
0
                anAccBlockSizeFromStart[i] * nVoxelsFromEnd;
2503
0
            const auto nMul = nMaxChunkMemory / nCurBlockSize;
2504
0
            if (nMul >= 2)
2505
0
            {
2506
0
                const auto nSizeThisDim(dims[i]->GetSize());
2507
0
                const auto nBlocksThisDim =
2508
0
                    DIV_ROUND_UP(nSizeThisDim, anChunkSize[i]);
2509
0
                anChunkSize[i] = static_cast<size_t>(std::min(
2510
0
                    anChunkSize[i] *
2511
0
                        std::min(static_cast<GUInt64>(nMul), nBlocksThisDim),
2512
0
                    nSizeThisDim));
2513
0
            }
2514
0
            nVoxelsFromEnd *= anChunkSize[i];
2515
0
        }
2516
0
    }
2517
0
    return anChunkSize;
2518
0
}
2519
2520
/************************************************************************/
2521
/*                         BaseRename()                                 */
2522
/************************************************************************/
2523
2524
//! @cond Doxygen_Suppress
2525
void GDALAbstractMDArray::BaseRename(const std::string &osNewName)
2526
0
{
2527
0
    m_osFullName.resize(m_osFullName.size() - m_osName.size());
2528
0
    m_osFullName += osNewName;
2529
0
    m_osName = osNewName;
2530
2531
0
    NotifyChildrenOfRenaming();
2532
0
}
2533
2534
//! @endcond
2535
2536
//! @cond Doxygen_Suppress
2537
/************************************************************************/
2538
/*                          ParentRenamed()                             */
2539
/************************************************************************/
2540
2541
void GDALAbstractMDArray::ParentRenamed(const std::string &osNewParentFullName)
2542
0
{
2543
0
    m_osFullName = osNewParentFullName;
2544
0
    m_osFullName += "/";
2545
0
    m_osFullName += m_osName;
2546
2547
0
    NotifyChildrenOfRenaming();
2548
0
}
2549
2550
//! @endcond
2551
2552
/************************************************************************/
2553
/*                             Deleted()                                */
2554
/************************************************************************/
2555
2556
//! @cond Doxygen_Suppress
2557
void GDALAbstractMDArray::Deleted()
2558
0
{
2559
0
    m_bValid = false;
2560
2561
0
    NotifyChildrenOfDeletion();
2562
0
}
2563
2564
//! @endcond
2565
2566
/************************************************************************/
2567
/*                        ParentDeleted()                               */
2568
/************************************************************************/
2569
2570
//! @cond Doxygen_Suppress
2571
void GDALAbstractMDArray::ParentDeleted()
2572
0
{
2573
0
    Deleted();
2574
0
}
2575
2576
//! @endcond
2577
2578
/************************************************************************/
2579
/*                     CheckValidAndErrorOutIfNot()                     */
2580
/************************************************************************/
2581
2582
//! @cond Doxygen_Suppress
2583
bool GDALAbstractMDArray::CheckValidAndErrorOutIfNot() const
2584
0
{
2585
0
    if (!m_bValid)
2586
0
    {
2587
0
        CPLError(CE_Failure, CPLE_AppDefined,
2588
0
                 "This object has been deleted. No action on it is possible");
2589
0
    }
2590
0
    return m_bValid;
2591
0
}
2592
2593
//! @endcond
2594
2595
/************************************************************************/
2596
/*                             SetUnit()                                */
2597
/************************************************************************/
2598
2599
/** Set the variable unit.
2600
 *
2601
 * Values should conform as much as possible with those allowed by
2602
 * the NetCDF CF conventions:
2603
 * http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.html#units
2604
 * but others might be returned.
2605
 *
2606
 * Few examples are "meter", "degrees", "second", ...
2607
 * Empty value means unknown.
2608
 *
2609
 * This is the same as the C function GDALMDArraySetUnit()
2610
 *
2611
 * @note Driver implementation: optionally implemented.
2612
 *
2613
 * @param osUnit unit name.
2614
 * @return true in case of success.
2615
 */
2616
bool GDALMDArray::SetUnit(CPL_UNUSED const std::string &osUnit)
2617
0
{
2618
0
    CPLError(CE_Failure, CPLE_NotSupported, "SetUnit() not implemented");
2619
0
    return false;
2620
0
}
2621
2622
/************************************************************************/
2623
/*                             GetUnit()                                */
2624
/************************************************************************/
2625
2626
/** Return the array unit.
2627
 *
2628
 * Values should conform as much as possible with those allowed by
2629
 * the NetCDF CF conventions:
2630
 * http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.html#units
2631
 * but others might be returned.
2632
 *
2633
 * Few examples are "meter", "degrees", "second", ...
2634
 * Empty value means unknown.
2635
 *
2636
 * This is the same as the C function GDALMDArrayGetUnit()
2637
 */
2638
const std::string &GDALMDArray::GetUnit() const
2639
0
{
2640
0
    static const std::string emptyString;
2641
0
    return emptyString;
2642
0
}
2643
2644
/************************************************************************/
2645
/*                          SetSpatialRef()                             */
2646
/************************************************************************/
2647
2648
/** Assign a spatial reference system object to the array.
2649
 *
2650
 * This is the same as the C function GDALMDArraySetSpatialRef().
2651
 */
2652
bool GDALMDArray::SetSpatialRef(CPL_UNUSED const OGRSpatialReference *poSRS)
2653
0
{
2654
0
    CPLError(CE_Failure, CPLE_NotSupported, "SetSpatialRef() not implemented");
2655
0
    return false;
2656
0
}
2657
2658
/************************************************************************/
2659
/*                          GetSpatialRef()                             */
2660
/************************************************************************/
2661
2662
/** Return the spatial reference system object associated with the array.
2663
 *
2664
 * This is the same as the C function GDALMDArrayGetSpatialRef().
2665
 */
2666
std::shared_ptr<OGRSpatialReference> GDALMDArray::GetSpatialRef() const
2667
0
{
2668
0
    return nullptr;
2669
0
}
2670
2671
/************************************************************************/
2672
/*                        GetRawNoDataValue()                           */
2673
/************************************************************************/
2674
2675
/** Return the nodata value as a "raw" value.
2676
 *
2677
 * The value returned might be nullptr in case of no nodata value. When
2678
 * a nodata value is registered, a non-nullptr will be returned whose size in
2679
 * bytes is GetDataType().GetSize().
2680
 *
2681
 * The returned value should not be modified or freed. It is valid until
2682
 * the array is destroyed, or the next call to GetRawNoDataValue() or
2683
 * SetRawNoDataValue(), or any similar methods.
2684
 *
2685
 * @note Driver implementation: this method shall be implemented if nodata
2686
 * is supported.
2687
 *
2688
 * This is the same as the C function GDALMDArrayGetRawNoDataValue().
2689
 *
2690
 * @return nullptr or a pointer to GetDataType().GetSize() bytes.
2691
 */
2692
const void *GDALMDArray::GetRawNoDataValue() const
2693
0
{
2694
0
    return nullptr;
2695
0
}
2696
2697
/************************************************************************/
2698
/*                        GetNoDataValueAsDouble()                      */
2699
/************************************************************************/
2700
2701
/** Return the nodata value as a double.
2702
 *
2703
 * This is the same as the C function GDALMDArrayGetNoDataValueAsDouble().
2704
 *
2705
 * @param pbHasNoData Pointer to a output boolean that will be set to true if
2706
 * a nodata value exists and can be converted to double. Might be nullptr.
2707
 *
2708
 * @return the nodata value as a double. A 0.0 value might also indicate the
2709
 * absence of a nodata value or an error in the conversion (*pbHasNoData will be
2710
 * set to false then).
2711
 */
2712
double GDALMDArray::GetNoDataValueAsDouble(bool *pbHasNoData) const
2713
0
{
2714
0
    const void *pNoData = GetRawNoDataValue();
2715
0
    double dfNoData = 0.0;
2716
0
    const auto &eDT = GetDataType();
2717
0
    const bool ok = pNoData != nullptr && eDT.GetClass() == GEDTC_NUMERIC;
2718
0
    if (ok)
2719
0
    {
2720
0
        GDALCopyWords64(pNoData, eDT.GetNumericDataType(), 0, &dfNoData,
2721
0
                        GDT_Float64, 0, 1);
2722
0
    }
2723
0
    if (pbHasNoData)
2724
0
        *pbHasNoData = ok;
2725
0
    return dfNoData;
2726
0
}
2727
2728
/************************************************************************/
2729
/*                        GetNoDataValueAsInt64()                       */
2730
/************************************************************************/
2731
2732
/** Return the nodata value as a Int64.
2733
 *
2734
 * @param pbHasNoData Pointer to a output boolean that will be set to true if
2735
 * a nodata value exists and can be converted to Int64. Might be nullptr.
2736
 *
2737
 * This is the same as the C function GDALMDArrayGetNoDataValueAsInt64().
2738
 *
2739
 * @return the nodata value as a Int64
2740
 *
2741
 * @since GDAL 3.5
2742
 */
2743
int64_t GDALMDArray::GetNoDataValueAsInt64(bool *pbHasNoData) const
2744
0
{
2745
0
    const void *pNoData = GetRawNoDataValue();
2746
0
    int64_t nNoData = GDAL_PAM_DEFAULT_NODATA_VALUE_INT64;
2747
0
    const auto &eDT = GetDataType();
2748
0
    const bool ok = pNoData != nullptr && eDT.GetClass() == GEDTC_NUMERIC;
2749
0
    if (ok)
2750
0
    {
2751
0
        GDALCopyWords64(pNoData, eDT.GetNumericDataType(), 0, &nNoData,
2752
0
                        GDT_Int64, 0, 1);
2753
0
    }
2754
0
    if (pbHasNoData)
2755
0
        *pbHasNoData = ok;
2756
0
    return nNoData;
2757
0
}
2758
2759
/************************************************************************/
2760
/*                       GetNoDataValueAsUInt64()                       */
2761
/************************************************************************/
2762
2763
/** Return the nodata value as a UInt64.
2764
 *
2765
 * This is the same as the C function GDALMDArrayGetNoDataValueAsUInt64().
2766
2767
 * @param pbHasNoData Pointer to a output boolean that will be set to true if
2768
 * a nodata value exists and can be converted to UInt64. Might be nullptr.
2769
 *
2770
 * @return the nodata value as a UInt64
2771
 *
2772
 * @since GDAL 3.5
2773
 */
2774
uint64_t GDALMDArray::GetNoDataValueAsUInt64(bool *pbHasNoData) const
2775
0
{
2776
0
    const void *pNoData = GetRawNoDataValue();
2777
0
    uint64_t nNoData = GDAL_PAM_DEFAULT_NODATA_VALUE_UINT64;
2778
0
    const auto &eDT = GetDataType();
2779
0
    const bool ok = pNoData != nullptr && eDT.GetClass() == GEDTC_NUMERIC;
2780
0
    if (ok)
2781
0
    {
2782
0
        GDALCopyWords64(pNoData, eDT.GetNumericDataType(), 0, &nNoData,
2783
0
                        GDT_UInt64, 0, 1);
2784
0
    }
2785
0
    if (pbHasNoData)
2786
0
        *pbHasNoData = ok;
2787
0
    return nNoData;
2788
0
}
2789
2790
/************************************************************************/
2791
/*                        SetRawNoDataValue()                           */
2792
/************************************************************************/
2793
2794
/** Set the nodata value as a "raw" value.
2795
 *
2796
 * The value passed might be nullptr in case of no nodata value. When
2797
 * a nodata value is registered, a non-nullptr whose size in
2798
 * bytes is GetDataType().GetSize() must be passed.
2799
 *
2800
 * This is the same as the C function GDALMDArraySetRawNoDataValue().
2801
 *
2802
 * @note Driver implementation: this method shall be implemented if setting
2803
 nodata
2804
 * is supported.
2805
2806
 * @return true in case of success.
2807
 */
2808
bool GDALMDArray::SetRawNoDataValue(CPL_UNUSED const void *pRawNoData)
2809
0
{
2810
0
    CPLError(CE_Failure, CPLE_NotSupported,
2811
0
             "SetRawNoDataValue() not implemented");
2812
0
    return false;
2813
0
}
2814
2815
/************************************************************************/
2816
/*                           SetNoDataValue()                           */
2817
/************************************************************************/
2818
2819
/** Set the nodata value as a double.
2820
 *
2821
 * If the natural data type of the attribute/array is not double, type
2822
 * conversion will occur to the type returned by GetDataType().
2823
 *
2824
 * This is the same as the C function GDALMDArraySetNoDataValueAsDouble().
2825
 *
2826
 * @return true in case of success.
2827
 */
2828
bool GDALMDArray::SetNoDataValue(double dfNoData)
2829
0
{
2830
0
    void *pRawNoData = CPLMalloc(GetDataType().GetSize());
2831
0
    bool bRet = false;
2832
0
    if (GDALExtendedDataType::CopyValue(
2833
0
            &dfNoData, GDALExtendedDataType::Create(GDT_Float64), pRawNoData,
2834
0
            GetDataType()))
2835
0
    {
2836
0
        bRet = SetRawNoDataValue(pRawNoData);
2837
0
    }
2838
0
    CPLFree(pRawNoData);
2839
0
    return bRet;
2840
0
}
2841
2842
/************************************************************************/
2843
/*                           SetNoDataValue()                           */
2844
/************************************************************************/
2845
2846
/** Set the nodata value as a Int64.
2847
 *
2848
 * If the natural data type of the attribute/array is not Int64, type conversion
2849
 * will occur to the type returned by GetDataType().
2850
 *
2851
 * This is the same as the C function GDALMDArraySetNoDataValueAsInt64().
2852
 *
2853
 * @return true in case of success.
2854
 *
2855
 * @since GDAL 3.5
2856
 */
2857
bool GDALMDArray::SetNoDataValue(int64_t nNoData)
2858
0
{
2859
0
    void *pRawNoData = CPLMalloc(GetDataType().GetSize());
2860
0
    bool bRet = false;
2861
0
    if (GDALExtendedDataType::CopyValue(&nNoData,
2862
0
                                        GDALExtendedDataType::Create(GDT_Int64),
2863
0
                                        pRawNoData, GetDataType()))
2864
0
    {
2865
0
        bRet = SetRawNoDataValue(pRawNoData);
2866
0
    }
2867
0
    CPLFree(pRawNoData);
2868
0
    return bRet;
2869
0
}
2870
2871
/************************************************************************/
2872
/*                           SetNoDataValue()                           */
2873
/************************************************************************/
2874
2875
/** Set the nodata value as a Int64.
2876
 *
2877
 * If the natural data type of the attribute/array is not Int64, type conversion
2878
 * will occur to the type returned by GetDataType().
2879
 *
2880
 * This is the same as the C function GDALMDArraySetNoDataValueAsUInt64().
2881
 *
2882
 * @return true in case of success.
2883
 *
2884
 * @since GDAL 3.5
2885
 */
2886
bool GDALMDArray::SetNoDataValue(uint64_t nNoData)
2887
0
{
2888
0
    void *pRawNoData = CPLMalloc(GetDataType().GetSize());
2889
0
    bool bRet = false;
2890
0
    if (GDALExtendedDataType::CopyValue(
2891
0
            &nNoData, GDALExtendedDataType::Create(GDT_UInt64), pRawNoData,
2892
0
            GetDataType()))
2893
0
    {
2894
0
        bRet = SetRawNoDataValue(pRawNoData);
2895
0
    }
2896
0
    CPLFree(pRawNoData);
2897
0
    return bRet;
2898
0
}
2899
2900
/************************************************************************/
2901
/*                            Resize()                                  */
2902
/************************************************************************/
2903
2904
/** Resize an array to new dimensions.
2905
 *
2906
 * Not all drivers may allow this operation, and with restrictions (e.g.
2907
 * for netCDF, this is limited to growing of "unlimited" dimensions)
2908
 *
2909
 * Resizing a dimension used in other arrays will cause those other arrays
2910
 * to be resized.
2911
 *
2912
 * This is the same as the C function GDALMDArrayResize().
2913
 *
2914
 * @param anNewDimSizes Array of GetDimensionCount() values containing the
2915
 *                      new size of each indexing dimension.
2916
 * @param papszOptions Options. (Driver specific)
2917
 * @return true in case of success.
2918
 * @since GDAL 3.7
2919
 */
2920
bool GDALMDArray::Resize(CPL_UNUSED const std::vector<GUInt64> &anNewDimSizes,
2921
                         CPL_UNUSED CSLConstList papszOptions)
2922
0
{
2923
0
    CPLError(CE_Failure, CPLE_NotSupported,
2924
0
             "Resize() is not supported for this array");
2925
0
    return false;
2926
0
}
2927
2928
/************************************************************************/
2929
/*                               SetScale()                             */
2930
/************************************************************************/
2931
2932
/** Set the scale value to apply to raw values.
2933
 *
2934
 * unscaled_value = raw_value * GetScale() + GetOffset()
2935
 *
2936
 * This is the same as the C function GDALMDArraySetScale() /
2937
 * GDALMDArraySetScaleEx().
2938
 *
2939
 * @note Driver implementation: this method shall be implemented if setting
2940
 * scale is supported.
2941
 *
2942
 * @param dfScale scale
2943
 * @param eStorageType Data type to which create the potential attribute that
2944
 * will store the scale. Added in GDAL 3.3 If let to its GDT_Unknown value, the
2945
 * implementation will decide automatically the data type. Note that changing
2946
 * the data type after initial setting might not be supported.
2947
 * @return true in case of success.
2948
 */
2949
bool GDALMDArray::SetScale(CPL_UNUSED double dfScale,
2950
                           CPL_UNUSED GDALDataType eStorageType)
2951
0
{
2952
0
    CPLError(CE_Failure, CPLE_NotSupported, "SetScale() not implemented");
2953
0
    return false;
2954
0
}
2955
2956
/************************************************************************/
2957
/*                               SetOffset)                             */
2958
/************************************************************************/
2959
2960
/** Set the offset value to apply to raw values.
2961
 *
2962
 * unscaled_value = raw_value * GetScale() + GetOffset()
2963
 *
2964
 * This is the same as the C function GDALMDArraySetOffset() /
2965
 * GDALMDArraySetOffsetEx().
2966
 *
2967
 * @note Driver implementation: this method shall be implemented if setting
2968
 * offset is supported.
2969
 *
2970
 * @param dfOffset Offset
2971
 * @param eStorageType Data type to which create the potential attribute that
2972
 * will store the offset. Added in GDAL 3.3 If let to its GDT_Unknown value, the
2973
 * implementation will decide automatically the data type. Note that changing
2974
 * the data type after initial setting might not be supported.
2975
 * @return true in case of success.
2976
 */
2977
bool GDALMDArray::SetOffset(CPL_UNUSED double dfOffset,
2978
                            CPL_UNUSED GDALDataType eStorageType)
2979
0
{
2980
0
    CPLError(CE_Failure, CPLE_NotSupported, "SetOffset() not implemented");
2981
0
    return false;
2982
0
}
2983
2984
/************************************************************************/
2985
/*                               GetScale()                             */
2986
/************************************************************************/
2987
2988
/** Get the scale value to apply to raw values.
2989
 *
2990
 * unscaled_value = raw_value * GetScale() + GetOffset()
2991
 *
2992
 * This is the same as the C function GDALMDArrayGetScale().
2993
 *
2994
 * @note Driver implementation: this method shall be implemented if gettings
2995
 * scale is supported.
2996
 *
2997
 * @param pbHasScale Pointer to a output boolean that will be set to true if
2998
 * a scale value exists. Might be nullptr.
2999
 * @param peStorageType Pointer to a output GDALDataType that will be set to
3000
 * the storage type of the scale value, when known/relevant. Otherwise will be
3001
 * set to GDT_Unknown. Might be nullptr. Since GDAL 3.3
3002
 *
3003
 * @return the scale value. A 1.0 value might also indicate the
3004
 * absence of a scale value.
3005
 */
3006
double GDALMDArray::GetScale(CPL_UNUSED bool *pbHasScale,
3007
                             CPL_UNUSED GDALDataType *peStorageType) const
3008
0
{
3009
0
    if (pbHasScale)
3010
0
        *pbHasScale = false;
3011
0
    return 1.0;
3012
0
}
3013
3014
/************************************************************************/
3015
/*                               GetOffset()                            */
3016
/************************************************************************/
3017
3018
/** Get the offset value to apply to raw values.
3019
 *
3020
 * unscaled_value = raw_value * GetScale() + GetOffset()
3021
 *
3022
 * This is the same as the C function GDALMDArrayGetOffset().
3023
 *
3024
 * @note Driver implementation: this method shall be implemented if gettings
3025
 * offset is supported.
3026
 *
3027
 * @param pbHasOffset Pointer to a output boolean that will be set to true if
3028
 * a offset value exists. Might be nullptr.
3029
 * @param peStorageType Pointer to a output GDALDataType that will be set to
3030
 * the storage type of the offset value, when known/relevant. Otherwise will be
3031
 * set to GDT_Unknown. Might be nullptr. Since GDAL 3.3
3032
 *
3033
 * @return the offset value. A 0.0 value might also indicate the
3034
 * absence of a offset value.
3035
 */
3036
double GDALMDArray::GetOffset(CPL_UNUSED bool *pbHasOffset,
3037
                              CPL_UNUSED GDALDataType *peStorageType) const
3038
0
{
3039
0
    if (pbHasOffset)
3040
0
        *pbHasOffset = false;
3041
0
    return 0.0;
3042
0
}
3043
3044
/************************************************************************/
3045
/*                         ProcessPerChunk()                            */
3046
/************************************************************************/
3047
3048
namespace
3049
{
3050
enum class Caller
3051
{
3052
    CALLER_END_OF_LOOP,
3053
    CALLER_IN_LOOP,
3054
};
3055
}
3056
3057
/** \brief Call a user-provided function to operate on an array chunk by chunk.
3058
 *
3059
 * This method is to be used when doing operations on an array, or a subset of
3060
 * it, in a chunk by chunk way.
3061
 *
3062
 * @param arrayStartIdx Values representing the starting index to use
3063
 *                      in each dimension (in [0, aoDims[i].GetSize()-1] range).
3064
 *                      Array of GetDimensionCount() values. Must not be
3065
 *                      nullptr, unless for a zero-dimensional array.
3066
 *
3067
 * @param count         Values representing the number of values to use in
3068
 *                      each dimension.
3069
 *                      Array of GetDimensionCount() values. Must not be
3070
 *                      nullptr, unless for a zero-dimensional array.
3071
 *
3072
 * @param chunkSize     Values representing the chunk size in each dimension.
3073
 *                      Might typically the output of GetProcessingChunkSize().
3074
 *                      Array of GetDimensionCount() values. Must not be
3075
 *                      nullptr, unless for a zero-dimensional array.
3076
 *
3077
 * @param pfnFunc       User-provided function of type FuncProcessPerChunkType.
3078
 *                      Must NOT be nullptr.
3079
 *
3080
 * @param pUserData     Pointer to pass as the value of the pUserData argument
3081
 * of FuncProcessPerChunkType. Might be nullptr (depends on pfnFunc.
3082
 *
3083
 * @return true in case of success.
3084
 */
3085
bool GDALAbstractMDArray::ProcessPerChunk(const GUInt64 *arrayStartIdx,
3086
                                          const GUInt64 *count,
3087
                                          const size_t *chunkSize,
3088
                                          FuncProcessPerChunkType pfnFunc,
3089
                                          void *pUserData)
3090
0
{
3091
0
    const auto &dims = GetDimensions();
3092
0
    if (dims.empty())
3093
0
    {
3094
0
        return pfnFunc(this, nullptr, nullptr, 1, 1, pUserData);
3095
0
    }
3096
3097
    // Sanity check
3098
0
    size_t nTotalChunkSize = 1;
3099
0
    for (size_t i = 0; i < dims.size(); i++)
3100
0
    {
3101
0
        const auto nSizeThisDim(dims[i]->GetSize());
3102
0
        if (count[i] == 0 || count[i] > nSizeThisDim ||
3103
0
            arrayStartIdx[i] > nSizeThisDim - count[i])
3104
0
        {
3105
0
            CPLError(CE_Failure, CPLE_AppDefined,
3106
0
                     "Inconsistent arrayStartIdx[] / count[] values "
3107
0
                     "regarding array size");
3108
0
            return false;
3109
0
        }
3110
0
        if (chunkSize[i] == 0 || chunkSize[i] > nSizeThisDim ||
3111
0
            chunkSize[i] > std::numeric_limits<size_t>::max() / nTotalChunkSize)
3112
0
        {
3113
0
            CPLError(CE_Failure, CPLE_AppDefined,
3114
0
                     "Inconsistent chunkSize[] values");
3115
0
            return false;
3116
0
        }
3117
0
        nTotalChunkSize *= chunkSize[i];
3118
0
    }
3119
3120
0
    size_t dimIdx = 0;
3121
0
    std::vector<GUInt64> chunkArrayStartIdx(dims.size());
3122
0
    std::vector<size_t> chunkCount(dims.size());
3123
3124
0
    struct Stack
3125
0
    {
3126
0
        GUInt64 nBlockCounter = 0;
3127
0
        GUInt64 nBlocksMinusOne = 0;
3128
0
        size_t first_count = 0;  // only used if nBlocks > 1
3129
0
        Caller return_point = Caller::CALLER_END_OF_LOOP;
3130
0
    };
3131
3132
0
    std::vector<Stack> stack(dims.size());
3133
0
    GUInt64 iCurChunk = 0;
3134
0
    GUInt64 nChunkCount = 1;
3135
0
    for (size_t i = 0; i < dims.size(); i++)
3136
0
    {
3137
0
        const auto nStartBlock = arrayStartIdx[i] / chunkSize[i];
3138
0
        const auto nEndBlock = (arrayStartIdx[i] + count[i] - 1) / chunkSize[i];
3139
0
        stack[i].nBlocksMinusOne = nEndBlock - nStartBlock;
3140
0
        nChunkCount *= 1 + stack[i].nBlocksMinusOne;
3141
0
        if (stack[i].nBlocksMinusOne == 0)
3142
0
        {
3143
0
            chunkArrayStartIdx[i] = arrayStartIdx[i];
3144
0
            chunkCount[i] = static_cast<size_t>(count[i]);
3145
0
        }
3146
0
        else
3147
0
        {
3148
0
            stack[i].first_count = static_cast<size_t>(
3149
0
                (nStartBlock + 1) * chunkSize[i] - arrayStartIdx[i]);
3150
0
        }
3151
0
    }
3152
3153
0
lbl_next_depth:
3154
0
    if (dimIdx == dims.size())
3155
0
    {
3156
0
        ++iCurChunk;
3157
0
        if (!pfnFunc(this, chunkArrayStartIdx.data(), chunkCount.data(),
3158
0
                     iCurChunk, nChunkCount, pUserData))
3159
0
        {
3160
0
            return false;
3161
0
        }
3162
0
    }
3163
0
    else
3164
0
    {
3165
0
        if (stack[dimIdx].nBlocksMinusOne != 0)
3166
0
        {
3167
0
            stack[dimIdx].nBlockCounter = stack[dimIdx].nBlocksMinusOne;
3168
0
            chunkArrayStartIdx[dimIdx] = arrayStartIdx[dimIdx];
3169
0
            chunkCount[dimIdx] = stack[dimIdx].first_count;
3170
0
            stack[dimIdx].return_point = Caller::CALLER_IN_LOOP;
3171
0
            while (true)
3172
0
            {
3173
0
                dimIdx++;
3174
0
                goto lbl_next_depth;
3175
0
            lbl_return_to_caller_in_loop:
3176
0
                --stack[dimIdx].nBlockCounter;
3177
0
                if (stack[dimIdx].nBlockCounter == 0)
3178
0
                    break;
3179
0
                chunkArrayStartIdx[dimIdx] += chunkCount[dimIdx];
3180
0
                chunkCount[dimIdx] = chunkSize[dimIdx];
3181
0
            }
3182
3183
0
            chunkArrayStartIdx[dimIdx] += chunkCount[dimIdx];
3184
0
            chunkCount[dimIdx] =
3185
0
                static_cast<size_t>(arrayStartIdx[dimIdx] + count[dimIdx] -
3186
0
                                    chunkArrayStartIdx[dimIdx]);
3187
0
            stack[dimIdx].return_point = Caller::CALLER_END_OF_LOOP;
3188
0
        }
3189
0
        dimIdx++;
3190
0
        goto lbl_next_depth;
3191
0
    lbl_return_to_caller_end_of_loop:
3192
0
        if (dimIdx == 0)
3193
0
            goto end;
3194
0
    }
3195
3196
0
    assert(dimIdx > 0);
3197
0
    dimIdx--;
3198
    // cppcheck-suppress negativeContainerIndex
3199
0
    switch (stack[dimIdx].return_point)
3200
0
    {
3201
0
        case Caller::CALLER_END_OF_LOOP:
3202
0
            goto lbl_return_to_caller_end_of_loop;
3203
0
        case Caller::CALLER_IN_LOOP:
3204
0
            goto lbl_return_to_caller_in_loop;
3205
0
    }
3206
0
end:
3207
0
    return true;
3208
0
}
3209
3210
/************************************************************************/
3211
/*                          GDALAttribute()                             */
3212
/************************************************************************/
3213
3214
//! @cond Doxygen_Suppress
3215
GDALAttribute::GDALAttribute(CPL_UNUSED const std::string &osParentName,
3216
                             CPL_UNUSED const std::string &osName)
3217
#if !defined(COMPILER_WARNS_ABOUT_ABSTRACT_VBASE_INIT)
3218
    : GDALAbstractMDArray(osParentName, osName)
3219
#endif
3220
0
{
3221
0
}
3222
3223
0
GDALAttribute::~GDALAttribute() = default;
3224
3225
//! @endcond
3226
3227
/************************************************************************/
3228
/*                        GetDimensionSize()                            */
3229
/************************************************************************/
3230
3231
/** Return the size of the dimensions of the attribute.
3232
 *
3233
 * This will be an empty array for a scalar (single value) attribute.
3234
 *
3235
 * This is the same as the C function GDALAttributeGetDimensionsSize().
3236
 */
3237
std::vector<GUInt64> GDALAttribute::GetDimensionsSize() const
3238
0
{
3239
0
    const auto &dims = GetDimensions();
3240
0
    std::vector<GUInt64> ret;
3241
0
    ret.reserve(dims.size());
3242
0
    for (const auto &dim : dims)
3243
0
        ret.push_back(dim->GetSize());
3244
0
    return ret;
3245
0
}
3246
3247
/************************************************************************/
3248
/*                            GDALRawResult()                           */
3249
/************************************************************************/
3250
3251
//! @cond Doxygen_Suppress
3252
GDALRawResult::GDALRawResult(GByte *raw, const GDALExtendedDataType &dt,
3253
                             size_t nEltCount)
3254
0
    : m_dt(dt), m_nEltCount(nEltCount), m_nSize(nEltCount * dt.GetSize()),
3255
0
      m_raw(raw)
3256
0
{
3257
0
}
3258
3259
//! @endcond
3260
3261
/************************************************************************/
3262
/*                            GDALRawResult()                           */
3263
/************************************************************************/
3264
3265
/** Move constructor. */
3266
GDALRawResult::GDALRawResult(GDALRawResult &&other)
3267
0
    : m_dt(std::move(other.m_dt)), m_nEltCount(other.m_nEltCount),
3268
0
      m_nSize(other.m_nSize), m_raw(other.m_raw)
3269
0
{
3270
0
    other.m_nEltCount = 0;
3271
0
    other.m_nSize = 0;
3272
0
    other.m_raw = nullptr;
3273
0
}
3274
3275
/************************************************************************/
3276
/*                               FreeMe()                               */
3277
/************************************************************************/
3278
3279
void GDALRawResult::FreeMe()
3280
0
{
3281
0
    if (m_raw && m_dt.NeedsFreeDynamicMemory())
3282
0
    {
3283
0
        GByte *pabyPtr = m_raw;
3284
0
        const auto nDTSize(m_dt.GetSize());
3285
0
        for (size_t i = 0; i < m_nEltCount; ++i)
3286
0
        {
3287
0
            m_dt.FreeDynamicMemory(pabyPtr);
3288
0
            pabyPtr += nDTSize;
3289
0
        }
3290
0
    }
3291
0
    VSIFree(m_raw);
3292
0
}
3293
3294
/************************************************************************/
3295
/*                             operator=()                              */
3296
/************************************************************************/
3297
3298
/** Move assignment. */
3299
GDALRawResult &GDALRawResult::operator=(GDALRawResult &&other)
3300
0
{
3301
0
    FreeMe();
3302
0
    m_dt = std::move(other.m_dt);
3303
0
    m_nEltCount = other.m_nEltCount;
3304
0
    m_nSize = other.m_nSize;
3305
0
    m_raw = other.m_raw;
3306
0
    other.m_nEltCount = 0;
3307
0
    other.m_nSize = 0;
3308
0
    other.m_raw = nullptr;
3309
0
    return *this;
3310
0
}
3311
3312
/************************************************************************/
3313
/*                         ~GDALRawResult()                             */
3314
/************************************************************************/
3315
3316
/** Destructor. */
3317
GDALRawResult::~GDALRawResult()
3318
0
{
3319
0
    FreeMe();
3320
0
}
3321
3322
/************************************************************************/
3323
/*                            StealData()                               */
3324
/************************************************************************/
3325
3326
//! @cond Doxygen_Suppress
3327
/** Return buffer to caller which becomes owner of it.
3328
 * Only to be used by GDALAttributeReadAsRaw().
3329
 */
3330
GByte *GDALRawResult::StealData()
3331
0
{
3332
0
    GByte *ret = m_raw;
3333
0
    m_raw = nullptr;
3334
0
    m_nEltCount = 0;
3335
0
    m_nSize = 0;
3336
0
    return ret;
3337
0
}
3338
3339
//! @endcond
3340
3341
/************************************************************************/
3342
/*                             ReadAsRaw()                              */
3343
/************************************************************************/
3344
3345
/** Return the raw value of an attribute.
3346
 *
3347
 *
3348
 * This is the same as the C function GDALAttributeReadAsRaw().
3349
 */
3350
GDALRawResult GDALAttribute::ReadAsRaw() const
3351
0
{
3352
0
    const auto nEltCount(GetTotalElementsCount());
3353
0
    const auto &dt(GetDataType());
3354
0
    const auto nDTSize(dt.GetSize());
3355
0
    GByte *res = static_cast<GByte *>(
3356
0
        VSI_MALLOC2_VERBOSE(static_cast<size_t>(nEltCount), nDTSize));
3357
0
    if (!res)
3358
0
        return GDALRawResult(nullptr, dt, 0);
3359
0
    const auto &dims = GetDimensions();
3360
0
    const auto nDims = GetDimensionCount();
3361
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3362
0
    std::vector<size_t> count(1 + nDims);
3363
0
    for (size_t i = 0; i < nDims; i++)
3364
0
    {
3365
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3366
0
    }
3367
0
    if (!Read(startIdx.data(), count.data(), nullptr, nullptr, dt, &res[0],
3368
0
              &res[0], static_cast<size_t>(nEltCount * nDTSize)))
3369
0
    {
3370
0
        VSIFree(res);
3371
0
        return GDALRawResult(nullptr, dt, 0);
3372
0
    }
3373
0
    return GDALRawResult(res, dt, static_cast<size_t>(nEltCount));
3374
0
}
3375
3376
/************************************************************************/
3377
/*                            ReadAsString()                            */
3378
/************************************************************************/
3379
3380
/** Return the value of an attribute as a string.
3381
 *
3382
 * The returned string should not be freed, and its lifetime does not
3383
 * excess a next call to ReadAsString() on the same object, or the deletion
3384
 * of the object itself.
3385
 *
3386
 * This function will only return the first element if there are several.
3387
 *
3388
 * This is the same as the C function GDALAttributeReadAsString()
3389
 *
3390
 * @return a string, or nullptr.
3391
 */
3392
const char *GDALAttribute::ReadAsString() const
3393
0
{
3394
0
    const auto nDims = GetDimensionCount();
3395
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3396
0
    std::vector<size_t> count(1 + nDims, 1);
3397
0
    char *szRet = nullptr;
3398
0
    if (!Read(startIdx.data(), count.data(), nullptr, nullptr,
3399
0
              GDALExtendedDataType::CreateString(), &szRet, &szRet,
3400
0
              sizeof(szRet)) ||
3401
0
        szRet == nullptr)
3402
0
    {
3403
0
        return nullptr;
3404
0
    }
3405
0
    m_osCachedVal = szRet;
3406
0
    CPLFree(szRet);
3407
0
    return m_osCachedVal.c_str();
3408
0
}
3409
3410
/************************************************************************/
3411
/*                            ReadAsInt()                               */
3412
/************************************************************************/
3413
3414
/** Return the value of an attribute as a integer.
3415
 *
3416
 * This function will only return the first element if there are several.
3417
 *
3418
 * It can fail if its value can not be converted to integer.
3419
 *
3420
 * This is the same as the C function GDALAttributeReadAsInt()
3421
 *
3422
 * @return a integer, or INT_MIN in case of error.
3423
 */
3424
int GDALAttribute::ReadAsInt() const
3425
0
{
3426
0
    const auto nDims = GetDimensionCount();
3427
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3428
0
    std::vector<size_t> count(1 + nDims, 1);
3429
0
    int nRet = INT_MIN;
3430
0
    Read(startIdx.data(), count.data(), nullptr, nullptr,
3431
0
         GDALExtendedDataType::Create(GDT_Int32), &nRet, &nRet, sizeof(nRet));
3432
0
    return nRet;
3433
0
}
3434
3435
/************************************************************************/
3436
/*                            ReadAsInt64()                             */
3437
/************************************************************************/
3438
3439
/** Return the value of an attribute as an int64_t.
3440
 *
3441
 * This function will only return the first element if there are several.
3442
 *
3443
 * It can fail if its value can not be converted to long.
3444
 *
3445
 * This is the same as the C function GDALAttributeReadAsInt64()
3446
 *
3447
 * @return an int64_t, or INT64_MIN in case of error.
3448
 */
3449
int64_t GDALAttribute::ReadAsInt64() const
3450
0
{
3451
0
    const auto nDims = GetDimensionCount();
3452
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3453
0
    std::vector<size_t> count(1 + nDims, 1);
3454
0
    int64_t nRet = INT64_MIN;
3455
0
    Read(startIdx.data(), count.data(), nullptr, nullptr,
3456
0
         GDALExtendedDataType::Create(GDT_Int64), &nRet, &nRet, sizeof(nRet));
3457
0
    return nRet;
3458
0
}
3459
3460
/************************************************************************/
3461
/*                            ReadAsDouble()                            */
3462
/************************************************************************/
3463
3464
/** Return the value of an attribute as a double.
3465
 *
3466
 * This function will only return the first element if there are several.
3467
 *
3468
 * It can fail if its value can not be converted to double.
3469
 *
3470
 * This is the same as the C function GDALAttributeReadAsInt()
3471
 *
3472
 * @return a double value.
3473
 */
3474
double GDALAttribute::ReadAsDouble() const
3475
0
{
3476
0
    const auto nDims = GetDimensionCount();
3477
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3478
0
    std::vector<size_t> count(1 + nDims, 1);
3479
0
    double dfRet = 0;
3480
0
    Read(startIdx.data(), count.data(), nullptr, nullptr,
3481
0
         GDALExtendedDataType::Create(GDT_Float64), &dfRet, &dfRet,
3482
0
         sizeof(dfRet));
3483
0
    return dfRet;
3484
0
}
3485
3486
/************************************************************************/
3487
/*                          ReadAsStringArray()                         */
3488
/************************************************************************/
3489
3490
/** Return the value of an attribute as an array of strings.
3491
 *
3492
 * This is the same as the C function GDALAttributeReadAsStringArray()
3493
 */
3494
CPLStringList GDALAttribute::ReadAsStringArray() const
3495
0
{
3496
0
    const auto nElts = GetTotalElementsCount();
3497
0
    if (nElts > static_cast<unsigned>(std::numeric_limits<int>::max() - 1))
3498
0
        return CPLStringList();
3499
0
    char **papszList = static_cast<char **>(
3500
0
        VSI_CALLOC_VERBOSE(static_cast<int>(nElts) + 1, sizeof(char *)));
3501
0
    const auto &dims = GetDimensions();
3502
0
    const auto nDims = GetDimensionCount();
3503
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3504
0
    std::vector<size_t> count(1 + nDims);
3505
0
    for (size_t i = 0; i < nDims; i++)
3506
0
    {
3507
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3508
0
    }
3509
0
    Read(startIdx.data(), count.data(), nullptr, nullptr,
3510
0
         GDALExtendedDataType::CreateString(), papszList, papszList,
3511
0
         sizeof(char *) * static_cast<int>(nElts));
3512
0
    for (int i = 0; i < static_cast<int>(nElts); i++)
3513
0
    {
3514
0
        if (papszList[i] == nullptr)
3515
0
            papszList[i] = CPLStrdup("");
3516
0
    }
3517
0
    return CPLStringList(papszList);
3518
0
}
3519
3520
/************************************************************************/
3521
/*                          ReadAsIntArray()                            */
3522
/************************************************************************/
3523
3524
/** Return the value of an attribute as an array of integers.
3525
 *
3526
 * This is the same as the C function GDALAttributeReadAsIntArray().
3527
 */
3528
std::vector<int> GDALAttribute::ReadAsIntArray() const
3529
0
{
3530
0
    const auto nElts = GetTotalElementsCount();
3531
#if SIZEOF_VOIDP == 4
3532
    if (nElts > static_cast<size_t>(nElts))
3533
        return {};
3534
#endif
3535
0
    std::vector<int> res(static_cast<size_t>(nElts));
3536
0
    const auto &dims = GetDimensions();
3537
0
    const auto nDims = GetDimensionCount();
3538
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3539
0
    std::vector<size_t> count(1 + nDims);
3540
0
    for (size_t i = 0; i < nDims; i++)
3541
0
    {
3542
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3543
0
    }
3544
0
    Read(startIdx.data(), count.data(), nullptr, nullptr,
3545
0
         GDALExtendedDataType::Create(GDT_Int32), &res[0], res.data(),
3546
0
         res.size() * sizeof(res[0]));
3547
0
    return res;
3548
0
}
3549
3550
/************************************************************************/
3551
/*                          ReadAsInt64Array()                          */
3552
/************************************************************************/
3553
3554
/** Return the value of an attribute as an array of int64_t.
3555
 *
3556
 * This is the same as the C function GDALAttributeReadAsInt64Array().
3557
 */
3558
std::vector<int64_t> GDALAttribute::ReadAsInt64Array() const
3559
0
{
3560
0
    const auto nElts = GetTotalElementsCount();
3561
#if SIZEOF_VOIDP == 4
3562
    if (nElts > static_cast<size_t>(nElts))
3563
        return {};
3564
#endif
3565
0
    std::vector<int64_t> res(static_cast<size_t>(nElts));
3566
0
    const auto &dims = GetDimensions();
3567
0
    const auto nDims = GetDimensionCount();
3568
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3569
0
    std::vector<size_t> count(1 + nDims);
3570
0
    for (size_t i = 0; i < nDims; i++)
3571
0
    {
3572
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3573
0
    }
3574
0
    Read(startIdx.data(), count.data(), nullptr, nullptr,
3575
0
         GDALExtendedDataType::Create(GDT_Int64), &res[0], res.data(),
3576
0
         res.size() * sizeof(res[0]));
3577
0
    return res;
3578
0
}
3579
3580
/************************************************************************/
3581
/*                         ReadAsDoubleArray()                          */
3582
/************************************************************************/
3583
3584
/** Return the value of an attribute as an array of double.
3585
 *
3586
 * This is the same as the C function GDALAttributeReadAsDoubleArray().
3587
 */
3588
std::vector<double> GDALAttribute::ReadAsDoubleArray() const
3589
0
{
3590
0
    const auto nElts = GetTotalElementsCount();
3591
#if SIZEOF_VOIDP == 4
3592
    if (nElts > static_cast<size_t>(nElts))
3593
        return {};
3594
#endif
3595
0
    std::vector<double> res(static_cast<size_t>(nElts));
3596
0
    const auto &dims = GetDimensions();
3597
0
    const auto nDims = GetDimensionCount();
3598
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3599
0
    std::vector<size_t> count(1 + nDims);
3600
0
    for (size_t i = 0; i < nDims; i++)
3601
0
    {
3602
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3603
0
    }
3604
0
    Read(startIdx.data(), count.data(), nullptr, nullptr,
3605
0
         GDALExtendedDataType::Create(GDT_Float64), &res[0], res.data(),
3606
0
         res.size() * sizeof(res[0]));
3607
0
    return res;
3608
0
}
3609
3610
/************************************************************************/
3611
/*                               Write()                                */
3612
/************************************************************************/
3613
3614
/** Write an attribute from raw values expressed in GetDataType()
3615
 *
3616
 * The values should be provided in the type of GetDataType() and there should
3617
 * be exactly GetTotalElementsCount() of them.
3618
 * If GetDataType() is a string, each value should be a char* pointer.
3619
 *
3620
 * This is the same as the C function GDALAttributeWriteRaw().
3621
 *
3622
 * @param pabyValue Buffer of nLen bytes.
3623
 * @param nLen Size of pabyValue in bytes. Should be equal to
3624
 *             GetTotalElementsCount() * GetDataType().GetSize()
3625
 * @return true in case of success.
3626
 */
3627
bool GDALAttribute::Write(const void *pabyValue, size_t nLen)
3628
0
{
3629
0
    if (nLen != GetTotalElementsCount() * GetDataType().GetSize())
3630
0
    {
3631
0
        CPLError(CE_Failure, CPLE_AppDefined,
3632
0
                 "Length is not of expected value");
3633
0
        return false;
3634
0
    }
3635
0
    const auto &dims = GetDimensions();
3636
0
    const auto nDims = GetDimensionCount();
3637
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3638
0
    std::vector<size_t> count(1 + nDims);
3639
0
    for (size_t i = 0; i < nDims; i++)
3640
0
    {
3641
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3642
0
    }
3643
0
    return Write(startIdx.data(), count.data(), nullptr, nullptr, GetDataType(),
3644
0
                 pabyValue, pabyValue, nLen);
3645
0
}
3646
3647
/************************************************************************/
3648
/*                               Write()                                */
3649
/************************************************************************/
3650
3651
/** Write an attribute from a string value.
3652
 *
3653
 * Type conversion will be performed if needed. If the attribute contains
3654
 * multiple values, only the first one will be updated.
3655
 *
3656
 * This is the same as the C function GDALAttributeWriteString().
3657
 *
3658
 * @param pszValue Pointer to a string.
3659
 * @return true in case of success.
3660
 */
3661
bool GDALAttribute::Write(const char *pszValue)
3662
0
{
3663
0
    const auto nDims = GetDimensionCount();
3664
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3665
0
    std::vector<size_t> count(1 + nDims, 1);
3666
0
    return Write(startIdx.data(), count.data(), nullptr, nullptr,
3667
0
                 GDALExtendedDataType::CreateString(), &pszValue, &pszValue,
3668
0
                 sizeof(pszValue));
3669
0
}
3670
3671
/************************************************************************/
3672
/*                              WriteInt()                              */
3673
/************************************************************************/
3674
3675
/** Write an attribute from a integer value.
3676
 *
3677
 * Type conversion will be performed if needed. If the attribute contains
3678
 * multiple values, only the first one will be updated.
3679
 *
3680
 * This is the same as the C function GDALAttributeWriteInt().
3681
 *
3682
 * @param nVal Value.
3683
 * @return true in case of success.
3684
 */
3685
bool GDALAttribute::WriteInt(int nVal)
3686
0
{
3687
0
    const auto nDims = GetDimensionCount();
3688
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3689
0
    std::vector<size_t> count(1 + nDims, 1);
3690
0
    return Write(startIdx.data(), count.data(), nullptr, nullptr,
3691
0
                 GDALExtendedDataType::Create(GDT_Int32), &nVal, &nVal,
3692
0
                 sizeof(nVal));
3693
0
}
3694
3695
/************************************************************************/
3696
/*                              WriteInt64()                             */
3697
/************************************************************************/
3698
3699
/** Write an attribute from an int64_t value.
3700
 *
3701
 * Type conversion will be performed if needed. If the attribute contains
3702
 * multiple values, only the first one will be updated.
3703
 *
3704
 * This is the same as the C function GDALAttributeWriteInt().
3705
 *
3706
 * @param nVal Value.
3707
 * @return true in case of success.
3708
 */
3709
bool GDALAttribute::WriteInt64(int64_t nVal)
3710
0
{
3711
0
    const auto nDims = GetDimensionCount();
3712
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3713
0
    std::vector<size_t> count(1 + nDims, 1);
3714
0
    return Write(startIdx.data(), count.data(), nullptr, nullptr,
3715
0
                 GDALExtendedDataType::Create(GDT_Int64), &nVal, &nVal,
3716
0
                 sizeof(nVal));
3717
0
}
3718
3719
/************************************************************************/
3720
/*                                Write()                               */
3721
/************************************************************************/
3722
3723
/** Write an attribute from a double value.
3724
 *
3725
 * Type conversion will be performed if needed. If the attribute contains
3726
 * multiple values, only the first one will be updated.
3727
 *
3728
 * This is the same as the C function GDALAttributeWriteDouble().
3729
 *
3730
 * @param dfVal Value.
3731
 * @return true in case of success.
3732
 */
3733
bool GDALAttribute::Write(double dfVal)
3734
0
{
3735
0
    const auto nDims = GetDimensionCount();
3736
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3737
0
    std::vector<size_t> count(1 + nDims, 1);
3738
0
    return Write(startIdx.data(), count.data(), nullptr, nullptr,
3739
0
                 GDALExtendedDataType::Create(GDT_Float64), &dfVal, &dfVal,
3740
0
                 sizeof(dfVal));
3741
0
}
3742
3743
/************************************************************************/
3744
/*                                Write()                               */
3745
/************************************************************************/
3746
3747
/** Write an attribute from an array of strings.
3748
 *
3749
 * Type conversion will be performed if needed.
3750
 *
3751
 * Exactly GetTotalElementsCount() strings must be provided
3752
 *
3753
 * This is the same as the C function GDALAttributeWriteStringArray().
3754
 *
3755
 * @param vals Array of strings.
3756
 * @return true in case of success.
3757
 */
3758
bool GDALAttribute::Write(CSLConstList vals)
3759
0
{
3760
0
    if (static_cast<size_t>(CSLCount(vals)) != GetTotalElementsCount())
3761
0
    {
3762
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid number of input values");
3763
0
        return false;
3764
0
    }
3765
0
    const auto nDims = GetDimensionCount();
3766
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3767
0
    std::vector<size_t> count(1 + nDims);
3768
0
    const auto &dims = GetDimensions();
3769
0
    for (size_t i = 0; i < nDims; i++)
3770
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3771
0
    return Write(startIdx.data(), count.data(), nullptr, nullptr,
3772
0
                 GDALExtendedDataType::CreateString(), vals, vals,
3773
0
                 static_cast<size_t>(GetTotalElementsCount()) * sizeof(char *));
3774
0
}
3775
3776
/************************************************************************/
3777
/*                                Write()                               */
3778
/************************************************************************/
3779
3780
/** Write an attribute from an array of int.
3781
 *
3782
 * Type conversion will be performed if needed.
3783
 *
3784
 * Exactly GetTotalElementsCount() strings must be provided
3785
 *
3786
 * This is the same as the C function GDALAttributeWriteIntArray()
3787
 *
3788
 * @param vals Array of int.
3789
 * @param nVals Should be equal to GetTotalElementsCount().
3790
 * @return true in case of success.
3791
 */
3792
bool GDALAttribute::Write(const int *vals, size_t nVals)
3793
0
{
3794
0
    if (nVals != GetTotalElementsCount())
3795
0
    {
3796
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid number of input values");
3797
0
        return false;
3798
0
    }
3799
0
    const auto nDims = GetDimensionCount();
3800
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3801
0
    std::vector<size_t> count(1 + nDims);
3802
0
    const auto &dims = GetDimensions();
3803
0
    for (size_t i = 0; i < nDims; i++)
3804
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3805
0
    return Write(startIdx.data(), count.data(), nullptr, nullptr,
3806
0
                 GDALExtendedDataType::Create(GDT_Int32), vals, vals,
3807
0
                 static_cast<size_t>(GetTotalElementsCount()) * sizeof(GInt32));
3808
0
}
3809
3810
/************************************************************************/
3811
/*                                Write()                               */
3812
/************************************************************************/
3813
3814
/** Write an attribute from an array of int64_t.
3815
 *
3816
 * Type conversion will be performed if needed.
3817
 *
3818
 * Exactly GetTotalElementsCount() strings must be provided
3819
 *
3820
 * This is the same as the C function GDALAttributeWriteLongArray()
3821
 *
3822
 * @param vals Array of int64_t.
3823
 * @param nVals Should be equal to GetTotalElementsCount().
3824
 * @return true in case of success.
3825
 */
3826
bool GDALAttribute::Write(const int64_t *vals, size_t nVals)
3827
0
{
3828
0
    if (nVals != GetTotalElementsCount())
3829
0
    {
3830
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid number of input values");
3831
0
        return false;
3832
0
    }
3833
0
    const auto nDims = GetDimensionCount();
3834
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3835
0
    std::vector<size_t> count(1 + nDims);
3836
0
    const auto &dims = GetDimensions();
3837
0
    for (size_t i = 0; i < nDims; i++)
3838
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3839
0
    return Write(startIdx.data(), count.data(), nullptr, nullptr,
3840
0
                 GDALExtendedDataType::Create(GDT_Int64), vals, vals,
3841
0
                 static_cast<size_t>(GetTotalElementsCount()) *
3842
0
                     sizeof(int64_t));
3843
0
}
3844
3845
/************************************************************************/
3846
/*                                Write()                               */
3847
/************************************************************************/
3848
3849
/** Write an attribute from an array of double.
3850
 *
3851
 * Type conversion will be performed if needed.
3852
 *
3853
 * Exactly GetTotalElementsCount() strings must be provided
3854
 *
3855
 * This is the same as the C function GDALAttributeWriteDoubleArray()
3856
 *
3857
 * @param vals Array of double.
3858
 * @param nVals Should be equal to GetTotalElementsCount().
3859
 * @return true in case of success.
3860
 */
3861
bool GDALAttribute::Write(const double *vals, size_t nVals)
3862
0
{
3863
0
    if (nVals != GetTotalElementsCount())
3864
0
    {
3865
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid number of input values");
3866
0
        return false;
3867
0
    }
3868
0
    const auto nDims = GetDimensionCount();
3869
0
    std::vector<GUInt64> startIdx(1 + nDims, 0);
3870
0
    std::vector<size_t> count(1 + nDims);
3871
0
    const auto &dims = GetDimensions();
3872
0
    for (size_t i = 0; i < nDims; i++)
3873
0
        count[i] = static_cast<size_t>(dims[i]->GetSize());
3874
0
    return Write(startIdx.data(), count.data(), nullptr, nullptr,
3875
0
                 GDALExtendedDataType::Create(GDT_Float64), vals, vals,
3876
0
                 static_cast<size_t>(GetTotalElementsCount()) * sizeof(double));
3877
0
}
3878
3879
/************************************************************************/
3880
/*                           GDALMDArray()                              */
3881
/************************************************************************/
3882
3883
//! @cond Doxygen_Suppress
3884
GDALMDArray::GDALMDArray(CPL_UNUSED const std::string &osParentName,
3885
                         CPL_UNUSED const std::string &osName,
3886
                         const std::string &osContext)
3887
    :
3888
#if !defined(COMPILER_WARNS_ABOUT_ABSTRACT_VBASE_INIT)
3889
      GDALAbstractMDArray(osParentName, osName),
3890
#endif
3891
0
      m_osContext(osContext)
3892
0
{
3893
0
}
3894
3895
//! @endcond
3896
3897
/************************************************************************/
3898
/*                           GetTotalCopyCost()                         */
3899
/************************************************************************/
3900
3901
/** Return a total "cost" to copy the array.
3902
 *
3903
 * Used as a parameter for CopyFrom()
3904
 */
3905
GUInt64 GDALMDArray::GetTotalCopyCost() const
3906
0
{
3907
0
    return COPY_COST + GetAttributes().size() * GDALAttribute::COPY_COST +
3908
0
           GetTotalElementsCount() * GetDataType().GetSize();
3909
0
}
3910
3911
/************************************************************************/
3912
/*                       CopyFromAllExceptValues()                      */
3913
/************************************************************************/
3914
3915
//! @cond Doxygen_Suppress
3916
3917
bool GDALMDArray::CopyFromAllExceptValues(const GDALMDArray *poSrcArray,
3918
                                          bool bStrict, GUInt64 &nCurCost,
3919
                                          const GUInt64 nTotalCost,
3920
                                          GDALProgressFunc pfnProgress,
3921
                                          void *pProgressData)
3922
0
{
3923
    // Nodata setting must be one of the first things done for TileDB
3924
0
    const void *pNoData = poSrcArray->GetRawNoDataValue();
3925
0
    if (pNoData && poSrcArray->GetDataType() == GetDataType())
3926
0
    {
3927
0
        SetRawNoDataValue(pNoData);
3928
0
    }
3929
3930
0
    const bool bThisIsUnscaledArray =
3931
0
        dynamic_cast<GDALMDArrayUnscaled *>(this) != nullptr;
3932
0
    auto attrs = poSrcArray->GetAttributes();
3933
0
    for (const auto &attr : attrs)
3934
0
    {
3935
0
        const auto &osAttrName = attr->GetName();
3936
0
        if (bThisIsUnscaledArray)
3937
0
        {
3938
0
            if (osAttrName == "missing_value" || osAttrName == "_FillValue" ||
3939
0
                osAttrName == "valid_min" || osAttrName == "valid_max" ||
3940
0
                osAttrName == "valid_range")
3941
0
            {
3942
0
                continue;
3943
0
            }
3944
0
        }
3945
3946
0
        auto dstAttr = CreateAttribute(osAttrName, attr->GetDimensionsSize(),
3947
0
                                       attr->GetDataType());
3948
0
        if (!dstAttr)
3949
0
        {
3950
0
            if (bStrict)
3951
0
                return false;
3952
0
            continue;
3953
0
        }
3954
0
        auto raw = attr->ReadAsRaw();
3955
0
        if (!dstAttr->Write(raw.data(), raw.size()) && bStrict)
3956
0
            return false;
3957
0
    }
3958
0
    if (!attrs.empty())
3959
0
    {
3960
0
        nCurCost += attrs.size() * GDALAttribute::COPY_COST;
3961
0
        if (pfnProgress &&
3962
0
            !pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData))
3963
0
            return false;
3964
0
    }
3965
3966
0
    auto srcSRS = poSrcArray->GetSpatialRef();
3967
0
    if (srcSRS)
3968
0
    {
3969
0
        SetSpatialRef(srcSRS.get());
3970
0
    }
3971
3972
0
    const std::string &osUnit(poSrcArray->GetUnit());
3973
0
    if (!osUnit.empty())
3974
0
    {
3975
0
        SetUnit(osUnit);
3976
0
    }
3977
3978
0
    bool bGotValue = false;
3979
0
    GDALDataType eOffsetStorageType = GDT_Unknown;
3980
0
    const double dfOffset =
3981
0
        poSrcArray->GetOffset(&bGotValue, &eOffsetStorageType);
3982
0
    if (bGotValue)
3983
0
    {
3984
0
        SetOffset(dfOffset, eOffsetStorageType);
3985
0
    }
3986
3987
0
    bGotValue = false;
3988
0
    GDALDataType eScaleStorageType = GDT_Unknown;
3989
0
    const double dfScale = poSrcArray->GetScale(&bGotValue, &eScaleStorageType);
3990
0
    if (bGotValue)
3991
0
    {
3992
0
        SetScale(dfScale, eScaleStorageType);
3993
0
    }
3994
3995
0
    return true;
3996
0
}
3997
3998
//! @endcond
3999
4000
/************************************************************************/
4001
/*                               CopyFrom()                             */
4002
/************************************************************************/
4003
4004
/** Copy the content of an array into a new (generally empty) array.
4005
 *
4006
 * @param poSrcDS    Source dataset. Might be nullptr (but for correct behavior
4007
 *                   of some output drivers this is not recommended)
4008
 * @param poSrcArray Source array. Should NOT be nullptr.
4009
 * @param bStrict Whether to enable strict mode. In strict mode, any error will
4010
 *                stop the copy. In relaxed mode, the copy will be attempted to
4011
 *                be pursued.
4012
 * @param nCurCost  Should be provided as a variable initially set to 0.
4013
 * @param nTotalCost Total cost from GetTotalCopyCost().
4014
 * @param pfnProgress Progress callback, or nullptr.
4015
 * @param pProgressData Progress user data, or nulptr.
4016
 *
4017
 * @return true in case of success (or partial success if bStrict == false).
4018
 */
4019
bool GDALMDArray::CopyFrom(CPL_UNUSED GDALDataset *poSrcDS,
4020
                           const GDALMDArray *poSrcArray, bool bStrict,
4021
                           GUInt64 &nCurCost, const GUInt64 nTotalCost,
4022
                           GDALProgressFunc pfnProgress, void *pProgressData)
4023
0
{
4024
0
    if (pfnProgress == nullptr)
4025
0
        pfnProgress = GDALDummyProgress;
4026
4027
0
    nCurCost += GDALMDArray::COPY_COST;
4028
4029
0
    if (!CopyFromAllExceptValues(poSrcArray, bStrict, nCurCost, nTotalCost,
4030
0
                                 pfnProgress, pProgressData))
4031
0
    {
4032
0
        return false;
4033
0
    }
4034
4035
0
    const auto &dims = poSrcArray->GetDimensions();
4036
0
    const auto nDTSize = poSrcArray->GetDataType().GetSize();
4037
0
    if (dims.empty())
4038
0
    {
4039
0
        std::vector<GByte> abyTmp(nDTSize);
4040
0
        if (!(poSrcArray->Read(nullptr, nullptr, nullptr, nullptr,
4041
0
                               GetDataType(), &abyTmp[0]) &&
4042
0
              Write(nullptr, nullptr, nullptr, nullptr, GetDataType(),
4043
0
                    &abyTmp[0])) &&
4044
0
            bStrict)
4045
0
        {
4046
0
            return false;
4047
0
        }
4048
0
        nCurCost += GetTotalElementsCount() * GetDataType().GetSize();
4049
0
        if (!pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData))
4050
0
            return false;
4051
0
    }
4052
0
    else
4053
0
    {
4054
0
        std::vector<GUInt64> arrayStartIdx(dims.size());
4055
0
        std::vector<GUInt64> count(dims.size());
4056
0
        for (size_t i = 0; i < dims.size(); i++)
4057
0
        {
4058
0
            count[i] = static_cast<size_t>(dims[i]->GetSize());
4059
0
        }
4060
4061
0
        struct CopyFunc
4062
0
        {
4063
0
            GDALMDArray *poDstArray = nullptr;
4064
0
            std::vector<GByte> abyTmp{};
4065
0
            GDALProgressFunc pfnProgress = nullptr;
4066
0
            void *pProgressData = nullptr;
4067
0
            GUInt64 nCurCost = 0;
4068
0
            GUInt64 nTotalCost = 0;
4069
0
            GUInt64 nTotalBytesThisArray = 0;
4070
0
            bool bStop = false;
4071
4072
0
            static bool f(GDALAbstractMDArray *l_poSrcArray,
4073
0
                          const GUInt64 *chunkArrayStartIdx,
4074
0
                          const size_t *chunkCount, GUInt64 iCurChunk,
4075
0
                          GUInt64 nChunkCount, void *pUserData)
4076
0
            {
4077
0
                const auto &dt(l_poSrcArray->GetDataType());
4078
0
                auto data = static_cast<CopyFunc *>(pUserData);
4079
0
                auto poDstArray = data->poDstArray;
4080
0
                if (!l_poSrcArray->Read(chunkArrayStartIdx, chunkCount, nullptr,
4081
0
                                        nullptr, dt, &data->abyTmp[0]))
4082
0
                {
4083
0
                    return false;
4084
0
                }
4085
0
                bool bRet =
4086
0
                    poDstArray->Write(chunkArrayStartIdx, chunkCount, nullptr,
4087
0
                                      nullptr, dt, &data->abyTmp[0]);
4088
0
                if (dt.NeedsFreeDynamicMemory())
4089
0
                {
4090
0
                    const auto l_nDTSize = dt.GetSize();
4091
0
                    GByte *ptr = &data->abyTmp[0];
4092
0
                    const size_t l_nDims(l_poSrcArray->GetDimensionCount());
4093
0
                    size_t nEltCount = 1;
4094
0
                    for (size_t i = 0; i < l_nDims; ++i)
4095
0
                    {
4096
0
                        nEltCount *= chunkCount[i];
4097
0
                    }
4098
0
                    for (size_t i = 0; i < nEltCount; i++)
4099
0
                    {
4100
0
                        dt.FreeDynamicMemory(ptr);
4101
0
                        ptr += l_nDTSize;
4102
0
                    }
4103
0
                }
4104
0
                if (!bRet)
4105
0
                {
4106
0
                    return false;
4107
0
                }
4108
4109
0
                double dfCurCost =
4110
0
                    double(data->nCurCost) + double(iCurChunk) / nChunkCount *
4111
0
                                                 data->nTotalBytesThisArray;
4112
0
                if (!data->pfnProgress(dfCurCost / data->nTotalCost, "",
4113
0
                                       data->pProgressData))
4114
0
                {
4115
0
                    data->bStop = true;
4116
0
                    return false;
4117
0
                }
4118
4119
0
                return true;
4120
0
            }
4121
0
        };
4122
4123
0
        CopyFunc copyFunc;
4124
0
        copyFunc.poDstArray = this;
4125
0
        copyFunc.nCurCost = nCurCost;
4126
0
        copyFunc.nTotalCost = nTotalCost;
4127
0
        copyFunc.nTotalBytesThisArray = GetTotalElementsCount() * nDTSize;
4128
0
        copyFunc.pfnProgress = pfnProgress;
4129
0
        copyFunc.pProgressData = pProgressData;
4130
0
        const char *pszSwathSize =
4131
0
            CPLGetConfigOption("GDAL_SWATH_SIZE", nullptr);
4132
0
        const size_t nMaxChunkSize =
4133
0
            pszSwathSize
4134
0
                ? static_cast<size_t>(
4135
0
                      std::min(GIntBig(std::numeric_limits<size_t>::max() / 2),
4136
0
                               CPLAtoGIntBig(pszSwathSize)))
4137
0
                : static_cast<size_t>(
4138
0
                      std::min(GIntBig(std::numeric_limits<size_t>::max() / 2),
4139
0
                               GDALGetCacheMax64() / 4));
4140
0
        const auto anChunkSizes(GetProcessingChunkSize(nMaxChunkSize));
4141
0
        size_t nRealChunkSize = nDTSize;
4142
0
        for (const auto &nChunkSize : anChunkSizes)
4143
0
        {
4144
0
            nRealChunkSize *= nChunkSize;
4145
0
        }
4146
0
        try
4147
0
        {
4148
0
            copyFunc.abyTmp.resize(nRealChunkSize);
4149
0
        }
4150
0
        catch (const std::exception &)
4151
0
        {
4152
0
            CPLError(CE_Failure, CPLE_OutOfMemory,
4153
0
                     "Cannot allocate temporary buffer");
4154
0
            nCurCost += copyFunc.nTotalBytesThisArray;
4155
0
            return false;
4156
0
        }
4157
0
        if (copyFunc.nTotalBytesThisArray != 0 &&
4158
0
            !const_cast<GDALMDArray *>(poSrcArray)
4159
0
                 ->ProcessPerChunk(arrayStartIdx.data(), count.data(),
4160
0
                                   anChunkSizes.data(), CopyFunc::f,
4161
0
                                   &copyFunc) &&
4162
0
            (bStrict || copyFunc.bStop))
4163
0
        {
4164
0
            nCurCost += copyFunc.nTotalBytesThisArray;
4165
0
            return false;
4166
0
        }
4167
0
        nCurCost += copyFunc.nTotalBytesThisArray;
4168
0
    }
4169
4170
0
    return true;
4171
0
}
4172
4173
/************************************************************************/
4174
/*                         GetStructuralInfo()                          */
4175
/************************************************************************/
4176
4177
/** Return structural information on the array.
4178
 *
4179
 * This may be the compression, etc..
4180
 *
4181
 * The return value should not be freed and is valid until GDALMDArray is
4182
 * released or this function called again.
4183
 *
4184
 * This is the same as the C function GDALMDArrayGetStructuralInfo().
4185
 */
4186
CSLConstList GDALMDArray::GetStructuralInfo() const
4187
0
{
4188
0
    return nullptr;
4189
0
}
4190
4191
/************************************************************************/
4192
/*                          AdviseRead()                                */
4193
/************************************************************************/
4194
4195
/** Advise driver of upcoming read requests.
4196
 *
4197
 * Some GDAL drivers operate more efficiently if they know in advance what
4198
 * set of upcoming read requests will be made.  The AdviseRead() method allows
4199
 * an application to notify the driver of the region of interest.
4200
 *
4201
 * Many drivers just ignore the AdviseRead() call, but it can dramatically
4202
 * accelerate access via some drivers. One such case is when reading through
4203
 * a DAP dataset with the netCDF driver (a in-memory cache array is then created
4204
 * with the region of interest defined by AdviseRead())
4205
 *
4206
 * This is the same as the C function GDALMDArrayAdviseRead().
4207
 *
4208
 * @param arrayStartIdx Values representing the starting index to read
4209
 *                      in each dimension (in [0, aoDims[i].GetSize()-1] range).
4210
 *                      Array of GetDimensionCount() values.
4211
 *                      Can be nullptr as a synonymous for [0 for i in
4212
 * range(GetDimensionCount() ]
4213
 *
4214
 * @param count         Values representing the number of values to extract in
4215
 *                      each dimension.
4216
 *                      Array of GetDimensionCount() values.
4217
 *                      Can be nullptr as a synonymous for
4218
 *                      [ aoDims[i].GetSize() - arrayStartIdx[i] for i in
4219
 * range(GetDimensionCount() ]
4220
 *
4221
 * @param papszOptions Driver specific options, or nullptr. Consult driver
4222
 * documentation.
4223
 *
4224
 * @return true in case of success (ignoring the advice is a success)
4225
 *
4226
 * @since GDAL 3.2
4227
 */
4228
bool GDALMDArray::AdviseRead(const GUInt64 *arrayStartIdx, const size_t *count,
4229
                             CSLConstList papszOptions) const
4230
0
{
4231
0
    const auto nDimCount = GetDimensionCount();
4232
0
    if (nDimCount == 0)
4233
0
        return true;
4234
4235
0
    std::vector<GUInt64> tmp_arrayStartIdx;
4236
0
    if (arrayStartIdx == nullptr)
4237
0
    {
4238
0
        tmp_arrayStartIdx.resize(nDimCount);
4239
0
        arrayStartIdx = tmp_arrayStartIdx.data();
4240
0
    }
4241
4242
0
    std::vector<size_t> tmp_count;
4243
0
    if (count == nullptr)
4244
0
    {
4245
0
        tmp_count.resize(nDimCount);
4246
0
        const auto &dims = GetDimensions();
4247
0
        for (size_t i = 0; i < nDimCount; i++)
4248
0
        {
4249
0
            const GUInt64 nSize = dims[i]->GetSize() - arrayStartIdx[i];
4250
#if SIZEOF_VOIDP < 8
4251
            if (nSize != static_cast<size_t>(nSize))
4252
            {
4253
                CPLError(CE_Failure, CPLE_AppDefined, "Integer overflow");
4254
                return false;
4255
            }
4256
#endif
4257
0
            tmp_count[i] = static_cast<size_t>(nSize);
4258
0
        }
4259
0
        count = tmp_count.data();
4260
0
    }
4261
4262
0
    std::vector<GInt64> tmp_arrayStep;
4263
0
    std::vector<GPtrDiff_t> tmp_bufferStride;
4264
0
    const GInt64 *arrayStep = nullptr;
4265
0
    const GPtrDiff_t *bufferStride = nullptr;
4266
0
    if (!CheckReadWriteParams(arrayStartIdx, count, arrayStep, bufferStride,
4267
0
                              GDALExtendedDataType::Create(GDT_Unknown),
4268
0
                              nullptr, nullptr, 0, tmp_arrayStep,
4269
0
                              tmp_bufferStride))
4270
0
    {
4271
0
        return false;
4272
0
    }
4273
4274
0
    return IAdviseRead(arrayStartIdx, count, papszOptions);
4275
0
}
4276
4277
/************************************************************************/
4278
/*                             IAdviseRead()                            */
4279
/************************************************************************/
4280
4281
//! @cond Doxygen_Suppress
4282
bool GDALMDArray::IAdviseRead(const GUInt64 *, const size_t *,
4283
                              CSLConstList /* papszOptions*/) const
4284
0
{
4285
0
    return true;
4286
0
}
4287
4288
//! @endcond
4289
4290
/************************************************************************/
4291
/*                            MassageName()                             */
4292
/************************************************************************/
4293
4294
//! @cond Doxygen_Suppress
4295
/*static*/ std::string GDALMDArray::MassageName(const std::string &inputName)
4296
0
{
4297
0
    std::string ret;
4298
0
    for (const char ch : inputName)
4299
0
    {
4300
0
        if (!isalnum(static_cast<unsigned char>(ch)))
4301
0
            ret += '_';
4302
0
        else
4303
0
            ret += ch;
4304
0
    }
4305
0
    return ret;
4306
0
}
4307
4308
//! @endcond
4309
4310
/************************************************************************/
4311
/*                         GetCacheRootGroup()                          */
4312
/************************************************************************/
4313
4314
//! @cond Doxygen_Suppress
4315
std::shared_ptr<GDALGroup>
4316
GDALMDArray::GetCacheRootGroup(bool bCanCreate,
4317
                               std::string &osCacheFilenameOut) const
4318
0
{
4319
0
    const auto &osFilename = GetFilename();
4320
0
    if (osFilename.empty())
4321
0
    {
4322
0
        CPLError(CE_Failure, CPLE_AppDefined,
4323
0
                 "Cannot cache an array with an empty filename");
4324
0
        return nullptr;
4325
0
    }
4326
4327
0
    osCacheFilenameOut = osFilename + ".gmac";
4328
0
    if (STARTS_WITH(osFilename.c_str(), "/vsicurl/http"))
4329
0
    {
4330
0
        const auto nPosQuestionMark = osFilename.find('?');
4331
0
        if (nPosQuestionMark != std::string::npos)
4332
0
        {
4333
0
            osCacheFilenameOut =
4334
0
                osFilename.substr(0, nPosQuestionMark)
4335
0
                    .append(".gmac")
4336
0
                    .append(osFilename.substr(nPosQuestionMark));
4337
0
        }
4338
0
    }
4339
0
    const char *pszProxy = PamGetProxy(osCacheFilenameOut.c_str());
4340
0
    if (pszProxy != nullptr)
4341
0
        osCacheFilenameOut = pszProxy;
4342
4343
0
    std::unique_ptr<GDALDataset> poDS;
4344
0
    VSIStatBufL sStat;
4345
0
    if (VSIStatL(osCacheFilenameOut.c_str(), &sStat) == 0)
4346
0
    {
4347
0
        poDS.reset(GDALDataset::Open(osCacheFilenameOut.c_str(),
4348
0
                                     GDAL_OF_MULTIDIM_RASTER | GDAL_OF_UPDATE,
4349
0
                                     nullptr, nullptr, nullptr));
4350
0
    }
4351
0
    if (poDS)
4352
0
    {
4353
0
        CPLDebug("GDAL", "Opening cache %s", osCacheFilenameOut.c_str());
4354
0
        return poDS->GetRootGroup();
4355
0
    }
4356
4357
0
    if (bCanCreate)
4358
0
    {
4359
0
        const char *pszDrvName = "netCDF";
4360
0
        GDALDriver *poDrv = GetGDALDriverManager()->GetDriverByName(pszDrvName);
4361
0
        if (poDrv == nullptr)
4362
0
        {
4363
0
            CPLError(CE_Failure, CPLE_AppDefined, "Cannot get driver %s",
4364
0
                     pszDrvName);
4365
0
            return nullptr;
4366
0
        }
4367
0
        {
4368
0
            CPLErrorHandlerPusher oHandlerPusher(CPLQuietErrorHandler);
4369
0
            CPLErrorStateBackuper oErrorStateBackuper;
4370
0
            poDS.reset(poDrv->CreateMultiDimensional(osCacheFilenameOut.c_str(),
4371
0
                                                     nullptr, nullptr));
4372
0
        }
4373
0
        if (!poDS)
4374
0
        {
4375
0
            pszProxy = PamAllocateProxy(osCacheFilenameOut.c_str());
4376
0
            if (pszProxy)
4377
0
            {
4378
0
                osCacheFilenameOut = pszProxy;
4379
0
                poDS.reset(poDrv->CreateMultiDimensional(
4380
0
                    osCacheFilenameOut.c_str(), nullptr, nullptr));
4381
0
            }
4382
0
        }
4383
0
        if (poDS)
4384
0
        {
4385
0
            CPLDebug("GDAL", "Creating cache %s", osCacheFilenameOut.c_str());
4386
0
            return poDS->GetRootGroup();
4387
0
        }
4388
0
        else
4389
0
        {
4390
0
            CPLError(CE_Failure, CPLE_AppDefined,
4391
0
                     "Cannot create %s. Set the GDAL_PAM_PROXY_DIR "
4392
0
                     "configuration option to write the cache in "
4393
0
                     "another directory",
4394
0
                     osCacheFilenameOut.c_str());
4395
0
        }
4396
0
    }
4397
4398
0
    return nullptr;
4399
0
}
4400
4401
//! @endcond
4402
4403
/************************************************************************/
4404
/*                              Cache()                                 */
4405
/************************************************************************/
4406
4407
/** Cache the content of the array into an auxiliary filename.
4408
 *
4409
 * The main purpose of this method is to be able to cache views that are
4410
 * expensive to compute, such as transposed arrays.
4411
 *
4412
 * The array will be stored in a file whose name is the one of
4413
 * GetFilename(), with an extra .gmac extension (stands for GDAL
4414
 * Multidimensional Array Cache). The cache is a netCDF dataset.
4415
 *
4416
 * If the .gmac file cannot be written next to the dataset, the
4417
 * GDAL_PAM_PROXY_DIR will be used, if set, to write the cache file into that
4418
 * directory.
4419
 *
4420
 * The GDALMDArray::Read() method will automatically use the cache when it
4421
 * exists. There is no timestamp checks between the source array and the cached
4422
 * array. If the source arrays changes, the cache must be manually deleted.
4423
 *
4424
 * This is the same as the C function GDALMDArrayCache()
4425
 *
4426
 * @note Driver implementation: optionally implemented.
4427
 *
4428
 * @param papszOptions List of options, null terminated, or NULL. Currently
4429
 *                     the only option supported is BLOCKSIZE=bs0,bs1,...,bsN
4430
 *                     to specify the block size of the cached array.
4431
 * @return true in case of success.
4432
 */
4433
bool GDALMDArray::Cache(CSLConstList papszOptions) const
4434
0
{
4435
0
    std::string osCacheFilename;
4436
0
    auto poRG = GetCacheRootGroup(true, osCacheFilename);
4437
0
    if (!poRG)
4438
0
        return false;
4439
4440
0
    const std::string osCachedArrayName(MassageName(GetFullName()));
4441
0
    if (poRG->OpenMDArray(osCachedArrayName))
4442
0
    {
4443
0
        CPLError(CE_Failure, CPLE_NotSupported,
4444
0
                 "An array with same name %s already exists in %s",
4445
0
                 osCachedArrayName.c_str(), osCacheFilename.c_str());
4446
0
        return false;
4447
0
    }
4448
4449
0
    CPLStringList aosOptions;
4450
0
    aosOptions.SetNameValue("COMPRESS", "DEFLATE");
4451
0
    const auto &aoDims = GetDimensions();
4452
0
    std::vector<std::shared_ptr<GDALDimension>> aoNewDims;
4453
0
    if (!aoDims.empty())
4454
0
    {
4455
0
        std::string osBlockSize(
4456
0
            CSLFetchNameValueDef(papszOptions, "BLOCKSIZE", ""));
4457
0
        if (osBlockSize.empty())
4458
0
        {
4459
0
            const auto anBlockSize = GetBlockSize();
4460
0
            int idxDim = 0;
4461
0
            for (auto nBlockSize : anBlockSize)
4462
0
            {
4463
0
                if (idxDim > 0)
4464
0
                    osBlockSize += ',';
4465
0
                if (nBlockSize == 0)
4466
0
                    nBlockSize = 256;
4467
0
                nBlockSize = std::min(nBlockSize, aoDims[idxDim]->GetSize());
4468
0
                osBlockSize +=
4469
0
                    std::to_string(static_cast<uint64_t>(nBlockSize));
4470
0
                idxDim++;
4471
0
            }
4472
0
        }
4473
0
        aosOptions.SetNameValue("BLOCKSIZE", osBlockSize.c_str());
4474
4475
0
        int idxDim = 0;
4476
0
        for (const auto &poDim : aoDims)
4477
0
        {
4478
0
            auto poNewDim = poRG->CreateDimension(
4479
0
                osCachedArrayName + '_' + std::to_string(idxDim),
4480
0
                poDim->GetType(), poDim->GetDirection(), poDim->GetSize());
4481
0
            if (!poNewDim)
4482
0
                return false;
4483
0
            aoNewDims.emplace_back(poNewDim);
4484
0
            idxDim++;
4485
0
        }
4486
0
    }
4487
4488
0
    auto poCachedArray = poRG->CreateMDArray(osCachedArrayName, aoNewDims,
4489
0
                                             GetDataType(), aosOptions.List());
4490
0
    if (!poCachedArray)
4491
0
    {
4492
0
        CPLError(CE_Failure, CPLE_NotSupported, "Cannot create %s in %s",
4493
0
                 osCachedArrayName.c_str(), osCacheFilename.c_str());
4494
0
        return false;
4495
0
    }
4496
4497
0
    GUInt64 nCost = 0;
4498
0
    return poCachedArray->CopyFrom(nullptr, this,
4499
0
                                   false,  // strict
4500
0
                                   nCost, GetTotalCopyCost(), nullptr, nullptr);
4501
0
}
4502
4503
/************************************************************************/
4504
/*                               Read()                                 */
4505
/************************************************************************/
4506
4507
bool GDALMDArray::Read(const GUInt64 *arrayStartIdx, const size_t *count,
4508
                       const GInt64 *arrayStep,         // step in elements
4509
                       const GPtrDiff_t *bufferStride,  // stride in elements
4510
                       const GDALExtendedDataType &bufferDataType,
4511
                       void *pDstBuffer, const void *pDstBufferAllocStart,
4512
                       size_t nDstBufferAllocSize) const
4513
0
{
4514
0
    if (!m_bHasTriedCachedArray)
4515
0
    {
4516
0
        m_bHasTriedCachedArray = true;
4517
0
        if (IsCacheable())
4518
0
        {
4519
0
            const auto &osFilename = GetFilename();
4520
0
            if (!osFilename.empty() &&
4521
0
                !EQUAL(CPLGetExtensionSafe(osFilename.c_str()).c_str(), "gmac"))
4522
0
            {
4523
0
                std::string osCacheFilename;
4524
0
                auto poRG = GetCacheRootGroup(false, osCacheFilename);
4525
0
                if (poRG)
4526
0
                {
4527
0
                    const std::string osCachedArrayName(
4528
0
                        MassageName(GetFullName()));
4529
0
                    m_poCachedArray = poRG->OpenMDArray(osCachedArrayName);
4530
0
                    if (m_poCachedArray)
4531
0
                    {
4532
0
                        const auto &dims = GetDimensions();
4533
0
                        const auto &cachedDims =
4534
0
                            m_poCachedArray->GetDimensions();
4535
0
                        const size_t nDims = dims.size();
4536
0
                        bool ok =
4537
0
                            m_poCachedArray->GetDataType() == GetDataType() &&
4538
0
                            cachedDims.size() == nDims;
4539
0
                        for (size_t i = 0; ok && i < nDims; ++i)
4540
0
                        {
4541
0
                            ok = dims[i]->GetSize() == cachedDims[i]->GetSize();
4542
0
                        }
4543
0
                        if (ok)
4544
0
                        {
4545
0
                            CPLDebug("GDAL", "Cached array for %s found in %s",
4546
0
                                     osCachedArrayName.c_str(),
4547
0
                                     osCacheFilename.c_str());
4548
0
                        }
4549
0
                        else
4550
0
                        {
4551
0
                            CPLError(CE_Warning, CPLE_AppDefined,
4552
0
                                     "Cached array %s in %s has incompatible "
4553
0
                                     "characteristics with current array.",
4554
0
                                     osCachedArrayName.c_str(),
4555
0
                                     osCacheFilename.c_str());
4556
0
                            m_poCachedArray.reset();
4557
0
                        }
4558
0
                    }
4559
0
                }
4560
0
            }
4561
0
        }
4562
0
    }
4563
4564
0
    const auto array = m_poCachedArray ? m_poCachedArray.get() : this;
4565
0
    if (!array->GetDataType().CanConvertTo(bufferDataType))
4566
0
    {
4567
0
        CPLError(CE_Failure, CPLE_AppDefined,
4568
0
                 "Array data type is not convertible to buffer data type");
4569
0
        return false;
4570
0
    }
4571
4572
0
    std::vector<GInt64> tmp_arrayStep;
4573
0
    std::vector<GPtrDiff_t> tmp_bufferStride;
4574
0
    if (!array->CheckReadWriteParams(arrayStartIdx, count, arrayStep,
4575
0
                                     bufferStride, bufferDataType, pDstBuffer,
4576
0
                                     pDstBufferAllocStart, nDstBufferAllocSize,
4577
0
                                     tmp_arrayStep, tmp_bufferStride))
4578
0
    {
4579
0
        return false;
4580
0
    }
4581
4582
0
    return array->IRead(arrayStartIdx, count, arrayStep, bufferStride,
4583
0
                        bufferDataType, pDstBuffer);
4584
0
}
4585
4586
/************************************************************************/
4587
/*                          GetRootGroup()                              */
4588
/************************************************************************/
4589
4590
/** Return the root group to which this arrays belongs too.
4591
 *
4592
 * Note that arrays may be free standing and some drivers may not implement
4593
 * this method, hence nullptr may be returned.
4594
 *
4595
 * It is used internally by the GetResampled() method to detect if GLT
4596
 * orthorectification is available.
4597
 *
4598
 * @return the root group, or nullptr.
4599
 * @since GDAL 3.8
4600
 */
4601
std::shared_ptr<GDALGroup> GDALMDArray::GetRootGroup() const
4602
0
{
4603
0
    return nullptr;
4604
0
}
4605
4606
//! @cond Doxygen_Suppress
4607
4608
/************************************************************************/
4609
/*                       IsTransposedRequest()                          */
4610
/************************************************************************/
4611
4612
bool GDALMDArray::IsTransposedRequest(
4613
    const size_t *count,
4614
    const GPtrDiff_t *bufferStride) const  // stride in elements
4615
0
{
4616
    /*
4617
    For example:
4618
    count = [2,3,4]
4619
    strides = [12, 4, 1]            (2-1)*12+(3-1)*4+(4-1)*1=23   ==> row major
4620
    stride [12, 1, 3]            (2-1)*12+(3-1)*1+(4-1)*3=23   ==>
4621
    (axis[0],axis[2],axis[1]) transposition [1, 8, 2]             (2-1)*1+
4622
    (3-1)*8+(4-1)*2=23   ==> (axis[2],axis[1],axis[0]) transposition
4623
    */
4624
0
    const size_t nDims(GetDimensionCount());
4625
0
    size_t nCurStrideForRowMajorStrides = 1;
4626
0
    bool bRowMajorStrides = true;
4627
0
    size_t nElts = 1;
4628
0
    size_t nLastIdx = 0;
4629
0
    for (size_t i = nDims; i > 0;)
4630
0
    {
4631
0
        --i;
4632
0
        if (bufferStride[i] < 0)
4633
0
            return false;
4634
0
        if (static_cast<size_t>(bufferStride[i]) !=
4635
0
            nCurStrideForRowMajorStrides)
4636
0
        {
4637
0
            bRowMajorStrides = false;
4638
0
        }
4639
        // Integer overflows have already been checked in CheckReadWriteParams()
4640
0
        nCurStrideForRowMajorStrides *= count[i];
4641
0
        nElts *= count[i];
4642
0
        nLastIdx += static_cast<size_t>(bufferStride[i]) * (count[i] - 1);
4643
0
    }
4644
0
    if (bRowMajorStrides)
4645
0
        return false;
4646
0
    return nLastIdx == nElts - 1;
4647
0
}
4648
4649
/************************************************************************/
4650
/*                   CopyToFinalBufferSameDataType()                    */
4651
/************************************************************************/
4652
4653
template <size_t N>
4654
void CopyToFinalBufferSameDataType(const void *pSrcBuffer, void *pDstBuffer,
4655
                                   size_t nDims, const size_t *count,
4656
                                   const GPtrDiff_t *bufferStride)
4657
0
{
4658
0
    std::vector<size_t> anStackCount(nDims);
4659
0
    std::vector<GByte *> pabyDstBufferStack(nDims + 1);
4660
0
    const GByte *pabySrcBuffer = static_cast<const GByte *>(pSrcBuffer);
4661
0
#if defined(__GNUC__)
4662
0
#pragma GCC diagnostic push
4663
0
#pragma GCC diagnostic ignored "-Wnull-dereference"
4664
0
#endif
4665
0
    pabyDstBufferStack[0] = static_cast<GByte *>(pDstBuffer);
4666
0
#if defined(__GNUC__)
4667
0
#pragma GCC diagnostic pop
4668
0
#endif
4669
0
    size_t iDim = 0;
4670
4671
0
lbl_next_depth:
4672
0
    if (iDim == nDims - 1)
4673
0
    {
4674
0
        size_t n = count[iDim];
4675
0
        GByte *pabyDstBuffer = pabyDstBufferStack[iDim];
4676
0
        const auto bufferStrideLastDim = bufferStride[iDim] * N;
4677
0
        while (n > 0)
4678
0
        {
4679
0
            --n;
4680
0
            memcpy(pabyDstBuffer, pabySrcBuffer, N);
4681
0
            pabyDstBuffer += bufferStrideLastDim;
4682
0
            pabySrcBuffer += N;
4683
0
        }
4684
0
    }
4685
0
    else
4686
0
    {
4687
0
        anStackCount[iDim] = count[iDim];
4688
0
        while (true)
4689
0
        {
4690
0
            ++iDim;
4691
0
            pabyDstBufferStack[iDim] = pabyDstBufferStack[iDim - 1];
4692
0
            goto lbl_next_depth;
4693
0
        lbl_return_to_caller_in_loop:
4694
0
            --iDim;
4695
0
            --anStackCount[iDim];
4696
0
            if (anStackCount[iDim] == 0)
4697
0
                break;
4698
0
            pabyDstBufferStack[iDim] += bufferStride[iDim] * N;
4699
0
        }
4700
0
    }
4701
0
    if (iDim > 0)
4702
0
        goto lbl_return_to_caller_in_loop;
4703
0
}
Unexecuted instantiation: void CopyToFinalBufferSameDataType<1ul>(void const*, void*, unsigned long, unsigned long const*, long long const*)
Unexecuted instantiation: void CopyToFinalBufferSameDataType<2ul>(void const*, void*, unsigned long, unsigned long const*, long long const*)
Unexecuted instantiation: void CopyToFinalBufferSameDataType<4ul>(void const*, void*, unsigned long, unsigned long const*, long long const*)
Unexecuted instantiation: void CopyToFinalBufferSameDataType<8ul>(void const*, void*, unsigned long, unsigned long const*, long long const*)
4704
4705
/************************************************************************/
4706
/*                        CopyToFinalBuffer()                           */
4707
/************************************************************************/
4708
4709
static void CopyToFinalBuffer(const void *pSrcBuffer,
4710
                              const GDALExtendedDataType &eSrcDataType,
4711
                              void *pDstBuffer,
4712
                              const GDALExtendedDataType &eDstDataType,
4713
                              size_t nDims, const size_t *count,
4714
                              const GPtrDiff_t *bufferStride)
4715
0
{
4716
0
    const size_t nSrcDataTypeSize(eSrcDataType.GetSize());
4717
    // Use specialized implementation for well-known data types when no
4718
    // type conversion is needed
4719
0
    if (eSrcDataType == eDstDataType)
4720
0
    {
4721
0
        if (nSrcDataTypeSize == 1)
4722
0
        {
4723
0
            CopyToFinalBufferSameDataType<1>(pSrcBuffer, pDstBuffer, nDims,
4724
0
                                             count, bufferStride);
4725
0
            return;
4726
0
        }
4727
0
        else if (nSrcDataTypeSize == 2)
4728
0
        {
4729
0
            CopyToFinalBufferSameDataType<2>(pSrcBuffer, pDstBuffer, nDims,
4730
0
                                             count, bufferStride);
4731
0
            return;
4732
0
        }
4733
0
        else if (nSrcDataTypeSize == 4)
4734
0
        {
4735
0
            CopyToFinalBufferSameDataType<4>(pSrcBuffer, pDstBuffer, nDims,
4736
0
                                             count, bufferStride);
4737
0
            return;
4738
0
        }
4739
0
        else if (nSrcDataTypeSize == 8)
4740
0
        {
4741
0
            CopyToFinalBufferSameDataType<8>(pSrcBuffer, pDstBuffer, nDims,
4742
0
                                             count, bufferStride);
4743
0
            return;
4744
0
        }
4745
0
    }
4746
4747
0
    const size_t nDstDataTypeSize(eDstDataType.GetSize());
4748
0
    std::vector<size_t> anStackCount(nDims);
4749
0
    std::vector<GByte *> pabyDstBufferStack(nDims + 1);
4750
0
    const GByte *pabySrcBuffer = static_cast<const GByte *>(pSrcBuffer);
4751
0
    pabyDstBufferStack[0] = static_cast<GByte *>(pDstBuffer);
4752
0
    size_t iDim = 0;
4753
4754
0
lbl_next_depth:
4755
0
    if (iDim == nDims - 1)
4756
0
    {
4757
0
        GDALExtendedDataType::CopyValues(pabySrcBuffer, eSrcDataType, 1,
4758
0
                                         pabyDstBufferStack[iDim], eDstDataType,
4759
0
                                         bufferStride[iDim], count[iDim]);
4760
0
        pabySrcBuffer += count[iDim] * nSrcDataTypeSize;
4761
0
    }
4762
0
    else
4763
0
    {
4764
0
        anStackCount[iDim] = count[iDim];
4765
0
        while (true)
4766
0
        {
4767
0
            ++iDim;
4768
0
            pabyDstBufferStack[iDim] = pabyDstBufferStack[iDim - 1];
4769
0
            goto lbl_next_depth;
4770
0
        lbl_return_to_caller_in_loop:
4771
0
            --iDim;
4772
0
            --anStackCount[iDim];
4773
0
            if (anStackCount[iDim] == 0)
4774
0
                break;
4775
0
            pabyDstBufferStack[iDim] += bufferStride[iDim] * nDstDataTypeSize;
4776
0
        }
4777
0
    }
4778
0
    if (iDim > 0)
4779
0
        goto lbl_return_to_caller_in_loop;
4780
0
}
4781
4782
/************************************************************************/
4783
/*                      TransposeLast2Dims()                            */
4784
/************************************************************************/
4785
4786
static bool TransposeLast2Dims(void *pDstBuffer,
4787
                               const GDALExtendedDataType &eDT,
4788
                               const size_t nDims, const size_t *count,
4789
                               const size_t nEltsNonLast2Dims)
4790
0
{
4791
0
    const size_t nEltsLast2Dims = count[nDims - 2] * count[nDims - 1];
4792
0
    const auto nDTSize = eDT.GetSize();
4793
0
    void *pTempBufferForLast2DimsTranspose =
4794
0
        VSI_MALLOC2_VERBOSE(nEltsLast2Dims, nDTSize);
4795
0
    if (pTempBufferForLast2DimsTranspose == nullptr)
4796
0
        return false;
4797
4798
0
    GByte *pabyDstBuffer = static_cast<GByte *>(pDstBuffer);
4799
0
    for (size_t i = 0; i < nEltsNonLast2Dims; ++i)
4800
0
    {
4801
0
        GDALTranspose2D(pabyDstBuffer, eDT.GetNumericDataType(),
4802
0
                        pTempBufferForLast2DimsTranspose,
4803
0
                        eDT.GetNumericDataType(), count[nDims - 1],
4804
0
                        count[nDims - 2]);
4805
0
        memcpy(pabyDstBuffer, pTempBufferForLast2DimsTranspose,
4806
0
               nDTSize * nEltsLast2Dims);
4807
0
        pabyDstBuffer += nDTSize * nEltsLast2Dims;
4808
0
    }
4809
4810
0
    VSIFree(pTempBufferForLast2DimsTranspose);
4811
4812
0
    return true;
4813
0
}
4814
4815
/************************************************************************/
4816
/*                      ReadForTransposedRequest()                      */
4817
/************************************************************************/
4818
4819
// Using the netCDF/HDF5 APIs to read a slice with strides that express a
4820
// transposed view yield to extremely poor/unusable performance. This fixes
4821
// this by using temporary memory to read in a contiguous buffer in a
4822
// row-major order, and then do the transposition to the final buffer.
4823
4824
bool GDALMDArray::ReadForTransposedRequest(
4825
    const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep,
4826
    const GPtrDiff_t *bufferStride, const GDALExtendedDataType &bufferDataType,
4827
    void *pDstBuffer) const
4828
0
{
4829
0
    const size_t nDims(GetDimensionCount());
4830
0
    if (nDims == 0)
4831
0
    {
4832
0
        CPLAssert(false);
4833
0
        return false;  // shouldn't happen
4834
0
    }
4835
0
    size_t nElts = 1;
4836
0
    for (size_t i = 0; i < nDims; ++i)
4837
0
        nElts *= count[i];
4838
4839
0
    std::vector<GPtrDiff_t> tmpBufferStrides(nDims);
4840
0
    tmpBufferStrides.back() = 1;
4841
0
    for (size_t i = nDims - 1; i > 0;)
4842
0
    {
4843
0
        --i;
4844
0
        tmpBufferStrides[i] = tmpBufferStrides[i + 1] * count[i + 1];
4845
0
    }
4846
4847
0
    const auto &eDT = GetDataType();
4848
0
    const auto nDTSize = eDT.GetSize();
4849
0
    if (bufferDataType == eDT && nDims >= 2 && bufferStride[nDims - 2] == 1 &&
4850
0
        static_cast<size_t>(bufferStride[nDims - 1]) == count[nDims - 2] &&
4851
0
        (nDTSize == 1 || nDTSize == 2 || nDTSize == 4 || nDTSize == 8))
4852
0
    {
4853
        // Optimization of the optimization if only the last 2 dims are
4854
        // transposed that saves on temporary buffer allocation
4855
0
        const size_t nEltsLast2Dims = count[nDims - 2] * count[nDims - 1];
4856
0
        size_t nCurStrideForRowMajorStrides = nEltsLast2Dims;
4857
0
        bool bRowMajorStridesForNonLast2Dims = true;
4858
0
        size_t nEltsNonLast2Dims = 1;
4859
0
        for (size_t i = nDims - 2; i > 0;)
4860
0
        {
4861
0
            --i;
4862
0
            if (static_cast<size_t>(bufferStride[i]) !=
4863
0
                nCurStrideForRowMajorStrides)
4864
0
            {
4865
0
                bRowMajorStridesForNonLast2Dims = false;
4866
0
            }
4867
            // Integer overflows have already been checked in
4868
            // CheckReadWriteParams()
4869
0
            nCurStrideForRowMajorStrides *= count[i];
4870
0
            nEltsNonLast2Dims *= count[i];
4871
0
        }
4872
0
        if (bRowMajorStridesForNonLast2Dims)
4873
0
        {
4874
            // We read in the final buffer!
4875
0
            if (!IRead(arrayStartIdx, count, arrayStep, tmpBufferStrides.data(),
4876
0
                       eDT, pDstBuffer))
4877
0
            {
4878
0
                return false;
4879
0
            }
4880
4881
0
            return TransposeLast2Dims(pDstBuffer, eDT, nDims, count,
4882
0
                                      nEltsNonLast2Dims);
4883
0
        }
4884
0
    }
4885
4886
0
    void *pTempBuffer = VSI_MALLOC2_VERBOSE(nElts, eDT.GetSize());
4887
0
    if (pTempBuffer == nullptr)
4888
0
        return false;
4889
4890
0
    if (!IRead(arrayStartIdx, count, arrayStep, tmpBufferStrides.data(), eDT,
4891
0
               pTempBuffer))
4892
0
    {
4893
0
        VSIFree(pTempBuffer);
4894
0
        return false;
4895
0
    }
4896
0
    CopyToFinalBuffer(pTempBuffer, eDT, pDstBuffer, bufferDataType, nDims,
4897
0
                      count, bufferStride);
4898
4899
0
    if (eDT.NeedsFreeDynamicMemory())
4900
0
    {
4901
0
        GByte *pabyPtr = static_cast<GByte *>(pTempBuffer);
4902
0
        for (size_t i = 0; i < nElts; ++i)
4903
0
        {
4904
0
            eDT.FreeDynamicMemory(pabyPtr);
4905
0
            pabyPtr += nDTSize;
4906
0
        }
4907
0
    }
4908
4909
0
    VSIFree(pTempBuffer);
4910
0
    return true;
4911
0
}
4912
4913
/************************************************************************/
4914
/*               IsStepOneContiguousRowMajorOrderedSameDataType()       */
4915
/************************************************************************/
4916
4917
// Returns true if at all following conditions are met:
4918
// arrayStep[] == 1, bufferDataType == GetDataType() and bufferStride[]
4919
// defines a row-major ordered contiguous buffer.
4920
bool GDALMDArray::IsStepOneContiguousRowMajorOrderedSameDataType(
4921
    const size_t *count, const GInt64 *arrayStep,
4922
    const GPtrDiff_t *bufferStride,
4923
    const GDALExtendedDataType &bufferDataType) const
4924
0
{
4925
0
    if (bufferDataType != GetDataType())
4926
0
        return false;
4927
0
    size_t nExpectedStride = 1;
4928
0
    for (size_t i = GetDimensionCount(); i > 0;)
4929
0
    {
4930
0
        --i;
4931
0
        if (arrayStep[i] != 1 || bufferStride[i] < 0 ||
4932
0
            static_cast<size_t>(bufferStride[i]) != nExpectedStride)
4933
0
        {
4934
0
            return false;
4935
0
        }
4936
0
        nExpectedStride *= count[i];
4937
0
    }
4938
0
    return true;
4939
0
}
4940
4941
/************************************************************************/
4942
/*                      ReadUsingContiguousIRead()                      */
4943
/************************************************************************/
4944
4945
// Used for example by the TileDB driver when requesting it with
4946
// arrayStep[] != 1, bufferDataType != GetDataType() or bufferStride[]
4947
// not defining a row-major ordered contiguous buffer.
4948
// Should only be called when at least one of the above conditions are true,
4949
// which can be tested with IsStepOneContiguousRowMajorOrderedSameDataType()
4950
// returning none.
4951
// This method will call IRead() again with arrayStep[] == 1,
4952
// bufferDataType == GetDataType() and bufferStride[] defining a row-major
4953
// ordered contiguous buffer, on a temporary buffer. And it will rearrange the
4954
// content of that temporary buffer onto pDstBuffer.
4955
bool GDALMDArray::ReadUsingContiguousIRead(
4956
    const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep,
4957
    const GPtrDiff_t *bufferStride, const GDALExtendedDataType &bufferDataType,
4958
    void *pDstBuffer) const
4959
0
{
4960
0
    const size_t nDims(GetDimensionCount());
4961
0
    std::vector<GUInt64> anTmpStartIdx(nDims);
4962
0
    std::vector<size_t> anTmpCount(nDims);
4963
0
    const auto &oType = GetDataType();
4964
0
    size_t nMemArraySize = oType.GetSize();
4965
0
    std::vector<GPtrDiff_t> anTmpStride(nDims);
4966
0
    GPtrDiff_t nStride = 1;
4967
0
    for (size_t i = nDims; i > 0;)
4968
0
    {
4969
0
        --i;
4970
0
        if (arrayStep[i] > 0)
4971
0
            anTmpStartIdx[i] = arrayStartIdx[i];
4972
0
        else
4973
0
            anTmpStartIdx[i] =
4974
0
                arrayStartIdx[i] - (count[i] - 1) * (-arrayStep[i]);
4975
0
        const uint64_t nCount =
4976
0
            (count[i] - 1) * static_cast<uint64_t>(std::abs(arrayStep[i])) + 1;
4977
0
        if (nCount > std::numeric_limits<size_t>::max() / nMemArraySize)
4978
0
        {
4979
0
            CPLError(CE_Failure, CPLE_AppDefined,
4980
0
                     "Read() failed due to too large memory requirement");
4981
0
            return false;
4982
0
        }
4983
0
        anTmpCount[i] = static_cast<size_t>(nCount);
4984
0
        nMemArraySize *= anTmpCount[i];
4985
0
        anTmpStride[i] = nStride;
4986
0
        nStride *= anTmpCount[i];
4987
0
    }
4988
0
    std::unique_ptr<void, decltype(&VSIFree)> pTmpBuffer(
4989
0
        VSI_MALLOC_VERBOSE(nMemArraySize), VSIFree);
4990
0
    if (!pTmpBuffer)
4991
0
        return false;
4992
0
    if (!IRead(anTmpStartIdx.data(), anTmpCount.data(),
4993
0
               std::vector<GInt64>(nDims, 1).data(),  // steps
4994
0
               anTmpStride.data(), oType, pTmpBuffer.get()))
4995
0
    {
4996
0
        return false;
4997
0
    }
4998
0
    std::vector<std::shared_ptr<GDALDimension>> apoTmpDims(nDims);
4999
0
    for (size_t i = 0; i < nDims; ++i)
5000
0
    {
5001
0
        if (arrayStep[i] > 0)
5002
0
            anTmpStartIdx[i] = 0;
5003
0
        else
5004
0
            anTmpStartIdx[i] = anTmpCount[i] - 1;
5005
0
        apoTmpDims[i] = std::make_shared<GDALDimension>(
5006
0
            std::string(), std::string(), std::string(), std::string(),
5007
0
            anTmpCount[i]);
5008
0
    }
5009
0
    auto poMEMArray =
5010
0
        MEMMDArray::Create(std::string(), std::string(), apoTmpDims, oType);
5011
0
    return poMEMArray->Init(static_cast<GByte *>(pTmpBuffer.get())) &&
5012
0
           poMEMArray->Read(anTmpStartIdx.data(), count, arrayStep,
5013
0
                            bufferStride, bufferDataType, pDstBuffer);
5014
0
}
5015
5016
//! @endcond
5017
5018
/************************************************************************/
5019
/*                       GDALSlicedMDArray                              */
5020
/************************************************************************/
5021
5022
class GDALSlicedMDArray final : public GDALPamMDArray
5023
{
5024
  private:
5025
    std::shared_ptr<GDALMDArray> m_poParent{};
5026
    std::vector<std::shared_ptr<GDALDimension>> m_dims{};
5027
    std::vector<size_t> m_mapDimIdxToParentDimIdx{};  // of size m_dims.size()
5028
    std::vector<Range>
5029
        m_parentRanges{};  // of size m_poParent->GetDimensionCount()
5030
5031
    mutable std::vector<GUInt64> m_parentStart;
5032
    mutable std::vector<size_t> m_parentCount;
5033
    mutable std::vector<GInt64> m_parentStep;
5034
    mutable std::vector<GPtrDiff_t> m_parentStride;
5035
5036
    void PrepareParentArrays(const GUInt64 *arrayStartIdx, const size_t *count,
5037
                             const GInt64 *arrayStep,
5038
                             const GPtrDiff_t *bufferStride) const;
5039
5040
  protected:
5041
    explicit GDALSlicedMDArray(
5042
        const std::shared_ptr<GDALMDArray> &poParent,
5043
        const std::string &viewExpr,
5044
        std::vector<std::shared_ptr<GDALDimension>> &&dims,
5045
        std::vector<size_t> &&mapDimIdxToParentDimIdx,
5046
        std::vector<Range> &&parentRanges)
5047
0
        : GDALAbstractMDArray(std::string(), "Sliced view of " +
5048
0
                                                 poParent->GetFullName() +
5049
0
                                                 " (" + viewExpr + ")"),
5050
0
          GDALPamMDArray(std::string(),
5051
0
                         "Sliced view of " + poParent->GetFullName() + " (" +
5052
0
                             viewExpr + ")",
5053
0
                         GDALPamMultiDim::GetPAM(poParent),
5054
0
                         poParent->GetContext()),
5055
0
          m_poParent(std::move(poParent)), m_dims(std::move(dims)),
5056
0
          m_mapDimIdxToParentDimIdx(std::move(mapDimIdxToParentDimIdx)),
5057
0
          m_parentRanges(std::move(parentRanges)),
5058
0
          m_parentStart(m_poParent->GetDimensionCount()),
5059
0
          m_parentCount(m_poParent->GetDimensionCount(), 1),
5060
0
          m_parentStep(m_poParent->GetDimensionCount()),
5061
0
          m_parentStride(m_poParent->GetDimensionCount())
5062
0
    {
5063
0
    }
5064
5065
    bool IRead(const GUInt64 *arrayStartIdx, const size_t *count,
5066
               const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
5067
               const GDALExtendedDataType &bufferDataType,
5068
               void *pDstBuffer) const override;
5069
5070
    bool IWrite(const GUInt64 *arrayStartIdx, const size_t *count,
5071
                const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
5072
                const GDALExtendedDataType &bufferDataType,
5073
                const void *pSrcBuffer) override;
5074
5075
    bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count,
5076
                     CSLConstList papszOptions) const override;
5077
5078
  public:
5079
    static std::shared_ptr<GDALSlicedMDArray>
5080
    Create(const std::shared_ptr<GDALMDArray> &poParent,
5081
           const std::string &viewExpr,
5082
           std::vector<std::shared_ptr<GDALDimension>> &&dims,
5083
           std::vector<size_t> &&mapDimIdxToParentDimIdx,
5084
           std::vector<Range> &&parentRanges)
5085
0
    {
5086
0
        CPLAssert(dims.size() == mapDimIdxToParentDimIdx.size());
5087
0
        CPLAssert(parentRanges.size() == poParent->GetDimensionCount());
5088
5089
0
        auto newAr(std::shared_ptr<GDALSlicedMDArray>(new GDALSlicedMDArray(
5090
0
            poParent, viewExpr, std::move(dims),
5091
0
            std::move(mapDimIdxToParentDimIdx), std::move(parentRanges))));
5092
0
        newAr->SetSelf(newAr);
5093
0
        return newAr;
5094
0
    }
5095
5096
    bool IsWritable() const override
5097
0
    {
5098
0
        return m_poParent->IsWritable();
5099
0
    }
5100
5101
    const std::string &GetFilename() const override
5102
0
    {
5103
0
        return m_poParent->GetFilename();
5104
0
    }
5105
5106
    const std::vector<std::shared_ptr<GDALDimension>> &
5107
    GetDimensions() const override
5108
0
    {
5109
0
        return m_dims;
5110
0
    }
5111
5112
    const GDALExtendedDataType &GetDataType() const override
5113
0
    {
5114
0
        return m_poParent->GetDataType();
5115
0
    }
5116
5117
    const std::string &GetUnit() const override
5118
0
    {
5119
0
        return m_poParent->GetUnit();
5120
0
    }
5121
5122
    // bool SetUnit(const std::string& osUnit) override  { return
5123
    // m_poParent->SetUnit(osUnit); }
5124
5125
    std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override
5126
0
    {
5127
0
        auto poSrcSRS = m_poParent->GetSpatialRef();
5128
0
        if (!poSrcSRS)
5129
0
            return nullptr;
5130
0
        auto srcMapping = poSrcSRS->GetDataAxisToSRSAxisMapping();
5131
0
        std::vector<int> dstMapping;
5132
0
        for (int srcAxis : srcMapping)
5133
0
        {
5134
0
            bool bFound = false;
5135
0
            for (size_t i = 0; i < m_mapDimIdxToParentDimIdx.size(); i++)
5136
0
            {
5137
0
                if (static_cast<int>(m_mapDimIdxToParentDimIdx[i]) ==
5138
0
                    srcAxis - 1)
5139
0
                {
5140
0
                    dstMapping.push_back(static_cast<int>(i) + 1);
5141
0
                    bFound = true;
5142
0
                    break;
5143
0
                }
5144
0
            }
5145
0
            if (!bFound)
5146
0
            {
5147
0
                dstMapping.push_back(0);
5148
0
            }
5149
0
        }
5150
0
        auto poClone(std::shared_ptr<OGRSpatialReference>(poSrcSRS->Clone()));
5151
0
        poClone->SetDataAxisToSRSAxisMapping(dstMapping);
5152
0
        return poClone;
5153
0
    }
5154
5155
    const void *GetRawNoDataValue() const override
5156
0
    {
5157
0
        return m_poParent->GetRawNoDataValue();
5158
0
    }
5159
5160
    // bool SetRawNoDataValue(const void* pRawNoData) override { return
5161
    // m_poParent->SetRawNoDataValue(pRawNoData); }
5162
5163
    double GetOffset(bool *pbHasOffset,
5164
                     GDALDataType *peStorageType) const override
5165
0
    {
5166
0
        return m_poParent->GetOffset(pbHasOffset, peStorageType);
5167
0
    }
5168
5169
    double GetScale(bool *pbHasScale,
5170
                    GDALDataType *peStorageType) const override
5171
0
    {
5172
0
        return m_poParent->GetScale(pbHasScale, peStorageType);
5173
0
    }
5174
5175
    // bool SetOffset(double dfOffset) override { return
5176
    // m_poParent->SetOffset(dfOffset); }
5177
5178
    // bool SetScale(double dfScale) override { return
5179
    // m_poParent->SetScale(dfScale); }
5180
5181
    std::vector<GUInt64> GetBlockSize() const override
5182
0
    {
5183
0
        std::vector<GUInt64> ret(GetDimensionCount());
5184
0
        const auto parentBlockSize(m_poParent->GetBlockSize());
5185
0
        for (size_t i = 0; i < m_mapDimIdxToParentDimIdx.size(); ++i)
5186
0
        {
5187
0
            const auto iOldAxis = m_mapDimIdxToParentDimIdx[i];
5188
0
            if (iOldAxis != static_cast<size_t>(-1))
5189
0
            {
5190
0
                ret[i] = parentBlockSize[iOldAxis];
5191
0
            }
5192
0
        }
5193
0
        return ret;
5194
0
    }
5195
5196
    std::shared_ptr<GDALAttribute>
5197
    GetAttribute(const std::string &osName) const override
5198
0
    {
5199
0
        return m_poParent->GetAttribute(osName);
5200
0
    }
5201
5202
    std::vector<std::shared_ptr<GDALAttribute>>
5203
    GetAttributes(CSLConstList papszOptions = nullptr) const override
5204
0
    {
5205
0
        return m_poParent->GetAttributes(papszOptions);
5206
0
    }
5207
};
5208
5209
/************************************************************************/
5210
/*                        PrepareParentArrays()                         */
5211
/************************************************************************/
5212
5213
void GDALSlicedMDArray::PrepareParentArrays(
5214
    const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep,
5215
    const GPtrDiff_t *bufferStride) const
5216
0
{
5217
0
    const size_t nParentDimCount = m_parentRanges.size();
5218
0
    for (size_t i = 0; i < nParentDimCount; i++)
5219
0
    {
5220
        // For dimensions in parent that have no existence in sliced array
5221
0
        m_parentStart[i] = m_parentRanges[i].m_nStartIdx;
5222
0
    }
5223
5224
0
    for (size_t i = 0; i < m_dims.size(); i++)
5225
0
    {
5226
0
        const auto iParent = m_mapDimIdxToParentDimIdx[i];
5227
0
        if (iParent != static_cast<size_t>(-1))
5228
0
        {
5229
0
            m_parentStart[iParent] =
5230
0
                m_parentRanges[iParent].m_nIncr >= 0
5231
0
                    ? m_parentRanges[iParent].m_nStartIdx +
5232
0
                          arrayStartIdx[i] * m_parentRanges[iParent].m_nIncr
5233
0
                    : m_parentRanges[iParent].m_nStartIdx -
5234
0
                          arrayStartIdx[i] *
5235
0
                              static_cast<GUInt64>(
5236
0
                                  -m_parentRanges[iParent].m_nIncr);
5237
0
            m_parentCount[iParent] = count[i];
5238
0
            if (arrayStep)
5239
0
            {
5240
0
                m_parentStep[iParent] =
5241
0
                    count[i] == 1 ? 1 :
5242
                                  // other checks should have ensured this does
5243
                        // not overflow
5244
0
                        arrayStep[i] * m_parentRanges[iParent].m_nIncr;
5245
0
            }
5246
0
            if (bufferStride)
5247
0
            {
5248
0
                m_parentStride[iParent] = bufferStride[i];
5249
0
            }
5250
0
        }
5251
0
    }
5252
0
}
5253
5254
/************************************************************************/
5255
/*                             IRead()                                  */
5256
/************************************************************************/
5257
5258
bool GDALSlicedMDArray::IRead(const GUInt64 *arrayStartIdx, const size_t *count,
5259
                              const GInt64 *arrayStep,
5260
                              const GPtrDiff_t *bufferStride,
5261
                              const GDALExtendedDataType &bufferDataType,
5262
                              void *pDstBuffer) const
5263
0
{
5264
0
    PrepareParentArrays(arrayStartIdx, count, arrayStep, bufferStride);
5265
0
    return m_poParent->Read(m_parentStart.data(), m_parentCount.data(),
5266
0
                            m_parentStep.data(), m_parentStride.data(),
5267
0
                            bufferDataType, pDstBuffer);
5268
0
}
5269
5270
/************************************************************************/
5271
/*                             IWrite()                                  */
5272
/************************************************************************/
5273
5274
bool GDALSlicedMDArray::IWrite(const GUInt64 *arrayStartIdx,
5275
                               const size_t *count, const GInt64 *arrayStep,
5276
                               const GPtrDiff_t *bufferStride,
5277
                               const GDALExtendedDataType &bufferDataType,
5278
                               const void *pSrcBuffer)
5279
0
{
5280
0
    PrepareParentArrays(arrayStartIdx, count, arrayStep, bufferStride);
5281
0
    return m_poParent->Write(m_parentStart.data(), m_parentCount.data(),
5282
0
                             m_parentStep.data(), m_parentStride.data(),
5283
0
                             bufferDataType, pSrcBuffer);
5284
0
}
5285
5286
/************************************************************************/
5287
/*                             IAdviseRead()                            */
5288
/************************************************************************/
5289
5290
bool GDALSlicedMDArray::IAdviseRead(const GUInt64 *arrayStartIdx,
5291
                                    const size_t *count,
5292
                                    CSLConstList papszOptions) const
5293
0
{
5294
0
    PrepareParentArrays(arrayStartIdx, count, nullptr, nullptr);
5295
0
    return m_poParent->AdviseRead(m_parentStart.data(), m_parentCount.data(),
5296
0
                                  papszOptions);
5297
0
}
5298
5299
/************************************************************************/
5300
/*                        CreateSlicedArray()                           */
5301
/************************************************************************/
5302
5303
static std::shared_ptr<GDALMDArray>
5304
CreateSlicedArray(const std::shared_ptr<GDALMDArray> &self,
5305
                  const std::string &viewExpr, const std::string &activeSlice,
5306
                  bool bRenameDimensions,
5307
                  std::vector<GDALMDArray::ViewSpec> &viewSpecs)
5308
0
{
5309
0
    const auto &srcDims(self->GetDimensions());
5310
0
    if (srcDims.empty())
5311
0
    {
5312
0
        CPLError(CE_Failure, CPLE_AppDefined, "Cannot slice a 0-d array");
5313
0
        return nullptr;
5314
0
    }
5315
5316
0
    CPLStringList aosTokens(CSLTokenizeString2(activeSlice.c_str(), ",", 0));
5317
0
    const auto nTokens = static_cast<size_t>(aosTokens.size());
5318
5319
0
    std::vector<std::shared_ptr<GDALDimension>> newDims;
5320
0
    std::vector<size_t> mapDimIdxToParentDimIdx;
5321
0
    std::vector<GDALSlicedMDArray::Range> parentRanges;
5322
0
    newDims.reserve(nTokens);
5323
0
    mapDimIdxToParentDimIdx.reserve(nTokens);
5324
0
    parentRanges.reserve(nTokens);
5325
5326
0
    bool bGotEllipsis = false;
5327
0
    size_t nCurSrcDim = 0;
5328
0
    for (size_t i = 0; i < nTokens; i++)
5329
0
    {
5330
0
        const char *pszIdxSpec = aosTokens[i];
5331
0
        if (EQUAL(pszIdxSpec, "..."))
5332
0
        {
5333
0
            if (bGotEllipsis)
5334
0
            {
5335
0
                CPLError(CE_Failure, CPLE_AppDefined,
5336
0
                         "Only one single ellipsis is supported");
5337
0
                return nullptr;
5338
0
            }
5339
0
            bGotEllipsis = true;
5340
0
            const auto nSubstitutionCount = srcDims.size() - (nTokens - 1);
5341
0
            for (size_t j = 0; j < nSubstitutionCount; j++, nCurSrcDim++)
5342
0
            {
5343
0
                parentRanges.emplace_back(0, 1);
5344
0
                newDims.push_back(srcDims[nCurSrcDim]);
5345
0
                mapDimIdxToParentDimIdx.push_back(nCurSrcDim);
5346
0
            }
5347
0
            continue;
5348
0
        }
5349
0
        else if (EQUAL(pszIdxSpec, "newaxis") ||
5350
0
                 EQUAL(pszIdxSpec, "np.newaxis"))
5351
0
        {
5352
0
            newDims.push_back(std::make_shared<GDALDimension>(
5353
0
                std::string(), "newaxis", std::string(), std::string(), 1));
5354
0
            mapDimIdxToParentDimIdx.push_back(static_cast<size_t>(-1));
5355
0
            continue;
5356
0
        }
5357
0
        else if (CPLGetValueType(pszIdxSpec) == CPL_VALUE_INTEGER)
5358
0
        {
5359
0
            if (nCurSrcDim >= srcDims.size())
5360
0
            {
5361
0
                CPLError(CE_Failure, CPLE_AppDefined, "Too many values in %s",
5362
0
                         activeSlice.c_str());
5363
0
                return nullptr;
5364
0
            }
5365
5366
0
            auto nVal = CPLAtoGIntBig(pszIdxSpec);
5367
0
            GUInt64 nDimSize = srcDims[nCurSrcDim]->GetSize();
5368
0
            if ((nVal >= 0 && static_cast<GUInt64>(nVal) >= nDimSize) ||
5369
0
                (nVal < 0 && nDimSize < static_cast<GUInt64>(-nVal)))
5370
0
            {
5371
0
                CPLError(CE_Failure, CPLE_AppDefined,
5372
0
                         "Index " CPL_FRMT_GIB " is out of bounds", nVal);
5373
0
                return nullptr;
5374
0
            }
5375
0
            if (nVal < 0)
5376
0
                nVal += nDimSize;
5377
0
            parentRanges.emplace_back(nVal, 0);
5378
0
        }
5379
0
        else
5380
0
        {
5381
0
            if (nCurSrcDim >= srcDims.size())
5382
0
            {
5383
0
                CPLError(CE_Failure, CPLE_AppDefined, "Too many values in %s",
5384
0
                         activeSlice.c_str());
5385
0
                return nullptr;
5386
0
            }
5387
5388
0
            CPLStringList aosRangeTokens(
5389
0
                CSLTokenizeString2(pszIdxSpec, ":", CSLT_ALLOWEMPTYTOKENS));
5390
0
            int nRangeTokens = aosRangeTokens.size();
5391
0
            if (nRangeTokens > 3)
5392
0
            {
5393
0
                CPLError(CE_Failure, CPLE_AppDefined, "Too many : in %s",
5394
0
                         pszIdxSpec);
5395
0
                return nullptr;
5396
0
            }
5397
0
            if (nRangeTokens <= 1)
5398
0
            {
5399
0
                CPLError(CE_Failure, CPLE_AppDefined, "Invalid value %s",
5400
0
                         pszIdxSpec);
5401
0
                return nullptr;
5402
0
            }
5403
0
            const char *pszStart = aosRangeTokens[0];
5404
0
            const char *pszEnd = aosRangeTokens[1];
5405
0
            const char *pszInc = (nRangeTokens == 3) ? aosRangeTokens[2] : "";
5406
0
            GDALSlicedMDArray::Range range;
5407
0
            const GUInt64 nDimSize(srcDims[nCurSrcDim]->GetSize());
5408
0
            range.m_nIncr = EQUAL(pszInc, "") ? 1 : CPLAtoGIntBig(pszInc);
5409
0
            if (range.m_nIncr == 0 ||
5410
0
                range.m_nIncr == std::numeric_limits<GInt64>::min())
5411
0
            {
5412
0
                CPLError(CE_Failure, CPLE_AppDefined, "Invalid increment");
5413
0
                return nullptr;
5414
0
            }
5415
0
            auto startIdx(CPLAtoGIntBig(pszStart));
5416
0
            if (startIdx < 0)
5417
0
            {
5418
0
                if (nDimSize < static_cast<GUInt64>(-startIdx))
5419
0
                    startIdx = 0;
5420
0
                else
5421
0
                    startIdx = nDimSize + startIdx;
5422
0
            }
5423
0
            const bool bPosIncr = range.m_nIncr > 0;
5424
0
            range.m_nStartIdx = startIdx;
5425
0
            range.m_nStartIdx = EQUAL(pszStart, "")
5426
0
                                    ? (bPosIncr ? 0 : nDimSize - 1)
5427
0
                                    : range.m_nStartIdx;
5428
0
            if (range.m_nStartIdx >= nDimSize - 1)
5429
0
                range.m_nStartIdx = nDimSize - 1;
5430
0
            auto endIdx(CPLAtoGIntBig(pszEnd));
5431
0
            if (endIdx < 0)
5432
0
            {
5433
0
                const auto positiveEndIdx = static_cast<GUInt64>(-endIdx);
5434
0
                if (nDimSize < positiveEndIdx)
5435
0
                    endIdx = 0;
5436
0
                else
5437
0
                    endIdx = nDimSize - positiveEndIdx;
5438
0
            }
5439
0
            GUInt64 nEndIdx = endIdx;
5440
0
            nEndIdx = EQUAL(pszEnd, "") ? (!bPosIncr ? 0 : nDimSize) : nEndIdx;
5441
0
            if ((bPosIncr && range.m_nStartIdx >= nEndIdx) ||
5442
0
                (!bPosIncr && range.m_nStartIdx <= nEndIdx))
5443
0
            {
5444
0
                CPLError(CE_Failure, CPLE_AppDefined,
5445
0
                         "Output dimension of size 0 is not allowed");
5446
0
                return nullptr;
5447
0
            }
5448
0
            int inc = (EQUAL(pszEnd, "") && !bPosIncr) ? 1 : 0;
5449
0
            const auto nAbsIncr = std::abs(range.m_nIncr);
5450
0
            const GUInt64 newSize =
5451
0
                bPosIncr
5452
0
                    ? DIV_ROUND_UP(nEndIdx - range.m_nStartIdx, nAbsIncr)
5453
0
                    : DIV_ROUND_UP(inc + range.m_nStartIdx - nEndIdx, nAbsIncr);
5454
0
            if (range.m_nStartIdx == 0 && range.m_nIncr == 1 &&
5455
0
                newSize == srcDims[nCurSrcDim]->GetSize())
5456
0
            {
5457
0
                newDims.push_back(srcDims[nCurSrcDim]);
5458
0
            }
5459
0
            else
5460
0
            {
5461
0
                std::string osNewDimName(srcDims[nCurSrcDim]->GetName());
5462
0
                if (bRenameDimensions)
5463
0
                {
5464
0
                    osNewDimName =
5465
0
                        "subset_" + srcDims[nCurSrcDim]->GetName() +
5466
0
                        CPLSPrintf("_" CPL_FRMT_GUIB "_" CPL_FRMT_GIB
5467
0
                                   "_" CPL_FRMT_GUIB,
5468
0
                                   static_cast<GUIntBig>(range.m_nStartIdx),
5469
0
                                   static_cast<GIntBig>(range.m_nIncr),
5470
0
                                   static_cast<GUIntBig>(newSize));
5471
0
                }
5472
0
                newDims.push_back(std::make_shared<GDALDimension>(
5473
0
                    std::string(), osNewDimName, srcDims[nCurSrcDim]->GetType(),
5474
0
                    range.m_nIncr > 0 ? srcDims[nCurSrcDim]->GetDirection()
5475
0
                                      : std::string(),
5476
0
                    newSize));
5477
0
            }
5478
0
            mapDimIdxToParentDimIdx.push_back(nCurSrcDim);
5479
0
            parentRanges.emplace_back(range);
5480
0
        }
5481
5482
0
        nCurSrcDim++;
5483
0
    }
5484
0
    for (; nCurSrcDim < srcDims.size(); nCurSrcDim++)
5485
0
    {
5486
0
        parentRanges.emplace_back(0, 1);
5487
0
        newDims.push_back(srcDims[nCurSrcDim]);
5488
0
        mapDimIdxToParentDimIdx.push_back(nCurSrcDim);
5489
0
    }
5490
5491
0
    GDALMDArray::ViewSpec viewSpec;
5492
0
    viewSpec.m_mapDimIdxToParentDimIdx = mapDimIdxToParentDimIdx;
5493
0
    viewSpec.m_parentRanges = parentRanges;
5494
0
    viewSpecs.emplace_back(std::move(viewSpec));
5495
5496
0
    return GDALSlicedMDArray::Create(self, viewExpr, std::move(newDims),
5497
0
                                     std::move(mapDimIdxToParentDimIdx),
5498
0
                                     std::move(parentRanges));
5499
0
}
5500
5501
/************************************************************************/
5502
/*                       GDALExtractFieldMDArray                        */
5503
/************************************************************************/
5504
5505
class GDALExtractFieldMDArray final : public GDALPamMDArray
5506
{
5507
  private:
5508
    std::shared_ptr<GDALMDArray> m_poParent{};
5509
    GDALExtendedDataType m_dt;
5510
    std::string m_srcCompName;
5511
    mutable std::vector<GByte> m_pabyNoData{};
5512
5513
  protected:
5514
    GDALExtractFieldMDArray(const std::shared_ptr<GDALMDArray> &poParent,
5515
                            const std::string &fieldName,
5516
                            const std::unique_ptr<GDALEDTComponent> &srcComp)
5517
0
        : GDALAbstractMDArray(std::string(), "Extract field " + fieldName +
5518
0
                                                 " of " +
5519
0
                                                 poParent->GetFullName()),
5520
0
          GDALPamMDArray(
5521
0
              std::string(),
5522
0
              "Extract field " + fieldName + " of " + poParent->GetFullName(),
5523
0
              GDALPamMultiDim::GetPAM(poParent), poParent->GetContext()),
5524
0
          m_poParent(poParent), m_dt(srcComp->GetType()),
5525
0
          m_srcCompName(srcComp->GetName())
5526
0
    {
5527
0
        m_pabyNoData.resize(m_dt.GetSize());
5528
0
    }
5529
5530
    bool IRead(const GUInt64 *arrayStartIdx, const size_t *count,
5531
               const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
5532
               const GDALExtendedDataType &bufferDataType,
5533
               void *pDstBuffer) const override;
5534
5535
    bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count,
5536
                     CSLConstList papszOptions) const override
5537
0
    {
5538
0
        return m_poParent->AdviseRead(arrayStartIdx, count, papszOptions);
5539
0
    }
5540
5541
  public:
5542
    static std::shared_ptr<GDALExtractFieldMDArray>
5543
    Create(const std::shared_ptr<GDALMDArray> &poParent,
5544
           const std::string &fieldName,
5545
           const std::unique_ptr<GDALEDTComponent> &srcComp)
5546
0
    {
5547
0
        auto newAr(std::shared_ptr<GDALExtractFieldMDArray>(
5548
0
            new GDALExtractFieldMDArray(poParent, fieldName, srcComp)));
5549
0
        newAr->SetSelf(newAr);
5550
0
        return newAr;
5551
0
    }
5552
5553
    ~GDALExtractFieldMDArray()
5554
0
    {
5555
0
        m_dt.FreeDynamicMemory(&m_pabyNoData[0]);
5556
0
    }
5557
5558
    bool IsWritable() const override
5559
0
    {
5560
0
        return m_poParent->IsWritable();
5561
0
    }
5562
5563
    const std::string &GetFilename() const override
5564
0
    {
5565
0
        return m_poParent->GetFilename();
5566
0
    }
5567
5568
    const std::vector<std::shared_ptr<GDALDimension>> &
5569
    GetDimensions() const override
5570
0
    {
5571
0
        return m_poParent->GetDimensions();
5572
0
    }
5573
5574
    const GDALExtendedDataType &GetDataType() const override
5575
0
    {
5576
0
        return m_dt;
5577
0
    }
5578
5579
    const std::string &GetUnit() const override
5580
0
    {
5581
0
        return m_poParent->GetUnit();
5582
0
    }
5583
5584
    std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override
5585
0
    {
5586
0
        return m_poParent->GetSpatialRef();
5587
0
    }
5588
5589
    const void *GetRawNoDataValue() const override
5590
0
    {
5591
0
        const void *parentNoData = m_poParent->GetRawNoDataValue();
5592
0
        if (parentNoData == nullptr)
5593
0
            return nullptr;
5594
5595
0
        m_dt.FreeDynamicMemory(&m_pabyNoData[0]);
5596
0
        memset(&m_pabyNoData[0], 0, m_dt.GetSize());
5597
5598
0
        std::vector<std::unique_ptr<GDALEDTComponent>> comps;
5599
0
        comps.emplace_back(std::unique_ptr<GDALEDTComponent>(
5600
0
            new GDALEDTComponent(m_srcCompName, 0, m_dt)));
5601
0
        auto tmpDT(GDALExtendedDataType::Create(std::string(), m_dt.GetSize(),
5602
0
                                                std::move(comps)));
5603
5604
0
        GDALExtendedDataType::CopyValue(parentNoData, m_poParent->GetDataType(),
5605
0
                                        &m_pabyNoData[0], tmpDT);
5606
5607
0
        return &m_pabyNoData[0];
5608
0
    }
5609
5610
    double GetOffset(bool *pbHasOffset,
5611
                     GDALDataType *peStorageType) const override
5612
0
    {
5613
0
        return m_poParent->GetOffset(pbHasOffset, peStorageType);
5614
0
    }
5615
5616
    double GetScale(bool *pbHasScale,
5617
                    GDALDataType *peStorageType) const override
5618
0
    {
5619
0
        return m_poParent->GetScale(pbHasScale, peStorageType);
5620
0
    }
5621
5622
    std::vector<GUInt64> GetBlockSize() const override
5623
0
    {
5624
0
        return m_poParent->GetBlockSize();
5625
0
    }
5626
};
5627
5628
/************************************************************************/
5629
/*                             IRead()                                  */
5630
/************************************************************************/
5631
5632
bool GDALExtractFieldMDArray::IRead(const GUInt64 *arrayStartIdx,
5633
                                    const size_t *count,
5634
                                    const GInt64 *arrayStep,
5635
                                    const GPtrDiff_t *bufferStride,
5636
                                    const GDALExtendedDataType &bufferDataType,
5637
                                    void *pDstBuffer) const
5638
0
{
5639
0
    std::vector<std::unique_ptr<GDALEDTComponent>> comps;
5640
0
    comps.emplace_back(std::unique_ptr<GDALEDTComponent>(
5641
0
        new GDALEDTComponent(m_srcCompName, 0, bufferDataType)));
5642
0
    auto tmpDT(GDALExtendedDataType::Create(
5643
0
        std::string(), bufferDataType.GetSize(), std::move(comps)));
5644
5645
0
    return m_poParent->Read(arrayStartIdx, count, arrayStep, bufferStride,
5646
0
                            tmpDT, pDstBuffer);
5647
0
}
5648
5649
/************************************************************************/
5650
/*                      CreateFieldNameExtractArray()                   */
5651
/************************************************************************/
5652
5653
static std::shared_ptr<GDALMDArray>
5654
CreateFieldNameExtractArray(const std::shared_ptr<GDALMDArray> &self,
5655
                            const std::string &fieldName)
5656
0
{
5657
0
    CPLAssert(self->GetDataType().GetClass() == GEDTC_COMPOUND);
5658
0
    const std::unique_ptr<GDALEDTComponent> *srcComp = nullptr;
5659
0
    for (const auto &comp : self->GetDataType().GetComponents())
5660
0
    {
5661
0
        if (comp->GetName() == fieldName)
5662
0
        {
5663
0
            srcComp = &comp;
5664
0
            break;
5665
0
        }
5666
0
    }
5667
0
    if (srcComp == nullptr)
5668
0
    {
5669
0
        CPLError(CE_Failure, CPLE_AppDefined, "Cannot find field %s",
5670
0
                 fieldName.c_str());
5671
0
        return nullptr;
5672
0
    }
5673
0
    return GDALExtractFieldMDArray::Create(self, fieldName, *srcComp);
5674
0
}
5675
5676
/************************************************************************/
5677
/*                             GetView()                                */
5678
/************************************************************************/
5679
5680
// clang-format off
5681
/** Return a view of the array using slicing or field access.
5682
 *
5683
 * The slice expression uses the same syntax as NumPy basic slicing and
5684
 * indexing. See
5685
 * https://www.numpy.org/devdocs/reference/arrays.indexing.html#basic-slicing-and-indexing
5686
 * Or it can use field access by name. See
5687
 * https://www.numpy.org/devdocs/reference/arrays.indexing.html#field-access
5688
 *
5689
 * Multiple [] bracket elements can be concatenated, with a slice expression
5690
 * or field name inside each.
5691
 *
5692
 * For basic slicing and indexing, inside each [] bracket element, a list of
5693
 * indexes that apply to successive source dimensions, can be specified, using
5694
 * integer indexing (e.g. 1), range indexing (start:stop:step), ellipsis (...)
5695
 * or newaxis, using a comma separator.
5696
 *
5697
 * Examples with a 2-dimensional array whose content is [[0,1,2,3],[4,5,6,7]].
5698
 * <ul>
5699
 * <li>GetView("[1][2]"): returns a 0-dimensional/scalar array with the value
5700
 *     at index 1 in the first dimension, and index 2 in the second dimension
5701
 *     from the source array. That is 5</li>
5702
 * <li>GetView("[1]")->GetView("[2]"): same as above. Above is actually
5703
 * implemented internally doing this intermediate slicing approach.</li>
5704
 * <li>GetView("[1,2]"): same as above, but a bit more performant.</li>
5705
 * <li>GetView("[1]"): returns a 1-dimensional array, sliced at index 1 in the
5706
 *     first dimension. That is [4,5,6,7].</li>
5707
 * <li>GetView("[:,2]"): returns a 1-dimensional array, sliced at index 2 in the
5708
 *     second dimension. That is [2,6].</li>
5709
 * <li>GetView("[:,2:3:]"): returns a 2-dimensional array, sliced at index 2 in
5710
 * the second dimension. That is [[2],[6]].</li>
5711
 * <li>GetView("[::,2]"): Same as
5712
 * above.</li> <li>GetView("[...,2]"): same as above, in that case, since the
5713
 * ellipsis only expands to one dimension here.</li>
5714
 * <li>GetView("[:,::2]"):
5715
 * returns a 2-dimensional array, with even-indexed elements of the second
5716
 * dimension. That is [[0,2],[4,6]].</li>
5717
 * <li>GetView("[:,1::2]"): returns a
5718
 * 2-dimensional array, with odd-indexed elements of the second dimension. That
5719
 * is [[1,3],[5,7]].</li>
5720
 * <li>GetView("[:,1:3:]"): returns a 2-dimensional
5721
 * array, with elements of the second dimension with index in the range [1,3[.
5722
 * That is [[1,2],[5,6]].</li>
5723
 * <li>GetView("[::-1,:]"): returns a 2-dimensional
5724
 * array, with the values in first dimension reversed. That is
5725
 * [[4,5,6,7],[0,1,2,3]].</li>
5726
 * <li>GetView("[newaxis,...]"): returns a
5727
 * 3-dimensional array, with an additional dimension of size 1 put at the
5728
 * beginning. That is [[[0,1,2,3],[4,5,6,7]]].</li>
5729
 * </ul>
5730
 *
5731
 * One difference with NumPy behavior is that ranges that would result in
5732
 * zero elements are not allowed (dimensions of size 0 not being allowed in the
5733
 * GDAL multidimensional model).
5734
 *
5735
 * For field access, the syntax to use is ["field_name"] or ['field_name'].
5736
 * Multiple field specification is not supported currently.
5737
 *
5738
 * Both type of access can be combined, e.g. GetView("[1]['field_name']")
5739
 *
5740
 * \note When using the GDAL Python bindings, natural Python syntax can be
5741
 * used. That is ar[0,::,1]["foo"] will be internally translated to
5742
 * ar.GetView("[0,::,1]['foo']")
5743
 * \note When using the C++ API and integer indexing only, you may use the
5744
 * at(idx0, idx1, ...) method.
5745
 *
5746
 * The returned array holds a reference to the original one, and thus is
5747
 * a view of it (not a copy). If the content of the original array changes,
5748
 * the content of the view array too. When using basic slicing and indexing,
5749
 * the view can be written if the underlying array is writable.
5750
 *
5751
 * This is the same as the C function GDALMDArrayGetView()
5752
 *
5753
 * @param viewExpr Expression expressing basic slicing and indexing, or field
5754
 * access.
5755
 * @return a new array, that holds a reference to the original one, and thus is
5756
 * a view of it (not a copy), or nullptr in case of error.
5757
 */
5758
// clang-format on
5759
5760
std::shared_ptr<GDALMDArray>
5761
GDALMDArray::GetView(const std::string &viewExpr) const
5762
0
{
5763
0
    std::vector<ViewSpec> viewSpecs;
5764
0
    return GetView(viewExpr, true, viewSpecs);
5765
0
}
5766
5767
//! @cond Doxygen_Suppress
5768
std::shared_ptr<GDALMDArray>
5769
GDALMDArray::GetView(const std::string &viewExpr, bool bRenameDimensions,
5770
                     std::vector<ViewSpec> &viewSpecs) const
5771
0
{
5772
0
    auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock());
5773
0
    if (!self)
5774
0
    {
5775
0
        CPLError(CE_Failure, CPLE_AppDefined,
5776
0
                 "Driver implementation issue: m_pSelf not set !");
5777
0
        return nullptr;
5778
0
    }
5779
0
    std::string curExpr(viewExpr);
5780
0
    while (true)
5781
0
    {
5782
0
        if (curExpr.empty() || curExpr[0] != '[')
5783
0
        {
5784
0
            CPLError(CE_Failure, CPLE_AppDefined,
5785
0
                     "Slice string should start with ['");
5786
0
            return nullptr;
5787
0
        }
5788
5789
0
        std::string fieldName;
5790
0
        size_t endExpr;
5791
0
        if (curExpr.size() > 2 && (curExpr[1] == '"' || curExpr[1] == '\''))
5792
0
        {
5793
0
            if (self->GetDataType().GetClass() != GEDTC_COMPOUND)
5794
0
            {
5795
0
                CPLError(CE_Failure, CPLE_AppDefined,
5796
0
                         "Field access not allowed on non-compound data type");
5797
0
                return nullptr;
5798
0
            }
5799
0
            size_t idx = 2;
5800
0
            for (; idx < curExpr.size(); idx++)
5801
0
            {
5802
0
                const char ch = curExpr[idx];
5803
0
                if (ch == curExpr[1])
5804
0
                    break;
5805
0
                if (ch == '\\' && idx + 1 < curExpr.size())
5806
0
                {
5807
0
                    fieldName += curExpr[idx + 1];
5808
0
                    idx++;
5809
0
                }
5810
0
                else
5811
0
                {
5812
0
                    fieldName += ch;
5813
0
                }
5814
0
            }
5815
0
            if (idx + 1 >= curExpr.size() || curExpr[idx + 1] != ']')
5816
0
            {
5817
0
                CPLError(CE_Failure, CPLE_AppDefined,
5818
0
                         "Invalid field access specification");
5819
0
                return nullptr;
5820
0
            }
5821
0
            endExpr = idx + 1;
5822
0
        }
5823
0
        else
5824
0
        {
5825
0
            endExpr = curExpr.find(']');
5826
0
        }
5827
0
        if (endExpr == std::string::npos)
5828
0
        {
5829
0
            CPLError(CE_Failure, CPLE_AppDefined, "Missing ]'");
5830
0
            return nullptr;
5831
0
        }
5832
0
        if (endExpr == 1)
5833
0
        {
5834
0
            CPLError(CE_Failure, CPLE_AppDefined, "[] not allowed");
5835
0
            return nullptr;
5836
0
        }
5837
0
        std::string activeSlice(curExpr.substr(1, endExpr - 1));
5838
5839
0
        if (!fieldName.empty())
5840
0
        {
5841
0
            ViewSpec viewSpec;
5842
0
            viewSpec.m_osFieldName = fieldName;
5843
0
            viewSpecs.emplace_back(std::move(viewSpec));
5844
0
        }
5845
5846
0
        auto newArray = !fieldName.empty()
5847
0
                            ? CreateFieldNameExtractArray(self, fieldName)
5848
0
                            : CreateSlicedArray(self, viewExpr, activeSlice,
5849
0
                                                bRenameDimensions, viewSpecs);
5850
5851
0
        if (endExpr == curExpr.size() - 1)
5852
0
        {
5853
0
            return newArray;
5854
0
        }
5855
0
        self = std::move(newArray);
5856
0
        curExpr = curExpr.substr(endExpr + 1);
5857
0
    }
5858
0
}
5859
5860
//! @endcond
5861
5862
std::shared_ptr<GDALMDArray>
5863
GDALMDArray::GetView(const std::vector<GUInt64> &indices) const
5864
0
{
5865
0
    std::string osExpr("[");
5866
0
    bool bFirst = true;
5867
0
    for (const auto &idx : indices)
5868
0
    {
5869
0
        if (!bFirst)
5870
0
            osExpr += ',';
5871
0
        bFirst = false;
5872
0
        osExpr += CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(idx));
5873
0
    }
5874
0
    return GetView(osExpr + ']');
5875
0
}
5876
5877
/************************************************************************/
5878
/*                            operator[]                                */
5879
/************************************************************************/
5880
5881
/** Return a view of the array using field access
5882
 *
5883
 * Equivalent of GetView("['fieldName']")
5884
 *
5885
 * \note When operationg on a shared_ptr, use (*array)["fieldName"] syntax.
5886
 */
5887
std::shared_ptr<GDALMDArray>
5888
GDALMDArray::operator[](const std::string &fieldName) const
5889
0
{
5890
0
    return GetView(CPLSPrintf("['%s']", CPLString(fieldName)
5891
0
                                            .replaceAll('\\', "\\\\")
5892
0
                                            .replaceAll('\'', "\\\'")
5893
0
                                            .c_str()));
5894
0
}
5895
5896
/************************************************************************/
5897
/*                      GDALMDArrayTransposed                           */
5898
/************************************************************************/
5899
5900
class GDALMDArrayTransposed final : public GDALPamMDArray
5901
{
5902
  private:
5903
    std::shared_ptr<GDALMDArray> m_poParent{};
5904
    std::vector<int> m_anMapNewAxisToOldAxis{};
5905
    std::vector<std::shared_ptr<GDALDimension>> m_dims{};
5906
5907
    mutable std::vector<GUInt64> m_parentStart;
5908
    mutable std::vector<size_t> m_parentCount;
5909
    mutable std::vector<GInt64> m_parentStep;
5910
    mutable std::vector<GPtrDiff_t> m_parentStride;
5911
5912
    void PrepareParentArrays(const GUInt64 *arrayStartIdx, const size_t *count,
5913
                             const GInt64 *arrayStep,
5914
                             const GPtrDiff_t *bufferStride) const;
5915
5916
    static std::string
5917
    MappingToStr(const std::vector<int> &anMapNewAxisToOldAxis)
5918
0
    {
5919
0
        std::string ret;
5920
0
        ret += '[';
5921
0
        for (size_t i = 0; i < anMapNewAxisToOldAxis.size(); ++i)
5922
0
        {
5923
0
            if (i > 0)
5924
0
                ret += ',';
5925
0
            ret += CPLSPrintf("%d", anMapNewAxisToOldAxis[i]);
5926
0
        }
5927
0
        ret += ']';
5928
0
        return ret;
5929
0
    }
5930
5931
  protected:
5932
    GDALMDArrayTransposed(const std::shared_ptr<GDALMDArray> &poParent,
5933
                          const std::vector<int> &anMapNewAxisToOldAxis,
5934
                          std::vector<std::shared_ptr<GDALDimension>> &&dims)
5935
0
        : GDALAbstractMDArray(std::string(),
5936
0
                              "Transposed view of " + poParent->GetFullName() +
5937
0
                                  " along " +
5938
0
                                  MappingToStr(anMapNewAxisToOldAxis)),
5939
0
          GDALPamMDArray(std::string(),
5940
0
                         "Transposed view of " + poParent->GetFullName() +
5941
0
                             " along " + MappingToStr(anMapNewAxisToOldAxis),
5942
0
                         GDALPamMultiDim::GetPAM(poParent),
5943
0
                         poParent->GetContext()),
5944
0
          m_poParent(std::move(poParent)),
5945
0
          m_anMapNewAxisToOldAxis(anMapNewAxisToOldAxis),
5946
0
          m_dims(std::move(dims)),
5947
0
          m_parentStart(m_poParent->GetDimensionCount()),
5948
0
          m_parentCount(m_poParent->GetDimensionCount()),
5949
0
          m_parentStep(m_poParent->GetDimensionCount()),
5950
0
          m_parentStride(m_poParent->GetDimensionCount())
5951
0
    {
5952
0
    }
5953
5954
    bool IRead(const GUInt64 *arrayStartIdx, const size_t *count,
5955
               const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
5956
               const GDALExtendedDataType &bufferDataType,
5957
               void *pDstBuffer) const override;
5958
5959
    bool IWrite(const GUInt64 *arrayStartIdx, const size_t *count,
5960
                const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
5961
                const GDALExtendedDataType &bufferDataType,
5962
                const void *pSrcBuffer) override;
5963
5964
    bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count,
5965
                     CSLConstList papszOptions) const override;
5966
5967
  public:
5968
    static std::shared_ptr<GDALMDArrayTransposed>
5969
    Create(const std::shared_ptr<GDALMDArray> &poParent,
5970
           const std::vector<int> &anMapNewAxisToOldAxis)
5971
0
    {
5972
0
        const auto &parentDims(poParent->GetDimensions());
5973
0
        std::vector<std::shared_ptr<GDALDimension>> dims;
5974
0
        for (const auto iOldAxis : anMapNewAxisToOldAxis)
5975
0
        {
5976
0
            if (iOldAxis < 0)
5977
0
            {
5978
0
                dims.push_back(std::make_shared<GDALDimension>(
5979
0
                    std::string(), "newaxis", std::string(), std::string(), 1));
5980
0
            }
5981
0
            else
5982
0
            {
5983
0
                dims.emplace_back(parentDims[iOldAxis]);
5984
0
            }
5985
0
        }
5986
5987
0
        auto newAr(
5988
0
            std::shared_ptr<GDALMDArrayTransposed>(new GDALMDArrayTransposed(
5989
0
                poParent, anMapNewAxisToOldAxis, std::move(dims))));
5990
0
        newAr->SetSelf(newAr);
5991
0
        return newAr;
5992
0
    }
5993
5994
    bool IsWritable() const override
5995
0
    {
5996
0
        return m_poParent->IsWritable();
5997
0
    }
5998
5999
    const std::string &GetFilename() const override
6000
0
    {
6001
0
        return m_poParent->GetFilename();
6002
0
    }
6003
6004
    const std::vector<std::shared_ptr<GDALDimension>> &
6005
    GetDimensions() const override
6006
0
    {
6007
0
        return m_dims;
6008
0
    }
6009
6010
    const GDALExtendedDataType &GetDataType() const override
6011
0
    {
6012
0
        return m_poParent->GetDataType();
6013
0
    }
6014
6015
    const std::string &GetUnit() const override
6016
0
    {
6017
0
        return m_poParent->GetUnit();
6018
0
    }
6019
6020
    std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override
6021
0
    {
6022
0
        auto poSrcSRS = m_poParent->GetSpatialRef();
6023
0
        if (!poSrcSRS)
6024
0
            return nullptr;
6025
0
        auto srcMapping = poSrcSRS->GetDataAxisToSRSAxisMapping();
6026
0
        std::vector<int> dstMapping;
6027
0
        for (int srcAxis : srcMapping)
6028
0
        {
6029
0
            bool bFound = false;
6030
0
            for (size_t i = 0; i < m_anMapNewAxisToOldAxis.size(); i++)
6031
0
            {
6032
0
                if (m_anMapNewAxisToOldAxis[i] == srcAxis - 1)
6033
0
                {
6034
0
                    dstMapping.push_back(static_cast<int>(i) + 1);
6035
0
                    bFound = true;
6036
0
                    break;
6037
0
                }
6038
0
            }
6039
0
            if (!bFound)
6040
0
            {
6041
0
                dstMapping.push_back(0);
6042
0
            }
6043
0
        }
6044
0
        auto poClone(std::shared_ptr<OGRSpatialReference>(poSrcSRS->Clone()));
6045
0
        poClone->SetDataAxisToSRSAxisMapping(dstMapping);
6046
0
        return poClone;
6047
0
    }
6048
6049
    const void *GetRawNoDataValue() const override
6050
0
    {
6051
0
        return m_poParent->GetRawNoDataValue();
6052
0
    }
6053
6054
    // bool SetRawNoDataValue(const void* pRawNoData) override { return
6055
    // m_poParent->SetRawNoDataValue(pRawNoData); }
6056
6057
    double GetOffset(bool *pbHasOffset,
6058
                     GDALDataType *peStorageType) const override
6059
0
    {
6060
0
        return m_poParent->GetOffset(pbHasOffset, peStorageType);
6061
0
    }
6062
6063
    double GetScale(bool *pbHasScale,
6064
                    GDALDataType *peStorageType) const override
6065
0
    {
6066
0
        return m_poParent->GetScale(pbHasScale, peStorageType);
6067
0
    }
6068
6069
    // bool SetOffset(double dfOffset) override { return
6070
    // m_poParent->SetOffset(dfOffset); }
6071
6072
    // bool SetScale(double dfScale) override { return
6073
    // m_poParent->SetScale(dfScale); }
6074
6075
    std::vector<GUInt64> GetBlockSize() const override
6076
0
    {
6077
0
        std::vector<GUInt64> ret(GetDimensionCount());
6078
0
        const auto parentBlockSize(m_poParent->GetBlockSize());
6079
0
        for (size_t i = 0; i < m_anMapNewAxisToOldAxis.size(); ++i)
6080
0
        {
6081
0
            const auto iOldAxis = m_anMapNewAxisToOldAxis[i];
6082
0
            if (iOldAxis >= 0)
6083
0
            {
6084
0
                ret[i] = parentBlockSize[iOldAxis];
6085
0
            }
6086
0
        }
6087
0
        return ret;
6088
0
    }
6089
6090
    std::shared_ptr<GDALAttribute>
6091
    GetAttribute(const std::string &osName) const override
6092
0
    {
6093
0
        return m_poParent->GetAttribute(osName);
6094
0
    }
6095
6096
    std::vector<std::shared_ptr<GDALAttribute>>
6097
    GetAttributes(CSLConstList papszOptions = nullptr) const override
6098
0
    {
6099
0
        return m_poParent->GetAttributes(papszOptions);
6100
0
    }
6101
};
6102
6103
/************************************************************************/
6104
/*                         PrepareParentArrays()                        */
6105
/************************************************************************/
6106
6107
void GDALMDArrayTransposed::PrepareParentArrays(
6108
    const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep,
6109
    const GPtrDiff_t *bufferStride) const
6110
0
{
6111
0
    for (size_t i = 0; i < m_anMapNewAxisToOldAxis.size(); ++i)
6112
0
    {
6113
0
        const auto iOldAxis = m_anMapNewAxisToOldAxis[i];
6114
0
        if (iOldAxis >= 0)
6115
0
        {
6116
0
            m_parentStart[iOldAxis] = arrayStartIdx[i];
6117
0
            m_parentCount[iOldAxis] = count[i];
6118
0
            if (arrayStep)  // only null when called from IAdviseRead()
6119
0
            {
6120
0
                m_parentStep[iOldAxis] = arrayStep[i];
6121
0
            }
6122
0
            if (bufferStride)  // only null when called from IAdviseRead()
6123
0
            {
6124
0
                m_parentStride[iOldAxis] = bufferStride[i];
6125
0
            }
6126
0
        }
6127
0
    }
6128
0
}
6129
6130
/************************************************************************/
6131
/*                             IRead()                                  */
6132
/************************************************************************/
6133
6134
bool GDALMDArrayTransposed::IRead(const GUInt64 *arrayStartIdx,
6135
                                  const size_t *count, const GInt64 *arrayStep,
6136
                                  const GPtrDiff_t *bufferStride,
6137
                                  const GDALExtendedDataType &bufferDataType,
6138
                                  void *pDstBuffer) const
6139
0
{
6140
0
    PrepareParentArrays(arrayStartIdx, count, arrayStep, bufferStride);
6141
0
    return m_poParent->Read(m_parentStart.data(), m_parentCount.data(),
6142
0
                            m_parentStep.data(), m_parentStride.data(),
6143
0
                            bufferDataType, pDstBuffer);
6144
0
}
6145
6146
/************************************************************************/
6147
/*                            IWrite()                                  */
6148
/************************************************************************/
6149
6150
bool GDALMDArrayTransposed::IWrite(const GUInt64 *arrayStartIdx,
6151
                                   const size_t *count, const GInt64 *arrayStep,
6152
                                   const GPtrDiff_t *bufferStride,
6153
                                   const GDALExtendedDataType &bufferDataType,
6154
                                   const void *pSrcBuffer)
6155
0
{
6156
0
    PrepareParentArrays(arrayStartIdx, count, arrayStep, bufferStride);
6157
0
    return m_poParent->Write(m_parentStart.data(), m_parentCount.data(),
6158
0
                             m_parentStep.data(), m_parentStride.data(),
6159
0
                             bufferDataType, pSrcBuffer);
6160
0
}
6161
6162
/************************************************************************/
6163
/*                             IAdviseRead()                            */
6164
/************************************************************************/
6165
6166
bool GDALMDArrayTransposed::IAdviseRead(const GUInt64 *arrayStartIdx,
6167
                                        const size_t *count,
6168
                                        CSLConstList papszOptions) const
6169
0
{
6170
0
    PrepareParentArrays(arrayStartIdx, count, nullptr, nullptr);
6171
0
    return m_poParent->AdviseRead(m_parentStart.data(), m_parentCount.data(),
6172
0
                                  papszOptions);
6173
0
}
6174
6175
/************************************************************************/
6176
/*                           Transpose()                                */
6177
/************************************************************************/
6178
6179
/** Return a view of the array whose axis have been reordered.
6180
 *
6181
 * The anMapNewAxisToOldAxis parameter should contain all the values between 0
6182
 * and GetDimensionCount() - 1, and each only once.
6183
 * -1 can be used as a special index value to ask for the insertion of a new
6184
 * axis of size 1.
6185
 * The new array will have anMapNewAxisToOldAxis.size() axis, and if i is the
6186
 * index of one of its dimension, it corresponds to the axis of index
6187
 * anMapNewAxisToOldAxis[i] from the current array.
6188
 *
6189
 * This is similar to the numpy.transpose() method
6190
 *
6191
 * The returned array holds a reference to the original one, and thus is
6192
 * a view of it (not a copy). If the content of the original array changes,
6193
 * the content of the view array too. The view can be written if the underlying
6194
 * array is writable.
6195
 *
6196
 * Note that I/O performance in such a transposed view might be poor.
6197
 *
6198
 * This is the same as the C function GDALMDArrayTranspose().
6199
 *
6200
 * @return a new array, that holds a reference to the original one, and thus is
6201
 * a view of it (not a copy), or nullptr in case of error.
6202
 */
6203
std::shared_ptr<GDALMDArray>
6204
GDALMDArray::Transpose(const std::vector<int> &anMapNewAxisToOldAxis) const
6205
0
{
6206
0
    auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock());
6207
0
    if (!self)
6208
0
    {
6209
0
        CPLError(CE_Failure, CPLE_AppDefined,
6210
0
                 "Driver implementation issue: m_pSelf not set !");
6211
0
        return nullptr;
6212
0
    }
6213
0
    const int nDims = static_cast<int>(GetDimensionCount());
6214
0
    std::vector<bool> alreadyUsedOldAxis(nDims, false);
6215
0
    int nCountOldAxis = 0;
6216
0
    for (const auto iOldAxis : anMapNewAxisToOldAxis)
6217
0
    {
6218
0
        if (iOldAxis < -1 || iOldAxis >= nDims)
6219
0
        {
6220
0
            CPLError(CE_Failure, CPLE_AppDefined, "Invalid axis number");
6221
0
            return nullptr;
6222
0
        }
6223
0
        if (iOldAxis >= 0)
6224
0
        {
6225
0
            if (alreadyUsedOldAxis[iOldAxis])
6226
0
            {
6227
0
                CPLError(CE_Failure, CPLE_AppDefined, "Axis %d is repeated",
6228
0
                         iOldAxis);
6229
0
                return nullptr;
6230
0
            }
6231
0
            alreadyUsedOldAxis[iOldAxis] = true;
6232
0
            nCountOldAxis++;
6233
0
        }
6234
0
    }
6235
0
    if (nCountOldAxis != nDims)
6236
0
    {
6237
0
        CPLError(CE_Failure, CPLE_AppDefined,
6238
0
                 "One or several original axis missing");
6239
0
        return nullptr;
6240
0
    }
6241
0
    return GDALMDArrayTransposed::Create(self, anMapNewAxisToOldAxis);
6242
0
}
6243
6244
/************************************************************************/
6245
/*                             IRead()                                  */
6246
/************************************************************************/
6247
6248
bool GDALMDArrayUnscaled::IRead(const GUInt64 *arrayStartIdx,
6249
                                const size_t *count, const GInt64 *arrayStep,
6250
                                const GPtrDiff_t *bufferStride,
6251
                                const GDALExtendedDataType &bufferDataType,
6252
                                void *pDstBuffer) const
6253
0
{
6254
0
    const double dfScale = m_dfScale;
6255
0
    const double dfOffset = m_dfOffset;
6256
0
    const bool bDTIsComplex = GDALDataTypeIsComplex(m_dt.GetNumericDataType());
6257
0
    const auto dtDouble =
6258
0
        GDALExtendedDataType::Create(bDTIsComplex ? GDT_CFloat64 : GDT_Float64);
6259
0
    const size_t nDTSize = dtDouble.GetSize();
6260
0
    const bool bTempBufferNeeded = (dtDouble != bufferDataType);
6261
6262
0
    double adfSrcNoData[2] = {0, 0};
6263
0
    if (m_bHasNoData)
6264
0
    {
6265
0
        GDALExtendedDataType::CopyValue(m_poParent->GetRawNoDataValue(),
6266
0
                                        m_poParent->GetDataType(),
6267
0
                                        &adfSrcNoData[0], dtDouble);
6268
0
    }
6269
6270
0
    const auto nDims = GetDimensions().size();
6271
0
    if (nDims == 0)
6272
0
    {
6273
0
        double adfVal[2];
6274
0
        if (!m_poParent->Read(arrayStartIdx, count, arrayStep, bufferStride,
6275
0
                              dtDouble, &adfVal[0]))
6276
0
        {
6277
0
            return false;
6278
0
        }
6279
0
        if (!m_bHasNoData || adfVal[0] != adfSrcNoData[0])
6280
0
        {
6281
0
            adfVal[0] = adfVal[0] * dfScale + dfOffset;
6282
0
            if (bDTIsComplex)
6283
0
            {
6284
0
                adfVal[1] = adfVal[1] * dfScale + dfOffset;
6285
0
            }
6286
0
            GDALExtendedDataType::CopyValue(&adfVal[0], dtDouble, pDstBuffer,
6287
0
                                            bufferDataType);
6288
0
        }
6289
0
        else
6290
0
        {
6291
0
            GDALExtendedDataType::CopyValue(m_abyRawNoData.data(), m_dt,
6292
0
                                            pDstBuffer, bufferDataType);
6293
0
        }
6294
0
        return true;
6295
0
    }
6296
6297
0
    std::vector<GPtrDiff_t> actualBufferStrideVector;
6298
0
    const GPtrDiff_t *actualBufferStridePtr = bufferStride;
6299
0
    void *pTempBuffer = pDstBuffer;
6300
0
    if (bTempBufferNeeded)
6301
0
    {
6302
0
        size_t nElts = 1;
6303
0
        actualBufferStrideVector.resize(nDims);
6304
0
        for (size_t i = 0; i < nDims; i++)
6305
0
            nElts *= count[i];
6306
0
        actualBufferStrideVector.back() = 1;
6307
0
        for (size_t i = nDims - 1; i > 0;)
6308
0
        {
6309
0
            --i;
6310
0
            actualBufferStrideVector[i] =
6311
0
                actualBufferStrideVector[i + 1] * count[i + 1];
6312
0
        }
6313
0
        actualBufferStridePtr = actualBufferStrideVector.data();
6314
0
        pTempBuffer = VSI_MALLOC2_VERBOSE(nDTSize, nElts);
6315
0
        if (!pTempBuffer)
6316
0
            return false;
6317
0
    }
6318
0
    if (!m_poParent->Read(arrayStartIdx, count, arrayStep,
6319
0
                          actualBufferStridePtr, dtDouble, pTempBuffer))
6320
0
    {
6321
0
        if (bTempBufferNeeded)
6322
0
            VSIFree(pTempBuffer);
6323
0
        return false;
6324
0
    }
6325
6326
0
    struct Stack
6327
0
    {
6328
0
        size_t nIters = 0;
6329
0
        double *src_ptr = nullptr;
6330
0
        GByte *dst_ptr = nullptr;
6331
0
        GPtrDiff_t src_inc_offset = 0;
6332
0
        GPtrDiff_t dst_inc_offset = 0;
6333
0
    };
6334
6335
0
    std::vector<Stack> stack(nDims);
6336
0
    const size_t nBufferDTSize = bufferDataType.GetSize();
6337
0
    for (size_t i = 0; i < nDims; i++)
6338
0
    {
6339
0
        stack[i].src_inc_offset =
6340
0
            actualBufferStridePtr[i] * (bDTIsComplex ? 2 : 1);
6341
0
        stack[i].dst_inc_offset =
6342
0
            static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize);
6343
0
    }
6344
0
    stack[0].src_ptr = static_cast<double *>(pTempBuffer);
6345
0
    stack[0].dst_ptr = static_cast<GByte *>(pDstBuffer);
6346
6347
0
    size_t dimIdx = 0;
6348
0
    const size_t nDimsMinus1 = nDims - 1;
6349
0
    GByte abyDstNoData[16];
6350
0
    CPLAssert(nBufferDTSize <= sizeof(abyDstNoData));
6351
0
    GDALExtendedDataType::CopyValue(m_abyRawNoData.data(), m_dt, abyDstNoData,
6352
0
                                    bufferDataType);
6353
6354
0
lbl_next_depth:
6355
0
    if (dimIdx == nDimsMinus1)
6356
0
    {
6357
0
        auto nIters = count[dimIdx];
6358
0
        double *padfVal = stack[dimIdx].src_ptr;
6359
0
        GByte *dst_ptr = stack[dimIdx].dst_ptr;
6360
0
        while (true)
6361
0
        {
6362
0
            if (!m_bHasNoData || padfVal[0] != adfSrcNoData[0])
6363
0
            {
6364
0
                padfVal[0] = padfVal[0] * dfScale + dfOffset;
6365
0
                if (bDTIsComplex)
6366
0
                {
6367
0
                    padfVal[1] = padfVal[1] * dfScale + dfOffset;
6368
0
                }
6369
0
                if (bTempBufferNeeded)
6370
0
                {
6371
0
                    GDALExtendedDataType::CopyValue(&padfVal[0], dtDouble,
6372
0
                                                    dst_ptr, bufferDataType);
6373
0
                }
6374
0
            }
6375
0
            else
6376
0
            {
6377
0
                memcpy(dst_ptr, abyDstNoData, nBufferDTSize);
6378
0
            }
6379
6380
0
            if ((--nIters) == 0)
6381
0
                break;
6382
0
            padfVal += stack[dimIdx].src_inc_offset;
6383
0
            dst_ptr += stack[dimIdx].dst_inc_offset;
6384
0
        }
6385
0
    }
6386
0
    else
6387
0
    {
6388
0
        stack[dimIdx].nIters = count[dimIdx];
6389
0
        while (true)
6390
0
        {
6391
0
            dimIdx++;
6392
0
            stack[dimIdx].src_ptr = stack[dimIdx - 1].src_ptr;
6393
0
            stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr;
6394
0
            goto lbl_next_depth;
6395
0
        lbl_return_to_caller:
6396
0
            dimIdx--;
6397
0
            if ((--stack[dimIdx].nIters) == 0)
6398
0
                break;
6399
0
            stack[dimIdx].src_ptr += stack[dimIdx].src_inc_offset;
6400
0
            stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset;
6401
0
        }
6402
0
    }
6403
0
    if (dimIdx > 0)
6404
0
        goto lbl_return_to_caller;
6405
6406
0
    if (bTempBufferNeeded)
6407
0
        VSIFree(pTempBuffer);
6408
0
    return true;
6409
0
}
6410
6411
/************************************************************************/
6412
/*                             IWrite()                                 */
6413
/************************************************************************/
6414
6415
bool GDALMDArrayUnscaled::IWrite(const GUInt64 *arrayStartIdx,
6416
                                 const size_t *count, const GInt64 *arrayStep,
6417
                                 const GPtrDiff_t *bufferStride,
6418
                                 const GDALExtendedDataType &bufferDataType,
6419
                                 const void *pSrcBuffer)
6420
0
{
6421
0
    const double dfScale = m_dfScale;
6422
0
    const double dfOffset = m_dfOffset;
6423
0
    const bool bDTIsComplex = GDALDataTypeIsComplex(m_dt.GetNumericDataType());
6424
0
    const auto dtDouble =
6425
0
        GDALExtendedDataType::Create(bDTIsComplex ? GDT_CFloat64 : GDT_Float64);
6426
0
    const size_t nDTSize = dtDouble.GetSize();
6427
0
    const bool bIsBufferDataTypeNativeDataType = (dtDouble == bufferDataType);
6428
0
    const bool bSelfAndParentHaveNoData =
6429
0
        m_bHasNoData && m_poParent->GetRawNoDataValue() != nullptr;
6430
0
    double dfNoData = 0;
6431
0
    if (m_bHasNoData)
6432
0
    {
6433
0
        GDALCopyWords64(m_abyRawNoData.data(), m_dt.GetNumericDataType(), 0,
6434
0
                        &dfNoData, GDT_Float64, 0, 1);
6435
0
    }
6436
6437
0
    double adfSrcNoData[2] = {0, 0};
6438
0
    if (bSelfAndParentHaveNoData)
6439
0
    {
6440
0
        GDALExtendedDataType::CopyValue(m_poParent->GetRawNoDataValue(),
6441
0
                                        m_poParent->GetDataType(),
6442
0
                                        &adfSrcNoData[0], dtDouble);
6443
0
    }
6444
6445
0
    const auto nDims = GetDimensions().size();
6446
0
    if (nDims == 0)
6447
0
    {
6448
0
        double adfVal[2];
6449
0
        GDALExtendedDataType::CopyValue(pSrcBuffer, bufferDataType, &adfVal[0],
6450
0
                                        dtDouble);
6451
0
        if (bSelfAndParentHaveNoData &&
6452
0
            (std::isnan(adfVal[0]) || adfVal[0] == dfNoData))
6453
0
        {
6454
0
            return m_poParent->Write(arrayStartIdx, count, arrayStep,
6455
0
                                     bufferStride, m_poParent->GetDataType(),
6456
0
                                     m_poParent->GetRawNoDataValue());
6457
0
        }
6458
0
        else
6459
0
        {
6460
0
            adfVal[0] = (adfVal[0] - dfOffset) / dfScale;
6461
0
            if (bDTIsComplex)
6462
0
            {
6463
0
                adfVal[1] = (adfVal[1] - dfOffset) / dfScale;
6464
0
            }
6465
0
            return m_poParent->Write(arrayStartIdx, count, arrayStep,
6466
0
                                     bufferStride, dtDouble, &adfVal[0]);
6467
0
        }
6468
0
    }
6469
6470
0
    std::vector<GPtrDiff_t> tmpBufferStrideVector;
6471
0
    size_t nElts = 1;
6472
0
    tmpBufferStrideVector.resize(nDims);
6473
0
    for (size_t i = 0; i < nDims; i++)
6474
0
        nElts *= count[i];
6475
0
    tmpBufferStrideVector.back() = 1;
6476
0
    for (size_t i = nDims - 1; i > 0;)
6477
0
    {
6478
0
        --i;
6479
0
        tmpBufferStrideVector[i] = tmpBufferStrideVector[i + 1] * count[i + 1];
6480
0
    }
6481
0
    const GPtrDiff_t *tmpBufferStridePtr = tmpBufferStrideVector.data();
6482
0
    void *pTempBuffer = VSI_MALLOC2_VERBOSE(nDTSize, nElts);
6483
0
    if (!pTempBuffer)
6484
0
        return false;
6485
6486
0
    struct Stack
6487
0
    {
6488
0
        size_t nIters = 0;
6489
0
        double *dst_ptr = nullptr;
6490
0
        const GByte *src_ptr = nullptr;
6491
0
        GPtrDiff_t src_inc_offset = 0;
6492
0
        GPtrDiff_t dst_inc_offset = 0;
6493
0
    };
6494
6495
0
    std::vector<Stack> stack(nDims);
6496
0
    const size_t nBufferDTSize = bufferDataType.GetSize();
6497
0
    for (size_t i = 0; i < nDims; i++)
6498
0
    {
6499
0
        stack[i].dst_inc_offset =
6500
0
            tmpBufferStridePtr[i] * (bDTIsComplex ? 2 : 1);
6501
0
        stack[i].src_inc_offset =
6502
0
            static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize);
6503
0
    }
6504
0
    stack[0].dst_ptr = static_cast<double *>(pTempBuffer);
6505
0
    stack[0].src_ptr = static_cast<const GByte *>(pSrcBuffer);
6506
6507
0
    size_t dimIdx = 0;
6508
0
    const size_t nDimsMinus1 = nDims - 1;
6509
6510
0
lbl_next_depth:
6511
0
    if (dimIdx == nDimsMinus1)
6512
0
    {
6513
0
        auto nIters = count[dimIdx];
6514
0
        double *dst_ptr = stack[dimIdx].dst_ptr;
6515
0
        const GByte *src_ptr = stack[dimIdx].src_ptr;
6516
0
        while (true)
6517
0
        {
6518
0
            double adfVal[2];
6519
0
            const double *padfSrcVal;
6520
0
            if (bIsBufferDataTypeNativeDataType)
6521
0
            {
6522
0
                padfSrcVal = reinterpret_cast<const double *>(src_ptr);
6523
0
            }
6524
0
            else
6525
0
            {
6526
0
                GDALExtendedDataType::CopyValue(src_ptr, bufferDataType,
6527
0
                                                &adfVal[0], dtDouble);
6528
0
                padfSrcVal = adfVal;
6529
0
            }
6530
6531
0
            if (bSelfAndParentHaveNoData &&
6532
0
                (std::isnan(padfSrcVal[0]) || padfSrcVal[0] == dfNoData))
6533
0
            {
6534
0
                dst_ptr[0] = adfSrcNoData[0];
6535
0
                if (bDTIsComplex)
6536
0
                {
6537
0
                    dst_ptr[1] = adfSrcNoData[1];
6538
0
                }
6539
0
            }
6540
0
            else
6541
0
            {
6542
0
                dst_ptr[0] = (padfSrcVal[0] - dfOffset) / dfScale;
6543
0
                if (bDTIsComplex)
6544
0
                {
6545
0
                    dst_ptr[1] = (padfSrcVal[1] - dfOffset) / dfScale;
6546
0
                }
6547
0
            }
6548
6549
0
            if ((--nIters) == 0)
6550
0
                break;
6551
0
            dst_ptr += stack[dimIdx].dst_inc_offset;
6552
0
            src_ptr += stack[dimIdx].src_inc_offset;
6553
0
        }
6554
0
    }
6555
0
    else
6556
0
    {
6557
0
        stack[dimIdx].nIters = count[dimIdx];
6558
0
        while (true)
6559
0
        {
6560
0
            dimIdx++;
6561
0
            stack[dimIdx].src_ptr = stack[dimIdx - 1].src_ptr;
6562
0
            stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr;
6563
0
            goto lbl_next_depth;
6564
0
        lbl_return_to_caller:
6565
0
            dimIdx--;
6566
0
            if ((--stack[dimIdx].nIters) == 0)
6567
0
                break;
6568
0
            stack[dimIdx].src_ptr += stack[dimIdx].src_inc_offset;
6569
0
            stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset;
6570
0
        }
6571
0
    }
6572
0
    if (dimIdx > 0)
6573
0
        goto lbl_return_to_caller;
6574
6575
    // If the parent array is not double/complex-double, then convert the
6576
    // values to it, before calling Write(), as some implementations can be
6577
    // very slow when doing the type conversion.
6578
0
    const auto &eParentDT = m_poParent->GetDataType();
6579
0
    const size_t nParentDTSize = eParentDT.GetSize();
6580
0
    if (nParentDTSize <= nDTSize / 2)
6581
0
    {
6582
        // Copy in-place by making sure that source and target do not overlap
6583
0
        const auto eNumericDT = dtDouble.GetNumericDataType();
6584
0
        const auto eParentNumericDT = eParentDT.GetNumericDataType();
6585
6586
        // Copy first element
6587
0
        {
6588
0
            std::vector<GByte> abyTemp(nParentDTSize);
6589
0
            GDALCopyWords64(static_cast<GByte *>(pTempBuffer), eNumericDT,
6590
0
                            static_cast<int>(nDTSize), &abyTemp[0],
6591
0
                            eParentNumericDT, static_cast<int>(nParentDTSize),
6592
0
                            1);
6593
0
            memcpy(pTempBuffer, abyTemp.data(), abyTemp.size());
6594
0
        }
6595
        // Remaining elements
6596
0
        for (size_t i = 1; i < nElts; ++i)
6597
0
        {
6598
0
            GDALCopyWords64(
6599
0
                static_cast<GByte *>(pTempBuffer) + i * nDTSize, eNumericDT, 0,
6600
0
                static_cast<GByte *>(pTempBuffer) + i * nParentDTSize,
6601
0
                eParentNumericDT, 0, 1);
6602
0
        }
6603
0
    }
6604
6605
0
    const bool ret =
6606
0
        m_poParent->Write(arrayStartIdx, count, arrayStep, tmpBufferStridePtr,
6607
0
                          eParentDT, pTempBuffer);
6608
6609
0
    VSIFree(pTempBuffer);
6610
0
    return ret;
6611
0
}
6612
6613
/************************************************************************/
6614
/*                           GetUnscaled()                              */
6615
/************************************************************************/
6616
6617
/** Return an array that is the unscaled version of the current one.
6618
 *
6619
 * That is each value of the unscaled array will be
6620
 * unscaled_value = raw_value * GetScale() + GetOffset()
6621
 *
6622
 * Starting with GDAL 3.3, the Write() method is implemented and will convert
6623
 * from unscaled values to raw values.
6624
 *
6625
 * This is the same as the C function GDALMDArrayGetUnscaled().
6626
 *
6627
 * @param dfOverriddenScale Custom scale value instead of GetScale()
6628
 * @param dfOverriddenOffset Custom offset value instead of GetOffset()
6629
 * @param dfOverriddenDstNodata Custom target nodata value instead of NaN
6630
 * @return a new array, that holds a reference to the original one, and thus is
6631
 * a view of it (not a copy), or nullptr in case of error.
6632
 */
6633
std::shared_ptr<GDALMDArray>
6634
GDALMDArray::GetUnscaled(double dfOverriddenScale, double dfOverriddenOffset,
6635
                         double dfOverriddenDstNodata) const
6636
0
{
6637
0
    auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock());
6638
0
    if (!self)
6639
0
    {
6640
0
        CPLError(CE_Failure, CPLE_AppDefined,
6641
0
                 "Driver implementation issue: m_pSelf not set !");
6642
0
        return nullptr;
6643
0
    }
6644
0
    if (GetDataType().GetClass() != GEDTC_NUMERIC)
6645
0
    {
6646
0
        CPLError(CE_Failure, CPLE_AppDefined,
6647
0
                 "GetUnscaled() only supports numeric data type");
6648
0
        return nullptr;
6649
0
    }
6650
0
    const double dfScale =
6651
0
        std::isnan(dfOverriddenScale) ? GetScale() : dfOverriddenScale;
6652
0
    const double dfOffset =
6653
0
        std::isnan(dfOverriddenOffset) ? GetOffset() : dfOverriddenOffset;
6654
0
    if (dfScale == 1.0 && dfOffset == 0.0)
6655
0
        return self;
6656
6657
0
    GDALDataType eDT = GDALDataTypeIsComplex(GetDataType().GetNumericDataType())
6658
0
                           ? GDT_CFloat64
6659
0
                           : GDT_Float64;
6660
0
    if (dfOverriddenScale == -1 && dfOverriddenOffset == 0)
6661
0
    {
6662
0
        if (GetDataType().GetNumericDataType() == GDT_Float16)
6663
0
            eDT = GDT_Float16;
6664
0
        if (GetDataType().GetNumericDataType() == GDT_Float32)
6665
0
            eDT = GDT_Float32;
6666
0
    }
6667
6668
0
    return GDALMDArrayUnscaled::Create(self, dfScale, dfOffset,
6669
0
                                       dfOverriddenDstNodata, eDT);
6670
0
}
6671
6672
/************************************************************************/
6673
/*                         GDALMDArrayMask                              */
6674
/************************************************************************/
6675
6676
class GDALMDArrayMask final : public GDALPamMDArray
6677
{
6678
  private:
6679
    std::shared_ptr<GDALMDArray> m_poParent{};
6680
    GDALExtendedDataType m_dt{GDALExtendedDataType::Create(GDT_Byte)};
6681
    double m_dfMissingValue = 0.0;
6682
    bool m_bHasMissingValue = false;
6683
    double m_dfFillValue = 0.0;
6684
    bool m_bHasFillValue = false;
6685
    double m_dfValidMin = 0.0;
6686
    bool m_bHasValidMin = false;
6687
    double m_dfValidMax = 0.0;
6688
    bool m_bHasValidMax = false;
6689
    std::vector<uint32_t> m_anValidFlagMasks{};
6690
    std::vector<uint32_t> m_anValidFlagValues{};
6691
6692
    bool Init(CSLConstList papszOptions);
6693
6694
    template <typename Type>
6695
    void
6696
    ReadInternal(const size_t *count, const GPtrDiff_t *bufferStride,
6697
                 const GDALExtendedDataType &bufferDataType, void *pDstBuffer,
6698
                 const void *pTempBuffer,
6699
                 const GDALExtendedDataType &oTmpBufferDT,
6700
                 const std::vector<GPtrDiff_t> &tmpBufferStrideVector) const;
6701
6702
  protected:
6703
    explicit GDALMDArrayMask(const std::shared_ptr<GDALMDArray> &poParent)
6704
0
        : GDALAbstractMDArray(std::string(),
6705
0
                              "Mask of " + poParent->GetFullName()),
6706
0
          GDALPamMDArray(std::string(), "Mask of " + poParent->GetFullName(),
6707
0
                         GDALPamMultiDim::GetPAM(poParent),
6708
0
                         poParent->GetContext()),
6709
0
          m_poParent(std::move(poParent))
6710
0
    {
6711
0
    }
6712
6713
    bool IRead(const GUInt64 *arrayStartIdx, const size_t *count,
6714
               const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
6715
               const GDALExtendedDataType &bufferDataType,
6716
               void *pDstBuffer) const override;
6717
6718
    bool IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count,
6719
                     CSLConstList papszOptions) const override
6720
0
    {
6721
0
        return m_poParent->AdviseRead(arrayStartIdx, count, papszOptions);
6722
0
    }
6723
6724
  public:
6725
    static std::shared_ptr<GDALMDArrayMask>
6726
    Create(const std::shared_ptr<GDALMDArray> &poParent,
6727
           CSLConstList papszOptions);
6728
6729
    bool IsWritable() const override
6730
0
    {
6731
0
        return false;
6732
0
    }
6733
6734
    const std::string &GetFilename() const override
6735
0
    {
6736
0
        return m_poParent->GetFilename();
6737
0
    }
6738
6739
    const std::vector<std::shared_ptr<GDALDimension>> &
6740
    GetDimensions() const override
6741
0
    {
6742
0
        return m_poParent->GetDimensions();
6743
0
    }
6744
6745
    const GDALExtendedDataType &GetDataType() const override
6746
0
    {
6747
0
        return m_dt;
6748
0
    }
6749
6750
    std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override
6751
0
    {
6752
0
        return m_poParent->GetSpatialRef();
6753
0
    }
6754
6755
    std::vector<GUInt64> GetBlockSize() const override
6756
0
    {
6757
0
        return m_poParent->GetBlockSize();
6758
0
    }
6759
};
6760
6761
/************************************************************************/
6762
/*                    GDALMDArrayMask::Create()                         */
6763
/************************************************************************/
6764
6765
/* static */ std::shared_ptr<GDALMDArrayMask>
6766
GDALMDArrayMask::Create(const std::shared_ptr<GDALMDArray> &poParent,
6767
                        CSLConstList papszOptions)
6768
0
{
6769
0
    auto newAr(std::shared_ptr<GDALMDArrayMask>(new GDALMDArrayMask(poParent)));
6770
0
    newAr->SetSelf(newAr);
6771
0
    if (!newAr->Init(papszOptions))
6772
0
        return nullptr;
6773
0
    return newAr;
6774
0
}
6775
6776
/************************************************************************/
6777
/*                    GDALMDArrayMask::Init()                           */
6778
/************************************************************************/
6779
6780
bool GDALMDArrayMask::Init(CSLConstList papszOptions)
6781
0
{
6782
0
    const auto GetSingleValNumericAttr =
6783
0
        [this](const char *pszAttrName, bool &bHasVal, double &dfVal)
6784
0
    {
6785
0
        auto poAttr = m_poParent->GetAttribute(pszAttrName);
6786
0
        if (poAttr && poAttr->GetDataType().GetClass() == GEDTC_NUMERIC)
6787
0
        {
6788
0
            const auto anDimSizes = poAttr->GetDimensionsSize();
6789
0
            if (anDimSizes.empty() ||
6790
0
                (anDimSizes.size() == 1 && anDimSizes[0] == 1))
6791
0
            {
6792
0
                bHasVal = true;
6793
0
                dfVal = poAttr->ReadAsDouble();
6794
0
            }
6795
0
        }
6796
0
    };
6797
6798
0
    GetSingleValNumericAttr("missing_value", m_bHasMissingValue,
6799
0
                            m_dfMissingValue);
6800
0
    GetSingleValNumericAttr("_FillValue", m_bHasFillValue, m_dfFillValue);
6801
0
    GetSingleValNumericAttr("valid_min", m_bHasValidMin, m_dfValidMin);
6802
0
    GetSingleValNumericAttr("valid_max", m_bHasValidMax, m_dfValidMax);
6803
6804
0
    {
6805
0
        auto poValidRange = m_poParent->GetAttribute("valid_range");
6806
0
        if (poValidRange && poValidRange->GetDimensionsSize().size() == 1 &&
6807
0
            poValidRange->GetDimensionsSize()[0] == 2 &&
6808
0
            poValidRange->GetDataType().GetClass() == GEDTC_NUMERIC)
6809
0
        {
6810
0
            m_bHasValidMin = true;
6811
0
            m_bHasValidMax = true;
6812
0
            auto vals = poValidRange->ReadAsDoubleArray();
6813
0
            CPLAssert(vals.size() == 2);
6814
0
            m_dfValidMin = vals[0];
6815
0
            m_dfValidMax = vals[1];
6816
0
        }
6817
0
    }
6818
6819
    // Take into account
6820
    // https://cfconventions.org/cf-conventions/cf-conventions.html#flags
6821
    // Cf GDALMDArray::GetMask() for semantics of UNMASK_FLAGS
6822
0
    const char *pszUnmaskFlags =
6823
0
        CSLFetchNameValue(papszOptions, "UNMASK_FLAGS");
6824
0
    if (pszUnmaskFlags)
6825
0
    {
6826
0
        const auto IsScalarStringAttr =
6827
0
            [](const std::shared_ptr<GDALAttribute> &poAttr)
6828
0
        {
6829
0
            return poAttr->GetDataType().GetClass() == GEDTC_STRING &&
6830
0
                   (poAttr->GetDimensionsSize().empty() ||
6831
0
                    (poAttr->GetDimensionsSize().size() == 1 &&
6832
0
                     poAttr->GetDimensionsSize()[0] == 1));
6833
0
        };
6834
6835
0
        auto poFlagMeanings = m_poParent->GetAttribute("flag_meanings");
6836
0
        if (!(poFlagMeanings && IsScalarStringAttr(poFlagMeanings)))
6837
0
        {
6838
0
            CPLError(CE_Failure, CPLE_AppDefined,
6839
0
                     "UNMASK_FLAGS option specified but array has no "
6840
0
                     "flag_meanings attribute");
6841
0
            return false;
6842
0
        }
6843
0
        const char *pszFlagMeanings = poFlagMeanings->ReadAsString();
6844
0
        if (!pszFlagMeanings)
6845
0
        {
6846
0
            CPLError(CE_Failure, CPLE_AppDefined,
6847
0
                     "Cannot read flag_meanings attribute");
6848
0
            return false;
6849
0
        }
6850
6851
0
        const auto IsSingleDimNumericAttr =
6852
0
            [](const std::shared_ptr<GDALAttribute> &poAttr)
6853
0
        {
6854
0
            return poAttr->GetDataType().GetClass() == GEDTC_NUMERIC &&
6855
0
                   poAttr->GetDimensionsSize().size() == 1;
6856
0
        };
6857
6858
0
        auto poFlagValues = m_poParent->GetAttribute("flag_values");
6859
0
        const bool bHasFlagValues =
6860
0
            poFlagValues && IsSingleDimNumericAttr(poFlagValues);
6861
6862
0
        auto poFlagMasks = m_poParent->GetAttribute("flag_masks");
6863
0
        const bool bHasFlagMasks =
6864
0
            poFlagMasks && IsSingleDimNumericAttr(poFlagMasks);
6865
6866
0
        if (!bHasFlagValues && !bHasFlagMasks)
6867
0
        {
6868
0
            CPLError(CE_Failure, CPLE_AppDefined,
6869
0
                     "Cannot find flag_values and/or flag_masks attribute");
6870
0
            return false;
6871
0
        }
6872
6873
0
        const CPLStringList aosUnmaskFlags(
6874
0
            CSLTokenizeString2(pszUnmaskFlags, ",", 0));
6875
0
        const CPLStringList aosFlagMeanings(
6876
0
            CSLTokenizeString2(pszFlagMeanings, " ", 0));
6877
6878
0
        if (bHasFlagValues)
6879
0
        {
6880
0
            const auto eType = poFlagValues->GetDataType().GetNumericDataType();
6881
            // We could support Int64 or UInt64, but more work...
6882
0
            if (eType != GDT_Byte && eType != GDT_Int8 && eType != GDT_UInt16 &&
6883
0
                eType != GDT_Int16 && eType != GDT_UInt32 && eType != GDT_Int32)
6884
0
            {
6885
0
                CPLError(CE_Failure, CPLE_NotSupported,
6886
0
                         "Unsupported data type for flag_values attribute: %s",
6887
0
                         GDALGetDataTypeName(eType));
6888
0
                return false;
6889
0
            }
6890
0
        }
6891
6892
0
        if (bHasFlagMasks)
6893
0
        {
6894
0
            const auto eType = poFlagMasks->GetDataType().GetNumericDataType();
6895
            // We could support Int64 or UInt64, but more work...
6896
0
            if (eType != GDT_Byte && eType != GDT_Int8 && eType != GDT_UInt16 &&
6897
0
                eType != GDT_Int16 && eType != GDT_UInt32 && eType != GDT_Int32)
6898
0
            {
6899
0
                CPLError(CE_Failure, CPLE_NotSupported,
6900
0
                         "Unsupported data type for flag_masks attribute: %s",
6901
0
                         GDALGetDataTypeName(eType));
6902
0
                return false;
6903
0
            }
6904
0
        }
6905
6906
0
        const std::vector<double> adfValues(
6907
0
            bHasFlagValues ? poFlagValues->ReadAsDoubleArray()
6908
0
                           : std::vector<double>());
6909
0
        const std::vector<double> adfMasks(
6910
0
            bHasFlagMasks ? poFlagMasks->ReadAsDoubleArray()
6911
0
                          : std::vector<double>());
6912
6913
0
        if (bHasFlagValues &&
6914
0
            adfValues.size() != static_cast<size_t>(aosFlagMeanings.size()))
6915
0
        {
6916
0
            CPLError(CE_Failure, CPLE_AppDefined,
6917
0
                     "Number of values in flag_values attribute is different "
6918
0
                     "from the one in flag_meanings");
6919
0
            return false;
6920
0
        }
6921
6922
0
        if (bHasFlagMasks &&
6923
0
            adfMasks.size() != static_cast<size_t>(aosFlagMeanings.size()))
6924
0
        {
6925
0
            CPLError(CE_Failure, CPLE_AppDefined,
6926
0
                     "Number of values in flag_masks attribute is different "
6927
0
                     "from the one in flag_meanings");
6928
0
            return false;
6929
0
        }
6930
6931
0
        for (int i = 0; i < aosUnmaskFlags.size(); ++i)
6932
0
        {
6933
0
            const int nIdxFlag = aosFlagMeanings.FindString(aosUnmaskFlags[i]);
6934
0
            if (nIdxFlag < 0)
6935
0
            {
6936
0
                CPLError(
6937
0
                    CE_Failure, CPLE_AppDefined,
6938
0
                    "Cannot fing flag %s in flag_meanings = '%s' attribute",
6939
0
                    aosUnmaskFlags[i], pszFlagMeanings);
6940
0
                return false;
6941
0
            }
6942
6943
0
            if (bHasFlagValues && adfValues[nIdxFlag] < 0)
6944
0
            {
6945
0
                CPLError(CE_Failure, CPLE_AppDefined,
6946
0
                         "Invalid value in flag_values[%d] = %f", nIdxFlag,
6947
0
                         adfValues[nIdxFlag]);
6948
0
                return false;
6949
0
            }
6950
6951
0
            if (bHasFlagMasks && adfMasks[nIdxFlag] < 0)
6952
0
            {
6953
0
                CPLError(CE_Failure, CPLE_AppDefined,
6954
0
                         "Invalid value in flag_masks[%d] = %f", nIdxFlag,
6955
0
                         adfMasks[nIdxFlag]);
6956
0
                return false;
6957
0
            }
6958
6959
0
            if (bHasFlagValues)
6960
0
            {
6961
0
                m_anValidFlagValues.push_back(
6962
0
                    static_cast<uint32_t>(adfValues[nIdxFlag]));
6963
0
            }
6964
6965
0
            if (bHasFlagMasks)
6966
0
            {
6967
0
                m_anValidFlagMasks.push_back(
6968
0
                    static_cast<uint32_t>(adfMasks[nIdxFlag]));
6969
0
            }
6970
0
        }
6971
0
    }
6972
6973
0
    return true;
6974
0
}
6975
6976
/************************************************************************/
6977
/*                             IRead()                                  */
6978
/************************************************************************/
6979
6980
bool GDALMDArrayMask::IRead(const GUInt64 *arrayStartIdx, const size_t *count,
6981
                            const GInt64 *arrayStep,
6982
                            const GPtrDiff_t *bufferStride,
6983
                            const GDALExtendedDataType &bufferDataType,
6984
                            void *pDstBuffer) const
6985
0
{
6986
0
    if (bufferDataType.GetClass() != GEDTC_NUMERIC)
6987
0
    {
6988
0
        CPLError(CE_Failure, CPLE_AppDefined,
6989
0
                 "%s: only reading to a numeric data type is supported",
6990
0
                 __func__);
6991
0
        return false;
6992
0
    }
6993
0
    size_t nElts = 1;
6994
0
    const size_t nDims = GetDimensionCount();
6995
0
    std::vector<GPtrDiff_t> tmpBufferStrideVector(nDims);
6996
0
    for (size_t i = 0; i < nDims; i++)
6997
0
        nElts *= count[i];
6998
0
    if (nDims > 0)
6999
0
    {
7000
0
        tmpBufferStrideVector.back() = 1;
7001
0
        for (size_t i = nDims - 1; i > 0;)
7002
0
        {
7003
0
            --i;
7004
0
            tmpBufferStrideVector[i] =
7005
0
                tmpBufferStrideVector[i + 1] * count[i + 1];
7006
0
        }
7007
0
    }
7008
7009
    /* Optimized case: if we are an integer data type and that there is no */
7010
    /* attribute that can be used to set mask = 0, then fill the mask buffer */
7011
    /* directly */
7012
0
    if (!m_bHasMissingValue && !m_bHasFillValue && !m_bHasValidMin &&
7013
0
        !m_bHasValidMax && m_anValidFlagValues.empty() &&
7014
0
        m_anValidFlagMasks.empty() &&
7015
0
        m_poParent->GetRawNoDataValue() == nullptr &&
7016
0
        GDALDataTypeIsInteger(m_poParent->GetDataType().GetNumericDataType()))
7017
0
    {
7018
0
        const bool bBufferDataTypeIsByte = bufferDataType == m_dt;
7019
0
        if (bBufferDataTypeIsByte)  // Byte case
7020
0
        {
7021
0
            bool bContiguous = true;
7022
0
            for (size_t i = 0; i < nDims; i++)
7023
0
            {
7024
0
                if (bufferStride[i] != tmpBufferStrideVector[i])
7025
0
                {
7026
0
                    bContiguous = false;
7027
0
                    break;
7028
0
                }
7029
0
            }
7030
0
            if (bContiguous)
7031
0
            {
7032
                // CPLDebug("GDAL", "GetMask(): contiguous case");
7033
0
                memset(pDstBuffer, 1, nElts);
7034
0
                return true;
7035
0
            }
7036
0
        }
7037
7038
0
        struct Stack
7039
0
        {
7040
0
            size_t nIters = 0;
7041
0
            GByte *dst_ptr = nullptr;
7042
0
            GPtrDiff_t dst_inc_offset = 0;
7043
0
        };
7044
7045
0
        std::vector<Stack> stack(std::max(static_cast<size_t>(1), nDims));
7046
0
        const size_t nBufferDTSize = bufferDataType.GetSize();
7047
0
        for (size_t i = 0; i < nDims; i++)
7048
0
        {
7049
0
            stack[i].dst_inc_offset =
7050
0
                static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize);
7051
0
        }
7052
0
        stack[0].dst_ptr = static_cast<GByte *>(pDstBuffer);
7053
7054
0
        size_t dimIdx = 0;
7055
0
        const size_t nDimsMinus1 = nDims > 0 ? nDims - 1 : 0;
7056
0
        GByte abyOne[16];  // 16 is sizeof GDT_CFloat64
7057
0
        CPLAssert(nBufferDTSize <= 16);
7058
0
        const GByte flag = 1;
7059
0
        GDALCopyWords64(&flag, GDT_Byte, 0, abyOne,
7060
0
                        bufferDataType.GetNumericDataType(), 0, 1);
7061
7062
0
    lbl_next_depth:
7063
0
        if (dimIdx == nDimsMinus1)
7064
0
        {
7065
0
            auto nIters = nDims > 0 ? count[dimIdx] : 1;
7066
0
            GByte *dst_ptr = stack[dimIdx].dst_ptr;
7067
7068
0
            while (true)
7069
0
            {
7070
                // cppcheck-suppress knownConditionTrueFalse
7071
0
                if (bBufferDataTypeIsByte)
7072
0
                {
7073
0
                    *dst_ptr = flag;
7074
0
                }
7075
0
                else
7076
0
                {
7077
0
                    memcpy(dst_ptr, abyOne, nBufferDTSize);
7078
0
                }
7079
7080
0
                if ((--nIters) == 0)
7081
0
                    break;
7082
0
                dst_ptr += stack[dimIdx].dst_inc_offset;
7083
0
            }
7084
0
        }
7085
0
        else
7086
0
        {
7087
0
            stack[dimIdx].nIters = count[dimIdx];
7088
0
            while (true)
7089
0
            {
7090
0
                dimIdx++;
7091
0
                stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr;
7092
0
                goto lbl_next_depth;
7093
0
            lbl_return_to_caller:
7094
0
                dimIdx--;
7095
0
                if ((--stack[dimIdx].nIters) == 0)
7096
0
                    break;
7097
0
                stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset;
7098
0
            }
7099
0
        }
7100
0
        if (dimIdx > 0)
7101
0
            goto lbl_return_to_caller;
7102
7103
0
        return true;
7104
0
    }
7105
7106
0
    const auto oTmpBufferDT =
7107
0
        GDALDataTypeIsComplex(m_poParent->GetDataType().GetNumericDataType())
7108
0
            ? GDALExtendedDataType::Create(GDT_Float64)
7109
0
            : m_poParent->GetDataType();
7110
0
    const size_t nTmpBufferDTSize = oTmpBufferDT.GetSize();
7111
0
    void *pTempBuffer = VSI_MALLOC2_VERBOSE(nTmpBufferDTSize, nElts);
7112
0
    if (!pTempBuffer)
7113
0
        return false;
7114
0
    if (!m_poParent->Read(arrayStartIdx, count, arrayStep,
7115
0
                          tmpBufferStrideVector.data(), oTmpBufferDT,
7116
0
                          pTempBuffer))
7117
0
    {
7118
0
        VSIFree(pTempBuffer);
7119
0
        return false;
7120
0
    }
7121
7122
0
    switch (oTmpBufferDT.GetNumericDataType())
7123
0
    {
7124
0
        case GDT_Byte:
7125
0
            ReadInternal<GByte>(count, bufferStride, bufferDataType, pDstBuffer,
7126
0
                                pTempBuffer, oTmpBufferDT,
7127
0
                                tmpBufferStrideVector);
7128
0
            break;
7129
7130
0
        case GDT_Int8:
7131
0
            ReadInternal<GInt8>(count, bufferStride, bufferDataType, pDstBuffer,
7132
0
                                pTempBuffer, oTmpBufferDT,
7133
0
                                tmpBufferStrideVector);
7134
0
            break;
7135
7136
0
        case GDT_UInt16:
7137
0
            ReadInternal<GUInt16>(count, bufferStride, bufferDataType,
7138
0
                                  pDstBuffer, pTempBuffer, oTmpBufferDT,
7139
0
                                  tmpBufferStrideVector);
7140
0
            break;
7141
7142
0
        case GDT_Int16:
7143
0
            ReadInternal<GInt16>(count, bufferStride, bufferDataType,
7144
0
                                 pDstBuffer, pTempBuffer, oTmpBufferDT,
7145
0
                                 tmpBufferStrideVector);
7146
0
            break;
7147
7148
0
        case GDT_UInt32:
7149
0
            ReadInternal<GUInt32>(count, bufferStride, bufferDataType,
7150
0
                                  pDstBuffer, pTempBuffer, oTmpBufferDT,
7151
0
                                  tmpBufferStrideVector);
7152
0
            break;
7153
7154
0
        case GDT_Int32:
7155
0
            ReadInternal<GInt32>(count, bufferStride, bufferDataType,
7156
0
                                 pDstBuffer, pTempBuffer, oTmpBufferDT,
7157
0
                                 tmpBufferStrideVector);
7158
0
            break;
7159
7160
0
        case GDT_UInt64:
7161
0
            ReadInternal<std::uint64_t>(count, bufferStride, bufferDataType,
7162
0
                                        pDstBuffer, pTempBuffer, oTmpBufferDT,
7163
0
                                        tmpBufferStrideVector);
7164
0
            break;
7165
7166
0
        case GDT_Int64:
7167
0
            ReadInternal<std::int64_t>(count, bufferStride, bufferDataType,
7168
0
                                       pDstBuffer, pTempBuffer, oTmpBufferDT,
7169
0
                                       tmpBufferStrideVector);
7170
0
            break;
7171
7172
0
        case GDT_Float16:
7173
0
            ReadInternal<GFloat16>(count, bufferStride, bufferDataType,
7174
0
                                   pDstBuffer, pTempBuffer, oTmpBufferDT,
7175
0
                                   tmpBufferStrideVector);
7176
0
            break;
7177
7178
0
        case GDT_Float32:
7179
0
            ReadInternal<float>(count, bufferStride, bufferDataType, pDstBuffer,
7180
0
                                pTempBuffer, oTmpBufferDT,
7181
0
                                tmpBufferStrideVector);
7182
0
            break;
7183
7184
0
        case GDT_Float64:
7185
0
            ReadInternal<double>(count, bufferStride, bufferDataType,
7186
0
                                 pDstBuffer, pTempBuffer, oTmpBufferDT,
7187
0
                                 tmpBufferStrideVector);
7188
0
            break;
7189
0
        case GDT_Unknown:
7190
0
        case GDT_CInt16:
7191
0
        case GDT_CInt32:
7192
0
        case GDT_CFloat16:
7193
0
        case GDT_CFloat32:
7194
0
        case GDT_CFloat64:
7195
0
        case GDT_TypeCount:
7196
0
            CPLAssert(false);
7197
0
            break;
7198
0
    }
7199
7200
0
    VSIFree(pTempBuffer);
7201
7202
0
    return true;
7203
0
}
7204
7205
/************************************************************************/
7206
/*                          IsValidForDT()                              */
7207
/************************************************************************/
7208
7209
template <typename Type> static bool IsValidForDT(double dfVal)
7210
0
{
7211
0
    if (std::isnan(dfVal))
7212
0
        return false;
7213
0
    if (dfVal < static_cast<double>(cpl::NumericLimits<Type>::lowest()))
7214
0
        return false;
7215
0
    if (dfVal > static_cast<double>(cpl::NumericLimits<Type>::max()))
7216
0
        return false;
7217
0
    return static_cast<double>(static_cast<Type>(dfVal)) == dfVal;
7218
0
}
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<unsigned char>(double)
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<signed char>(double)
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<unsigned short>(double)
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<short>(double)
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<unsigned int>(double)
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<int>(double)
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<unsigned long>(double)
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<long>(double)
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<cpl::Float16>(double)
Unexecuted instantiation: gdalmultidim.cpp:bool IsValidForDT<float>(double)
7219
7220
template <> bool IsValidForDT<double>(double)
7221
0
{
7222
0
    return true;
7223
0
}
7224
7225
/************************************************************************/
7226
/*                              IsNan()                                 */
7227
/************************************************************************/
7228
7229
template <typename Type> inline bool IsNan(Type)
7230
0
{
7231
0
    return false;
7232
0
}
Unexecuted instantiation: bool IsNan<unsigned char>(unsigned char)
Unexecuted instantiation: bool IsNan<signed char>(signed char)
Unexecuted instantiation: bool IsNan<unsigned short>(unsigned short)
Unexecuted instantiation: bool IsNan<short>(short)
Unexecuted instantiation: bool IsNan<unsigned int>(unsigned int)
Unexecuted instantiation: bool IsNan<int>(int)
Unexecuted instantiation: bool IsNan<unsigned long>(unsigned long)
Unexecuted instantiation: bool IsNan<long>(long)
Unexecuted instantiation: bool IsNan<cpl::Float16>(cpl::Float16)
7233
7234
template <> bool IsNan<double>(double val)
7235
0
{
7236
0
    return std::isnan(val);
7237
0
}
7238
7239
template <> bool IsNan<float>(float val)
7240
0
{
7241
0
    return std::isnan(val);
7242
0
}
7243
7244
/************************************************************************/
7245
/*                         ReadInternal()                               */
7246
/************************************************************************/
7247
7248
template <typename Type>
7249
void GDALMDArrayMask::ReadInternal(
7250
    const size_t *count, const GPtrDiff_t *bufferStride,
7251
    const GDALExtendedDataType &bufferDataType, void *pDstBuffer,
7252
    const void *pTempBuffer, const GDALExtendedDataType &oTmpBufferDT,
7253
    const std::vector<GPtrDiff_t> &tmpBufferStrideVector) const
7254
0
{
7255
0
    const size_t nDims = GetDimensionCount();
7256
7257
0
    const auto castValue = [](bool &bHasVal, double dfVal) -> Type
7258
0
    {
7259
0
        if (bHasVal)
7260
0
        {
7261
0
            if (IsValidForDT<Type>(dfVal))
7262
0
            {
7263
0
                return static_cast<Type>(dfVal);
7264
0
            }
7265
0
            else
7266
0
            {
7267
0
                bHasVal = false;
7268
0
            }
7269
0
        }
7270
0
        return 0;
7271
0
    };
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<signed char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<cpl::Float16>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<float>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<double>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(bool&, double)#1}::operator()(bool&, double) const
7272
7273
0
    const void *pSrcRawNoDataValue = m_poParent->GetRawNoDataValue();
7274
0
    bool bHasNodataValue = pSrcRawNoDataValue != nullptr;
7275
0
    const Type nNoDataValue =
7276
0
        castValue(bHasNodataValue, m_poParent->GetNoDataValueAsDouble());
7277
0
    bool bHasMissingValue = m_bHasMissingValue;
7278
0
    const Type nMissingValue = castValue(bHasMissingValue, m_dfMissingValue);
7279
0
    bool bHasFillValue = m_bHasFillValue;
7280
0
    const Type nFillValue = castValue(bHasFillValue, m_dfFillValue);
7281
0
    bool bHasValidMin = m_bHasValidMin;
7282
0
    const Type nValidMin = castValue(bHasValidMin, m_dfValidMin);
7283
0
    bool bHasValidMax = m_bHasValidMax;
7284
0
    const Type nValidMax = castValue(bHasValidMax, m_dfValidMax);
7285
0
    const bool bHasValidFlags =
7286
0
        !m_anValidFlagValues.empty() || !m_anValidFlagMasks.empty();
7287
7288
0
    const auto IsValidFlag = [this](Type v)
7289
0
    {
7290
0
        if (!m_anValidFlagValues.empty() && !m_anValidFlagMasks.empty())
7291
0
        {
7292
0
            for (size_t i = 0; i < m_anValidFlagValues.size(); ++i)
7293
0
            {
7294
0
                if ((static_cast<uint32_t>(v) & m_anValidFlagMasks[i]) ==
7295
0
                    m_anValidFlagValues[i])
7296
0
                {
7297
0
                    return true;
7298
0
                }
7299
0
            }
7300
0
        }
7301
0
        else if (!m_anValidFlagValues.empty())
7302
0
        {
7303
0
            for (size_t i = 0; i < m_anValidFlagValues.size(); ++i)
7304
0
            {
7305
0
                if (static_cast<uint32_t>(v) == m_anValidFlagValues[i])
7306
0
                {
7307
0
                    return true;
7308
0
                }
7309
0
            }
7310
0
        }
7311
0
        else /* if( !m_anValidFlagMasks.empty() ) */
7312
0
        {
7313
0
            for (size_t i = 0; i < m_anValidFlagMasks.size(); ++i)
7314
0
            {
7315
0
                if ((static_cast<uint32_t>(v) & m_anValidFlagMasks[i]) != 0)
7316
0
                {
7317
0
                    return true;
7318
0
                }
7319
0
            }
7320
0
        }
7321
0
        return false;
7322
0
    };
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(unsigned char)#1}::operator()(unsigned char) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<signed char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(signed char)#1}::operator()(signed char) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(unsigned short)#1}::operator()(unsigned short) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(short)#1}::operator()(short) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(unsigned int)#1}::operator()(unsigned int) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(int)#1}::operator()(int) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<unsigned long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(unsigned long)#1}::operator()(unsigned long) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(long)#1}::operator()(long) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<cpl::Float16>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(cpl::Float16)#1}::operator()(cpl::Float16) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<float>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(float)#1}::operator()(float) const
Unexecuted instantiation: GDALMDArrayMask::ReadInternal<double>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const::{lambda(double)#1}::operator()(double) const
7323
7324
0
#define GET_MASK_FOR_SAMPLE(v)                                                 \
7325
0
    static_cast<GByte>(!IsNan(v) && !(bHasNodataValue && v == nNoDataValue) && \
7326
0
                       !(bHasMissingValue && v == nMissingValue) &&            \
7327
0
                       !(bHasFillValue && v == nFillValue) &&                  \
7328
0
                       !(bHasValidMin && v < nValidMin) &&                     \
7329
0
                       !(bHasValidMax && v > nValidMax) &&                     \
7330
0
                       (!bHasValidFlags || IsValidFlag(v)));
7331
7332
0
    const bool bBufferDataTypeIsByte = bufferDataType == m_dt;
7333
    /* Optimized case: Byte output and output buffer is contiguous */
7334
0
    if (bBufferDataTypeIsByte)
7335
0
    {
7336
0
        bool bContiguous = true;
7337
0
        for (size_t i = 0; i < nDims; i++)
7338
0
        {
7339
0
            if (bufferStride[i] != tmpBufferStrideVector[i])
7340
0
            {
7341
0
                bContiguous = false;
7342
0
                break;
7343
0
            }
7344
0
        }
7345
0
        if (bContiguous)
7346
0
        {
7347
0
            size_t nElts = 1;
7348
0
            for (size_t i = 0; i < nDims; i++)
7349
0
                nElts *= count[i];
7350
7351
0
            for (size_t i = 0; i < nElts; i++)
7352
0
            {
7353
0
                const Type *pSrc = static_cast<const Type *>(pTempBuffer) + i;
7354
0
                static_cast<GByte *>(pDstBuffer)[i] =
7355
0
                    GET_MASK_FOR_SAMPLE(*pSrc);
7356
0
            }
7357
0
            return;
7358
0
        }
7359
0
    }
7360
7361
0
    const size_t nTmpBufferDTSize = oTmpBufferDT.GetSize();
7362
7363
0
    struct Stack
7364
0
    {
7365
0
        size_t nIters = 0;
7366
0
        const GByte *src_ptr = nullptr;
7367
0
        GByte *dst_ptr = nullptr;
7368
0
        GPtrDiff_t src_inc_offset = 0;
7369
0
        GPtrDiff_t dst_inc_offset = 0;
7370
0
    };
7371
7372
0
    std::vector<Stack> stack(std::max(static_cast<size_t>(1), nDims));
7373
0
    const size_t nBufferDTSize = bufferDataType.GetSize();
7374
0
    for (size_t i = 0; i < nDims; i++)
7375
0
    {
7376
0
        stack[i].src_inc_offset = static_cast<GPtrDiff_t>(
7377
0
            tmpBufferStrideVector[i] * nTmpBufferDTSize);
7378
0
        stack[i].dst_inc_offset =
7379
0
            static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize);
7380
0
    }
7381
0
    stack[0].src_ptr = static_cast<const GByte *>(pTempBuffer);
7382
0
    stack[0].dst_ptr = static_cast<GByte *>(pDstBuffer);
7383
7384
0
    size_t dimIdx = 0;
7385
0
    const size_t nDimsMinus1 = nDims > 0 ? nDims - 1 : 0;
7386
0
    GByte abyZeroOrOne[2][16];  // 16 is sizeof GDT_CFloat64
7387
0
    CPLAssert(nBufferDTSize <= 16);
7388
0
    for (GByte flag = 0; flag <= 1; flag++)
7389
0
    {
7390
0
        GDALCopyWords64(&flag, m_dt.GetNumericDataType(), 0, abyZeroOrOne[flag],
7391
0
                        bufferDataType.GetNumericDataType(), 0, 1);
7392
0
    }
7393
7394
0
lbl_next_depth:
7395
0
    if (dimIdx == nDimsMinus1)
7396
0
    {
7397
0
        auto nIters = nDims > 0 ? count[dimIdx] : 1;
7398
0
        const GByte *src_ptr = stack[dimIdx].src_ptr;
7399
0
        GByte *dst_ptr = stack[dimIdx].dst_ptr;
7400
7401
0
        while (true)
7402
0
        {
7403
0
            const Type *pSrc = reinterpret_cast<const Type *>(src_ptr);
7404
0
            const GByte flag = GET_MASK_FOR_SAMPLE(*pSrc);
7405
7406
0
            if (bBufferDataTypeIsByte)
7407
0
            {
7408
0
                *dst_ptr = flag;
7409
0
            }
7410
0
            else
7411
0
            {
7412
0
                memcpy(dst_ptr, abyZeroOrOne[flag], nBufferDTSize);
7413
0
            }
7414
7415
0
            if ((--nIters) == 0)
7416
0
                break;
7417
0
            src_ptr += stack[dimIdx].src_inc_offset;
7418
0
            dst_ptr += stack[dimIdx].dst_inc_offset;
7419
0
        }
7420
0
    }
7421
0
    else
7422
0
    {
7423
0
        stack[dimIdx].nIters = count[dimIdx];
7424
0
        while (true)
7425
0
        {
7426
0
            dimIdx++;
7427
0
            stack[dimIdx].src_ptr = stack[dimIdx - 1].src_ptr;
7428
0
            stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr;
7429
0
            goto lbl_next_depth;
7430
0
        lbl_return_to_caller:
7431
0
            dimIdx--;
7432
0
            if ((--stack[dimIdx].nIters) == 0)
7433
0
                break;
7434
0
            stack[dimIdx].src_ptr += stack[dimIdx].src_inc_offset;
7435
0
            stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset;
7436
0
        }
7437
0
    }
7438
0
    if (dimIdx > 0)
7439
0
        goto lbl_return_to_caller;
7440
0
}
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<unsigned char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<signed char>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<unsigned short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<short>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<unsigned int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<int>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<unsigned long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<long>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<cpl::Float16>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<float>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
Unexecuted instantiation: void GDALMDArrayMask::ReadInternal<double>(unsigned long const*, long long const*, GDALExtendedDataType const&, void*, void const*, GDALExtendedDataType const&, std::__1::vector<long long, std::__1::allocator<long long> > const&) const
7441
7442
/************************************************************************/
7443
/*                            GetMask()                                 */
7444
/************************************************************************/
7445
7446
/** Return an array that is a mask for the current array
7447
7448
 This array will be of type Byte, with values set to 0 to indicate invalid
7449
 pixels of the current array, and values set to 1 to indicate valid pixels.
7450
7451
 The generic implementation honours the NoDataValue, as well as various
7452
 netCDF CF attributes: missing_value, _FillValue, valid_min, valid_max
7453
 and valid_range.
7454
7455
 Starting with GDAL 3.8, option UNMASK_FLAGS=flag_meaning_1[,flag_meaning_2,...]
7456
 can be used to specify strings of the "flag_meanings" attribute
7457
 (cf https://cfconventions.org/cf-conventions/cf-conventions.html#flags)
7458
 for which pixels matching any of those flags will be set at 1 in the mask array,
7459
 and pixels matching none of those flags will be set at 0.
7460
 For example, let's consider the following netCDF variable defined with:
7461
 \verbatim
7462
 l2p_flags:valid_min = 0s ;
7463
 l2p_flags:valid_max = 256s ;
7464
 l2p_flags:flag_meanings = "microwave land ice lake river reserved_for_future_use unused_currently unused_currently unused_currently" ;
7465
 l2p_flags:flag_masks = 1s, 2s, 4s, 8s, 16s, 32s, 64s, 128s, 256s ;
7466
 \endverbatim
7467
7468
 GetMask(["UNMASK_FLAGS=microwave,land"]) will return an array such that:
7469
 - for pixel values *outside* valid_range [0,256], the mask value will be 0.
7470
 - for a pixel value with bit 0 or bit 1 at 1 within [0,256], the mask value
7471
   will be 1.
7472
 - for a pixel value with bit 0 and bit 1 at 0 within [0,256], the mask value
7473
   will be 0.
7474
7475
 This is the same as the C function GDALMDArrayGetMask().
7476
7477
 @param papszOptions NULL-terminated list of options, or NULL.
7478
7479
 @return a new array, that holds a reference to the original one, and thus is
7480
 a view of it (not a copy), or nullptr in case of error.
7481
*/
7482
std::shared_ptr<GDALMDArray>
7483
GDALMDArray::GetMask(CSLConstList papszOptions) const
7484
0
{
7485
0
    auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock());
7486
0
    if (!self)
7487
0
    {
7488
0
        CPLError(CE_Failure, CPLE_AppDefined,
7489
0
                 "Driver implementation issue: m_pSelf not set !");
7490
0
        return nullptr;
7491
0
    }
7492
0
    if (GetDataType().GetClass() != GEDTC_NUMERIC)
7493
0
    {
7494
0
        CPLError(CE_Failure, CPLE_AppDefined,
7495
0
                 "GetMask() only supports numeric data type");
7496
0
        return nullptr;
7497
0
    }
7498
0
    return GDALMDArrayMask::Create(self, papszOptions);
7499
0
}
7500
7501
/************************************************************************/
7502
/*                         IsRegularlySpaced()                          */
7503
/************************************************************************/
7504
7505
/** Returns whether an array is a 1D regularly spaced array.
7506
 *
7507
 * @param[out] dfStart     First value in the array
7508
 * @param[out] dfIncrement Increment/spacing between consecutive values.
7509
 * @return true if the array is regularly spaced.
7510
 */
7511
bool GDALMDArray::IsRegularlySpaced(double &dfStart, double &dfIncrement) const
7512
0
{
7513
0
    dfStart = 0;
7514
0
    dfIncrement = 0;
7515
0
    if (GetDimensionCount() != 1 || GetDataType().GetClass() != GEDTC_NUMERIC)
7516
0
        return false;
7517
0
    const auto nSize = GetDimensions()[0]->GetSize();
7518
0
    if (nSize <= 1 || nSize > 10 * 1000 * 1000)
7519
0
        return false;
7520
7521
0
    size_t nCount = static_cast<size_t>(nSize);
7522
0
    std::vector<double> adfTmp;
7523
0
    try
7524
0
    {
7525
0
        adfTmp.resize(nCount);
7526
0
    }
7527
0
    catch (const std::exception &)
7528
0
    {
7529
0
        return false;
7530
0
    }
7531
7532
0
    GUInt64 anStart[1] = {0};
7533
0
    size_t anCount[1] = {nCount};
7534
7535
0
    const auto IsRegularlySpacedInternal =
7536
0
        [&dfStart, &dfIncrement, &anCount, &adfTmp]()
7537
0
    {
7538
0
        dfStart = adfTmp[0];
7539
0
        dfIncrement = (adfTmp[anCount[0] - 1] - adfTmp[0]) / (anCount[0] - 1);
7540
0
        if (dfIncrement == 0)
7541
0
        {
7542
0
            return false;
7543
0
        }
7544
0
        for (size_t i = 1; i < anCount[0]; i++)
7545
0
        {
7546
0
            if (fabs((adfTmp[i] - adfTmp[i - 1]) - dfIncrement) >
7547
0
                1e-3 * fabs(dfIncrement))
7548
0
            {
7549
0
                return false;
7550
0
            }
7551
0
        }
7552
0
        return true;
7553
0
    };
7554
7555
    // First try with the first block(s). This can avoid excessive processing
7556
    // time, for example with Zarr datasets.
7557
    // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=37636 and
7558
    // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=39273
7559
0
    const auto nBlockSize = GetBlockSize()[0];
7560
0
    if (nCount >= 5 && nBlockSize <= nCount / 2)
7561
0
    {
7562
0
        size_t nReducedCount =
7563
0
            std::max<size_t>(3, static_cast<size_t>(nBlockSize));
7564
0
        while (nReducedCount < 256 && nReducedCount <= (nCount - 2) / 2)
7565
0
            nReducedCount *= 2;
7566
0
        anCount[0] = nReducedCount;
7567
0
        if (!Read(anStart, anCount, nullptr, nullptr,
7568
0
                  GDALExtendedDataType::Create(GDT_Float64), &adfTmp[0]))
7569
0
        {
7570
0
            return false;
7571
0
        }
7572
0
        if (!IsRegularlySpacedInternal())
7573
0
        {
7574
0
            return false;
7575
0
        }
7576
7577
        // Get next values
7578
0
        anStart[0] = nReducedCount;
7579
0
        anCount[0] = nCount - nReducedCount;
7580
0
    }
7581
7582
0
    if (!Read(anStart, anCount, nullptr, nullptr,
7583
0
              GDALExtendedDataType::Create(GDT_Float64),
7584
0
              &adfTmp[static_cast<size_t>(anStart[0])]))
7585
0
    {
7586
0
        return false;
7587
0
    }
7588
7589
0
    return IsRegularlySpacedInternal();
7590
0
}
7591
7592
/************************************************************************/
7593
/*                         GuessGeoTransform()                          */
7594
/************************************************************************/
7595
7596
/** Returns whether 2 specified dimensions form a geotransform
7597
 *
7598
 * @param nDimX                Index of the X axis.
7599
 * @param nDimY                Index of the Y axis.
7600
 * @param bPixelIsPoint        Whether the geotransform should be returned
7601
 *                             with the pixel-is-point (pixel-center) convention
7602
 *                             (bPixelIsPoint = true), or with the pixel-is-area
7603
 *                             (top left corner convention)
7604
 *                             (bPixelIsPoint = false)
7605
 * @param[out] adfGeoTransform Computed geotransform
7606
 * @return true if a geotransform could be computed.
7607
 */
7608
bool GDALMDArray::GuessGeoTransform(size_t nDimX, size_t nDimY,
7609
                                    bool bPixelIsPoint,
7610
                                    double adfGeoTransform[6]) const
7611
0
{
7612
0
    const auto &dims(GetDimensions());
7613
0
    auto poVarX = dims[nDimX]->GetIndexingVariable();
7614
0
    auto poVarY = dims[nDimY]->GetIndexingVariable();
7615
0
    double dfXStart = 0.0;
7616
0
    double dfXSpacing = 0.0;
7617
0
    double dfYStart = 0.0;
7618
0
    double dfYSpacing = 0.0;
7619
0
    if (poVarX && poVarX->GetDimensionCount() == 1 &&
7620
0
        poVarX->GetDimensions()[0]->GetSize() == dims[nDimX]->GetSize() &&
7621
0
        poVarY && poVarY->GetDimensionCount() == 1 &&
7622
0
        poVarY->GetDimensions()[0]->GetSize() == dims[nDimY]->GetSize() &&
7623
0
        poVarX->IsRegularlySpaced(dfXStart, dfXSpacing) &&
7624
0
        poVarY->IsRegularlySpaced(dfYStart, dfYSpacing))
7625
0
    {
7626
0
        adfGeoTransform[0] = dfXStart - (bPixelIsPoint ? 0 : dfXSpacing / 2);
7627
0
        adfGeoTransform[1] = dfXSpacing;
7628
0
        adfGeoTransform[2] = 0;
7629
0
        adfGeoTransform[3] = dfYStart - (bPixelIsPoint ? 0 : dfYSpacing / 2);
7630
0
        adfGeoTransform[4] = 0;
7631
0
        adfGeoTransform[5] = dfYSpacing;
7632
0
        return true;
7633
0
    }
7634
0
    return false;
7635
0
}
7636
7637
/************************************************************************/
7638
/*                       GDALMDArrayResampled                           */
7639
/************************************************************************/
7640
7641
class GDALMDArrayResampledDataset;
7642
7643
class GDALMDArrayResampledDatasetRasterBand final : public GDALRasterBand
7644
{
7645
  protected:
7646
    CPLErr IReadBlock(int, int, void *) override;
7647
    CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
7648
                     int nYSize, void *pData, int nBufXSize, int nBufYSize,
7649
                     GDALDataType eBufType, GSpacing nPixelSpaceBuf,
7650
                     GSpacing nLineSpaceBuf,
7651
                     GDALRasterIOExtraArg *psExtraArg) override;
7652
7653
  public:
7654
    explicit GDALMDArrayResampledDatasetRasterBand(
7655
        GDALMDArrayResampledDataset *poDSIn);
7656
7657
    double GetNoDataValue(int *pbHasNoData) override;
7658
};
7659
7660
class GDALMDArrayResampledDataset final : public GDALPamDataset
7661
{
7662
    friend class GDALMDArrayResampled;
7663
    friend class GDALMDArrayResampledDatasetRasterBand;
7664
7665
    std::shared_ptr<GDALMDArray> m_poArray;
7666
    const size_t m_iXDim;
7667
    const size_t m_iYDim;
7668
    double m_adfGeoTransform[6]{0, 1, 0, 0, 0, 1};
7669
    bool m_bHasGT = false;
7670
    mutable std::shared_ptr<OGRSpatialReference> m_poSRS{};
7671
7672
    std::vector<GUInt64> m_anOffset{};
7673
    std::vector<size_t> m_anCount{};
7674
    std::vector<GPtrDiff_t> m_anStride{};
7675
7676
    std::string m_osFilenameLong{};
7677
    std::string m_osFilenameLat{};
7678
7679
  public:
7680
    GDALMDArrayResampledDataset(const std::shared_ptr<GDALMDArray> &array,
7681
                                size_t iXDim, size_t iYDim)
7682
0
        : m_poArray(array), m_iXDim(iXDim), m_iYDim(iYDim),
7683
0
          m_anOffset(m_poArray->GetDimensionCount(), 0),
7684
0
          m_anCount(m_poArray->GetDimensionCount(), 1),
7685
0
          m_anStride(m_poArray->GetDimensionCount(), 0)
7686
0
    {
7687
0
        const auto &dims(m_poArray->GetDimensions());
7688
7689
0
        nRasterYSize = static_cast<int>(
7690
0
            std::min(static_cast<GUInt64>(INT_MAX), dims[iYDim]->GetSize()));
7691
0
        nRasterXSize = static_cast<int>(
7692
0
            std::min(static_cast<GUInt64>(INT_MAX), dims[iXDim]->GetSize()));
7693
7694
0
        m_bHasGT = m_poArray->GuessGeoTransform(m_iXDim, m_iYDim, false,
7695
0
                                                m_adfGeoTransform);
7696
7697
0
        SetBand(1, new GDALMDArrayResampledDatasetRasterBand(this));
7698
0
    }
7699
7700
    ~GDALMDArrayResampledDataset() override;
7701
7702
    CPLErr GetGeoTransform(double *padfGeoTransform) override
7703
0
    {
7704
0
        memcpy(padfGeoTransform, m_adfGeoTransform, 6 * sizeof(double));
7705
0
        return m_bHasGT ? CE_None : CE_Failure;
7706
0
    }
7707
7708
    const OGRSpatialReference *GetSpatialRef() const override
7709
0
    {
7710
0
        m_poSRS = m_poArray->GetSpatialRef();
7711
0
        if (m_poSRS)
7712
0
        {
7713
0
            m_poSRS.reset(m_poSRS->Clone());
7714
0
            auto axisMapping = m_poSRS->GetDataAxisToSRSAxisMapping();
7715
0
            for (auto &m : axisMapping)
7716
0
            {
7717
0
                if (m == static_cast<int>(m_iXDim) + 1)
7718
0
                    m = 1;
7719
0
                else if (m == static_cast<int>(m_iYDim) + 1)
7720
0
                    m = 2;
7721
0
            }
7722
0
            m_poSRS->SetDataAxisToSRSAxisMapping(axisMapping);
7723
0
        }
7724
0
        return m_poSRS.get();
7725
0
    }
7726
7727
    void SetGeolocationArray(const std::string &osFilenameLong,
7728
                             const std::string &osFilenameLat)
7729
0
    {
7730
0
        m_osFilenameLong = osFilenameLong;
7731
0
        m_osFilenameLat = osFilenameLat;
7732
0
        CPLStringList aosGeoLoc;
7733
0
        aosGeoLoc.SetNameValue("LINE_OFFSET", "0");
7734
0
        aosGeoLoc.SetNameValue("LINE_STEP", "1");
7735
0
        aosGeoLoc.SetNameValue("PIXEL_OFFSET", "0");
7736
0
        aosGeoLoc.SetNameValue("PIXEL_STEP", "1");
7737
0
        aosGeoLoc.SetNameValue("SRS", SRS_WKT_WGS84_LAT_LONG);  // FIXME?
7738
0
        aosGeoLoc.SetNameValue("X_BAND", "1");
7739
0
        aosGeoLoc.SetNameValue("X_DATASET", m_osFilenameLong.c_str());
7740
0
        aosGeoLoc.SetNameValue("Y_BAND", "1");
7741
0
        aosGeoLoc.SetNameValue("Y_DATASET", m_osFilenameLat.c_str());
7742
0
        aosGeoLoc.SetNameValue("GEOREFERENCING_CONVENTION", "PIXEL_CENTER");
7743
0
        SetMetadata(aosGeoLoc.List(), "GEOLOCATION");
7744
0
    }
7745
};
7746
7747
GDALMDArrayResampledDataset::~GDALMDArrayResampledDataset()
7748
0
{
7749
0
    if (!m_osFilenameLong.empty())
7750
0
        VSIUnlink(m_osFilenameLong.c_str());
7751
0
    if (!m_osFilenameLat.empty())
7752
0
        VSIUnlink(m_osFilenameLat.c_str());
7753
0
}
7754
7755
/************************************************************************/
7756
/*                   GDALMDArrayResampledDatasetRasterBand()            */
7757
/************************************************************************/
7758
7759
GDALMDArrayResampledDatasetRasterBand::GDALMDArrayResampledDatasetRasterBand(
7760
    GDALMDArrayResampledDataset *poDSIn)
7761
0
{
7762
0
    const auto &poArray(poDSIn->m_poArray);
7763
0
    const auto blockSize(poArray->GetBlockSize());
7764
0
    nBlockYSize = (blockSize[poDSIn->m_iYDim])
7765
0
                      ? static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX),
7766
0
                                                  blockSize[poDSIn->m_iYDim]))
7767
0
                      : 1;
7768
0
    nBlockXSize = blockSize[poDSIn->m_iXDim]
7769
0
                      ? static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX),
7770
0
                                                  blockSize[poDSIn->m_iXDim]))
7771
0
                      : poDSIn->GetRasterXSize();
7772
0
    eDataType = poArray->GetDataType().GetNumericDataType();
7773
0
    eAccess = poDSIn->eAccess;
7774
0
}
7775
7776
/************************************************************************/
7777
/*                           GetNoDataValue()                           */
7778
/************************************************************************/
7779
7780
double GDALMDArrayResampledDatasetRasterBand::GetNoDataValue(int *pbHasNoData)
7781
0
{
7782
0
    auto l_poDS(cpl::down_cast<GDALMDArrayResampledDataset *>(poDS));
7783
0
    const auto &poArray(l_poDS->m_poArray);
7784
0
    bool bHasNodata = false;
7785
0
    double dfRes = poArray->GetNoDataValueAsDouble(&bHasNodata);
7786
0
    if (pbHasNoData)
7787
0
        *pbHasNoData = bHasNodata;
7788
0
    return dfRes;
7789
0
}
7790
7791
/************************************************************************/
7792
/*                            IReadBlock()                              */
7793
/************************************************************************/
7794
7795
CPLErr GDALMDArrayResampledDatasetRasterBand::IReadBlock(int nBlockXOff,
7796
                                                         int nBlockYOff,
7797
                                                         void *pImage)
7798
0
{
7799
0
    const int nDTSize(GDALGetDataTypeSizeBytes(eDataType));
7800
0
    const int nXOff = nBlockXOff * nBlockXSize;
7801
0
    const int nYOff = nBlockYOff * nBlockYSize;
7802
0
    const int nReqXSize = std::min(nRasterXSize - nXOff, nBlockXSize);
7803
0
    const int nReqYSize = std::min(nRasterYSize - nYOff, nBlockYSize);
7804
0
    GDALRasterIOExtraArg sExtraArg;
7805
0
    INIT_RASTERIO_EXTRA_ARG(sExtraArg);
7806
0
    return IRasterIO(GF_Read, nXOff, nYOff, nReqXSize, nReqYSize, pImage,
7807
0
                     nReqXSize, nReqYSize, eDataType, nDTSize,
7808
0
                     static_cast<GSpacing>(nDTSize) * nBlockXSize, &sExtraArg);
7809
0
}
7810
7811
/************************************************************************/
7812
/*                            IRasterIO()                               */
7813
/************************************************************************/
7814
7815
CPLErr GDALMDArrayResampledDatasetRasterBand::IRasterIO(
7816
    GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
7817
    void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
7818
    GSpacing nPixelSpaceBuf, GSpacing nLineSpaceBuf,
7819
    GDALRasterIOExtraArg *psExtraArg)
7820
0
{
7821
0
    auto l_poDS(cpl::down_cast<GDALMDArrayResampledDataset *>(poDS));
7822
0
    const auto &poArray(l_poDS->m_poArray);
7823
0
    const int nBufferDTSize(GDALGetDataTypeSizeBytes(eBufType));
7824
0
    if (eRWFlag == GF_Read && nXSize == nBufXSize && nYSize == nBufYSize &&
7825
0
        nBufferDTSize > 0 && (nPixelSpaceBuf % nBufferDTSize) == 0 &&
7826
0
        (nLineSpaceBuf % nBufferDTSize) == 0)
7827
0
    {
7828
0
        l_poDS->m_anOffset[l_poDS->m_iXDim] = static_cast<GUInt64>(nXOff);
7829
0
        l_poDS->m_anCount[l_poDS->m_iXDim] = static_cast<size_t>(nXSize);
7830
0
        l_poDS->m_anStride[l_poDS->m_iXDim] =
7831
0
            static_cast<GPtrDiff_t>(nPixelSpaceBuf / nBufferDTSize);
7832
7833
0
        l_poDS->m_anOffset[l_poDS->m_iYDim] = static_cast<GUInt64>(nYOff);
7834
0
        l_poDS->m_anCount[l_poDS->m_iYDim] = static_cast<size_t>(nYSize);
7835
0
        l_poDS->m_anStride[l_poDS->m_iYDim] =
7836
0
            static_cast<GPtrDiff_t>(nLineSpaceBuf / nBufferDTSize);
7837
7838
0
        return poArray->Read(l_poDS->m_anOffset.data(),
7839
0
                             l_poDS->m_anCount.data(), nullptr,
7840
0
                             l_poDS->m_anStride.data(),
7841
0
                             GDALExtendedDataType::Create(eBufType), pData)
7842
0
                   ? CE_None
7843
0
                   : CE_Failure;
7844
0
    }
7845
0
    return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
7846
0
                                     pData, nBufXSize, nBufYSize, eBufType,
7847
0
                                     nPixelSpaceBuf, nLineSpaceBuf, psExtraArg);
7848
0
}
7849
7850
class GDALMDArrayResampled final : public GDALPamMDArray
7851
{
7852
  private:
7853
    std::shared_ptr<GDALMDArray> m_poParent{};
7854
    std::vector<std::shared_ptr<GDALDimension>> m_apoDims;
7855
    std::vector<GUInt64> m_anBlockSize;
7856
    GDALExtendedDataType m_dt;
7857
    std::shared_ptr<OGRSpatialReference> m_poSRS{};
7858
    std::shared_ptr<GDALMDArray> m_poVarX{};
7859
    std::shared_ptr<GDALMDArray> m_poVarY{};
7860
    std::unique_ptr<GDALMDArrayResampledDataset> m_poParentDS{};
7861
    std::unique_ptr<GDALDataset> m_poReprojectedDS{};
7862
7863
  protected:
7864
    GDALMDArrayResampled(
7865
        const std::shared_ptr<GDALMDArray> &poParent,
7866
        const std::vector<std::shared_ptr<GDALDimension>> &apoDims,
7867
        const std::vector<GUInt64> &anBlockSize)
7868
0
        : GDALAbstractMDArray(std::string(),
7869
0
                              "Resampled view of " + poParent->GetFullName()),
7870
0
          GDALPamMDArray(
7871
0
              std::string(), "Resampled view of " + poParent->GetFullName(),
7872
0
              GDALPamMultiDim::GetPAM(poParent), poParent->GetContext()),
7873
0
          m_poParent(std::move(poParent)), m_apoDims(apoDims),
7874
0
          m_anBlockSize(anBlockSize), m_dt(m_poParent->GetDataType())
7875
0
    {
7876
0
        CPLAssert(apoDims.size() == m_poParent->GetDimensionCount());
7877
0
        CPLAssert(anBlockSize.size() == m_poParent->GetDimensionCount());
7878
0
    }
7879
7880
    bool IRead(const GUInt64 *arrayStartIdx, const size_t *count,
7881
               const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
7882
               const GDALExtendedDataType &bufferDataType,
7883
               void *pDstBuffer) const override;
7884
7885
  public:
7886
    static std::shared_ptr<GDALMDArray>
7887
    Create(const std::shared_ptr<GDALMDArray> &poParent,
7888
           const std::vector<std::shared_ptr<GDALDimension>> &apoNewDims,
7889
           GDALRIOResampleAlg resampleAlg,
7890
           const OGRSpatialReference *poTargetSRS, CSLConstList papszOptions);
7891
7892
    ~GDALMDArrayResampled()
7893
0
    {
7894
        // First close the warped VRT
7895
0
        m_poReprojectedDS.reset();
7896
0
        m_poParentDS.reset();
7897
0
    }
7898
7899
    bool IsWritable() const override
7900
0
    {
7901
0
        return false;
7902
0
    }
7903
7904
    const std::string &GetFilename() const override
7905
0
    {
7906
0
        return m_poParent->GetFilename();
7907
0
    }
7908
7909
    const std::vector<std::shared_ptr<GDALDimension>> &
7910
    GetDimensions() const override
7911
0
    {
7912
0
        return m_apoDims;
7913
0
    }
7914
7915
    const GDALExtendedDataType &GetDataType() const override
7916
0
    {
7917
0
        return m_dt;
7918
0
    }
7919
7920
    std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override
7921
0
    {
7922
0
        return m_poSRS;
7923
0
    }
7924
7925
    std::vector<GUInt64> GetBlockSize() const override
7926
0
    {
7927
0
        return m_anBlockSize;
7928
0
    }
7929
7930
    std::shared_ptr<GDALAttribute>
7931
    GetAttribute(const std::string &osName) const override
7932
0
    {
7933
0
        return m_poParent->GetAttribute(osName);
7934
0
    }
7935
7936
    std::vector<std::shared_ptr<GDALAttribute>>
7937
    GetAttributes(CSLConstList papszOptions = nullptr) const override
7938
0
    {
7939
0
        return m_poParent->GetAttributes(papszOptions);
7940
0
    }
7941
7942
    const std::string &GetUnit() const override
7943
0
    {
7944
0
        return m_poParent->GetUnit();
7945
0
    }
7946
7947
    const void *GetRawNoDataValue() const override
7948
0
    {
7949
0
        return m_poParent->GetRawNoDataValue();
7950
0
    }
7951
7952
    double GetOffset(bool *pbHasOffset,
7953
                     GDALDataType *peStorageType) const override
7954
0
    {
7955
0
        return m_poParent->GetOffset(pbHasOffset, peStorageType);
7956
0
    }
7957
7958
    double GetScale(bool *pbHasScale,
7959
                    GDALDataType *peStorageType) const override
7960
0
    {
7961
0
        return m_poParent->GetScale(pbHasScale, peStorageType);
7962
0
    }
7963
};
7964
7965
/************************************************************************/
7966
/*                   GDALMDArrayResampled::Create()                     */
7967
/************************************************************************/
7968
7969
std::shared_ptr<GDALMDArray> GDALMDArrayResampled::Create(
7970
    const std::shared_ptr<GDALMDArray> &poParent,
7971
    const std::vector<std::shared_ptr<GDALDimension>> &apoNewDimsIn,
7972
    GDALRIOResampleAlg resampleAlg, const OGRSpatialReference *poTargetSRS,
7973
    CSLConstList /* papszOptions */)
7974
0
{
7975
0
    const char *pszResampleAlg = "nearest";
7976
0
    bool unsupported = false;
7977
0
    switch (resampleAlg)
7978
0
    {
7979
0
        case GRIORA_NearestNeighbour:
7980
0
            pszResampleAlg = "nearest";
7981
0
            break;
7982
0
        case GRIORA_Bilinear:
7983
0
            pszResampleAlg = "bilinear";
7984
0
            break;
7985
0
        case GRIORA_Cubic:
7986
0
            pszResampleAlg = "cubic";
7987
0
            break;
7988
0
        case GRIORA_CubicSpline:
7989
0
            pszResampleAlg = "cubicspline";
7990
0
            break;
7991
0
        case GRIORA_Lanczos:
7992
0
            pszResampleAlg = "lanczos";
7993
0
            break;
7994
0
        case GRIORA_Average:
7995
0
            pszResampleAlg = "average";
7996
0
            break;
7997
0
        case GRIORA_Mode:
7998
0
            pszResampleAlg = "mode";
7999
0
            break;
8000
0
        case GRIORA_Gauss:
8001
0
            unsupported = true;
8002
0
            break;
8003
0
        case GRIORA_RESERVED_START:
8004
0
            unsupported = true;
8005
0
            break;
8006
0
        case GRIORA_RESERVED_END:
8007
0
            unsupported = true;
8008
0
            break;
8009
0
        case GRIORA_RMS:
8010
0
            pszResampleAlg = "rms";
8011
0
            break;
8012
0
    }
8013
0
    if (unsupported)
8014
0
    {
8015
0
        CPLError(CE_Failure, CPLE_NotSupported,
8016
0
                 "Unsupported resample method for GetResampled()");
8017
0
        return nullptr;
8018
0
    }
8019
8020
0
    if (poParent->GetDimensionCount() < 2)
8021
0
    {
8022
0
        CPLError(CE_Failure, CPLE_NotSupported,
8023
0
                 "GetResampled() only supports 2 dimensions or more");
8024
0
        return nullptr;
8025
0
    }
8026
8027
0
    const auto &aoParentDims = poParent->GetDimensions();
8028
0
    if (apoNewDimsIn.size() != aoParentDims.size())
8029
0
    {
8030
0
        CPLError(CE_Failure, CPLE_AppDefined,
8031
0
                 "GetResampled(): apoNewDims size should be the same as "
8032
0
                 "GetDimensionCount()");
8033
0
        return nullptr;
8034
0
    }
8035
8036
0
    std::vector<std::shared_ptr<GDALDimension>> apoNewDims;
8037
0
    apoNewDims.reserve(apoNewDimsIn.size());
8038
8039
0
    std::vector<GUInt64> anBlockSize;
8040
0
    anBlockSize.reserve(apoNewDimsIn.size());
8041
0
    const auto &anParentBlockSize = poParent->GetBlockSize();
8042
8043
0
    auto apoParentDims = poParent->GetDimensions();
8044
    // Special case for NASA EMIT datasets
8045
0
    const bool bYXBandOrder = (apoParentDims.size() == 3 &&
8046
0
                               apoParentDims[0]->GetName() == "downtrack" &&
8047
0
                               apoParentDims[1]->GetName() == "crosstrack" &&
8048
0
                               apoParentDims[2]->GetName() == "bands");
8049
8050
0
    const size_t iYDimParent =
8051
0
        bYXBandOrder ? 0 : poParent->GetDimensionCount() - 2;
8052
0
    const size_t iXDimParent =
8053
0
        bYXBandOrder ? 1 : poParent->GetDimensionCount() - 1;
8054
8055
0
    for (unsigned i = 0; i < apoNewDimsIn.size(); ++i)
8056
0
    {
8057
0
        if (i == iYDimParent || i == iXDimParent)
8058
0
            continue;
8059
0
        if (apoNewDimsIn[i] == nullptr)
8060
0
        {
8061
0
            apoNewDims.emplace_back(aoParentDims[i]);
8062
0
        }
8063
0
        else if (apoNewDimsIn[i]->GetSize() != aoParentDims[i]->GetSize() ||
8064
0
                 apoNewDimsIn[i]->GetName() != aoParentDims[i]->GetName())
8065
0
        {
8066
0
            CPLError(CE_Failure, CPLE_AppDefined,
8067
0
                     "GetResampled(): apoNewDims[%u] should be the same "
8068
0
                     "as its parent",
8069
0
                     i);
8070
0
            return nullptr;
8071
0
        }
8072
0
        else
8073
0
        {
8074
0
            apoNewDims.emplace_back(aoParentDims[i]);
8075
0
        }
8076
0
        anBlockSize.emplace_back(anParentBlockSize[i]);
8077
0
    }
8078
8079
0
    std::unique_ptr<GDALMDArrayResampledDataset> poParentDS(
8080
0
        new GDALMDArrayResampledDataset(poParent, iXDimParent, iYDimParent));
8081
8082
0
    double dfXStart = 0.0;
8083
0
    double dfXSpacing = 0.0;
8084
0
    bool gotXSpacing = false;
8085
0
    auto poNewDimX = apoNewDimsIn[iXDimParent];
8086
0
    if (poNewDimX)
8087
0
    {
8088
0
        if (poNewDimX->GetSize() > static_cast<GUInt64>(INT_MAX))
8089
0
        {
8090
0
            CPLError(CE_Failure, CPLE_NotSupported,
8091
0
                     "Too big size for X dimension");
8092
0
            return nullptr;
8093
0
        }
8094
0
        auto var = poNewDimX->GetIndexingVariable();
8095
0
        if (var)
8096
0
        {
8097
0
            if (var->GetDimensionCount() != 1 ||
8098
0
                var->GetDimensions()[0]->GetSize() != poNewDimX->GetSize() ||
8099
0
                !var->IsRegularlySpaced(dfXStart, dfXSpacing))
8100
0
            {
8101
0
                CPLError(CE_Failure, CPLE_NotSupported,
8102
0
                         "New X dimension should be indexed by a regularly "
8103
0
                         "spaced variable");
8104
0
                return nullptr;
8105
0
            }
8106
0
            gotXSpacing = true;
8107
0
        }
8108
0
    }
8109
8110
0
    double dfYStart = 0.0;
8111
0
    double dfYSpacing = 0.0;
8112
0
    auto poNewDimY = apoNewDimsIn[iYDimParent];
8113
0
    bool gotYSpacing = false;
8114
0
    if (poNewDimY)
8115
0
    {
8116
0
        if (poNewDimY->GetSize() > static_cast<GUInt64>(INT_MAX))
8117
0
        {
8118
0
            CPLError(CE_Failure, CPLE_NotSupported,
8119
0
                     "Too big size for Y dimension");
8120
0
            return nullptr;
8121
0
        }
8122
0
        auto var = poNewDimY->GetIndexingVariable();
8123
0
        if (var)
8124
0
        {
8125
0
            if (var->GetDimensionCount() != 1 ||
8126
0
                var->GetDimensions()[0]->GetSize() != poNewDimY->GetSize() ||
8127
0
                !var->IsRegularlySpaced(dfYStart, dfYSpacing))
8128
0
            {
8129
0
                CPLError(CE_Failure, CPLE_NotSupported,
8130
0
                         "New Y dimension should be indexed by a regularly "
8131
0
                         "spaced variable");
8132
0
                return nullptr;
8133
0
            }
8134
0
            gotYSpacing = true;
8135
0
        }
8136
0
    }
8137
8138
    // This limitation could probably be removed
8139
0
    if ((gotXSpacing && !gotYSpacing) || (!gotXSpacing && gotYSpacing))
8140
0
    {
8141
0
        CPLError(CE_Failure, CPLE_NotSupported,
8142
0
                 "Either none of new X or Y dimension should have an indexing "
8143
0
                 "variable, or both should both should have one.");
8144
0
        return nullptr;
8145
0
    }
8146
8147
0
    std::string osDstWKT;
8148
0
    if (poTargetSRS)
8149
0
    {
8150
0
        char *pszDstWKT = nullptr;
8151
0
        if (poTargetSRS->exportToWkt(&pszDstWKT) != OGRERR_NONE)
8152
0
        {
8153
0
            CPLFree(pszDstWKT);
8154
0
            return nullptr;
8155
0
        }
8156
0
        osDstWKT = pszDstWKT;
8157
0
        CPLFree(pszDstWKT);
8158
0
    }
8159
8160
    // Use coordinate variables for geolocation array
8161
0
    const auto apoCoordinateVars = poParent->GetCoordinateVariables();
8162
0
    bool useGeolocationArray = false;
8163
0
    if (apoCoordinateVars.size() >= 2)
8164
0
    {
8165
0
        std::shared_ptr<GDALMDArray> poLongVar;
8166
0
        std::shared_ptr<GDALMDArray> poLatVar;
8167
0
        for (const auto &poCoordVar : apoCoordinateVars)
8168
0
        {
8169
0
            const auto &osName = poCoordVar->GetName();
8170
0
            const auto poAttr = poCoordVar->GetAttribute("standard_name");
8171
0
            std::string osStandardName;
8172
0
            if (poAttr && poAttr->GetDataType().GetClass() == GEDTC_STRING &&
8173
0
                poAttr->GetDimensionCount() == 0)
8174
0
            {
8175
0
                const char *pszStandardName = poAttr->ReadAsString();
8176
0
                if (pszStandardName)
8177
0
                    osStandardName = pszStandardName;
8178
0
            }
8179
0
            if (osName == "lon" || osName == "longitude" ||
8180
0
                osName == "Longitude" || osStandardName == "longitude")
8181
0
            {
8182
0
                poLongVar = poCoordVar;
8183
0
            }
8184
0
            else if (osName == "lat" || osName == "latitude" ||
8185
0
                     osName == "Latitude" || osStandardName == "latitude")
8186
0
            {
8187
0
                poLatVar = poCoordVar;
8188
0
            }
8189
0
        }
8190
0
        if (poLatVar != nullptr && poLongVar != nullptr)
8191
0
        {
8192
0
            const auto longDimCount = poLongVar->GetDimensionCount();
8193
0
            const auto &longDims = poLongVar->GetDimensions();
8194
0
            const auto latDimCount = poLatVar->GetDimensionCount();
8195
0
            const auto &latDims = poLatVar->GetDimensions();
8196
0
            const auto xDimSize = aoParentDims[iXDimParent]->GetSize();
8197
0
            const auto yDimSize = aoParentDims[iYDimParent]->GetSize();
8198
0
            if (longDimCount == 1 && longDims[0]->GetSize() == xDimSize &&
8199
0
                latDimCount == 1 && latDims[0]->GetSize() == yDimSize)
8200
0
            {
8201
                // Geolocation arrays are 1D, and of consistent size with
8202
                // the variable
8203
0
                useGeolocationArray = true;
8204
0
            }
8205
0
            else if ((longDimCount == 2 ||
8206
0
                      (longDimCount == 3 && longDims[0]->GetSize() == 1)) &&
8207
0
                     longDims[longDimCount - 2]->GetSize() == yDimSize &&
8208
0
                     longDims[longDimCount - 1]->GetSize() == xDimSize &&
8209
0
                     (latDimCount == 2 ||
8210
0
                      (latDimCount == 3 && latDims[0]->GetSize() == 1)) &&
8211
0
                     latDims[latDimCount - 2]->GetSize() == yDimSize &&
8212
0
                     latDims[latDimCount - 1]->GetSize() == xDimSize)
8213
8214
0
            {
8215
                // Geolocation arrays are 2D (or 3D with first dimension of
8216
                // size 1, as found in Sentinel 5P products), and of consistent
8217
                // size with the variable
8218
0
                useGeolocationArray = true;
8219
0
            }
8220
0
            else
8221
0
            {
8222
0
                CPLDebug(
8223
0
                    "GDAL",
8224
0
                    "Longitude and latitude coordinate variables found, "
8225
0
                    "but their characteristics are not compatible of using "
8226
0
                    "them as geolocation arrays");
8227
0
            }
8228
0
            if (useGeolocationArray)
8229
0
            {
8230
0
                CPLDebug("GDAL",
8231
0
                         "Setting geolocation array from variables %s and %s",
8232
0
                         poLongVar->GetName().c_str(),
8233
0
                         poLatVar->GetName().c_str());
8234
0
                const std::string osFilenameLong =
8235
0
                    VSIMemGenerateHiddenFilename("longitude.tif");
8236
0
                const std::string osFilenameLat =
8237
0
                    VSIMemGenerateHiddenFilename("latitude.tif");
8238
0
                std::unique_ptr<GDALDataset> poTmpLongDS(
8239
0
                    longDimCount == 1
8240
0
                        ? poLongVar->AsClassicDataset(0, 0)
8241
0
                        : poLongVar->AsClassicDataset(longDimCount - 1,
8242
0
                                                      longDimCount - 2));
8243
0
                auto hTIFFLongDS = GDALTranslate(
8244
0
                    osFilenameLong.c_str(),
8245
0
                    GDALDataset::ToHandle(poTmpLongDS.get()), nullptr, nullptr);
8246
0
                std::unique_ptr<GDALDataset> poTmpLatDS(
8247
0
                    latDimCount == 1 ? poLatVar->AsClassicDataset(0, 0)
8248
0
                                     : poLatVar->AsClassicDataset(
8249
0
                                           latDimCount - 1, latDimCount - 2));
8250
0
                auto hTIFFLatDS = GDALTranslate(
8251
0
                    osFilenameLat.c_str(),
8252
0
                    GDALDataset::ToHandle(poTmpLatDS.get()), nullptr, nullptr);
8253
0
                const bool bError =
8254
0
                    (hTIFFLatDS == nullptr || hTIFFLongDS == nullptr);
8255
0
                GDALClose(hTIFFLongDS);
8256
0
                GDALClose(hTIFFLatDS);
8257
0
                if (bError)
8258
0
                {
8259
0
                    VSIUnlink(osFilenameLong.c_str());
8260
0
                    VSIUnlink(osFilenameLat.c_str());
8261
0
                    return nullptr;
8262
0
                }
8263
8264
0
                poParentDS->SetGeolocationArray(osFilenameLong, osFilenameLat);
8265
0
            }
8266
0
        }
8267
0
        else
8268
0
        {
8269
0
            CPLDebug("GDAL",
8270
0
                     "Coordinate variables available for %s, but "
8271
0
                     "longitude and/or latitude variables were not identified",
8272
0
                     poParent->GetName().c_str());
8273
0
        }
8274
0
    }
8275
8276
    // Build gdalwarp arguments
8277
0
    CPLStringList aosArgv;
8278
8279
0
    aosArgv.AddString("-of");
8280
0
    aosArgv.AddString("VRT");
8281
8282
0
    aosArgv.AddString("-r");
8283
0
    aosArgv.AddString(pszResampleAlg);
8284
8285
0
    if (!osDstWKT.empty())
8286
0
    {
8287
0
        aosArgv.AddString("-t_srs");
8288
0
        aosArgv.AddString(osDstWKT.c_str());
8289
0
    }
8290
8291
0
    if (useGeolocationArray)
8292
0
        aosArgv.AddString("-geoloc");
8293
8294
0
    if (gotXSpacing && gotYSpacing)
8295
0
    {
8296
0
        const double dfXMin = dfXStart - dfXSpacing / 2;
8297
0
        const double dfXMax =
8298
0
            dfXMin + dfXSpacing * static_cast<double>(poNewDimX->GetSize());
8299
0
        const double dfYMax = dfYStart - dfYSpacing / 2;
8300
0
        const double dfYMin =
8301
0
            dfYMax + dfYSpacing * static_cast<double>(poNewDimY->GetSize());
8302
0
        aosArgv.AddString("-te");
8303
0
        aosArgv.AddString(CPLSPrintf("%.17g", dfXMin));
8304
0
        aosArgv.AddString(CPLSPrintf("%.17g", dfYMin));
8305
0
        aosArgv.AddString(CPLSPrintf("%.17g", dfXMax));
8306
0
        aosArgv.AddString(CPLSPrintf("%.17g", dfYMax));
8307
0
    }
8308
8309
0
    if (poNewDimX && poNewDimY)
8310
0
    {
8311
0
        aosArgv.AddString("-ts");
8312
0
        aosArgv.AddString(
8313
0
            CPLSPrintf("%d", static_cast<int>(poNewDimX->GetSize())));
8314
0
        aosArgv.AddString(
8315
0
            CPLSPrintf("%d", static_cast<int>(poNewDimY->GetSize())));
8316
0
    }
8317
0
    else if (poNewDimX)
8318
0
    {
8319
0
        aosArgv.AddString("-ts");
8320
0
        aosArgv.AddString(
8321
0
            CPLSPrintf("%d", static_cast<int>(poNewDimX->GetSize())));
8322
0
        aosArgv.AddString("0");
8323
0
    }
8324
0
    else if (poNewDimY)
8325
0
    {
8326
0
        aosArgv.AddString("-ts");
8327
0
        aosArgv.AddString("0");
8328
0
        aosArgv.AddString(
8329
0
            CPLSPrintf("%d", static_cast<int>(poNewDimY->GetSize())));
8330
0
    }
8331
8332
    // Create a warped VRT dataset
8333
0
    GDALWarpAppOptions *psOptions =
8334
0
        GDALWarpAppOptionsNew(aosArgv.List(), nullptr);
8335
0
    GDALDatasetH hSrcDS = GDALDataset::ToHandle(poParentDS.get());
8336
0
    std::unique_ptr<GDALDataset> poReprojectedDS(GDALDataset::FromHandle(
8337
0
        GDALWarp("", nullptr, 1, &hSrcDS, psOptions, nullptr)));
8338
0
    GDALWarpAppOptionsFree(psOptions);
8339
0
    if (poReprojectedDS == nullptr)
8340
0
        return nullptr;
8341
8342
0
    int nBlockXSize;
8343
0
    int nBlockYSize;
8344
0
    poReprojectedDS->GetRasterBand(1)->GetBlockSize(&nBlockXSize, &nBlockYSize);
8345
0
    anBlockSize.emplace_back(nBlockYSize);
8346
0
    anBlockSize.emplace_back(nBlockXSize);
8347
8348
0
    double adfGeoTransform[6] = {0, 0, 0, 0, 0, 0};
8349
0
    CPLErr eErr = poReprojectedDS->GetGeoTransform(adfGeoTransform);
8350
0
    CPLAssert(eErr == CE_None);
8351
0
    CPL_IGNORE_RET_VAL(eErr);
8352
8353
0
    auto poDimY = std::make_shared<GDALDimensionWeakIndexingVar>(
8354
0
        std::string(), "dimY", GDAL_DIM_TYPE_HORIZONTAL_Y, "NORTH",
8355
0
        poReprojectedDS->GetRasterYSize());
8356
0
    auto varY = GDALMDArrayRegularlySpaced::Create(
8357
0
        std::string(), poDimY->GetName(), poDimY,
8358
0
        adfGeoTransform[3] + adfGeoTransform[5] / 2, adfGeoTransform[5], 0);
8359
0
    poDimY->SetIndexingVariable(varY);
8360
8361
0
    auto poDimX = std::make_shared<GDALDimensionWeakIndexingVar>(
8362
0
        std::string(), "dimX", GDAL_DIM_TYPE_HORIZONTAL_X, "EAST",
8363
0
        poReprojectedDS->GetRasterXSize());
8364
0
    auto varX = GDALMDArrayRegularlySpaced::Create(
8365
0
        std::string(), poDimX->GetName(), poDimX,
8366
0
        adfGeoTransform[0] + adfGeoTransform[1] / 2, adfGeoTransform[1], 0);
8367
0
    poDimX->SetIndexingVariable(varX);
8368
8369
0
    apoNewDims.emplace_back(poDimY);
8370
0
    apoNewDims.emplace_back(poDimX);
8371
0
    auto newAr(std::shared_ptr<GDALMDArrayResampled>(
8372
0
        new GDALMDArrayResampled(poParent, apoNewDims, anBlockSize)));
8373
0
    newAr->SetSelf(newAr);
8374
0
    if (poTargetSRS)
8375
0
    {
8376
0
        newAr->m_poSRS.reset(poTargetSRS->Clone());
8377
0
    }
8378
0
    else
8379
0
    {
8380
0
        newAr->m_poSRS = poParent->GetSpatialRef();
8381
0
    }
8382
0
    newAr->m_poVarX = varX;
8383
0
    newAr->m_poVarY = varY;
8384
0
    newAr->m_poReprojectedDS = std::move(poReprojectedDS);
8385
0
    newAr->m_poParentDS = std::move(poParentDS);
8386
8387
    // If the input array is y,x,band ordered, the above newAr is
8388
    // actually band,y,x ordered as it is more convenient for
8389
    // GDALMDArrayResampled::IRead() implementation. But transpose that
8390
    // array to the order of the input array
8391
0
    if (bYXBandOrder)
8392
0
        return newAr->Transpose(std::vector<int>{1, 2, 0});
8393
8394
0
    return newAr;
8395
0
}
8396
8397
/************************************************************************/
8398
/*                   GDALMDArrayResampled::IRead()                      */
8399
/************************************************************************/
8400
8401
bool GDALMDArrayResampled::IRead(const GUInt64 *arrayStartIdx,
8402
                                 const size_t *count, const GInt64 *arrayStep,
8403
                                 const GPtrDiff_t *bufferStride,
8404
                                 const GDALExtendedDataType &bufferDataType,
8405
                                 void *pDstBuffer) const
8406
0
{
8407
0
    if (bufferDataType.GetClass() != GEDTC_NUMERIC)
8408
0
        return false;
8409
8410
0
    struct Stack
8411
0
    {
8412
0
        size_t nIters = 0;
8413
0
        GByte *dst_ptr = nullptr;
8414
0
        GPtrDiff_t dst_inc_offset = 0;
8415
0
    };
8416
8417
0
    const auto nDims = GetDimensionCount();
8418
0
    std::vector<Stack> stack(nDims + 1);  // +1 to avoid -Wnull-dereference
8419
0
    const size_t nBufferDTSize = bufferDataType.GetSize();
8420
0
    for (size_t i = 0; i < nDims; i++)
8421
0
    {
8422
0
        stack[i].dst_inc_offset =
8423
0
            static_cast<GPtrDiff_t>(bufferStride[i] * nBufferDTSize);
8424
0
    }
8425
0
    stack[0].dst_ptr = static_cast<GByte *>(pDstBuffer);
8426
8427
0
    size_t dimIdx = 0;
8428
0
    const size_t iDimY = nDims - 2;
8429
0
    const size_t iDimX = nDims - 1;
8430
    // Use an array to avoid a false positive warning from CLang Static
8431
    // Analyzer about flushCaches being never read
8432
0
    bool flushCaches[] = {false};
8433
0
    const bool bYXBandOrder =
8434
0
        m_poParentDS->m_iYDim == 0 && m_poParentDS->m_iXDim == 1;
8435
8436
0
lbl_next_depth:
8437
0
    if (dimIdx == iDimY)
8438
0
    {
8439
0
        if (flushCaches[0])
8440
0
        {
8441
0
            flushCaches[0] = false;
8442
            // When changing of 2D slice, flush GDAL 2D buffers
8443
0
            m_poParentDS->FlushCache(false);
8444
0
            m_poReprojectedDS->FlushCache(false);
8445
0
        }
8446
8447
0
        if (!GDALMDRasterIOFromBand(m_poReprojectedDS->GetRasterBand(1),
8448
0
                                    GF_Read, iDimX, iDimY, arrayStartIdx, count,
8449
0
                                    arrayStep, bufferStride, bufferDataType,
8450
0
                                    stack[dimIdx].dst_ptr))
8451
0
        {
8452
0
            return false;
8453
0
        }
8454
0
    }
8455
0
    else
8456
0
    {
8457
0
        stack[dimIdx].nIters = count[dimIdx];
8458
0
        if (m_poParentDS->m_anOffset[bYXBandOrder ? 2 : dimIdx] !=
8459
0
            arrayStartIdx[dimIdx])
8460
0
        {
8461
0
            flushCaches[0] = true;
8462
0
        }
8463
0
        m_poParentDS->m_anOffset[bYXBandOrder ? 2 : dimIdx] =
8464
0
            arrayStartIdx[dimIdx];
8465
0
        while (true)
8466
0
        {
8467
0
            dimIdx++;
8468
0
            stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr;
8469
0
            goto lbl_next_depth;
8470
0
        lbl_return_to_caller:
8471
0
            dimIdx--;
8472
0
            if ((--stack[dimIdx].nIters) == 0)
8473
0
                break;
8474
0
            flushCaches[0] = true;
8475
0
            ++m_poParentDS->m_anOffset[bYXBandOrder ? 2 : dimIdx];
8476
0
            stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset;
8477
0
        }
8478
0
    }
8479
0
    if (dimIdx > 0)
8480
0
        goto lbl_return_to_caller;
8481
8482
0
    return true;
8483
0
}
8484
8485
/************************************************************************/
8486
/*                           GetResampled()                             */
8487
/************************************************************************/
8488
8489
/** Return an array that is a resampled / reprojected view of the current array
8490
 *
8491
 * This is the same as the C function GDALMDArrayGetResampled().
8492
 *
8493
 * Currently this method can only resample along the last 2 dimensions, unless
8494
 * orthorectifying a NASA EMIT dataset.
8495
 *
8496
 * For NASA EMIT datasets, if apoNewDims[] and poTargetSRS is NULL, the
8497
 * geometry lookup table (GLT) is used by default for fast orthorectification.
8498
 *
8499
 * Options available are:
8500
 * <ul>
8501
 * <li>EMIT_ORTHORECTIFICATION=YES/NO: defaults to YES for a NASA EMIT dataset.
8502
 * Can be set to NO to use generic reprojection method.
8503
 * </li>
8504
 * <li>USE_GOOD_WAVELENGTHS=YES/NO: defaults to YES. Only used for EMIT
8505
 * orthorectification to take into account the value of the
8506
 * /sensor_band_parameters/good_wavelengths array to decide if slices of the
8507
 * current array along the band dimension are valid.</li>
8508
 * </ul>
8509
 *
8510
 * @param apoNewDims New dimensions. Its size should be GetDimensionCount().
8511
 *                   apoNewDims[i] can be NULL to let the method automatically
8512
 *                   determine it.
8513
 * @param resampleAlg Resampling algorithm
8514
 * @param poTargetSRS Target SRS, or nullptr
8515
 * @param papszOptions NULL-terminated list of options, or NULL.
8516
 *
8517
 * @return a new array, that holds a reference to the original one, and thus is
8518
 * a view of it (not a copy), or nullptr in case of error.
8519
 *
8520
 * @since 3.4
8521
 */
8522
std::shared_ptr<GDALMDArray> GDALMDArray::GetResampled(
8523
    const std::vector<std::shared_ptr<GDALDimension>> &apoNewDims,
8524
    GDALRIOResampleAlg resampleAlg, const OGRSpatialReference *poTargetSRS,
8525
    CSLConstList papszOptions) const
8526
0
{
8527
0
    auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock());
8528
0
    if (!self)
8529
0
    {
8530
0
        CPLError(CE_Failure, CPLE_AppDefined,
8531
0
                 "Driver implementation issue: m_pSelf not set !");
8532
0
        return nullptr;
8533
0
    }
8534
0
    if (GetDataType().GetClass() != GEDTC_NUMERIC)
8535
0
    {
8536
0
        CPLError(CE_Failure, CPLE_AppDefined,
8537
0
                 "GetResampled() only supports numeric data type");
8538
0
        return nullptr;
8539
0
    }
8540
8541
    // Special case for NASA EMIT datasets
8542
0
    auto apoDims = GetDimensions();
8543
0
    if (poTargetSRS == nullptr &&
8544
0
        ((apoDims.size() == 3 && apoDims[0]->GetName() == "downtrack" &&
8545
0
          apoDims[1]->GetName() == "crosstrack" &&
8546
0
          apoDims[2]->GetName() == "bands" &&
8547
0
          (apoNewDims == std::vector<std::shared_ptr<GDALDimension>>(3) ||
8548
0
           apoNewDims ==
8549
0
               std::vector<std::shared_ptr<GDALDimension>>{nullptr, nullptr,
8550
0
                                                           apoDims[2]})) ||
8551
0
         (apoDims.size() == 2 && apoDims[0]->GetName() == "downtrack" &&
8552
0
          apoDims[1]->GetName() == "crosstrack" &&
8553
0
          apoNewDims == std::vector<std::shared_ptr<GDALDimension>>(2))) &&
8554
0
        CPLTestBool(CSLFetchNameValueDef(papszOptions,
8555
0
                                         "EMIT_ORTHORECTIFICATION", "YES")))
8556
0
    {
8557
0
        auto poRootGroup = GetRootGroup();
8558
0
        if (poRootGroup)
8559
0
        {
8560
0
            auto poAttrGeotransform = poRootGroup->GetAttribute("geotransform");
8561
0
            auto poLocationGroup = poRootGroup->OpenGroup("location");
8562
0
            if (poAttrGeotransform &&
8563
0
                poAttrGeotransform->GetDataType().GetClass() == GEDTC_NUMERIC &&
8564
0
                poAttrGeotransform->GetDimensionCount() == 1 &&
8565
0
                poAttrGeotransform->GetDimensionsSize()[0] == 6 &&
8566
0
                poLocationGroup)
8567
0
            {
8568
0
                auto poGLT_X = poLocationGroup->OpenMDArray("glt_x");
8569
0
                auto poGLT_Y = poLocationGroup->OpenMDArray("glt_y");
8570
0
                if (poGLT_X && poGLT_X->GetDimensionCount() == 2 &&
8571
0
                    poGLT_X->GetDimensions()[0]->GetName() == "ortho_y" &&
8572
0
                    poGLT_X->GetDimensions()[1]->GetName() == "ortho_x" &&
8573
0
                    poGLT_Y && poGLT_Y->GetDimensionCount() == 2 &&
8574
0
                    poGLT_Y->GetDimensions()[0]->GetName() == "ortho_y" &&
8575
0
                    poGLT_Y->GetDimensions()[1]->GetName() == "ortho_x")
8576
0
                {
8577
0
                    return CreateGLTOrthorectified(
8578
0
                        self, poRootGroup, poGLT_X, poGLT_Y,
8579
0
                        /* nGLTIndexOffset = */ -1,
8580
0
                        poAttrGeotransform->ReadAsDoubleArray(), papszOptions);
8581
0
                }
8582
0
            }
8583
0
        }
8584
0
    }
8585
8586
0
    if (CPLTestBool(CSLFetchNameValueDef(papszOptions,
8587
0
                                         "EMIT_ORTHORECTIFICATION", "NO")))
8588
0
    {
8589
0
        CPLError(CE_Failure, CPLE_AppDefined,
8590
0
                 "EMIT_ORTHORECTIFICATION required, but dataset and/or "
8591
0
                 "parameters are not compatible with it");
8592
0
        return nullptr;
8593
0
    }
8594
8595
0
    return GDALMDArrayResampled::Create(self, apoNewDims, resampleAlg,
8596
0
                                        poTargetSRS, papszOptions);
8597
0
}
8598
8599
/************************************************************************/
8600
/*                         GDALDatasetFromArray()                       */
8601
/************************************************************************/
8602
8603
class GDALDatasetFromArray;
8604
8605
namespace
8606
{
8607
struct MetadataItem
8608
{
8609
    std::shared_ptr<GDALAbstractMDArray> poArray{};
8610
    std::string osName{};
8611
    std::string osDefinition{};
8612
    bool bDefinitionUsesPctForG = false;
8613
};
8614
8615
struct BandImageryMetadata
8616
{
8617
    std::shared_ptr<GDALAbstractMDArray> poCentralWavelengthArray{};
8618
    double dfCentralWavelengthToMicrometer = 1.0;
8619
    std::shared_ptr<GDALAbstractMDArray> poFWHMArray{};
8620
    double dfFWHMToMicrometer = 1.0;
8621
};
8622
8623
}  // namespace
8624
8625
class GDALRasterBandFromArray final : public GDALPamRasterBand
8626
{
8627
    std::vector<GUInt64> m_anOffset{};
8628
    std::vector<size_t> m_anCount{};
8629
    std::vector<GPtrDiff_t> m_anStride{};
8630
8631
  protected:
8632
    CPLErr IReadBlock(int, int, void *) override;
8633
    CPLErr IWriteBlock(int, int, void *) override;
8634
    CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
8635
                     int nYSize, void *pData, int nBufXSize, int nBufYSize,
8636
                     GDALDataType eBufType, GSpacing nPixelSpaceBuf,
8637
                     GSpacing nLineSpaceBuf,
8638
                     GDALRasterIOExtraArg *psExtraArg) override;
8639
8640
  public:
8641
    explicit GDALRasterBandFromArray(
8642
        GDALDatasetFromArray *poDSIn,
8643
        const std::vector<GUInt64> &anOtherDimCoord,
8644
        const std::vector<std::vector<MetadataItem>>
8645
            &aoBandParameterMetadataItems,
8646
        const std::vector<BandImageryMetadata> &aoBandImageryMetadata,
8647
        double dfDelay, time_t nStartTime, bool &bHasWarned);
8648
8649
    double GetNoDataValue(int *pbHasNoData) override;
8650
    int64_t GetNoDataValueAsInt64(int *pbHasNoData) override;
8651
    uint64_t GetNoDataValueAsUInt64(int *pbHasNoData) override;
8652
    double GetOffset(int *pbHasOffset) override;
8653
    double GetScale(int *pbHasScale) override;
8654
    const char *GetUnitType() override;
8655
    GDALColorInterp GetColorInterpretation() override;
8656
};
8657
8658
class GDALDatasetFromArray final : public GDALPamDataset
8659
{
8660
    friend class GDALRasterBandFromArray;
8661
8662
    std::shared_ptr<GDALMDArray> m_poArray;
8663
    size_t m_iXDim;
8664
    size_t m_iYDim;
8665
    double m_adfGeoTransform[6]{0, 1, 0, 0, 0, 1};
8666
    bool m_bHasGT = false;
8667
    mutable std::shared_ptr<OGRSpatialReference> m_poSRS{};
8668
    GDALMultiDomainMetadata m_oMDD{};
8669
    std::string m_osOvrFilename{};
8670
8671
  public:
8672
    GDALDatasetFromArray(const std::shared_ptr<GDALMDArray> &array,
8673
                         size_t iXDim, size_t iYDim)
8674
0
        : m_poArray(array), m_iXDim(iXDim), m_iYDim(iYDim)
8675
0
    {
8676
        // Initialize an overview filename from the filename of the array
8677
        // and its name.
8678
0
        const std::string &osFilename = m_poArray->GetFilename();
8679
0
        if (!osFilename.empty())
8680
0
        {
8681
0
            m_osOvrFilename = osFilename;
8682
0
            m_osOvrFilename += '.';
8683
0
            for (char ch : m_poArray->GetName())
8684
0
            {
8685
0
                if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') ||
8686
0
                    (ch >= 'a' && ch <= 'z') || ch == '_')
8687
0
                {
8688
0
                    m_osOvrFilename += ch;
8689
0
                }
8690
0
                else
8691
0
                {
8692
0
                    m_osOvrFilename += '_';
8693
0
                }
8694
0
            }
8695
0
            m_osOvrFilename += ".ovr";
8696
0
            oOvManager.Initialize(this);
8697
0
        }
8698
0
    }
8699
8700
    static GDALDatasetFromArray *
8701
    Create(const std::shared_ptr<GDALMDArray> &array, size_t iXDim,
8702
           size_t iYDim, const std::shared_ptr<GDALGroup> &poRootGroup,
8703
           CSLConstList papszOptions);
8704
8705
    ~GDALDatasetFromArray() override;
8706
8707
    CPLErr Close() override
8708
0
    {
8709
0
        CPLErr eErr = CE_None;
8710
0
        if (nOpenFlags != OPEN_FLAGS_CLOSED)
8711
0
        {
8712
0
            if (GDALDatasetFromArray::FlushCache(/*bAtClosing=*/true) !=
8713
0
                CE_None)
8714
0
                eErr = CE_Failure;
8715
0
            m_poArray.reset();
8716
0
        }
8717
0
        return eErr;
8718
0
    }
8719
8720
    CPLErr GetGeoTransform(double *padfGeoTransform) override
8721
0
    {
8722
0
        memcpy(padfGeoTransform, m_adfGeoTransform, 6 * sizeof(double));
8723
0
        return m_bHasGT ? CE_None : CE_Failure;
8724
0
    }
8725
8726
    const OGRSpatialReference *GetSpatialRef() const override
8727
0
    {
8728
0
        if (m_poArray->GetDimensionCount() < 2)
8729
0
            return nullptr;
8730
0
        m_poSRS = m_poArray->GetSpatialRef();
8731
0
        if (m_poSRS)
8732
0
        {
8733
0
            m_poSRS.reset(m_poSRS->Clone());
8734
0
            auto axisMapping = m_poSRS->GetDataAxisToSRSAxisMapping();
8735
0
            for (auto &m : axisMapping)
8736
0
            {
8737
0
                if (m == static_cast<int>(m_iXDim) + 1)
8738
0
                    m = 1;
8739
0
                else if (m == static_cast<int>(m_iYDim) + 1)
8740
0
                    m = 2;
8741
0
            }
8742
0
            m_poSRS->SetDataAxisToSRSAxisMapping(axisMapping);
8743
0
        }
8744
0
        return m_poSRS.get();
8745
0
    }
8746
8747
    CPLErr SetMetadata(char **papszMetadata, const char *pszDomain) override
8748
0
    {
8749
0
        return m_oMDD.SetMetadata(papszMetadata, pszDomain);
8750
0
    }
8751
8752
    char **GetMetadata(const char *pszDomain) override
8753
0
    {
8754
0
        return m_oMDD.GetMetadata(pszDomain);
8755
0
    }
8756
8757
    const char *GetMetadataItem(const char *pszName,
8758
                                const char *pszDomain) override
8759
0
    {
8760
0
        if (!m_osOvrFilename.empty() && pszName &&
8761
0
            EQUAL(pszName, "OVERVIEW_FILE") && pszDomain &&
8762
0
            EQUAL(pszDomain, "OVERVIEWS"))
8763
0
        {
8764
0
            return m_osOvrFilename.c_str();
8765
0
        }
8766
0
        return m_oMDD.GetMetadataItem(pszName, pszDomain);
8767
0
    }
8768
};
8769
8770
GDALDatasetFromArray::~GDALDatasetFromArray()
8771
0
{
8772
0
    GDALDatasetFromArray::Close();
8773
0
}
8774
8775
/************************************************************************/
8776
/*                      GDALRasterBandFromArray()                       */
8777
/************************************************************************/
8778
8779
GDALRasterBandFromArray::GDALRasterBandFromArray(
8780
    GDALDatasetFromArray *poDSIn, const std::vector<GUInt64> &anOtherDimCoord,
8781
    const std::vector<std::vector<MetadataItem>> &aoBandParameterMetadataItems,
8782
    const std::vector<BandImageryMetadata> &aoBandImageryMetadata,
8783
    double dfDelay, time_t nStartTime, bool &bHasWarned)
8784
0
{
8785
0
    const auto &poArray(poDSIn->m_poArray);
8786
0
    const auto &dims(poArray->GetDimensions());
8787
0
    const auto nDimCount(dims.size());
8788
0
    const auto blockSize(poArray->GetBlockSize());
8789
0
    nBlockYSize = (nDimCount >= 2 && blockSize[poDSIn->m_iYDim])
8790
0
                      ? static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX),
8791
0
                                                  blockSize[poDSIn->m_iYDim]))
8792
0
                      : 1;
8793
0
    nBlockXSize = blockSize[poDSIn->m_iXDim]
8794
0
                      ? static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX),
8795
0
                                                  blockSize[poDSIn->m_iXDim]))
8796
0
                      : poDSIn->GetRasterXSize();
8797
0
    eDataType = poArray->GetDataType().GetNumericDataType();
8798
0
    eAccess = poDSIn->eAccess;
8799
0
    m_anOffset.resize(nDimCount);
8800
0
    m_anCount.resize(nDimCount, 1);
8801
0
    m_anStride.resize(nDimCount);
8802
0
    for (size_t i = 0, j = 0; i < nDimCount; ++i)
8803
0
    {
8804
0
        if (i != poDSIn->m_iXDim && !(nDimCount >= 2 && i == poDSIn->m_iYDim))
8805
0
        {
8806
0
            std::string dimName(dims[i]->GetName());
8807
0
            GUInt64 nIndex = anOtherDimCoord[j];
8808
            // Detect subset_{orig_dim_name}_{start}_{incr}_{size} names of
8809
            // subsetted dimensions as generated by GetView()
8810
0
            if (STARTS_WITH(dimName.c_str(), "subset_"))
8811
0
            {
8812
0
                CPLStringList aosTokens(
8813
0
                    CSLTokenizeString2(dimName.c_str(), "_", 0));
8814
0
                if (aosTokens.size() == 5)
8815
0
                {
8816
0
                    dimName = aosTokens[1];
8817
0
                    const auto nStartDim = static_cast<GUInt64>(CPLScanUIntBig(
8818
0
                        aosTokens[2], static_cast<int>(strlen(aosTokens[2]))));
8819
0
                    const auto nIncrDim = CPLAtoGIntBig(aosTokens[3]);
8820
0
                    nIndex = nIncrDim > 0 ? nStartDim + nIndex * nIncrDim
8821
0
                                          : nStartDim - (nIndex * -nIncrDim);
8822
0
                }
8823
0
            }
8824
0
            if (nDimCount != 3 || dimName != "Band")
8825
0
            {
8826
0
                SetMetadataItem(
8827
0
                    CPLSPrintf("DIM_%s_INDEX", dimName.c_str()),
8828
0
                    CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(nIndex)));
8829
0
            }
8830
8831
0
            auto indexingVar = dims[i]->GetIndexingVariable();
8832
8833
            // If the indexing variable is also listed in band parameter arrays,
8834
            // then don't use our default formatting
8835
0
            if (indexingVar)
8836
0
            {
8837
0
                for (const auto &oItem : aoBandParameterMetadataItems[j])
8838
0
                {
8839
0
                    if (oItem.poArray->GetFullName() ==
8840
0
                        indexingVar->GetFullName())
8841
0
                    {
8842
0
                        indexingVar.reset();
8843
0
                        break;
8844
0
                    }
8845
0
                }
8846
0
            }
8847
8848
0
            if (indexingVar && indexingVar->GetDimensionCount() == 1 &&
8849
0
                indexingVar->GetDimensions()[0]->GetSize() ==
8850
0
                    dims[i]->GetSize())
8851
0
            {
8852
0
                if (dfDelay >= 0 && time(nullptr) - nStartTime > dfDelay)
8853
0
                {
8854
0
                    if (!bHasWarned)
8855
0
                    {
8856
0
                        CPLError(
8857
0
                            CE_Warning, CPLE_AppDefined,
8858
0
                            "Maximum delay to load band metadata from "
8859
0
                            "dimension indexing variables has expired. "
8860
0
                            "Increase the value of the "
8861
0
                            "LOAD_EXTRA_DIM_METADATA_DELAY "
8862
0
                            "option of GDALMDArray::AsClassicDataset() "
8863
0
                            "(also accessible as the "
8864
0
                            "GDAL_LOAD_EXTRA_DIM_METADATA_DELAY "
8865
0
                            "configuration option), "
8866
0
                            "or set it to 'unlimited' for unlimited delay. ");
8867
0
                        bHasWarned = true;
8868
0
                    }
8869
0
                }
8870
0
                else
8871
0
                {
8872
0
                    size_t nCount = 1;
8873
0
                    const auto &dt(indexingVar->GetDataType());
8874
0
                    std::vector<GByte> abyTmp(dt.GetSize());
8875
0
                    if (indexingVar->Read(&(anOtherDimCoord[j]), &nCount,
8876
0
                                          nullptr, nullptr, dt, &abyTmp[0]))
8877
0
                    {
8878
0
                        char *pszTmp = nullptr;
8879
0
                        GDALExtendedDataType::CopyValue(
8880
0
                            &abyTmp[0], dt, &pszTmp,
8881
0
                            GDALExtendedDataType::CreateString());
8882
0
                        if (pszTmp)
8883
0
                        {
8884
0
                            SetMetadataItem(
8885
0
                                CPLSPrintf("DIM_%s_VALUE", dimName.c_str()),
8886
0
                                pszTmp);
8887
0
                            CPLFree(pszTmp);
8888
0
                        }
8889
8890
0
                        const auto &unit(indexingVar->GetUnit());
8891
0
                        if (!unit.empty())
8892
0
                        {
8893
0
                            SetMetadataItem(
8894
0
                                CPLSPrintf("DIM_%s_UNIT", dimName.c_str()),
8895
0
                                unit.c_str());
8896
0
                        }
8897
0
                    }
8898
0
                }
8899
0
            }
8900
8901
0
            for (const auto &oItem : aoBandParameterMetadataItems[j])
8902
0
            {
8903
0
                CPLString osVal;
8904
8905
0
                size_t nCount = 1;
8906
0
                const auto &dt(oItem.poArray->GetDataType());
8907
0
                if (oItem.bDefinitionUsesPctForG)
8908
0
                {
8909
                    // There is one and only one %[x][.y]f|g in osDefinition
8910
0
                    std::vector<GByte> abyTmp(dt.GetSize());
8911
0
                    if (oItem.poArray->Read(&(anOtherDimCoord[j]), &nCount,
8912
0
                                            nullptr, nullptr, dt, &abyTmp[0]))
8913
0
                    {
8914
0
                        double dfVal = 0;
8915
0
                        GDALExtendedDataType::CopyValue(
8916
0
                            &abyTmp[0], dt, &dfVal,
8917
0
                            GDALExtendedDataType::Create(GDT_Float64));
8918
0
                        osVal.Printf(oItem.osDefinition.c_str(), dfVal);
8919
0
                    }
8920
0
                }
8921
0
                else
8922
0
                {
8923
                    // There should be zero or one %s in osDefinition
8924
0
                    char *pszValue = nullptr;
8925
0
                    if (dt.GetClass() == GEDTC_STRING)
8926
0
                    {
8927
0
                        CPL_IGNORE_RET_VAL(oItem.poArray->Read(
8928
0
                            &(anOtherDimCoord[j]), &nCount, nullptr, nullptr,
8929
0
                            dt, &pszValue));
8930
0
                    }
8931
0
                    else
8932
0
                    {
8933
0
                        std::vector<GByte> abyTmp(dt.GetSize());
8934
0
                        if (oItem.poArray->Read(&(anOtherDimCoord[j]), &nCount,
8935
0
                                                nullptr, nullptr, dt,
8936
0
                                                &abyTmp[0]))
8937
0
                        {
8938
0
                            GDALExtendedDataType::CopyValue(
8939
0
                                &abyTmp[0], dt, &pszValue,
8940
0
                                GDALExtendedDataType::CreateString());
8941
0
                        }
8942
0
                    }
8943
8944
0
                    if (pszValue)
8945
0
                    {
8946
0
                        osVal.Printf(oItem.osDefinition.c_str(), pszValue);
8947
0
                        CPLFree(pszValue);
8948
0
                    }
8949
0
                }
8950
0
                if (!osVal.empty())
8951
0
                    SetMetadataItem(oItem.osName.c_str(), osVal);
8952
0
            }
8953
8954
0
            if (aoBandImageryMetadata[j].poCentralWavelengthArray)
8955
0
            {
8956
0
                auto &poCentralWavelengthArray =
8957
0
                    aoBandImageryMetadata[j].poCentralWavelengthArray;
8958
0
                size_t nCount = 1;
8959
0
                const auto &dt(poCentralWavelengthArray->GetDataType());
8960
0
                std::vector<GByte> abyTmp(dt.GetSize());
8961
0
                if (poCentralWavelengthArray->Read(&(anOtherDimCoord[j]),
8962
0
                                                   &nCount, nullptr, nullptr,
8963
0
                                                   dt, &abyTmp[0]))
8964
0
                {
8965
0
                    double dfVal = 0;
8966
0
                    GDALExtendedDataType::CopyValue(
8967
0
                        &abyTmp[0], dt, &dfVal,
8968
0
                        GDALExtendedDataType::Create(GDT_Float64));
8969
0
                    SetMetadataItem(
8970
0
                        "CENTRAL_WAVELENGTH_UM",
8971
0
                        CPLSPrintf(
8972
0
                            "%g", dfVal * aoBandImageryMetadata[j]
8973
0
                                              .dfCentralWavelengthToMicrometer),
8974
0
                        "IMAGERY");
8975
0
                }
8976
0
            }
8977
8978
0
            if (aoBandImageryMetadata[j].poFWHMArray)
8979
0
            {
8980
0
                auto &poFWHMArray = aoBandImageryMetadata[j].poFWHMArray;
8981
0
                size_t nCount = 1;
8982
0
                const auto &dt(poFWHMArray->GetDataType());
8983
0
                std::vector<GByte> abyTmp(dt.GetSize());
8984
0
                if (poFWHMArray->Read(&(anOtherDimCoord[j]), &nCount, nullptr,
8985
0
                                      nullptr, dt, &abyTmp[0]))
8986
0
                {
8987
0
                    double dfVal = 0;
8988
0
                    GDALExtendedDataType::CopyValue(
8989
0
                        &abyTmp[0], dt, &dfVal,
8990
0
                        GDALExtendedDataType::Create(GDT_Float64));
8991
0
                    SetMetadataItem(
8992
0
                        "FWHM_UM",
8993
0
                        CPLSPrintf("%g", dfVal * aoBandImageryMetadata[j]
8994
0
                                                     .dfFWHMToMicrometer),
8995
0
                        "IMAGERY");
8996
0
                }
8997
0
            }
8998
8999
0
            m_anOffset[i] = anOtherDimCoord[j];
9000
0
            j++;
9001
0
        }
9002
0
    }
9003
0
}
9004
9005
/************************************************************************/
9006
/*                           GetNoDataValue()                           */
9007
/************************************************************************/
9008
9009
double GDALRasterBandFromArray::GetNoDataValue(int *pbHasNoData)
9010
0
{
9011
0
    auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS));
9012
0
    const auto &poArray(l_poDS->m_poArray);
9013
0
    bool bHasNodata = false;
9014
0
    const auto res = poArray->GetNoDataValueAsDouble(&bHasNodata);
9015
0
    if (pbHasNoData)
9016
0
        *pbHasNoData = bHasNodata;
9017
0
    return res;
9018
0
}
9019
9020
/************************************************************************/
9021
/*                       GetNoDataValueAsInt64()                        */
9022
/************************************************************************/
9023
9024
int64_t GDALRasterBandFromArray::GetNoDataValueAsInt64(int *pbHasNoData)
9025
0
{
9026
0
    auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS));
9027
0
    const auto &poArray(l_poDS->m_poArray);
9028
0
    bool bHasNodata = false;
9029
0
    const auto nodata = poArray->GetNoDataValueAsInt64(&bHasNodata);
9030
0
    if (pbHasNoData)
9031
0
        *pbHasNoData = bHasNodata;
9032
0
    return nodata;
9033
0
}
9034
9035
/************************************************************************/
9036
/*                      GetNoDataValueAsUInt64()                        */
9037
/************************************************************************/
9038
9039
uint64_t GDALRasterBandFromArray::GetNoDataValueAsUInt64(int *pbHasNoData)
9040
0
{
9041
0
    auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS));
9042
0
    const auto &poArray(l_poDS->m_poArray);
9043
0
    bool bHasNodata = false;
9044
0
    const auto nodata = poArray->GetNoDataValueAsUInt64(&bHasNodata);
9045
0
    if (pbHasNoData)
9046
0
        *pbHasNoData = bHasNodata;
9047
0
    return nodata;
9048
0
}
9049
9050
/************************************************************************/
9051
/*                             GetOffset()                              */
9052
/************************************************************************/
9053
9054
double GDALRasterBandFromArray::GetOffset(int *pbHasOffset)
9055
0
{
9056
0
    auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS));
9057
0
    const auto &poArray(l_poDS->m_poArray);
9058
0
    bool bHasValue = false;
9059
0
    double dfRes = poArray->GetOffset(&bHasValue);
9060
0
    if (pbHasOffset)
9061
0
        *pbHasOffset = bHasValue;
9062
0
    return dfRes;
9063
0
}
9064
9065
/************************************************************************/
9066
/*                           GetUnitType()                              */
9067
/************************************************************************/
9068
9069
const char *GDALRasterBandFromArray::GetUnitType()
9070
0
{
9071
0
    auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS));
9072
0
    const auto &poArray(l_poDS->m_poArray);
9073
0
    return poArray->GetUnit().c_str();
9074
0
}
9075
9076
/************************************************************************/
9077
/*                             GetScale()                              */
9078
/************************************************************************/
9079
9080
double GDALRasterBandFromArray::GetScale(int *pbHasScale)
9081
0
{
9082
0
    auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS));
9083
0
    const auto &poArray(l_poDS->m_poArray);
9084
0
    bool bHasValue = false;
9085
0
    double dfRes = poArray->GetScale(&bHasValue);
9086
0
    if (pbHasScale)
9087
0
        *pbHasScale = bHasValue;
9088
0
    return dfRes;
9089
0
}
9090
9091
/************************************************************************/
9092
/*                            IReadBlock()                              */
9093
/************************************************************************/
9094
9095
CPLErr GDALRasterBandFromArray::IReadBlock(int nBlockXOff, int nBlockYOff,
9096
                                           void *pImage)
9097
0
{
9098
0
    const int nDTSize(GDALGetDataTypeSizeBytes(eDataType));
9099
0
    const int nXOff = nBlockXOff * nBlockXSize;
9100
0
    const int nYOff = nBlockYOff * nBlockYSize;
9101
0
    const int nReqXSize = std::min(nRasterXSize - nXOff, nBlockXSize);
9102
0
    const int nReqYSize = std::min(nRasterYSize - nYOff, nBlockYSize);
9103
0
    GDALRasterIOExtraArg sExtraArg;
9104
0
    INIT_RASTERIO_EXTRA_ARG(sExtraArg);
9105
0
    return IRasterIO(GF_Read, nXOff, nYOff, nReqXSize, nReqYSize, pImage,
9106
0
                     nReqXSize, nReqYSize, eDataType, nDTSize,
9107
0
                     static_cast<GSpacing>(nDTSize) * nBlockXSize, &sExtraArg);
9108
0
}
9109
9110
/************************************************************************/
9111
/*                            IWriteBlock()                             */
9112
/************************************************************************/
9113
9114
CPLErr GDALRasterBandFromArray::IWriteBlock(int nBlockXOff, int nBlockYOff,
9115
                                            void *pImage)
9116
0
{
9117
0
    const int nDTSize(GDALGetDataTypeSizeBytes(eDataType));
9118
0
    const int nXOff = nBlockXOff * nBlockXSize;
9119
0
    const int nYOff = nBlockYOff * nBlockYSize;
9120
0
    const int nReqXSize = std::min(nRasterXSize - nXOff, nBlockXSize);
9121
0
    const int nReqYSize = std::min(nRasterYSize - nYOff, nBlockYSize);
9122
0
    GDALRasterIOExtraArg sExtraArg;
9123
0
    INIT_RASTERIO_EXTRA_ARG(sExtraArg);
9124
0
    return IRasterIO(GF_Write, nXOff, nYOff, nReqXSize, nReqYSize, pImage,
9125
0
                     nReqXSize, nReqYSize, eDataType, nDTSize,
9126
0
                     static_cast<GSpacing>(nDTSize) * nBlockXSize, &sExtraArg);
9127
0
}
9128
9129
/************************************************************************/
9130
/*                            IRasterIO()                               */
9131
/************************************************************************/
9132
9133
CPLErr GDALRasterBandFromArray::IRasterIO(GDALRWFlag eRWFlag, int nXOff,
9134
                                          int nYOff, int nXSize, int nYSize,
9135
                                          void *pData, int nBufXSize,
9136
                                          int nBufYSize, GDALDataType eBufType,
9137
                                          GSpacing nPixelSpaceBuf,
9138
                                          GSpacing nLineSpaceBuf,
9139
                                          GDALRasterIOExtraArg *psExtraArg)
9140
0
{
9141
0
    auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS));
9142
0
    const auto &poArray(l_poDS->m_poArray);
9143
0
    const int nBufferDTSize(GDALGetDataTypeSizeBytes(eBufType));
9144
0
    if (nXSize == nBufXSize && nYSize == nBufYSize && nBufferDTSize > 0 &&
9145
0
        (nPixelSpaceBuf % nBufferDTSize) == 0 &&
9146
0
        (nLineSpaceBuf % nBufferDTSize) == 0)
9147
0
    {
9148
0
        m_anOffset[l_poDS->m_iXDim] = static_cast<GUInt64>(nXOff);
9149
0
        m_anCount[l_poDS->m_iXDim] = static_cast<size_t>(nXSize);
9150
0
        m_anStride[l_poDS->m_iXDim] =
9151
0
            static_cast<GPtrDiff_t>(nPixelSpaceBuf / nBufferDTSize);
9152
0
        if (poArray->GetDimensionCount() >= 2)
9153
0
        {
9154
0
            m_anOffset[l_poDS->m_iYDim] = static_cast<GUInt64>(nYOff);
9155
0
            m_anCount[l_poDS->m_iYDim] = static_cast<size_t>(nYSize);
9156
0
            m_anStride[l_poDS->m_iYDim] =
9157
0
                static_cast<GPtrDiff_t>(nLineSpaceBuf / nBufferDTSize);
9158
0
        }
9159
0
        if (eRWFlag == GF_Read)
9160
0
        {
9161
0
            return poArray->Read(m_anOffset.data(), m_anCount.data(), nullptr,
9162
0
                                 m_anStride.data(),
9163
0
                                 GDALExtendedDataType::Create(eBufType), pData)
9164
0
                       ? CE_None
9165
0
                       : CE_Failure;
9166
0
        }
9167
0
        else
9168
0
        {
9169
0
            return poArray->Write(m_anOffset.data(), m_anCount.data(), nullptr,
9170
0
                                  m_anStride.data(),
9171
0
                                  GDALExtendedDataType::Create(eBufType), pData)
9172
0
                       ? CE_None
9173
0
                       : CE_Failure;
9174
0
        }
9175
0
    }
9176
0
    return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
9177
0
                                     pData, nBufXSize, nBufYSize, eBufType,
9178
0
                                     nPixelSpaceBuf, nLineSpaceBuf, psExtraArg);
9179
0
}
9180
9181
/************************************************************************/
9182
/*                      GetColorInterpretation()                        */
9183
/************************************************************************/
9184
9185
GDALColorInterp GDALRasterBandFromArray::GetColorInterpretation()
9186
0
{
9187
0
    auto l_poDS(cpl::down_cast<GDALDatasetFromArray *>(poDS));
9188
0
    const auto &poArray(l_poDS->m_poArray);
9189
0
    auto poAttr = poArray->GetAttribute("COLOR_INTERPRETATION");
9190
0
    if (poAttr && poAttr->GetDataType().GetClass() == GEDTC_STRING)
9191
0
    {
9192
0
        bool bOK = false;
9193
0
        GUInt64 nStartIndex = 0;
9194
0
        if (poArray->GetDimensionCount() == 2 &&
9195
0
            poAttr->GetDimensionCount() == 0)
9196
0
        {
9197
0
            bOK = true;
9198
0
        }
9199
0
        else if (poArray->GetDimensionCount() == 3)
9200
0
        {
9201
0
            uint64_t nExtraDimSamples = 1;
9202
0
            const auto &apoDims = poArray->GetDimensions();
9203
0
            for (size_t i = 0; i < apoDims.size(); ++i)
9204
0
            {
9205
0
                if (i != l_poDS->m_iXDim && i != l_poDS->m_iYDim)
9206
0
                    nExtraDimSamples *= apoDims[i]->GetSize();
9207
0
            }
9208
0
            if (poAttr->GetDimensionsSize() ==
9209
0
                std::vector<GUInt64>{static_cast<GUInt64>(nExtraDimSamples)})
9210
0
            {
9211
0
                bOK = true;
9212
0
            }
9213
0
            nStartIndex = nBand - 1;
9214
0
        }
9215
0
        if (bOK)
9216
0
        {
9217
0
            const auto oStringDT = GDALExtendedDataType::CreateString();
9218
0
            const size_t nCount = 1;
9219
0
            const GInt64 arrayStep = 1;
9220
0
            const GPtrDiff_t bufferStride = 1;
9221
0
            char *pszValue = nullptr;
9222
0
            poAttr->Read(&nStartIndex, &nCount, &arrayStep, &bufferStride,
9223
0
                         oStringDT, &pszValue);
9224
0
            if (pszValue)
9225
0
            {
9226
0
                const auto eColorInterp =
9227
0
                    GDALGetColorInterpretationByName(pszValue);
9228
0
                CPLFree(pszValue);
9229
0
                return eColorInterp;
9230
0
            }
9231
0
        }
9232
0
    }
9233
0
    return GCI_Undefined;
9234
0
}
9235
9236
/************************************************************************/
9237
/*                    GDALDatasetFromArray::Create()                    */
9238
/************************************************************************/
9239
9240
GDALDatasetFromArray *GDALDatasetFromArray::Create(
9241
    const std::shared_ptr<GDALMDArray> &array, size_t iXDim, size_t iYDim,
9242
    const std::shared_ptr<GDALGroup> &poRootGroup, CSLConstList papszOptions)
9243
9244
0
{
9245
0
    const auto nDimCount(array->GetDimensionCount());
9246
0
    if (nDimCount == 0)
9247
0
    {
9248
0
        CPLError(CE_Failure, CPLE_NotSupported,
9249
0
                 "Unsupported number of dimensions");
9250
0
        return nullptr;
9251
0
    }
9252
0
    if (array->GetDataType().GetClass() != GEDTC_NUMERIC ||
9253
0
        array->GetDataType().GetNumericDataType() == GDT_Unknown)
9254
0
    {
9255
0
        CPLError(CE_Failure, CPLE_NotSupported,
9256
0
                 "Only arrays with numeric data types "
9257
0
                 "can be exposed as classic GDALDataset");
9258
0
        return nullptr;
9259
0
    }
9260
0
    if (iXDim >= nDimCount ||
9261
0
        (nDimCount >= 2 && (iYDim >= nDimCount || iXDim == iYDim)))
9262
0
    {
9263
0
        CPLError(CE_Failure, CPLE_NotSupported, "Invalid iXDim and/or iYDim");
9264
0
        return nullptr;
9265
0
    }
9266
0
    GUInt64 nTotalBands = 1;
9267
0
    const auto &dims(array->GetDimensions());
9268
0
    for (size_t i = 0; i < nDimCount; ++i)
9269
0
    {
9270
0
        if (i != iXDim && !(nDimCount >= 2 && i == iYDim))
9271
0
        {
9272
0
            if (dims[i]->GetSize() > 65536 / nTotalBands)
9273
0
            {
9274
0
                CPLError(CE_Failure, CPLE_AppDefined,
9275
0
                         "Too many bands. Operate on a sliced view");
9276
0
                return nullptr;
9277
0
            }
9278
0
            nTotalBands *= dims[i]->GetSize();
9279
0
        }
9280
0
    }
9281
9282
0
    std::map<std::string, size_t> oMapArrayDimNameToExtraDimIdx;
9283
0
    std::vector<size_t> oMapArrayExtraDimIdxToOriginalIdx;
9284
0
    for (size_t i = 0, j = 0; i < nDimCount; ++i)
9285
0
    {
9286
0
        if (i != iXDim && !(nDimCount >= 2 && i == iYDim))
9287
0
        {
9288
0
            oMapArrayDimNameToExtraDimIdx[dims[i]->GetName()] = j;
9289
0
            oMapArrayExtraDimIdxToOriginalIdx.push_back(i);
9290
0
            ++j;
9291
0
        }
9292
0
    }
9293
9294
0
    const size_t nNewDimCount = nDimCount >= 2 ? nDimCount - 2 : 0;
9295
9296
0
    const char *pszBandMetadata =
9297
0
        CSLFetchNameValue(papszOptions, "BAND_METADATA");
9298
0
    std::vector<std::vector<MetadataItem>> aoBandParameterMetadataItems(
9299
0
        nNewDimCount);
9300
0
    if (pszBandMetadata)
9301
0
    {
9302
0
        if (!poRootGroup)
9303
0
        {
9304
0
            CPLError(CE_Failure, CPLE_AppDefined,
9305
0
                     "Root group should be provided when BAND_METADATA is set");
9306
0
            return nullptr;
9307
0
        }
9308
0
        CPLJSONDocument oDoc;
9309
0
        if (!oDoc.LoadMemory(pszBandMetadata))
9310
0
        {
9311
0
            CPLError(CE_Failure, CPLE_AppDefined,
9312
0
                     "Invalid JSON content for BAND_METADATA");
9313
0
            return nullptr;
9314
0
        }
9315
0
        auto oRoot = oDoc.GetRoot();
9316
0
        if (oRoot.GetType() != CPLJSONObject::Type::Array)
9317
0
        {
9318
0
            CPLError(CE_Failure, CPLE_AppDefined,
9319
0
                     "Value of BAND_METADATA should be an array");
9320
0
            return nullptr;
9321
0
        }
9322
9323
0
        auto oArray = oRoot.ToArray();
9324
0
        for (int j = 0; j < oArray.Size(); ++j)
9325
0
        {
9326
0
            const auto oJsonItem = oArray[j];
9327
0
            MetadataItem oItem;
9328
0
            size_t iExtraDimIdx = 0;
9329
9330
0
            const auto osBandArrayFullname = oJsonItem.GetString("array");
9331
0
            const auto osBandAttributeName = oJsonItem.GetString("attribute");
9332
0
            std::shared_ptr<GDALMDArray> poArray;
9333
0
            std::shared_ptr<GDALAttribute> poAttribute;
9334
0
            if (osBandArrayFullname.empty() && osBandAttributeName.empty())
9335
0
            {
9336
0
                CPLError(CE_Failure, CPLE_AppDefined,
9337
0
                         "BAND_METADATA[%d][\"array\"] or "
9338
0
                         "BAND_METADATA[%d][\"attribute\"] is missing",
9339
0
                         j, j);
9340
0
                return nullptr;
9341
0
            }
9342
0
            else if (!osBandArrayFullname.empty() &&
9343
0
                     !osBandAttributeName.empty())
9344
0
            {
9345
0
                CPLError(
9346
0
                    CE_Failure, CPLE_AppDefined,
9347
0
                    "BAND_METADATA[%d][\"array\"] and "
9348
0
                    "BAND_METADATA[%d][\"attribute\"] are mutually exclusive",
9349
0
                    j, j);
9350
0
                return nullptr;
9351
0
            }
9352
0
            else if (!osBandArrayFullname.empty())
9353
0
            {
9354
0
                poArray =
9355
0
                    poRootGroup->OpenMDArrayFromFullname(osBandArrayFullname);
9356
0
                if (!poArray)
9357
0
                {
9358
0
                    CPLError(CE_Failure, CPLE_AppDefined,
9359
0
                             "Array %s cannot be found",
9360
0
                             osBandArrayFullname.c_str());
9361
0
                    return nullptr;
9362
0
                }
9363
0
                if (poArray->GetDimensionCount() != 1)
9364
0
                {
9365
0
                    CPLError(CE_Failure, CPLE_AppDefined,
9366
0
                             "Array %s is not a 1D array",
9367
0
                             osBandArrayFullname.c_str());
9368
0
                    return nullptr;
9369
0
                }
9370
0
                const auto &osAuxArrayDimName =
9371
0
                    poArray->GetDimensions()[0]->GetName();
9372
0
                auto oIter =
9373
0
                    oMapArrayDimNameToExtraDimIdx.find(osAuxArrayDimName);
9374
0
                if (oIter == oMapArrayDimNameToExtraDimIdx.end())
9375
0
                {
9376
0
                    CPLError(
9377
0
                        CE_Failure, CPLE_AppDefined,
9378
0
                        "Dimension %s of array %s is not a non-X/Y dimension "
9379
0
                        "of array %s",
9380
0
                        osAuxArrayDimName.c_str(), osBandArrayFullname.c_str(),
9381
0
                        array->GetName().c_str());
9382
0
                    return nullptr;
9383
0
                }
9384
0
                iExtraDimIdx = oIter->second;
9385
0
                CPLAssert(iExtraDimIdx < nNewDimCount);
9386
0
            }
9387
0
            else
9388
0
            {
9389
0
                CPLAssert(!osBandAttributeName.empty());
9390
0
                poAttribute = !osBandAttributeName.empty() &&
9391
0
                                      osBandAttributeName[0] == '/'
9392
0
                                  ? poRootGroup->OpenAttributeFromFullname(
9393
0
                                        osBandAttributeName)
9394
0
                                  : array->GetAttribute(osBandAttributeName);
9395
0
                if (!poAttribute)
9396
0
                {
9397
0
                    CPLError(CE_Failure, CPLE_AppDefined,
9398
0
                             "Attribute %s cannot be found",
9399
0
                             osBandAttributeName.c_str());
9400
0
                    return nullptr;
9401
0
                }
9402
0
                const auto aoAttrDims = poAttribute->GetDimensionsSize();
9403
0
                if (aoAttrDims.size() != 1)
9404
0
                {
9405
0
                    CPLError(CE_Failure, CPLE_AppDefined,
9406
0
                             "Attribute %s is not a 1D array",
9407
0
                             osBandAttributeName.c_str());
9408
0
                    return nullptr;
9409
0
                }
9410
0
                bool found = false;
9411
0
                for (const auto &iter : oMapArrayDimNameToExtraDimIdx)
9412
0
                {
9413
0
                    if (dims[oMapArrayExtraDimIdxToOriginalIdx[iter.second]]
9414
0
                            ->GetSize() == aoAttrDims[0])
9415
0
                    {
9416
0
                        if (found)
9417
0
                        {
9418
0
                            CPLError(CE_Failure, CPLE_AppDefined,
9419
0
                                     "Several dimensions of %s have the same "
9420
0
                                     "size as attribute %s. Cannot infer which "
9421
0
                                     "one to bind to!",
9422
0
                                     array->GetName().c_str(),
9423
0
                                     osBandAttributeName.c_str());
9424
0
                            return nullptr;
9425
0
                        }
9426
0
                        found = true;
9427
0
                        iExtraDimIdx = iter.second;
9428
0
                    }
9429
0
                }
9430
0
                if (!found)
9431
0
                {
9432
0
                    CPLError(
9433
0
                        CE_Failure, CPLE_AppDefined,
9434
0
                        "No dimension of %s has the same size as attribute %s",
9435
0
                        array->GetName().c_str(), osBandAttributeName.c_str());
9436
0
                    return nullptr;
9437
0
                }
9438
0
            }
9439
9440
0
            oItem.osName = oJsonItem.GetString("item_name");
9441
0
            if (oItem.osName.empty())
9442
0
            {
9443
0
                CPLError(CE_Failure, CPLE_AppDefined,
9444
0
                         "BAND_METADATA[%d][\"item_name\"] is missing", j);
9445
0
                return nullptr;
9446
0
            }
9447
9448
0
            const auto osDefinition = oJsonItem.GetString("item_value", "%s");
9449
9450
            // Check correctness of definition
9451
0
            bool bFirstNumericFormatter = true;
9452
0
            std::string osModDefinition;
9453
0
            bool bDefinitionUsesPctForG = false;
9454
0
            for (size_t k = 0; k < osDefinition.size(); ++k)
9455
0
            {
9456
0
                if (osDefinition[k] == '%')
9457
0
                {
9458
0
                    osModDefinition += osDefinition[k];
9459
0
                    if (k + 1 == osDefinition.size())
9460
0
                    {
9461
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9462
0
                                 "Value of "
9463
0
                                 "BAND_METADATA[%d][\"item_value\"] = "
9464
0
                                 "%s is invalid at offset %d",
9465
0
                                 j, osDefinition.c_str(), int(k));
9466
0
                        return nullptr;
9467
0
                    }
9468
0
                    ++k;
9469
0
                    if (osDefinition[k] == '%')
9470
0
                    {
9471
0
                        osModDefinition += osDefinition[k];
9472
0
                        continue;
9473
0
                    }
9474
0
                    if (!bFirstNumericFormatter)
9475
0
                    {
9476
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9477
0
                                 "Value of "
9478
0
                                 "BAND_METADATA[%d][\"item_value\"] = %s is "
9479
0
                                 "invalid at offset %d: %%[x][.y]f|g or %%s "
9480
0
                                 "formatters should be specified at most once",
9481
0
                                 j, osDefinition.c_str(), int(k));
9482
0
                        return nullptr;
9483
0
                    }
9484
0
                    bFirstNumericFormatter = false;
9485
0
                    for (; k < osDefinition.size(); ++k)
9486
0
                    {
9487
0
                        osModDefinition += osDefinition[k];
9488
0
                        if (!((osDefinition[k] >= '0' &&
9489
0
                               osDefinition[k] <= '9') ||
9490
0
                              osDefinition[k] == '.'))
9491
0
                            break;
9492
0
                    }
9493
0
                    if (k == osDefinition.size() ||
9494
0
                        (osDefinition[k] != 'f' && osDefinition[k] != 'g' &&
9495
0
                         osDefinition[k] != 's'))
9496
0
                    {
9497
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9498
0
                                 "Value of "
9499
0
                                 "BAND_METADATA[%d][\"item_value\"] = "
9500
0
                                 "%s is invalid at offset %d: only "
9501
0
                                 "%%[x][.y]f|g or %%s formatters are accepted",
9502
0
                                 j, osDefinition.c_str(), int(k));
9503
0
                        return nullptr;
9504
0
                    }
9505
0
                    bDefinitionUsesPctForG =
9506
0
                        (osDefinition[k] == 'f' || osDefinition[k] == 'g');
9507
0
                    if (bDefinitionUsesPctForG)
9508
0
                    {
9509
0
                        if (poArray &&
9510
0
                            poArray->GetDataType().GetClass() != GEDTC_NUMERIC)
9511
0
                        {
9512
0
                            CPLError(CE_Failure, CPLE_AppDefined,
9513
0
                                     "Data type of %s array is not numeric",
9514
0
                                     poArray->GetName().c_str());
9515
0
                            return nullptr;
9516
0
                        }
9517
0
                        else if (poAttribute &&
9518
0
                                 poAttribute->GetDataType().GetClass() !=
9519
0
                                     GEDTC_NUMERIC)
9520
0
                        {
9521
0
                            CPLError(CE_Failure, CPLE_AppDefined,
9522
0
                                     "Data type of %s attribute is not numeric",
9523
0
                                     poAttribute->GetFullName().c_str());
9524
0
                            return nullptr;
9525
0
                        }
9526
0
                    }
9527
0
                }
9528
0
                else if (osDefinition[k] == '$' &&
9529
0
                         k + 1 < osDefinition.size() &&
9530
0
                         osDefinition[k + 1] == '{')
9531
0
                {
9532
0
                    const auto nPos = osDefinition.find('}', k);
9533
0
                    if (nPos == std::string::npos)
9534
0
                    {
9535
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9536
0
                                 "Value of "
9537
0
                                 "BAND_METADATA[%d][\"item_value\"] = "
9538
0
                                 "%s is invalid at offset %d",
9539
0
                                 j, osDefinition.c_str(), int(k));
9540
0
                        return nullptr;
9541
0
                    }
9542
0
                    const auto osAttrName =
9543
0
                        osDefinition.substr(k + 2, nPos - (k + 2));
9544
0
                    std::shared_ptr<GDALAttribute> poAttr;
9545
0
                    if (poArray && !osAttrName.empty() && osAttrName[0] != '/')
9546
0
                    {
9547
0
                        poAttr = poArray->GetAttribute(osAttrName);
9548
0
                        if (!poAttr)
9549
0
                        {
9550
0
                            CPLError(
9551
0
                                CE_Failure, CPLE_AppDefined,
9552
0
                                "Value of "
9553
0
                                "BAND_METADATA[%d][\"item_value\"] = "
9554
0
                                "%s is invalid: %s is not an attribute of %s",
9555
0
                                j, osDefinition.c_str(), osAttrName.c_str(),
9556
0
                                poArray->GetName().c_str());
9557
0
                            return nullptr;
9558
0
                        }
9559
0
                    }
9560
0
                    else
9561
0
                    {
9562
0
                        poAttr =
9563
0
                            poRootGroup->OpenAttributeFromFullname(osAttrName);
9564
0
                        if (!poAttr)
9565
0
                        {
9566
0
                            CPLError(CE_Failure, CPLE_AppDefined,
9567
0
                                     "Value of "
9568
0
                                     "BAND_METADATA[%d][\"item_value\"] = "
9569
0
                                     "%s is invalid: %s is not an attribute",
9570
0
                                     j, osDefinition.c_str(),
9571
0
                                     osAttrName.c_str());
9572
0
                            return nullptr;
9573
0
                        }
9574
0
                    }
9575
0
                    k = nPos;
9576
0
                    const char *pszValue = poAttr->ReadAsString();
9577
0
                    if (!pszValue)
9578
0
                    {
9579
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9580
0
                                 "Cannot get value of attribute %s as a "
9581
0
                                 "string",
9582
0
                                 osAttrName.c_str());
9583
0
                        return nullptr;
9584
0
                    }
9585
0
                    osModDefinition += pszValue;
9586
0
                }
9587
0
                else
9588
0
                {
9589
0
                    osModDefinition += osDefinition[k];
9590
0
                }
9591
0
            }
9592
9593
0
            if (poArray)
9594
0
                oItem.poArray = std::move(poArray);
9595
0
            else
9596
0
                oItem.poArray = std::move(poAttribute);
9597
0
            oItem.osDefinition = std::move(osModDefinition);
9598
0
            oItem.bDefinitionUsesPctForG = bDefinitionUsesPctForG;
9599
9600
0
            aoBandParameterMetadataItems[iExtraDimIdx].emplace_back(
9601
0
                std::move(oItem));
9602
0
        }
9603
0
    }
9604
9605
0
    std::vector<BandImageryMetadata> aoBandImageryMetadata(nNewDimCount);
9606
0
    const char *pszBandImageryMetadata =
9607
0
        CSLFetchNameValue(papszOptions, "BAND_IMAGERY_METADATA");
9608
0
    if (pszBandImageryMetadata)
9609
0
    {
9610
0
        if (!poRootGroup)
9611
0
        {
9612
0
            CPLError(CE_Failure, CPLE_AppDefined,
9613
0
                     "Root group should be provided when BAND_IMAGERY_METADATA "
9614
0
                     "is set");
9615
0
            return nullptr;
9616
0
        }
9617
0
        CPLJSONDocument oDoc;
9618
0
        if (!oDoc.LoadMemory(pszBandImageryMetadata))
9619
0
        {
9620
0
            CPLError(CE_Failure, CPLE_AppDefined,
9621
0
                     "Invalid JSON content for BAND_IMAGERY_METADATA");
9622
0
            return nullptr;
9623
0
        }
9624
0
        auto oRoot = oDoc.GetRoot();
9625
0
        if (oRoot.GetType() != CPLJSONObject::Type::Object)
9626
0
        {
9627
0
            CPLError(CE_Failure, CPLE_AppDefined,
9628
0
                     "Value of BAND_IMAGERY_METADATA should be an object");
9629
0
            return nullptr;
9630
0
        }
9631
0
        for (const auto &oJsonItem : oRoot.GetChildren())
9632
0
        {
9633
0
            if (oJsonItem.GetName() == "CENTRAL_WAVELENGTH_UM" ||
9634
0
                oJsonItem.GetName() == "FWHM_UM")
9635
0
            {
9636
0
                const auto osBandArrayFullname = oJsonItem.GetString("array");
9637
0
                const auto osBandAttributeName =
9638
0
                    oJsonItem.GetString("attribute");
9639
0
                std::shared_ptr<GDALMDArray> poArray;
9640
0
                std::shared_ptr<GDALAttribute> poAttribute;
9641
0
                size_t iExtraDimIdx = 0;
9642
0
                if (osBandArrayFullname.empty() && osBandAttributeName.empty())
9643
0
                {
9644
0
                    CPLError(CE_Failure, CPLE_AppDefined,
9645
0
                             "BAND_IMAGERY_METADATA[\"%s\"][\"array\"] or "
9646
0
                             "BAND_IMAGERY_METADATA[\"%s\"][\"attribute\"] is "
9647
0
                             "missing",
9648
0
                             oJsonItem.GetName().c_str(),
9649
0
                             oJsonItem.GetName().c_str());
9650
0
                    return nullptr;
9651
0
                }
9652
0
                else if (!osBandArrayFullname.empty() &&
9653
0
                         !osBandAttributeName.empty())
9654
0
                {
9655
0
                    CPLError(CE_Failure, CPLE_AppDefined,
9656
0
                             "BAND_IMAGERY_METADATA[\"%s\"][\"array\"] and "
9657
0
                             "BAND_IMAGERY_METADATA[\"%s\"][\"attribute\"] are "
9658
0
                             "mutually exclusive",
9659
0
                             oJsonItem.GetName().c_str(),
9660
0
                             oJsonItem.GetName().c_str());
9661
0
                    return nullptr;
9662
0
                }
9663
0
                else if (!osBandArrayFullname.empty())
9664
0
                {
9665
0
                    poArray = poRootGroup->OpenMDArrayFromFullname(
9666
0
                        osBandArrayFullname);
9667
0
                    if (!poArray)
9668
0
                    {
9669
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9670
0
                                 "Array %s cannot be found",
9671
0
                                 osBandArrayFullname.c_str());
9672
0
                        return nullptr;
9673
0
                    }
9674
0
                    if (poArray->GetDimensionCount() != 1)
9675
0
                    {
9676
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9677
0
                                 "Array %s is not a 1D array",
9678
0
                                 osBandArrayFullname.c_str());
9679
0
                        return nullptr;
9680
0
                    }
9681
0
                    const auto &osAuxArrayDimName =
9682
0
                        poArray->GetDimensions()[0]->GetName();
9683
0
                    auto oIter =
9684
0
                        oMapArrayDimNameToExtraDimIdx.find(osAuxArrayDimName);
9685
0
                    if (oIter == oMapArrayDimNameToExtraDimIdx.end())
9686
0
                    {
9687
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9688
0
                                 "Dimension \"%s\" of array \"%s\" is not a "
9689
0
                                 "non-X/Y dimension of array \"%s\"",
9690
0
                                 osAuxArrayDimName.c_str(),
9691
0
                                 osBandArrayFullname.c_str(),
9692
0
                                 array->GetName().c_str());
9693
0
                        return nullptr;
9694
0
                    }
9695
0
                    iExtraDimIdx = oIter->second;
9696
0
                    CPLAssert(iExtraDimIdx < nNewDimCount);
9697
0
                }
9698
0
                else
9699
0
                {
9700
0
                    poAttribute =
9701
0
                        !osBandAttributeName.empty() &&
9702
0
                                osBandAttributeName[0] == '/'
9703
0
                            ? poRootGroup->OpenAttributeFromFullname(
9704
0
                                  osBandAttributeName)
9705
0
                            : array->GetAttribute(osBandAttributeName);
9706
0
                    if (!poAttribute)
9707
0
                    {
9708
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9709
0
                                 "Attribute %s cannot be found",
9710
0
                                 osBandAttributeName.c_str());
9711
0
                        return nullptr;
9712
0
                    }
9713
0
                    const auto aoAttrDims = poAttribute->GetDimensionsSize();
9714
0
                    if (aoAttrDims.size() != 1)
9715
0
                    {
9716
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9717
0
                                 "Attribute %s is not a 1D array",
9718
0
                                 osBandAttributeName.c_str());
9719
0
                        return nullptr;
9720
0
                    }
9721
0
                    bool found = false;
9722
0
                    for (const auto &iter : oMapArrayDimNameToExtraDimIdx)
9723
0
                    {
9724
0
                        if (dims[oMapArrayExtraDimIdxToOriginalIdx[iter.second]]
9725
0
                                ->GetSize() == aoAttrDims[0])
9726
0
                        {
9727
0
                            if (found)
9728
0
                            {
9729
0
                                CPLError(CE_Failure, CPLE_AppDefined,
9730
0
                                         "Several dimensions of %s have the "
9731
0
                                         "same size as attribute %s. Cannot "
9732
0
                                         "infer which one to bind to!",
9733
0
                                         array->GetName().c_str(),
9734
0
                                         osBandAttributeName.c_str());
9735
0
                                return nullptr;
9736
0
                            }
9737
0
                            found = true;
9738
0
                            iExtraDimIdx = iter.second;
9739
0
                        }
9740
0
                    }
9741
0
                    if (!found)
9742
0
                    {
9743
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9744
0
                                 "No dimension of %s has the same size as "
9745
0
                                 "attribute %s",
9746
0
                                 array->GetName().c_str(),
9747
0
                                 osBandAttributeName.c_str());
9748
0
                        return nullptr;
9749
0
                    }
9750
0
                }
9751
9752
0
                std::string osUnit = oJsonItem.GetString("unit", "um");
9753
0
                if (STARTS_WITH(osUnit.c_str(), "${"))
9754
0
                {
9755
0
                    if (osUnit.back() != '}')
9756
0
                    {
9757
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9758
0
                                 "Value of "
9759
0
                                 "BAND_IMAGERY_METADATA[\"%s\"][\"unit\"] = "
9760
0
                                 "%s is invalid",
9761
0
                                 oJsonItem.GetName().c_str(), osUnit.c_str());
9762
0
                        return nullptr;
9763
0
                    }
9764
0
                    const auto osAttrName = osUnit.substr(2, osUnit.size() - 3);
9765
0
                    std::shared_ptr<GDALAttribute> poAttr;
9766
0
                    if (poArray && !osAttrName.empty() && osAttrName[0] != '/')
9767
0
                    {
9768
0
                        poAttr = poArray->GetAttribute(osAttrName);
9769
0
                        if (!poAttr)
9770
0
                        {
9771
0
                            CPLError(
9772
0
                                CE_Failure, CPLE_AppDefined,
9773
0
                                "Value of "
9774
0
                                "BAND_IMAGERY_METADATA[\"%s\"][\"unit\"] = "
9775
0
                                "%s is invalid: %s is not an attribute of %s",
9776
0
                                oJsonItem.GetName().c_str(), osUnit.c_str(),
9777
0
                                osAttrName.c_str(),
9778
0
                                osBandArrayFullname.c_str());
9779
0
                            return nullptr;
9780
0
                        }
9781
0
                    }
9782
0
                    else
9783
0
                    {
9784
0
                        poAttr =
9785
0
                            poRootGroup->OpenAttributeFromFullname(osAttrName);
9786
0
                        if (!poAttr)
9787
0
                        {
9788
0
                            CPLError(
9789
0
                                CE_Failure, CPLE_AppDefined,
9790
0
                                "Value of "
9791
0
                                "BAND_IMAGERY_METADATA[\"%s\"][\"unit\"] = "
9792
0
                                "%s is invalid: %s is not an attribute",
9793
0
                                oJsonItem.GetName().c_str(), osUnit.c_str(),
9794
0
                                osAttrName.c_str());
9795
0
                            return nullptr;
9796
0
                        }
9797
0
                    }
9798
9799
0
                    const char *pszValue = poAttr->ReadAsString();
9800
0
                    if (!pszValue)
9801
0
                    {
9802
0
                        CPLError(CE_Failure, CPLE_AppDefined,
9803
0
                                 "Cannot get value of attribute %s of %s as a "
9804
0
                                 "string",
9805
0
                                 osAttrName.c_str(),
9806
0
                                 osBandArrayFullname.c_str());
9807
0
                        return nullptr;
9808
0
                    }
9809
0
                    osUnit = pszValue;
9810
0
                }
9811
0
                double dfConvToUM = 1.0;
9812
0
                if (osUnit == "nm" || osUnit == "nanometre" ||
9813
0
                    osUnit == "nanometres" || osUnit == "nanometer" ||
9814
0
                    osUnit == "nanometers")
9815
0
                {
9816
0
                    dfConvToUM = 1e-3;
9817
0
                }
9818
0
                else if (!(osUnit == "um" || osUnit == "micrometre" ||
9819
0
                           osUnit == "micrometres" || osUnit == "micrometer" ||
9820
0
                           osUnit == "micrometers"))
9821
0
                {
9822
0
                    CPLError(CE_Failure, CPLE_AppDefined,
9823
0
                             "Unhandled value for "
9824
0
                             "BAND_IMAGERY_METADATA[\"%s\"][\"unit\"] = %s",
9825
0
                             oJsonItem.GetName().c_str(), osUnit.c_str());
9826
0
                    return nullptr;
9827
0
                }
9828
9829
0
                BandImageryMetadata &item = aoBandImageryMetadata[iExtraDimIdx];
9830
9831
0
                std::shared_ptr<GDALAbstractMDArray> abstractArray;
9832
0
                if (poArray)
9833
0
                    abstractArray = std::move(poArray);
9834
0
                else
9835
0
                    abstractArray = std::move(poAttribute);
9836
0
                if (oJsonItem.GetName() == "CENTRAL_WAVELENGTH_UM")
9837
0
                {
9838
0
                    item.poCentralWavelengthArray = std::move(abstractArray);
9839
0
                    item.dfCentralWavelengthToMicrometer = dfConvToUM;
9840
0
                }
9841
0
                else
9842
0
                {
9843
0
                    item.poFWHMArray = std::move(abstractArray);
9844
0
                    item.dfFWHMToMicrometer = dfConvToUM;
9845
0
                }
9846
0
            }
9847
0
            else
9848
0
            {
9849
0
                CPLError(CE_Warning, CPLE_AppDefined,
9850
0
                         "Ignored member \"%s\" in BAND_IMAGERY_METADATA",
9851
0
                         oJsonItem.GetName().c_str());
9852
0
            }
9853
0
        }
9854
0
    }
9855
9856
0
    auto poDS = std::make_unique<GDALDatasetFromArray>(array, iXDim, iYDim);
9857
9858
0
    poDS->eAccess = array->IsWritable() ? GA_Update : GA_ReadOnly;
9859
9860
0
    poDS->nRasterYSize =
9861
0
        nDimCount < 2 ? 1
9862
0
                      : static_cast<int>(std::min(static_cast<GUInt64>(INT_MAX),
9863
0
                                                  dims[iYDim]->GetSize()));
9864
0
    poDS->nRasterXSize = static_cast<int>(
9865
0
        std::min(static_cast<GUInt64>(INT_MAX), dims[iXDim]->GetSize()));
9866
9867
0
    std::vector<GUInt64> anOtherDimCoord(nNewDimCount);
9868
0
    std::vector<GUInt64> anStackIters(nDimCount);
9869
0
    std::vector<size_t> anMapNewToOld(nNewDimCount);
9870
0
    for (size_t i = 0, j = 0; i < nDimCount; ++i)
9871
0
    {
9872
0
        if (i != iXDim && !(nDimCount >= 2 && i == iYDim))
9873
0
        {
9874
0
            anMapNewToOld[j] = i;
9875
0
            j++;
9876
0
        }
9877
0
    }
9878
9879
0
    poDS->m_bHasGT =
9880
0
        array->GuessGeoTransform(iXDim, iYDim, false, poDS->m_adfGeoTransform);
9881
9882
0
    const auto attrs(array->GetAttributes());
9883
0
    for (const auto &attr : attrs)
9884
0
    {
9885
0
        if (attr->GetName() != "COLOR_INTERPRETATION")
9886
0
        {
9887
0
            auto stringArray = attr->ReadAsStringArray();
9888
0
            std::string val;
9889
0
            if (stringArray.size() > 1)
9890
0
            {
9891
0
                val += '{';
9892
0
            }
9893
0
            for (int i = 0; i < stringArray.size(); ++i)
9894
0
            {
9895
0
                if (i > 0)
9896
0
                    val += ',';
9897
0
                val += stringArray[i];
9898
0
            }
9899
0
            if (stringArray.size() > 1)
9900
0
            {
9901
0
                val += '}';
9902
0
            }
9903
0
            poDS->m_oMDD.SetMetadataItem(attr->GetName().c_str(), val.c_str());
9904
0
        }
9905
0
    }
9906
9907
0
    const char *pszDelay = CSLFetchNameValueDef(
9908
0
        papszOptions, "LOAD_EXTRA_DIM_METADATA_DELAY",
9909
0
        CPLGetConfigOption("GDAL_LOAD_EXTRA_DIM_METADATA_DELAY", "5"));
9910
0
    const double dfDelay =
9911
0
        EQUAL(pszDelay, "unlimited") ? -1 : CPLAtof(pszDelay);
9912
0
    const auto nStartTime = time(nullptr);
9913
0
    bool bHasWarned = false;
9914
    // Instantiate bands by iterating over non-XY variables
9915
0
    size_t iDim = 0;
9916
0
    int nCurBand = 1;
9917
0
lbl_next_depth:
9918
0
    if (iDim < nNewDimCount)
9919
0
    {
9920
0
        anStackIters[iDim] = dims[anMapNewToOld[iDim]]->GetSize();
9921
0
        anOtherDimCoord[iDim] = 0;
9922
0
        while (true)
9923
0
        {
9924
0
            ++iDim;
9925
0
            goto lbl_next_depth;
9926
0
        lbl_return_to_caller:
9927
0
            --iDim;
9928
0
            --anStackIters[iDim];
9929
0
            if (anStackIters[iDim] == 0)
9930
0
                break;
9931
0
            ++anOtherDimCoord[iDim];
9932
0
        }
9933
0
    }
9934
0
    else
9935
0
    {
9936
0
        poDS->SetBand(nCurBand,
9937
0
                      new GDALRasterBandFromArray(
9938
0
                          poDS.get(), anOtherDimCoord,
9939
0
                          aoBandParameterMetadataItems, aoBandImageryMetadata,
9940
0
                          dfDelay, nStartTime, bHasWarned));
9941
0
        ++nCurBand;
9942
0
    }
9943
0
    if (iDim > 0)
9944
0
        goto lbl_return_to_caller;
9945
9946
0
    if (!array->GetFilename().empty())
9947
0
    {
9948
0
        poDS->SetPhysicalFilename(array->GetFilename().c_str());
9949
0
        std::string osDerivedDatasetName(
9950
0
            CPLSPrintf("AsClassicDataset(%d,%d) view of %s", int(iXDim),
9951
0
                       int(iYDim), array->GetFullName().c_str()));
9952
0
        if (!array->GetContext().empty())
9953
0
        {
9954
0
            osDerivedDatasetName += " with context ";
9955
0
            osDerivedDatasetName += array->GetContext();
9956
0
        }
9957
0
        poDS->SetDerivedDatasetName(osDerivedDatasetName.c_str());
9958
0
        poDS->TryLoadXML();
9959
9960
0
        for (const auto &[pszKey, pszValue] :
9961
0
             cpl::IterateNameValue(static_cast<CSLConstList>(
9962
0
                 poDS->GDALPamDataset::GetMetadata())))
9963
0
        {
9964
0
            poDS->m_oMDD.SetMetadataItem(pszKey, pszValue);
9965
0
        }
9966
0
    }
9967
9968
0
    return poDS.release();
9969
0
}
9970
9971
/************************************************************************/
9972
/*                          AsClassicDataset()                         */
9973
/************************************************************************/
9974
9975
/** Return a view of this array as a "classic" GDALDataset (ie 2D)
9976
 *
9977
 * In the case of > 2D arrays, additional dimensions will be represented as
9978
 * raster bands.
9979
 *
9980
 * The "reverse" method is GDALRasterBand::AsMDArray().
9981
 *
9982
 * This is the same as the C function GDALMDArrayAsClassicDataset().
9983
 *
9984
 * @param iXDim Index of the dimension that will be used as the X/width axis.
9985
 * @param iYDim Index of the dimension that will be used as the Y/height axis.
9986
 *              Ignored if the dimension count is 1.
9987
 * @param poRootGroup (Added in GDAL 3.8) Root group. Used with the BAND_METADATA
9988
 *                    and BAND_IMAGERY_METADATA option.
9989
 * @param papszOptions (Added in GDAL 3.8) Null-terminated list of options, or
9990
 *                     nullptr. Current supported options are:
9991
 *                     <ul>
9992
 *                     <li>BAND_METADATA: JSON serialized array defining which
9993
 *                         arrays of the poRootGroup, indexed by non-X and Y
9994
 *                         dimensions, should be mapped as band metadata items.
9995
 *                         Each array item should be an object with the
9996
 *                         following members:
9997
 *                         - "array": full name of a band parameter array.
9998
 *                           Such array must be a one
9999
 *                           dimensional array, and its dimension must be one of
10000
 *                           the dimensions of the array on which the method is
10001
 *                           called (excluding the X and Y dimensons).
10002
 *                         - "attribute": name relative to *this array or full
10003
 *                           name of a single dimension numeric array whose size
10004
 *                           must be one of the dimensions of *this array
10005
 *                           (excluding the X and Y dimensons).
10006
 *                           "array" and "attribute" are mutually exclusive.
10007
 *                         - "item_name": band metadata item name
10008
 *                         - "item_value": (optional) String, where "%[x][.y]f",
10009
 *                           "%[x][.y]g" or "%s" printf-like formatting can be
10010
 *                           used to format the corresponding value of the
10011
 *                           parameter array. The percentage character should be
10012
 *                           repeated: "%%"
10013
 *                           "${attribute_name}" can also be used to include the
10014
 *                           value of an attribute for "array" when set and if
10015
 *                           not starting with '/'. Otherwise if starting with
10016
 *                           '/', it is the full path to the attribute.
10017
 *
10018
 *                           If "item_value" is not provided, a default formatting
10019
 *                           of the value will be applied.
10020
 *
10021
 *                         Example:
10022
 *                         [
10023
 *                            {
10024
 *                              "array": "/sensor_band_parameters/wavelengths",
10025
 *                              "item_name": "WAVELENGTH",
10026
 *                              "item_value": "%.1f ${units}"
10027
 *                            },
10028
 *                            {
10029
 *                              "array": "/sensor_band_parameters/fwhm",
10030
 *                              "item_name": "FWHM"
10031
 *                            },
10032
 *                            {
10033
 *                              "array": "/sensor_band_parameters/fwhm",
10034
 *                              "item_name": "FWHM_UNIT",
10035
 *                              "item_value": "${units}"
10036
 *                            }
10037
 *                         ]
10038
 *
10039
 *                         Example for Planet Labs Tanager radiance products:
10040
 *                         [
10041
 *                            {
10042
 *                              "attribute": "center_wavelengths",
10043
 *                              "item_name": "WAVELENGTH",
10044
 *                              "item_value": "%.1f ${center_wavelengths_units}"
10045
 *                            }
10046
 *                         ]
10047
 *
10048
 *                     </li>
10049
 *                     <li>BAND_IMAGERY_METADATA: (GDAL >= 3.11)
10050
 *                         JSON serialized object defining which arrays of the
10051
 *                         poRootGroup, indexed by non-X and Y dimensions,
10052
 *                         should be mapped as band metadata items in the
10053
 *                         band IMAGERY domain.
10054
 *                         The object currently accepts 2 members:
10055
 *                         - "CENTRAL_WAVELENGTH_UM": Central Wavelength in
10056
 *                           micrometers.
10057
 *                         - "FWHM_UM": Full-width half-maximum
10058
 *                           in micrometers.
10059
 *                         The value of each member should be an object with the
10060
 *                         following members:
10061
 *                         - "array": full name of a band parameter array.
10062
 *                           Such array must be a one dimensional array, and its
10063
 *                           dimension must be one of the dimensions of the
10064
 *                           array on which the method is called
10065
 *                           (excluding the X and Y dimensons).
10066
 *                         - "attribute": name relative to *this array or full
10067
 *                           name of a single dimension numeric array whose size
10068
 *                           must be one of the dimensions of *this array
10069
 *                           (excluding the X and Y dimensons).
10070
 *                           "array" and "attribute" are mutually exclusive,
10071
 *                           and one of them is required.
10072
 *                         - "unit": (optional) unit of the values pointed in
10073
 *                           the above array.
10074
 *                           Can be a literal string or a string of the form
10075
 *                           "${attribute_name}" to point to an attribute for
10076
 *                           "array" when set and if no starting
10077
 *                           with '/'. Otherwise if starting with '/', it is
10078
 *                           the full path to the attribute.
10079
 *                           Accepted values are "um", "micrometer"
10080
 *                           (with UK vs US spelling, singular or plural), "nm",
10081
 *                           "nanometer" (with UK vs US spelling, singular or
10082
 *                           plural)
10083
 *                           If not provided, micrometer is assumed.
10084
 *
10085
 *                         Example for EMIT datasets:
10086
 *                         {
10087
 *                            "CENTRAL_WAVELENGTH_UM": {
10088
 *                                "array": "/sensor_band_parameters/wavelengths",
10089
 *                                "unit": "${units}"
10090
 *                            },
10091
 *                            "FWHM_UM": {
10092
 *                                "array": "/sensor_band_parameters/fwhm",
10093
 *                                "unit": "${units}"
10094
 *                            }
10095
 *                         }
10096
 *
10097
 *                         Example for Planet Labs Tanager radiance products:
10098
 *                         {
10099
 *                            "CENTRAL_WAVELENGTH_UM": {
10100
 *                              "attribute": "center_wavelengths",
10101
 *                              "unit": "${center_wavelengths_units}"
10102
 *                            },
10103
 *                            "FWHM_UM": {
10104
 *                              "attribute": "fwhm",
10105
 *                              "unit": "${fwhm_units}"
10106
 *                            }
10107
 *                         }
10108
 *
10109
 *                     </li>
10110
 *                     <li>LOAD_EXTRA_DIM_METADATA_DELAY: Maximum delay in
10111
 *                         seconds allowed to set the DIM_{dimname}_VALUE band
10112
 *                         metadata items from the indexing variable of the
10113
 *                         dimensions.
10114
 *                         Default value is 5. 'unlimited' can be used to mean
10115
 *                         unlimited delay. Can also be defined globally with
10116
 *                         the GDAL_LOAD_EXTRA_DIM_METADATA_DELAY configuration
10117
 *                         option.</li>
10118
 *                     </ul>
10119
 * @return a new GDALDataset that must be freed with GDALClose(), or nullptr
10120
 */
10121
GDALDataset *
10122
GDALMDArray::AsClassicDataset(size_t iXDim, size_t iYDim,
10123
                              const std::shared_ptr<GDALGroup> &poRootGroup,
10124
                              CSLConstList papszOptions) const
10125
0
{
10126
0
    auto self = std::dynamic_pointer_cast<GDALMDArray>(m_pSelf.lock());
10127
0
    if (!self)
10128
0
    {
10129
0
        CPLError(CE_Failure, CPLE_AppDefined,
10130
0
                 "Driver implementation issue: m_pSelf not set !");
10131
0
        return nullptr;
10132
0
    }
10133
0
    return GDALDatasetFromArray::Create(self, iXDim, iYDim, poRootGroup,
10134
0
                                        papszOptions);
10135
0
}
10136
10137
/************************************************************************/
10138
/*                           GetStatistics()                            */
10139
/************************************************************************/
10140
10141
/**
10142
 * \brief Fetch statistics.
10143
 *
10144
 * Returns the minimum, maximum, mean and standard deviation of all
10145
 * pixel values in this array.
10146
 *
10147
 * If bForce is FALSE results will only be returned if it can be done
10148
 * quickly (i.e. without scanning the data).  If bForce is FALSE and
10149
 * results cannot be returned efficiently, the method will return CE_Warning
10150
 * but no warning will have been issued.   This is a non-standard use of
10151
 * the CE_Warning return value to indicate "nothing done".
10152
 *
10153
 * When cached statistics are not available, and bForce is TRUE,
10154
 * ComputeStatistics() is called.
10155
 *
10156
 * Note that file formats using PAM (Persistent Auxiliary Metadata) services
10157
 * will generally cache statistics in the .aux.xml file allowing fast fetch
10158
 * after the first request.
10159
 *
10160
 * Cached statistics can be cleared with GDALDataset::ClearStatistics().
10161
 *
10162
 * This method is the same as the C function GDALMDArrayGetStatistics().
10163
 *
10164
 * @param bApproxOK Currently ignored. In the future, should be set to true
10165
 * if statistics on the whole array are wished, or to false if a subset of it
10166
 * may be used.
10167
 *
10168
 * @param bForce If false statistics will only be returned if it can
10169
 * be done without rescanning the image.
10170
 *
10171
 * @param pdfMin Location into which to load image minimum (may be NULL).
10172
 *
10173
 * @param pdfMax Location into which to load image maximum (may be NULL).-
10174
 *
10175
 * @param pdfMean Location into which to load image mean (may be NULL).
10176
 *
10177
 * @param pdfStdDev Location into which to load image standard deviation
10178
 * (may be NULL).
10179
 *
10180
 * @param pnValidCount Number of samples whose value is different from the
10181
 * nodata value. (may be NULL)
10182
 *
10183
 * @param pfnProgress a function to call to report progress, or NULL.
10184
 *
10185
 * @param pProgressData application data to pass to the progress function.
10186
 *
10187
 * @return CE_None on success, CE_Warning if no values returned,
10188
 * CE_Failure if an error occurs.
10189
 *
10190
 * @since GDAL 3.2
10191
 */
10192
10193
CPLErr GDALMDArray::GetStatistics(bool bApproxOK, bool bForce, double *pdfMin,
10194
                                  double *pdfMax, double *pdfMean,
10195
                                  double *pdfStdDev, GUInt64 *pnValidCount,
10196
                                  GDALProgressFunc pfnProgress,
10197
                                  void *pProgressData)
10198
0
{
10199
0
    if (!bForce)
10200
0
        return CE_Warning;
10201
10202
0
    return ComputeStatistics(bApproxOK, pdfMin, pdfMax, pdfMean, pdfStdDev,
10203
0
                             pnValidCount, pfnProgress, pProgressData, nullptr)
10204
0
               ? CE_None
10205
0
               : CE_Failure;
10206
0
}
10207
10208
/************************************************************************/
10209
/*                         ComputeStatistics()                          */
10210
/************************************************************************/
10211
10212
/**
10213
 * \brief Compute statistics.
10214
 *
10215
 * Returns the minimum, maximum, mean and standard deviation of all
10216
 * pixel values in this array.
10217
 *
10218
 * Pixels taken into account in statistics are those whose mask value
10219
 * (as determined by GetMask()) is non-zero.
10220
 *
10221
 * Once computed, the statistics will generally be "set" back on the
10222
 * owing dataset.
10223
 *
10224
 * Cached statistics can be cleared with GDALDataset::ClearStatistics().
10225
 *
10226
 * This method is the same as the C functions GDALMDArrayComputeStatistics().
10227
 * and GDALMDArrayComputeStatisticsEx().
10228
 *
10229
 * @param bApproxOK Currently ignored. In the future, should be set to true
10230
 * if statistics on the whole array are wished, or to false if a subset of it
10231
 * may be used.
10232
 *
10233
 * @param pdfMin Location into which to load image minimum (may be NULL).
10234
 *
10235
 * @param pdfMax Location into which to load image maximum (may be NULL).-
10236
 *
10237
 * @param pdfMean Location into which to load image mean (may be NULL).
10238
 *
10239
 * @param pdfStdDev Location into which to load image standard deviation
10240
 * (may be NULL).
10241
 *
10242
 * @param pnValidCount Number of samples whose value is different from the
10243
 * nodata value. (may be NULL)
10244
 *
10245
 * @param pfnProgress a function to call to report progress, or NULL.
10246
 *
10247
 * @param pProgressData application data to pass to the progress function.
10248
 *
10249
 * @param papszOptions NULL-terminated list of options, of NULL. Added in 3.8.
10250
 *                     Options are driver specific. For now the netCDF and Zarr
10251
 *                     drivers recognize UPDATE_METADATA=YES, whose effect is
10252
 *                     to add or update the actual_range attribute with the
10253
 *                     computed min/max, only if done on the full array, in non
10254
 *                     approximate mode, and the dataset is opened in update
10255
 *                     mode.
10256
 *
10257
 * @return true on success
10258
 *
10259
 * @since GDAL 3.2
10260
 */
10261
10262
bool GDALMDArray::ComputeStatistics(bool bApproxOK, double *pdfMin,
10263
                                    double *pdfMax, double *pdfMean,
10264
                                    double *pdfStdDev, GUInt64 *pnValidCount,
10265
                                    GDALProgressFunc pfnProgress,
10266
                                    void *pProgressData,
10267
                                    CSLConstList papszOptions)
10268
0
{
10269
0
    struct StatsPerChunkType
10270
0
    {
10271
0
        const GDALMDArray *array = nullptr;
10272
0
        std::shared_ptr<GDALMDArray> poMask{};
10273
0
        double dfMin = cpl::NumericLimits<double>::max();
10274
0
        double dfMax = -cpl::NumericLimits<double>::max();
10275
0
        double dfMean = 0.0;
10276
0
        double dfM2 = 0.0;
10277
0
        GUInt64 nValidCount = 0;
10278
0
        std::vector<GByte> abyData{};
10279
0
        std::vector<double> adfData{};
10280
0
        std::vector<GByte> abyMaskData{};
10281
0
        GDALProgressFunc pfnProgress = nullptr;
10282
0
        void *pProgressData = nullptr;
10283
0
    };
10284
10285
0
    const auto PerChunkFunc = [](GDALAbstractMDArray *,
10286
0
                                 const GUInt64 *chunkArrayStartIdx,
10287
0
                                 const size_t *chunkCount, GUInt64 iCurChunk,
10288
0
                                 GUInt64 nChunkCount, void *pUserData)
10289
0
    {
10290
0
        StatsPerChunkType *data = static_cast<StatsPerChunkType *>(pUserData);
10291
0
        const GDALMDArray *array = data->array;
10292
0
        const GDALMDArray *poMask = data->poMask.get();
10293
0
        const size_t nDims = array->GetDimensionCount();
10294
0
        size_t nVals = 1;
10295
0
        for (size_t i = 0; i < nDims; i++)
10296
0
            nVals *= chunkCount[i];
10297
10298
        // Get mask
10299
0
        data->abyMaskData.resize(nVals);
10300
0
        if (!(poMask->Read(chunkArrayStartIdx, chunkCount, nullptr, nullptr,
10301
0
                           poMask->GetDataType(), &data->abyMaskData[0])))
10302
0
        {
10303
0
            return false;
10304
0
        }
10305
10306
        // Get data
10307
0
        const auto &oType = array->GetDataType();
10308
0
        if (oType.GetNumericDataType() == GDT_Float64)
10309
0
        {
10310
0
            data->adfData.resize(nVals);
10311
0
            if (!array->Read(chunkArrayStartIdx, chunkCount, nullptr, nullptr,
10312
0
                             oType, &data->adfData[0]))
10313
0
            {
10314
0
                return false;
10315
0
            }
10316
0
        }
10317
0
        else
10318
0
        {
10319
0
            data->abyData.resize(nVals * oType.GetSize());
10320
0
            if (!array->Read(chunkArrayStartIdx, chunkCount, nullptr, nullptr,
10321
0
                             oType, &data->abyData[0]))
10322
0
            {
10323
0
                return false;
10324
0
            }
10325
0
            data->adfData.resize(nVals);
10326
0
            GDALCopyWords64(&data->abyData[0], oType.GetNumericDataType(),
10327
0
                            static_cast<int>(oType.GetSize()),
10328
0
                            &data->adfData[0], GDT_Float64,
10329
0
                            static_cast<int>(sizeof(double)),
10330
0
                            static_cast<GPtrDiff_t>(nVals));
10331
0
        }
10332
0
        for (size_t i = 0; i < nVals; i++)
10333
0
        {
10334
0
            if (data->abyMaskData[i])
10335
0
            {
10336
0
                const double dfValue = data->adfData[i];
10337
0
                data->dfMin = std::min(data->dfMin, dfValue);
10338
0
                data->dfMax = std::max(data->dfMax, dfValue);
10339
0
                data->nValidCount++;
10340
0
                const double dfDelta = dfValue - data->dfMean;
10341
0
                data->dfMean += dfDelta / data->nValidCount;
10342
0
                data->dfM2 += dfDelta * (dfValue - data->dfMean);
10343
0
            }
10344
0
        }
10345
0
        if (data->pfnProgress &&
10346
0
            !data->pfnProgress(static_cast<double>(iCurChunk + 1) / nChunkCount,
10347
0
                               "", data->pProgressData))
10348
0
        {
10349
0
            return false;
10350
0
        }
10351
0
        return true;
10352
0
    };
10353
10354
0
    const auto &oType = GetDataType();
10355
0
    if (oType.GetClass() != GEDTC_NUMERIC ||
10356
0
        GDALDataTypeIsComplex(oType.GetNumericDataType()))
10357
0
    {
10358
0
        CPLError(
10359
0
            CE_Failure, CPLE_NotSupported,
10360
0
            "Statistics can only be computed on non-complex numeric data type");
10361
0
        return false;
10362
0
    }
10363
10364
0
    const size_t nDims = GetDimensionCount();
10365
0
    std::vector<GUInt64> arrayStartIdx(nDims);
10366
0
    std::vector<GUInt64> count(nDims);
10367
0
    const auto &poDims = GetDimensions();
10368
0
    for (size_t i = 0; i < nDims; i++)
10369
0
    {
10370
0
        count[i] = poDims[i]->GetSize();
10371
0
    }
10372
0
    const char *pszSwathSize = CPLGetConfigOption("GDAL_SWATH_SIZE", nullptr);
10373
0
    const size_t nMaxChunkSize =
10374
0
        pszSwathSize
10375
0
            ? static_cast<size_t>(
10376
0
                  std::min(GIntBig(std::numeric_limits<size_t>::max() / 2),
10377
0
                           CPLAtoGIntBig(pszSwathSize)))
10378
0
            : static_cast<size_t>(
10379
0
                  std::min(GIntBig(std::numeric_limits<size_t>::max() / 2),
10380
0
                           GDALGetCacheMax64() / 4));
10381
0
    StatsPerChunkType sData;
10382
0
    sData.array = this;
10383
0
    sData.poMask = GetMask(nullptr);
10384
0
    if (sData.poMask == nullptr)
10385
0
    {
10386
0
        return false;
10387
0
    }
10388
0
    sData.pfnProgress = pfnProgress;
10389
0
    sData.pProgressData = pProgressData;
10390
0
    if (!ProcessPerChunk(arrayStartIdx.data(), count.data(),
10391
0
                         GetProcessingChunkSize(nMaxChunkSize).data(),
10392
0
                         PerChunkFunc, &sData))
10393
0
    {
10394
0
        return false;
10395
0
    }
10396
10397
0
    if (pdfMin)
10398
0
        *pdfMin = sData.dfMin;
10399
10400
0
    if (pdfMax)
10401
0
        *pdfMax = sData.dfMax;
10402
10403
0
    if (pdfMean)
10404
0
        *pdfMean = sData.dfMean;
10405
10406
0
    const double dfStdDev =
10407
0
        sData.nValidCount > 0 ? sqrt(sData.dfM2 / sData.nValidCount) : 0.0;
10408
0
    if (pdfStdDev)
10409
0
        *pdfStdDev = dfStdDev;
10410
10411
0
    if (pnValidCount)
10412
0
        *pnValidCount = sData.nValidCount;
10413
10414
0
    SetStatistics(bApproxOK, sData.dfMin, sData.dfMax, sData.dfMean, dfStdDev,
10415
0
                  sData.nValidCount, papszOptions);
10416
10417
0
    return true;
10418
0
}
10419
10420
/************************************************************************/
10421
/*                            SetStatistics()                           */
10422
/************************************************************************/
10423
//! @cond Doxygen_Suppress
10424
bool GDALMDArray::SetStatistics(bool /* bApproxStats */, double /* dfMin */,
10425
                                double /* dfMax */, double /* dfMean */,
10426
                                double /* dfStdDev */,
10427
                                GUInt64 /* nValidCount */,
10428
                                CSLConstList /* papszOptions */)
10429
0
{
10430
0
    CPLDebug("GDAL", "Cannot save statistics on a non-PAM MDArray");
10431
0
    return false;
10432
0
}
10433
10434
//! @endcond
10435
10436
/************************************************************************/
10437
/*                           ClearStatistics()                          */
10438
/************************************************************************/
10439
10440
/**
10441
 * \brief Clear statistics.
10442
 *
10443
 * @since GDAL 3.4
10444
 */
10445
void GDALMDArray::ClearStatistics()
10446
0
{
10447
0
}
10448
10449
/************************************************************************/
10450
/*                      GetCoordinateVariables()                        */
10451
/************************************************************************/
10452
10453
/**
10454
 * \brief Return coordinate variables.
10455
 *
10456
 * Coordinate variables are an alternate way of indexing an array that can
10457
 * be sometimes used. For example, an array collected through remote sensing
10458
 * might be indexed by (scanline, pixel). But there can be
10459
 * a longitude and latitude arrays alongside that are also both indexed by
10460
 * (scanline, pixel), and are referenced from operational arrays for
10461
 * reprojection purposes.
10462
 *
10463
 * For netCDF, this will return the arrays referenced by the "coordinates"
10464
 * attribute.
10465
 *
10466
 * This method is the same as the C function
10467
 * GDALMDArrayGetCoordinateVariables().
10468
 *
10469
 * @return a vector of arrays
10470
 *
10471
 * @since GDAL 3.4
10472
 */
10473
10474
std::vector<std::shared_ptr<GDALMDArray>>
10475
GDALMDArray::GetCoordinateVariables() const
10476
0
{
10477
0
    return {};
10478
0
}
10479
10480
/************************************************************************/
10481
/*                       ~GDALExtendedDataType()                        */
10482
/************************************************************************/
10483
10484
0
GDALExtendedDataType::~GDALExtendedDataType() = default;
10485
10486
/************************************************************************/
10487
/*                        GDALExtendedDataType()                        */
10488
/************************************************************************/
10489
10490
GDALExtendedDataType::GDALExtendedDataType(size_t nMaxStringLength,
10491
                                           GDALExtendedDataTypeSubType eSubType)
10492
0
    : m_eClass(GEDTC_STRING), m_eSubType(eSubType), m_nSize(sizeof(char *)),
10493
0
      m_nMaxStringLength(nMaxStringLength)
10494
0
{
10495
0
}
10496
10497
/************************************************************************/
10498
/*                        GDALExtendedDataType()                        */
10499
/************************************************************************/
10500
10501
GDALExtendedDataType::GDALExtendedDataType(GDALDataType eType)
10502
0
    : m_eClass(GEDTC_NUMERIC), m_eNumericDT(eType),
10503
0
      m_nSize(GDALGetDataTypeSizeBytes(eType))
10504
0
{
10505
0
}
10506
10507
/************************************************************************/
10508
/*                        GDALExtendedDataType()                        */
10509
/************************************************************************/
10510
10511
GDALExtendedDataType::GDALExtendedDataType(
10512
    const std::string &osName, GDALDataType eBaseType,
10513
    std::unique_ptr<GDALRasterAttributeTable> poRAT)
10514
0
    : m_osName(osName), m_eClass(GEDTC_NUMERIC), m_eNumericDT(eBaseType),
10515
0
      m_nSize(GDALGetDataTypeSizeBytes(eBaseType)), m_poRAT(std::move(poRAT))
10516
0
{
10517
0
}
10518
10519
/************************************************************************/
10520
/*                        GDALExtendedDataType()                        */
10521
/************************************************************************/
10522
10523
GDALExtendedDataType::GDALExtendedDataType(
10524
    const std::string &osName, size_t nTotalSize,
10525
    std::vector<std::unique_ptr<GDALEDTComponent>> &&components)
10526
0
    : m_osName(osName), m_eClass(GEDTC_COMPOUND),
10527
0
      m_aoComponents(std::move(components)), m_nSize(nTotalSize)
10528
0
{
10529
0
}
10530
10531
/************************************************************************/
10532
/*                        GDALExtendedDataType()                        */
10533
/************************************************************************/
10534
10535
/** Copy constructor. */
10536
GDALExtendedDataType::GDALExtendedDataType(const GDALExtendedDataType &other)
10537
0
    : m_osName(other.m_osName), m_eClass(other.m_eClass),
10538
0
      m_eSubType(other.m_eSubType), m_eNumericDT(other.m_eNumericDT),
10539
0
      m_nSize(other.m_nSize), m_nMaxStringLength(other.m_nMaxStringLength),
10540
0
      m_poRAT(other.m_poRAT ? other.m_poRAT->Clone() : nullptr)
10541
0
{
10542
0
    if (m_eClass == GEDTC_COMPOUND)
10543
0
    {
10544
0
        for (const auto &elt : other.m_aoComponents)
10545
0
        {
10546
0
            m_aoComponents.emplace_back(new GDALEDTComponent(*elt));
10547
0
        }
10548
0
    }
10549
0
}
10550
10551
/************************************************************************/
10552
/*                            operator= ()                              */
10553
/************************************************************************/
10554
10555
/** Copy assignment. */
10556
GDALExtendedDataType &
10557
GDALExtendedDataType::operator=(const GDALExtendedDataType &other)
10558
0
{
10559
0
    if (this != &other)
10560
0
    {
10561
0
        m_osName = other.m_osName;
10562
0
        m_eClass = other.m_eClass;
10563
0
        m_eSubType = other.m_eSubType;
10564
0
        m_eNumericDT = other.m_eNumericDT;
10565
0
        m_nSize = other.m_nSize;
10566
0
        m_nMaxStringLength = other.m_nMaxStringLength;
10567
0
        m_poRAT.reset(other.m_poRAT ? other.m_poRAT->Clone() : nullptr);
10568
0
        m_aoComponents.clear();
10569
0
        if (m_eClass == GEDTC_COMPOUND)
10570
0
        {
10571
0
            for (const auto &elt : other.m_aoComponents)
10572
0
            {
10573
0
                m_aoComponents.emplace_back(new GDALEDTComponent(*elt));
10574
0
            }
10575
0
        }
10576
0
    }
10577
0
    return *this;
10578
0
}
10579
10580
/************************************************************************/
10581
/*                            operator= ()                              */
10582
/************************************************************************/
10583
10584
/** Move assignment. */
10585
GDALExtendedDataType &
10586
GDALExtendedDataType::operator=(GDALExtendedDataType &&other)
10587
0
{
10588
0
    m_osName = std::move(other.m_osName);
10589
0
    m_eClass = other.m_eClass;
10590
0
    m_eSubType = other.m_eSubType;
10591
0
    m_eNumericDT = other.m_eNumericDT;
10592
0
    m_nSize = other.m_nSize;
10593
0
    m_nMaxStringLength = other.m_nMaxStringLength;
10594
0
    m_aoComponents = std::move(other.m_aoComponents);
10595
0
    m_poRAT = std::move(other.m_poRAT);
10596
0
    other.m_eClass = GEDTC_NUMERIC;
10597
0
    other.m_eNumericDT = GDT_Unknown;
10598
0
    other.m_nSize = 0;
10599
0
    other.m_nMaxStringLength = 0;
10600
0
    return *this;
10601
0
}
10602
10603
/************************************************************************/
10604
/*                           Create()                                   */
10605
/************************************************************************/
10606
10607
/** Return a new GDALExtendedDataType of class GEDTC_NUMERIC.
10608
 *
10609
 * This is the same as the C function GDALExtendedDataTypeCreate()
10610
 *
10611
 * @param eType Numeric data type. Must be different from GDT_Unknown and
10612
 * GDT_TypeCount
10613
 */
10614
GDALExtendedDataType GDALExtendedDataType::Create(GDALDataType eType)
10615
0
{
10616
0
    return GDALExtendedDataType(eType);
10617
0
}
10618
10619
/************************************************************************/
10620
/*                           Create()                                   */
10621
/************************************************************************/
10622
10623
/** Return a new GDALExtendedDataType from a raster attribute table.
10624
 *
10625
 * @param osName Type name
10626
 * @param eBaseType Base integer data type.
10627
 * @param poRAT Raster attribute table. Must not be NULL.
10628
 * @since 3.12
10629
 */
10630
GDALExtendedDataType
10631
GDALExtendedDataType::Create(const std::string &osName, GDALDataType eBaseType,
10632
                             std::unique_ptr<GDALRasterAttributeTable> poRAT)
10633
0
{
10634
0
    return GDALExtendedDataType(osName, eBaseType, std::move(poRAT));
10635
0
}
10636
10637
/************************************************************************/
10638
/*                           Create()                                   */
10639
/************************************************************************/
10640
10641
/** Return a new GDALExtendedDataType of class GEDTC_COMPOUND.
10642
 *
10643
 * This is the same as the C function GDALExtendedDataTypeCreateCompound()
10644
 *
10645
 * @param osName Type name.
10646
 * @param nTotalSize Total size of the type in bytes.
10647
 *                   Should be large enough to store all components.
10648
 * @param components Components of the compound type.
10649
 */
10650
GDALExtendedDataType GDALExtendedDataType::Create(
10651
    const std::string &osName, size_t nTotalSize,
10652
    std::vector<std::unique_ptr<GDALEDTComponent>> &&components)
10653
0
{
10654
0
    size_t nLastOffset = 0;
10655
    // Some arbitrary threshold to avoid potential integer overflows
10656
0
    if (nTotalSize > static_cast<size_t>(std::numeric_limits<int>::max() / 2))
10657
0
    {
10658
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid offset/size");
10659
0
        return GDALExtendedDataType(GDT_Unknown);
10660
0
    }
10661
0
    for (const auto &comp : components)
10662
0
    {
10663
        // Check alignment too ?
10664
0
        if (comp->GetOffset() < nLastOffset)
10665
0
        {
10666
0
            CPLError(CE_Failure, CPLE_AppDefined, "Invalid offset/size");
10667
0
            return GDALExtendedDataType(GDT_Unknown);
10668
0
        }
10669
0
        nLastOffset = comp->GetOffset() + comp->GetType().GetSize();
10670
0
    }
10671
0
    if (nTotalSize < nLastOffset)
10672
0
    {
10673
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid offset/size");
10674
0
        return GDALExtendedDataType(GDT_Unknown);
10675
0
    }
10676
0
    if (nTotalSize == 0 || components.empty())
10677
0
    {
10678
0
        CPLError(CE_Failure, CPLE_AppDefined, "Empty compound not allowed");
10679
0
        return GDALExtendedDataType(GDT_Unknown);
10680
0
    }
10681
0
    return GDALExtendedDataType(osName, nTotalSize, std::move(components));
10682
0
}
10683
10684
/************************************************************************/
10685
/*                           Create()                                   */
10686
/************************************************************************/
10687
10688
/** Return a new GDALExtendedDataType of class GEDTC_STRING.
10689
 *
10690
 * This is the same as the C function GDALExtendedDataTypeCreateString().
10691
 *
10692
 * @param nMaxStringLength maximum length of a string in bytes. 0 if
10693
 * unknown/unlimited
10694
 * @param eSubType Subtype.
10695
 */
10696
GDALExtendedDataType
10697
GDALExtendedDataType::CreateString(size_t nMaxStringLength,
10698
                                   GDALExtendedDataTypeSubType eSubType)
10699
0
{
10700
0
    return GDALExtendedDataType(nMaxStringLength, eSubType);
10701
0
}
10702
10703
/************************************************************************/
10704
/*                           operator==()                               */
10705
/************************************************************************/
10706
10707
/** Equality operator.
10708
 *
10709
 * This is the same as the C function GDALExtendedDataTypeEquals().
10710
 */
10711
bool GDALExtendedDataType::operator==(const GDALExtendedDataType &other) const
10712
0
{
10713
0
    if (m_eClass != other.m_eClass || m_eSubType != other.m_eSubType ||
10714
0
        m_nSize != other.m_nSize || m_osName != other.m_osName)
10715
0
    {
10716
0
        return false;
10717
0
    }
10718
0
    if (m_eClass == GEDTC_NUMERIC)
10719
0
    {
10720
0
        return m_eNumericDT == other.m_eNumericDT;
10721
0
    }
10722
0
    if (m_eClass == GEDTC_STRING)
10723
0
    {
10724
0
        return true;
10725
0
    }
10726
0
    CPLAssert(m_eClass == GEDTC_COMPOUND);
10727
0
    if (m_aoComponents.size() != other.m_aoComponents.size())
10728
0
    {
10729
0
        return false;
10730
0
    }
10731
0
    for (size_t i = 0; i < m_aoComponents.size(); i++)
10732
0
    {
10733
0
        if (!(*m_aoComponents[i] == *other.m_aoComponents[i]))
10734
0
        {
10735
0
            return false;
10736
0
        }
10737
0
    }
10738
0
    return true;
10739
0
}
10740
10741
/************************************************************************/
10742
/*                        CanConvertTo()                                */
10743
/************************************************************************/
10744
10745
/** Return whether this data type can be converted to the other one.
10746
 *
10747
 * This is the same as the C function GDALExtendedDataTypeCanConvertTo().
10748
 *
10749
 * @param other Target data type for the conversion being considered.
10750
 */
10751
bool GDALExtendedDataType::CanConvertTo(const GDALExtendedDataType &other) const
10752
0
{
10753
0
    if (m_eClass == GEDTC_NUMERIC)
10754
0
    {
10755
0
        if (m_eNumericDT == GDT_Unknown)
10756
0
            return false;
10757
0
        if (other.m_eClass == GEDTC_NUMERIC &&
10758
0
            other.m_eNumericDT == GDT_Unknown)
10759
0
            return false;
10760
0
        return other.m_eClass == GEDTC_NUMERIC ||
10761
0
               other.m_eClass == GEDTC_STRING;
10762
0
    }
10763
0
    if (m_eClass == GEDTC_STRING)
10764
0
    {
10765
0
        return other.m_eClass == m_eClass;
10766
0
    }
10767
0
    CPLAssert(m_eClass == GEDTC_COMPOUND);
10768
0
    if (other.m_eClass != GEDTC_COMPOUND)
10769
0
        return false;
10770
0
    std::map<std::string, const std::unique_ptr<GDALEDTComponent> *>
10771
0
        srcComponents;
10772
0
    for (const auto &srcComp : m_aoComponents)
10773
0
    {
10774
0
        srcComponents[srcComp->GetName()] = &srcComp;
10775
0
    }
10776
0
    for (const auto &dstComp : other.m_aoComponents)
10777
0
    {
10778
0
        auto oIter = srcComponents.find(dstComp->GetName());
10779
0
        if (oIter == srcComponents.end())
10780
0
            return false;
10781
0
        if (!(*(oIter->second))->GetType().CanConvertTo(dstComp->GetType()))
10782
0
            return false;
10783
0
    }
10784
0
    return true;
10785
0
}
10786
10787
/************************************************************************/
10788
/*                     NeedsFreeDynamicMemory()                         */
10789
/************************************************************************/
10790
10791
/** Return whether the data type holds dynamically allocated memory, that
10792
 * needs to be freed with FreeDynamicMemory().
10793
 *
10794
 */
10795
bool GDALExtendedDataType::NeedsFreeDynamicMemory() const
10796
0
{
10797
0
    switch (m_eClass)
10798
0
    {
10799
0
        case GEDTC_STRING:
10800
0
            return true;
10801
10802
0
        case GEDTC_NUMERIC:
10803
0
            return false;
10804
10805
0
        case GEDTC_COMPOUND:
10806
0
        {
10807
0
            for (const auto &comp : m_aoComponents)
10808
0
            {
10809
0
                if (comp->GetType().NeedsFreeDynamicMemory())
10810
0
                    return true;
10811
0
            }
10812
0
        }
10813
0
    }
10814
0
    return false;
10815
0
}
10816
10817
/************************************************************************/
10818
/*                        FreeDynamicMemory()                           */
10819
/************************************************************************/
10820
10821
/** Release the dynamic memory (strings typically) from a raw value.
10822
 *
10823
 * This is the same as the C function GDALExtendedDataTypeFreeDynamicMemory().
10824
 *
10825
 * @param pBuffer Raw buffer of a single element of an attribute or array value.
10826
 */
10827
void GDALExtendedDataType::FreeDynamicMemory(void *pBuffer) const
10828
0
{
10829
0
    switch (m_eClass)
10830
0
    {
10831
0
        case GEDTC_STRING:
10832
0
        {
10833
0
            char *pszStr;
10834
0
            memcpy(&pszStr, pBuffer, sizeof(char *));
10835
0
            if (pszStr)
10836
0
            {
10837
0
                VSIFree(pszStr);
10838
0
            }
10839
0
            break;
10840
0
        }
10841
10842
0
        case GEDTC_NUMERIC:
10843
0
        {
10844
0
            break;
10845
0
        }
10846
10847
0
        case GEDTC_COMPOUND:
10848
0
        {
10849
0
            GByte *pabyBuffer = static_cast<GByte *>(pBuffer);
10850
0
            for (const auto &comp : m_aoComponents)
10851
0
            {
10852
0
                comp->GetType().FreeDynamicMemory(pabyBuffer +
10853
0
                                                  comp->GetOffset());
10854
0
            }
10855
0
            break;
10856
0
        }
10857
0
    }
10858
0
}
10859
10860
/************************************************************************/
10861
/*                      ~GDALEDTComponent()                             */
10862
/************************************************************************/
10863
10864
0
GDALEDTComponent::~GDALEDTComponent() = default;
10865
10866
/************************************************************************/
10867
/*                      GDALEDTComponent()                              */
10868
/************************************************************************/
10869
10870
/** constructor of a GDALEDTComponent
10871
 *
10872
 * This is the same as the C function GDALEDTComponendCreate()
10873
 *
10874
 * @param name Component name
10875
 * @param offset Offset in byte of the component in the compound data type.
10876
 *               In case of nesting of compound data type, this should be
10877
 *               the offset to the immediate belonging data type, not to the
10878
 *               higher level one.
10879
 * @param type   Component data type.
10880
 */
10881
GDALEDTComponent::GDALEDTComponent(const std::string &name, size_t offset,
10882
                                   const GDALExtendedDataType &type)
10883
0
    : m_osName(name), m_nOffset(offset), m_oType(type)
10884
0
{
10885
0
}
10886
10887
/************************************************************************/
10888
/*                      GDALEDTComponent()                              */
10889
/************************************************************************/
10890
10891
/** Copy constructor. */
10892
0
GDALEDTComponent::GDALEDTComponent(const GDALEDTComponent &) = default;
10893
10894
/************************************************************************/
10895
/*                           operator==()                               */
10896
/************************************************************************/
10897
10898
/** Equality operator.
10899
 */
10900
bool GDALEDTComponent::operator==(const GDALEDTComponent &other) const
10901
0
{
10902
0
    return m_osName == other.m_osName && m_nOffset == other.m_nOffset &&
10903
0
           m_oType == other.m_oType;
10904
0
}
10905
10906
/************************************************************************/
10907
/*                        ~GDALDimension()                              */
10908
/************************************************************************/
10909
10910
0
GDALDimension::~GDALDimension() = default;
10911
10912
/************************************************************************/
10913
/*                         GDALDimension()                              */
10914
/************************************************************************/
10915
10916
//! @cond Doxygen_Suppress
10917
/** Constructor.
10918
 *
10919
 * @param osParentName Parent name
10920
 * @param osName name
10921
 * @param osType type. See GetType().
10922
 * @param osDirection direction. See GetDirection().
10923
 * @param nSize size.
10924
 */
10925
GDALDimension::GDALDimension(const std::string &osParentName,
10926
                             const std::string &osName,
10927
                             const std::string &osType,
10928
                             const std::string &osDirection, GUInt64 nSize)
10929
0
    : m_osName(osName),
10930
      m_osFullName(
10931
0
          !osParentName.empty()
10932
0
              ? ((osParentName == "/" ? "/" : osParentName + "/") + osName)
10933
0
              : osName),
10934
0
      m_osType(osType), m_osDirection(osDirection), m_nSize(nSize)
10935
0
{
10936
0
}
10937
10938
//! @endcond
10939
10940
/************************************************************************/
10941
/*                         GetIndexingVariable()                        */
10942
/************************************************************************/
10943
10944
/** Return the variable that is used to index the dimension (if there is one).
10945
 *
10946
 * This is the array, typically one-dimensional, describing the values taken
10947
 * by the dimension.
10948
 */
10949
std::shared_ptr<GDALMDArray> GDALDimension::GetIndexingVariable() const
10950
0
{
10951
0
    return nullptr;
10952
0
}
10953
10954
/************************************************************************/
10955
/*                         SetIndexingVariable()                        */
10956
/************************************************************************/
10957
10958
/** Set the variable that is used to index the dimension.
10959
 *
10960
 * This is the array, typically one-dimensional, describing the values taken
10961
 * by the dimension.
10962
 *
10963
 * Optionally implemented by drivers.
10964
 *
10965
 * Drivers known to implement it: MEM.
10966
 *
10967
 * @param poArray Variable to use to index the dimension.
10968
 * @return true in case of success.
10969
 */
10970
bool GDALDimension::SetIndexingVariable(
10971
    CPL_UNUSED std::shared_ptr<GDALMDArray> poArray)
10972
0
{
10973
0
    CPLError(CE_Failure, CPLE_NotSupported,
10974
0
             "SetIndexingVariable() not implemented");
10975
0
    return false;
10976
0
}
10977
10978
/************************************************************************/
10979
/*                            Rename()                                  */
10980
/************************************************************************/
10981
10982
/** Rename the dimension.
10983
 *
10984
 * This is not implemented by all drivers.
10985
 *
10986
 * Drivers known to implement it: MEM, netCDF, ZARR.
10987
 *
10988
 * This is the same as the C function GDALDimensionRename().
10989
 *
10990
 * @param osNewName New name.
10991
 *
10992
 * @return true in case of success
10993
 * @since GDAL 3.8
10994
 */
10995
bool GDALDimension::Rename(CPL_UNUSED const std::string &osNewName)
10996
0
{
10997
0
    CPLError(CE_Failure, CPLE_NotSupported, "Rename() not implemented");
10998
0
    return false;
10999
0
}
11000
11001
/************************************************************************/
11002
/*                         BaseRename()                                 */
11003
/************************************************************************/
11004
11005
//! @cond Doxygen_Suppress
11006
void GDALDimension::BaseRename(const std::string &osNewName)
11007
0
{
11008
0
    m_osFullName.resize(m_osFullName.size() - m_osName.size());
11009
0
    m_osFullName += osNewName;
11010
0
    m_osName = osNewName;
11011
0
}
11012
11013
//! @endcond
11014
11015
//! @cond Doxygen_Suppress
11016
/************************************************************************/
11017
/*                          ParentRenamed()                             */
11018
/************************************************************************/
11019
11020
void GDALDimension::ParentRenamed(const std::string &osNewParentFullName)
11021
0
{
11022
0
    m_osFullName = osNewParentFullName;
11023
0
    m_osFullName += "/";
11024
0
    m_osFullName += m_osName;
11025
0
}
11026
11027
//! @endcond
11028
11029
//! @cond Doxygen_Suppress
11030
/************************************************************************/
11031
/*                          ParentDeleted()                             */
11032
/************************************************************************/
11033
11034
void GDALDimension::ParentDeleted()
11035
0
{
11036
0
}
11037
11038
//! @endcond
11039
11040
/************************************************************************/
11041
/************************************************************************/
11042
/************************************************************************/
11043
/*                              C API                                   */
11044
/************************************************************************/
11045
/************************************************************************/
11046
/************************************************************************/
11047
11048
/************************************************************************/
11049
/*                      GDALExtendedDataTypeCreate()                    */
11050
/************************************************************************/
11051
11052
/** Return a new GDALExtendedDataType of class GEDTC_NUMERIC.
11053
 *
11054
 * This is the same as the C++ method GDALExtendedDataType::Create()
11055
 *
11056
 * The returned handle should be freed with GDALExtendedDataTypeRelease().
11057
 *
11058
 * @param eType Numeric data type. Must be different from GDT_Unknown and
11059
 * GDT_TypeCount
11060
 *
11061
 * @return a new GDALExtendedDataTypeH handle, or nullptr.
11062
 */
11063
GDALExtendedDataTypeH GDALExtendedDataTypeCreate(GDALDataType eType)
11064
0
{
11065
0
    if (CPL_UNLIKELY(eType == GDT_Unknown || eType == GDT_TypeCount))
11066
0
    {
11067
0
        CPLError(CE_Failure, CPLE_IllegalArg,
11068
0
                 "Illegal GDT_Unknown/GDT_TypeCount argument");
11069
0
        return nullptr;
11070
0
    }
11071
0
    return new GDALExtendedDataTypeHS(
11072
0
        new GDALExtendedDataType(GDALExtendedDataType::Create(eType)));
11073
0
}
11074
11075
/************************************************************************/
11076
/*                    GDALExtendedDataTypeCreateString()                */
11077
/************************************************************************/
11078
11079
/** Return a new GDALExtendedDataType of class GEDTC_STRING.
11080
 *
11081
 * This is the same as the C++ method GDALExtendedDataType::CreateString()
11082
 *
11083
 * The returned handle should be freed with GDALExtendedDataTypeRelease().
11084
 *
11085
 * @return a new GDALExtendedDataTypeH handle, or nullptr.
11086
 */
11087
GDALExtendedDataTypeH GDALExtendedDataTypeCreateString(size_t nMaxStringLength)
11088
0
{
11089
0
    return new GDALExtendedDataTypeHS(new GDALExtendedDataType(
11090
0
        GDALExtendedDataType::CreateString(nMaxStringLength)));
11091
0
}
11092
11093
/************************************************************************/
11094
/*                   GDALExtendedDataTypeCreateStringEx()               */
11095
/************************************************************************/
11096
11097
/** Return a new GDALExtendedDataType of class GEDTC_STRING.
11098
 *
11099
 * This is the same as the C++ method GDALExtendedDataType::CreateString()
11100
 *
11101
 * The returned handle should be freed with GDALExtendedDataTypeRelease().
11102
 *
11103
 * @return a new GDALExtendedDataTypeH handle, or nullptr.
11104
 * @since GDAL 3.4
11105
 */
11106
GDALExtendedDataTypeH
11107
GDALExtendedDataTypeCreateStringEx(size_t nMaxStringLength,
11108
                                   GDALExtendedDataTypeSubType eSubType)
11109
0
{
11110
0
    return new GDALExtendedDataTypeHS(new GDALExtendedDataType(
11111
0
        GDALExtendedDataType::CreateString(nMaxStringLength, eSubType)));
11112
0
}
11113
11114
/************************************************************************/
11115
/*                   GDALExtendedDataTypeCreateCompound()               */
11116
/************************************************************************/
11117
11118
/** Return a new GDALExtendedDataType of class GEDTC_COMPOUND.
11119
 *
11120
 * This is the same as the C++ method GDALExtendedDataType::Create(const
11121
 * std::string&, size_t, std::vector<std::unique_ptr<GDALEDTComponent>>&&)
11122
 *
11123
 * The returned handle should be freed with GDALExtendedDataTypeRelease().
11124
 *
11125
 * @param pszName Type name.
11126
 * @param nTotalSize Total size of the type in bytes.
11127
 *                   Should be large enough to store all components.
11128
 * @param nComponents Number of components in comps array.
11129
 * @param comps Components.
11130
 * @return a new GDALExtendedDataTypeH handle, or nullptr.
11131
 */
11132
GDALExtendedDataTypeH
11133
GDALExtendedDataTypeCreateCompound(const char *pszName, size_t nTotalSize,
11134
                                   size_t nComponents,
11135
                                   const GDALEDTComponentH *comps)
11136
0
{
11137
0
    std::vector<std::unique_ptr<GDALEDTComponent>> compsCpp;
11138
0
    for (size_t i = 0; i < nComponents; i++)
11139
0
    {
11140
0
        compsCpp.emplace_back(std::unique_ptr<GDALEDTComponent>(
11141
0
            new GDALEDTComponent(*(comps[i]->m_poImpl.get()))));
11142
0
    }
11143
0
    auto dt = GDALExtendedDataType::Create(pszName ? pszName : "", nTotalSize,
11144
0
                                           std::move(compsCpp));
11145
0
    if (dt.GetClass() != GEDTC_COMPOUND)
11146
0
        return nullptr;
11147
0
    return new GDALExtendedDataTypeHS(new GDALExtendedDataType(dt));
11148
0
}
11149
11150
/************************************************************************/
11151
/*                     GDALExtendedDataTypeRelease()                    */
11152
/************************************************************************/
11153
11154
/** Release the GDAL in-memory object associated with a GDALExtendedDataTypeH.
11155
 *
11156
 * Note: when applied on a object coming from a driver, this does not
11157
 * destroy the object in the file, database, etc...
11158
 */
11159
void GDALExtendedDataTypeRelease(GDALExtendedDataTypeH hEDT)
11160
0
{
11161
0
    delete hEDT;
11162
0
}
11163
11164
/************************************************************************/
11165
/*                     GDALExtendedDataTypeGetName()                    */
11166
/************************************************************************/
11167
11168
/** Return type name.
11169
 *
11170
 * This is the same as the C++ method GDALExtendedDataType::GetName()
11171
 */
11172
const char *GDALExtendedDataTypeGetName(GDALExtendedDataTypeH hEDT)
11173
0
{
11174
0
    VALIDATE_POINTER1(hEDT, __func__, "");
11175
0
    return hEDT->m_poImpl->GetName().c_str();
11176
0
}
11177
11178
/************************************************************************/
11179
/*                     GDALExtendedDataTypeGetClass()                    */
11180
/************************************************************************/
11181
11182
/** Return type class.
11183
 *
11184
 * This is the same as the C++ method GDALExtendedDataType::GetClass()
11185
 */
11186
GDALExtendedDataTypeClass
11187
GDALExtendedDataTypeGetClass(GDALExtendedDataTypeH hEDT)
11188
0
{
11189
0
    VALIDATE_POINTER1(hEDT, __func__, GEDTC_NUMERIC);
11190
0
    return hEDT->m_poImpl->GetClass();
11191
0
}
11192
11193
/************************************************************************/
11194
/*               GDALExtendedDataTypeGetNumericDataType()               */
11195
/************************************************************************/
11196
11197
/** Return numeric data type (only valid when GetClass() == GEDTC_NUMERIC)
11198
 *
11199
 * This is the same as the C++ method GDALExtendedDataType::GetNumericDataType()
11200
 */
11201
GDALDataType GDALExtendedDataTypeGetNumericDataType(GDALExtendedDataTypeH hEDT)
11202
0
{
11203
0
    VALIDATE_POINTER1(hEDT, __func__, GDT_Unknown);
11204
0
    return hEDT->m_poImpl->GetNumericDataType();
11205
0
}
11206
11207
/************************************************************************/
11208
/*                   GDALExtendedDataTypeGetSize()                      */
11209
/************************************************************************/
11210
11211
/** Return data type size in bytes.
11212
 *
11213
 * This is the same as the C++ method GDALExtendedDataType::GetSize()
11214
 */
11215
size_t GDALExtendedDataTypeGetSize(GDALExtendedDataTypeH hEDT)
11216
0
{
11217
0
    VALIDATE_POINTER1(hEDT, __func__, 0);
11218
0
    return hEDT->m_poImpl->GetSize();
11219
0
}
11220
11221
/************************************************************************/
11222
/*              GDALExtendedDataTypeGetMaxStringLength()                */
11223
/************************************************************************/
11224
11225
/** Return the maximum length of a string in bytes.
11226
 *
11227
 * 0 indicates unknown/unlimited string.
11228
 *
11229
 * This is the same as the C++ method GDALExtendedDataType::GetMaxStringLength()
11230
 */
11231
size_t GDALExtendedDataTypeGetMaxStringLength(GDALExtendedDataTypeH hEDT)
11232
0
{
11233
0
    VALIDATE_POINTER1(hEDT, __func__, 0);
11234
0
    return hEDT->m_poImpl->GetMaxStringLength();
11235
0
}
11236
11237
/************************************************************************/
11238
/*                    GDALExtendedDataTypeCanConvertTo()                */
11239
/************************************************************************/
11240
11241
/** Return whether this data type can be converted to the other one.
11242
 *
11243
 * This is the same as the C function GDALExtendedDataType::CanConvertTo()
11244
 *
11245
 * @param hSourceEDT Source data type for the conversion being considered.
11246
 * @param hTargetEDT Target data type for the conversion being considered.
11247
 * @return TRUE if hSourceEDT can be convert to hTargetEDT. FALSE otherwise.
11248
 */
11249
int GDALExtendedDataTypeCanConvertTo(GDALExtendedDataTypeH hSourceEDT,
11250
                                     GDALExtendedDataTypeH hTargetEDT)
11251
0
{
11252
0
    VALIDATE_POINTER1(hSourceEDT, __func__, FALSE);
11253
0
    VALIDATE_POINTER1(hTargetEDT, __func__, FALSE);
11254
0
    return hSourceEDT->m_poImpl->CanConvertTo(*(hTargetEDT->m_poImpl));
11255
0
}
11256
11257
/************************************************************************/
11258
/*                        GDALExtendedDataTypeEquals()                  */
11259
/************************************************************************/
11260
11261
/** Return whether this data type is equal to another one.
11262
 *
11263
 * This is the same as the C++ method GDALExtendedDataType::operator==()
11264
 *
11265
 * @param hFirstEDT First data type.
11266
 * @param hSecondEDT Second data type.
11267
 * @return TRUE if they are equal. FALSE otherwise.
11268
 */
11269
int GDALExtendedDataTypeEquals(GDALExtendedDataTypeH hFirstEDT,
11270
                               GDALExtendedDataTypeH hSecondEDT)
11271
0
{
11272
0
    VALIDATE_POINTER1(hFirstEDT, __func__, FALSE);
11273
0
    VALIDATE_POINTER1(hSecondEDT, __func__, FALSE);
11274
0
    return *(hFirstEDT->m_poImpl) == *(hSecondEDT->m_poImpl);
11275
0
}
11276
11277
/************************************************************************/
11278
/*                    GDALExtendedDataTypeGetSubType()                  */
11279
/************************************************************************/
11280
11281
/** Return the subtype of a type.
11282
 *
11283
 * This is the same as the C++ method GDALExtendedDataType::GetSubType()
11284
 *
11285
 * @param hEDT Data type.
11286
 * @return subtype.
11287
 * @since 3.4
11288
 */
11289
GDALExtendedDataTypeSubType
11290
GDALExtendedDataTypeGetSubType(GDALExtendedDataTypeH hEDT)
11291
0
{
11292
0
    VALIDATE_POINTER1(hEDT, __func__, GEDTST_NONE);
11293
0
    return hEDT->m_poImpl->GetSubType();
11294
0
}
11295
11296
/************************************************************************/
11297
/*                      GDALExtendedDataTypeGetRAT()                    */
11298
/************************************************************************/
11299
11300
/** Return associated raster attribute table, when there is one.
11301
 *
11302
 * * For the netCDF driver, the RAT will capture enumerated types, with
11303
 * a "value" column with an integer value and a "name" column with the
11304
 * associated name.
11305
 * This is the same as the C++ method GDALExtendedDataType::GetRAT()
11306
 *
11307
 * @param hEDT Data type.
11308
 * @return raster attribute (owned by GDALExtendedDataTypeH), or NULL
11309
 * @since 3.12
11310
 */
11311
GDALRasterAttributeTableH GDALExtendedDataTypeGetRAT(GDALExtendedDataTypeH hEDT)
11312
0
{
11313
0
    VALIDATE_POINTER1(hEDT, __func__, nullptr);
11314
0
    return GDALRasterAttributeTable::ToHandle(
11315
0
        const_cast<GDALRasterAttributeTable *>(hEDT->m_poImpl->GetRAT()));
11316
0
}
11317
11318
/************************************************************************/
11319
/*                     GDALExtendedDataTypeGetComponents()              */
11320
/************************************************************************/
11321
11322
/** Return the components of the data type (only valid when GetClass() ==
11323
 * GEDTC_COMPOUND)
11324
 *
11325
 * The returned array and its content must be freed with
11326
 * GDALExtendedDataTypeFreeComponents(). If only the array itself needs to be
11327
 * freed, CPLFree() should be called (and GDALExtendedDataTypeRelease() on
11328
 * individual array members).
11329
 *
11330
 * This is the same as the C++ method GDALExtendedDataType::GetComponents()
11331
 *
11332
 * @param hEDT Data type
11333
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
11334
 * @return an array of *pnCount components.
11335
 */
11336
GDALEDTComponentH *GDALExtendedDataTypeGetComponents(GDALExtendedDataTypeH hEDT,
11337
                                                     size_t *pnCount)
11338
0
{
11339
0
    VALIDATE_POINTER1(hEDT, __func__, nullptr);
11340
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
11341
0
    const auto &components = hEDT->m_poImpl->GetComponents();
11342
0
    auto ret = static_cast<GDALEDTComponentH *>(
11343
0
        CPLMalloc(sizeof(GDALEDTComponentH) * components.size()));
11344
0
    for (size_t i = 0; i < components.size(); i++)
11345
0
    {
11346
0
        ret[i] = new GDALEDTComponentHS(*components[i].get());
11347
0
    }
11348
0
    *pnCount = components.size();
11349
0
    return ret;
11350
0
}
11351
11352
/************************************************************************/
11353
/*                     GDALExtendedDataTypeFreeComponents()             */
11354
/************************************************************************/
11355
11356
/** Free the return of GDALExtendedDataTypeGetComponents().
11357
 *
11358
 * @param components return value of GDALExtendedDataTypeGetComponents()
11359
 * @param nCount *pnCount value returned by GDALExtendedDataTypeGetComponents()
11360
 */
11361
void GDALExtendedDataTypeFreeComponents(GDALEDTComponentH *components,
11362
                                        size_t nCount)
11363
0
{
11364
0
    for (size_t i = 0; i < nCount; i++)
11365
0
    {
11366
0
        delete components[i];
11367
0
    }
11368
0
    CPLFree(components);
11369
0
}
11370
11371
/************************************************************************/
11372
/*                         GDALEDTComponentCreate()                     */
11373
/************************************************************************/
11374
11375
/** Create a new GDALEDTComponent.
11376
 *
11377
 * The returned value must be freed with GDALEDTComponentRelease().
11378
 *
11379
 * This is the same as the C++ constructor GDALEDTComponent::GDALEDTComponent().
11380
 */
11381
GDALEDTComponentH GDALEDTComponentCreate(const char *pszName, size_t nOffset,
11382
                                         GDALExtendedDataTypeH hType)
11383
0
{
11384
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
11385
0
    VALIDATE_POINTER1(hType, __func__, nullptr);
11386
0
    return new GDALEDTComponentHS(
11387
0
        GDALEDTComponent(pszName, nOffset, *(hType->m_poImpl.get())));
11388
0
}
11389
11390
/************************************************************************/
11391
/*                         GDALEDTComponentRelease()                    */
11392
/************************************************************************/
11393
11394
/** Release the GDAL in-memory object associated with a GDALEDTComponentH.
11395
 *
11396
 * Note: when applied on a object coming from a driver, this does not
11397
 * destroy the object in the file, database, etc...
11398
 */
11399
void GDALEDTComponentRelease(GDALEDTComponentH hComp)
11400
0
{
11401
0
    delete hComp;
11402
0
}
11403
11404
/************************************************************************/
11405
/*                         GDALEDTComponentGetName()                    */
11406
/************************************************************************/
11407
11408
/** Return the name.
11409
 *
11410
 * The returned pointer is valid until hComp is released.
11411
 *
11412
 * This is the same as the C++ method GDALEDTComponent::GetName().
11413
 */
11414
const char *GDALEDTComponentGetName(GDALEDTComponentH hComp)
11415
0
{
11416
0
    VALIDATE_POINTER1(hComp, __func__, nullptr);
11417
0
    return hComp->m_poImpl->GetName().c_str();
11418
0
}
11419
11420
/************************************************************************/
11421
/*                       GDALEDTComponentGetOffset()                    */
11422
/************************************************************************/
11423
11424
/** Return the offset (in bytes) of the component in the compound data type.
11425
 *
11426
 * This is the same as the C++ method GDALEDTComponent::GetOffset().
11427
 */
11428
size_t GDALEDTComponentGetOffset(GDALEDTComponentH hComp)
11429
0
{
11430
0
    VALIDATE_POINTER1(hComp, __func__, 0);
11431
0
    return hComp->m_poImpl->GetOffset();
11432
0
}
11433
11434
/************************************************************************/
11435
/*                       GDALEDTComponentGetType()                      */
11436
/************************************************************************/
11437
11438
/** Return the data type of the component.
11439
 *
11440
 * This is the same as the C++ method GDALEDTComponent::GetType().
11441
 */
11442
GDALExtendedDataTypeH GDALEDTComponentGetType(GDALEDTComponentH hComp)
11443
0
{
11444
0
    VALIDATE_POINTER1(hComp, __func__, nullptr);
11445
0
    return new GDALExtendedDataTypeHS(
11446
0
        new GDALExtendedDataType(hComp->m_poImpl->GetType()));
11447
0
}
11448
11449
/************************************************************************/
11450
/*                           GDALGroupRelease()                         */
11451
/************************************************************************/
11452
11453
/** Release the GDAL in-memory object associated with a GDALGroupH.
11454
 *
11455
 * Note: when applied on a object coming from a driver, this does not
11456
 * destroy the object in the file, database, etc...
11457
 */
11458
void GDALGroupRelease(GDALGroupH hGroup)
11459
0
{
11460
0
    delete hGroup;
11461
0
}
11462
11463
/************************************************************************/
11464
/*                           GDALGroupGetName()                         */
11465
/************************************************************************/
11466
11467
/** Return the name of the group.
11468
 *
11469
 * The returned pointer is valid until hGroup is released.
11470
 *
11471
 * This is the same as the C++ method GDALGroup::GetName().
11472
 */
11473
const char *GDALGroupGetName(GDALGroupH hGroup)
11474
0
{
11475
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11476
0
    return hGroup->m_poImpl->GetName().c_str();
11477
0
}
11478
11479
/************************************************************************/
11480
/*                         GDALGroupGetFullName()                       */
11481
/************************************************************************/
11482
11483
/** Return the full name of the group.
11484
 *
11485
 * The returned pointer is valid until hGroup is released.
11486
 *
11487
 * This is the same as the C++ method GDALGroup::GetFullName().
11488
 */
11489
const char *GDALGroupGetFullName(GDALGroupH hGroup)
11490
0
{
11491
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11492
0
    return hGroup->m_poImpl->GetFullName().c_str();
11493
0
}
11494
11495
/************************************************************************/
11496
/*                          GDALGroupGetMDArrayNames()                  */
11497
/************************************************************************/
11498
11499
/** Return the list of multidimensional array names contained in this group.
11500
 *
11501
 * This is the same as the C++ method GDALGroup::GetGroupNames().
11502
 *
11503
 * @return the array names, to be freed with CSLDestroy()
11504
 */
11505
char **GDALGroupGetMDArrayNames(GDALGroupH hGroup, CSLConstList papszOptions)
11506
0
{
11507
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11508
0
    auto names = hGroup->m_poImpl->GetMDArrayNames(papszOptions);
11509
0
    CPLStringList res;
11510
0
    for (const auto &name : names)
11511
0
    {
11512
0
        res.AddString(name.c_str());
11513
0
    }
11514
0
    return res.StealList();
11515
0
}
11516
11517
/************************************************************************/
11518
/*                  GDALGroupGetMDArrayFullNamesRecursive()             */
11519
/************************************************************************/
11520
11521
/** Return the list of multidimensional array full names contained in this
11522
 * group and its subgroups.
11523
 *
11524
 * This is the same as the C++ method GDALGroup::GetMDArrayFullNamesRecursive().
11525
 *
11526
 * @return the array names, to be freed with CSLDestroy()
11527
 *
11528
 * @since 3.11
11529
 */
11530
char **GDALGroupGetMDArrayFullNamesRecursive(GDALGroupH hGroup,
11531
                                             CSLConstList papszGroupOptions,
11532
                                             CSLConstList papszArrayOptions)
11533
0
{
11534
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11535
0
    auto names = hGroup->m_poImpl->GetMDArrayFullNamesRecursive(
11536
0
        papszGroupOptions, papszArrayOptions);
11537
0
    CPLStringList res;
11538
0
    for (const auto &name : names)
11539
0
    {
11540
0
        res.AddString(name.c_str());
11541
0
    }
11542
0
    return res.StealList();
11543
0
}
11544
11545
/************************************************************************/
11546
/*                          GDALGroupOpenMDArray()                      */
11547
/************************************************************************/
11548
11549
/** Open and return a multidimensional array.
11550
 *
11551
 * This is the same as the C++ method GDALGroup::OpenMDArray().
11552
 *
11553
 * @return the array, to be freed with GDALMDArrayRelease(), or nullptr.
11554
 */
11555
GDALMDArrayH GDALGroupOpenMDArray(GDALGroupH hGroup, const char *pszMDArrayName,
11556
                                  CSLConstList papszOptions)
11557
0
{
11558
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11559
0
    VALIDATE_POINTER1(pszMDArrayName, __func__, nullptr);
11560
0
    auto array = hGroup->m_poImpl->OpenMDArray(std::string(pszMDArrayName),
11561
0
                                               papszOptions);
11562
0
    if (!array)
11563
0
        return nullptr;
11564
0
    return new GDALMDArrayHS(array);
11565
0
}
11566
11567
/************************************************************************/
11568
/*                  GDALGroupOpenMDArrayFromFullname()                  */
11569
/************************************************************************/
11570
11571
/** Open and return a multidimensional array from its fully qualified name.
11572
 *
11573
 * This is the same as the C++ method GDALGroup::OpenMDArrayFromFullname().
11574
 *
11575
 * @return the array, to be freed with GDALMDArrayRelease(), or nullptr.
11576
 *
11577
 * @since GDAL 3.2
11578
 */
11579
GDALMDArrayH GDALGroupOpenMDArrayFromFullname(GDALGroupH hGroup,
11580
                                              const char *pszFullname,
11581
                                              CSLConstList papszOptions)
11582
0
{
11583
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11584
0
    VALIDATE_POINTER1(pszFullname, __func__, nullptr);
11585
0
    auto array = hGroup->m_poImpl->OpenMDArrayFromFullname(
11586
0
        std::string(pszFullname), papszOptions);
11587
0
    if (!array)
11588
0
        return nullptr;
11589
0
    return new GDALMDArrayHS(array);
11590
0
}
11591
11592
/************************************************************************/
11593
/*                      GDALGroupResolveMDArray()                       */
11594
/************************************************************************/
11595
11596
/** Locate an array in a group and its subgroups by name.
11597
 *
11598
 * See GDALGroup::ResolveMDArray() for description of the behavior.
11599
 * @since GDAL 3.2
11600
 */
11601
GDALMDArrayH GDALGroupResolveMDArray(GDALGroupH hGroup, const char *pszName,
11602
                                     const char *pszStartingPoint,
11603
                                     CSLConstList papszOptions)
11604
0
{
11605
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11606
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
11607
0
    VALIDATE_POINTER1(pszStartingPoint, __func__, nullptr);
11608
0
    auto array = hGroup->m_poImpl->ResolveMDArray(
11609
0
        std::string(pszName), std::string(pszStartingPoint), papszOptions);
11610
0
    if (!array)
11611
0
        return nullptr;
11612
0
    return new GDALMDArrayHS(array);
11613
0
}
11614
11615
/************************************************************************/
11616
/*                        GDALGroupGetGroupNames()                      */
11617
/************************************************************************/
11618
11619
/** Return the list of sub-groups contained in this group.
11620
 *
11621
 * This is the same as the C++ method GDALGroup::GetGroupNames().
11622
 *
11623
 * @return the group names, to be freed with CSLDestroy()
11624
 */
11625
char **GDALGroupGetGroupNames(GDALGroupH hGroup, CSLConstList papszOptions)
11626
0
{
11627
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11628
0
    auto names = hGroup->m_poImpl->GetGroupNames(papszOptions);
11629
0
    CPLStringList res;
11630
0
    for (const auto &name : names)
11631
0
    {
11632
0
        res.AddString(name.c_str());
11633
0
    }
11634
0
    return res.StealList();
11635
0
}
11636
11637
/************************************************************************/
11638
/*                           GDALGroupOpenGroup()                       */
11639
/************************************************************************/
11640
11641
/** Open and return a sub-group.
11642
 *
11643
 * This is the same as the C++ method GDALGroup::OpenGroup().
11644
 *
11645
 * @return the sub-group, to be freed with GDALGroupRelease(), or nullptr.
11646
 */
11647
GDALGroupH GDALGroupOpenGroup(GDALGroupH hGroup, const char *pszSubGroupName,
11648
                              CSLConstList papszOptions)
11649
0
{
11650
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11651
0
    VALIDATE_POINTER1(pszSubGroupName, __func__, nullptr);
11652
0
    auto subGroup =
11653
0
        hGroup->m_poImpl->OpenGroup(std::string(pszSubGroupName), papszOptions);
11654
0
    if (!subGroup)
11655
0
        return nullptr;
11656
0
    return new GDALGroupHS(subGroup);
11657
0
}
11658
11659
/************************************************************************/
11660
/*                   GDALGroupGetVectorLayerNames()                     */
11661
/************************************************************************/
11662
11663
/** Return the list of layer names contained in this group.
11664
 *
11665
 * This is the same as the C++ method GDALGroup::GetVectorLayerNames().
11666
 *
11667
 * @return the group names, to be freed with CSLDestroy()
11668
 * @since 3.4
11669
 */
11670
char **GDALGroupGetVectorLayerNames(GDALGroupH hGroup,
11671
                                    CSLConstList papszOptions)
11672
0
{
11673
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11674
0
    auto names = hGroup->m_poImpl->GetVectorLayerNames(papszOptions);
11675
0
    CPLStringList res;
11676
0
    for (const auto &name : names)
11677
0
    {
11678
0
        res.AddString(name.c_str());
11679
0
    }
11680
0
    return res.StealList();
11681
0
}
11682
11683
/************************************************************************/
11684
/*                      GDALGroupOpenVectorLayer()                      */
11685
/************************************************************************/
11686
11687
/** Open and return a vector layer.
11688
 *
11689
 * This is the same as the C++ method GDALGroup::OpenVectorLayer().
11690
 *
11691
 * Note that the vector layer is owned by its parent GDALDatasetH, and thus
11692
 * the returned handled if only valid while the parent GDALDatasetH is kept
11693
 * opened.
11694
 *
11695
 * @return the vector layer, or nullptr.
11696
 * @since 3.4
11697
 */
11698
OGRLayerH GDALGroupOpenVectorLayer(GDALGroupH hGroup,
11699
                                   const char *pszVectorLayerName,
11700
                                   CSLConstList papszOptions)
11701
0
{
11702
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11703
0
    VALIDATE_POINTER1(pszVectorLayerName, __func__, nullptr);
11704
0
    return OGRLayer::ToHandle(hGroup->m_poImpl->OpenVectorLayer(
11705
0
        std::string(pszVectorLayerName), papszOptions));
11706
0
}
11707
11708
/************************************************************************/
11709
/*                       GDALGroupOpenMDArrayFromFullname()             */
11710
/************************************************************************/
11711
11712
/** Open and return a sub-group from its fully qualified name.
11713
 *
11714
 * This is the same as the C++ method GDALGroup::OpenGroupFromFullname().
11715
 *
11716
 * @return the sub-group, to be freed with GDALGroupRelease(), or nullptr.
11717
 *
11718
 * @since GDAL 3.2
11719
 */
11720
GDALGroupH GDALGroupOpenGroupFromFullname(GDALGroupH hGroup,
11721
                                          const char *pszFullname,
11722
                                          CSLConstList papszOptions)
11723
0
{
11724
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11725
0
    VALIDATE_POINTER1(pszFullname, __func__, nullptr);
11726
0
    auto subGroup = hGroup->m_poImpl->OpenGroupFromFullname(
11727
0
        std::string(pszFullname), papszOptions);
11728
0
    if (!subGroup)
11729
0
        return nullptr;
11730
0
    return new GDALGroupHS(subGroup);
11731
0
}
11732
11733
/************************************************************************/
11734
/*                         GDALGroupGetDimensions()                     */
11735
/************************************************************************/
11736
11737
/** Return the list of dimensions contained in this group and used by its
11738
 * arrays.
11739
 *
11740
 * The returned array must be freed with GDALReleaseDimensions().  If only the
11741
 * array itself needs to be freed, CPLFree() should be called (and
11742
 * GDALDimensionRelease() on individual array members).
11743
 *
11744
 * This is the same as the C++ method GDALGroup::GetDimensions().
11745
 *
11746
 * @param hGroup Group.
11747
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
11748
 * @param papszOptions Driver specific options determining how dimensions
11749
 * should be retrieved. Pass nullptr for default behavior.
11750
 *
11751
 * @return an array of *pnCount dimensions.
11752
 */
11753
GDALDimensionH *GDALGroupGetDimensions(GDALGroupH hGroup, size_t *pnCount,
11754
                                       CSLConstList papszOptions)
11755
0
{
11756
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11757
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
11758
0
    auto dims = hGroup->m_poImpl->GetDimensions(papszOptions);
11759
0
    auto ret = static_cast<GDALDimensionH *>(
11760
0
        CPLMalloc(sizeof(GDALDimensionH) * dims.size()));
11761
0
    for (size_t i = 0; i < dims.size(); i++)
11762
0
    {
11763
0
        ret[i] = new GDALDimensionHS(dims[i]);
11764
0
    }
11765
0
    *pnCount = dims.size();
11766
0
    return ret;
11767
0
}
11768
11769
/************************************************************************/
11770
/*                          GDALGroupGetAttribute()                     */
11771
/************************************************************************/
11772
11773
/** Return an attribute by its name.
11774
 *
11775
 * This is the same as the C++ method GDALIHasAttribute::GetAttribute()
11776
 *
11777
 * The returned attribute must be freed with GDALAttributeRelease().
11778
 */
11779
GDALAttributeH GDALGroupGetAttribute(GDALGroupH hGroup, const char *pszName)
11780
0
{
11781
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11782
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
11783
0
    auto attr = hGroup->m_poImpl->GetAttribute(std::string(pszName));
11784
0
    if (attr)
11785
0
        return new GDALAttributeHS(attr);
11786
0
    return nullptr;
11787
0
}
11788
11789
/************************************************************************/
11790
/*                         GDALGroupGetAttributes()                     */
11791
/************************************************************************/
11792
11793
/** Return the list of attributes contained in this group.
11794
 *
11795
 * The returned array must be freed with GDALReleaseAttributes(). If only the
11796
 * array itself needs to be freed, CPLFree() should be called (and
11797
 * GDALAttributeRelease() on individual array members).
11798
 *
11799
 * This is the same as the C++ method GDALGroup::GetAttributes().
11800
 *
11801
 * @param hGroup Group.
11802
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
11803
 * @param papszOptions Driver specific options determining how attributes
11804
 * should be retrieved. Pass nullptr for default behavior.
11805
 *
11806
 * @return an array of *pnCount attributes.
11807
 */
11808
GDALAttributeH *GDALGroupGetAttributes(GDALGroupH hGroup, size_t *pnCount,
11809
                                       CSLConstList papszOptions)
11810
0
{
11811
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11812
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
11813
0
    auto attrs = hGroup->m_poImpl->GetAttributes(papszOptions);
11814
0
    auto ret = static_cast<GDALAttributeH *>(
11815
0
        CPLMalloc(sizeof(GDALAttributeH) * attrs.size()));
11816
0
    for (size_t i = 0; i < attrs.size(); i++)
11817
0
    {
11818
0
        ret[i] = new GDALAttributeHS(attrs[i]);
11819
0
    }
11820
0
    *pnCount = attrs.size();
11821
0
    return ret;
11822
0
}
11823
11824
/************************************************************************/
11825
/*                     GDALGroupGetStructuralInfo()                     */
11826
/************************************************************************/
11827
11828
/** Return structural information on the group.
11829
 *
11830
 * This may be the compression, etc..
11831
 *
11832
 * The return value should not be freed and is valid until GDALGroup is
11833
 * released or this function called again.
11834
 *
11835
 * This is the same as the C++ method GDALGroup::GetStructuralInfo().
11836
 */
11837
CSLConstList GDALGroupGetStructuralInfo(GDALGroupH hGroup)
11838
0
{
11839
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11840
0
    return hGroup->m_poImpl->GetStructuralInfo();
11841
0
}
11842
11843
/************************************************************************/
11844
/*                   GDALGroupGetDataTypeCount()                        */
11845
/************************************************************************/
11846
11847
/** Return the number of data types associated with the group
11848
 * (typically enumerations).
11849
 *
11850
 * This is the same as the C++ method GDALGroup::GetDataTypes().size().
11851
 *
11852
 * @since 3.12
11853
 */
11854
size_t GDALGroupGetDataTypeCount(GDALGroupH hGroup)
11855
0
{
11856
0
    VALIDATE_POINTER1(hGroup, __func__, 0);
11857
0
    return hGroup->m_poImpl->GetDataTypes().size();
11858
0
}
11859
11860
/************************************************************************/
11861
/*                      GDALGroupGetDataType()                          */
11862
/************************************************************************/
11863
11864
/** Return one of the data types associated with the group.
11865
 *
11866
 * This is the same as the C++ method GDALGroup::GetDataTypes()[].
11867
 *
11868
 * @return a type to release with GDALExtendedDataTypeRelease() once done,
11869
 * or nullptr in case of error.
11870
 * @since 3.12
11871
 */
11872
GDALExtendedDataTypeH GDALGroupGetDataType(GDALGroupH hGroup, size_t nIdx)
11873
0
{
11874
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11875
0
    if (nIdx >= hGroup->m_poImpl->GetDataTypes().size())
11876
0
        return nullptr;
11877
0
    return new GDALExtendedDataTypeHS(new GDALExtendedDataType(
11878
0
        *(hGroup->m_poImpl->GetDataTypes()[nIdx].get())));
11879
0
}
11880
11881
/************************************************************************/
11882
/*                         GDALReleaseAttributes()                      */
11883
/************************************************************************/
11884
11885
/** Free the return of GDALGroupGetAttributes() or GDALMDArrayGetAttributes()
11886
 *
11887
 * @param attributes return pointer of above methods
11888
 * @param nCount *pnCount value returned by above methods
11889
 */
11890
void GDALReleaseAttributes(GDALAttributeH *attributes, size_t nCount)
11891
0
{
11892
0
    for (size_t i = 0; i < nCount; i++)
11893
0
    {
11894
0
        delete attributes[i];
11895
0
    }
11896
0
    CPLFree(attributes);
11897
0
}
11898
11899
/************************************************************************/
11900
/*                         GDALGroupCreateGroup()                       */
11901
/************************************************************************/
11902
11903
/** Create a sub-group within a group.
11904
 *
11905
 * This is the same as the C++ method GDALGroup::CreateGroup().
11906
 *
11907
 * @return the sub-group, to be freed with GDALGroupRelease(), or nullptr.
11908
 */
11909
GDALGroupH GDALGroupCreateGroup(GDALGroupH hGroup, const char *pszSubGroupName,
11910
                                CSLConstList papszOptions)
11911
0
{
11912
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11913
0
    VALIDATE_POINTER1(pszSubGroupName, __func__, nullptr);
11914
0
    auto ret = hGroup->m_poImpl->CreateGroup(std::string(pszSubGroupName),
11915
0
                                             papszOptions);
11916
0
    if (!ret)
11917
0
        return nullptr;
11918
0
    return new GDALGroupHS(ret);
11919
0
}
11920
11921
/************************************************************************/
11922
/*                         GDALGroupDeleteGroup()                       */
11923
/************************************************************************/
11924
11925
/** Delete a sub-group from a group.
11926
 *
11927
 * After this call, if a previously obtained instance of the deleted object
11928
 * is still alive, no method other than for freeing it should be invoked.
11929
 *
11930
 * This is the same as the C++ method GDALGroup::DeleteGroup().
11931
 *
11932
 * @return true in case of success.
11933
 * @since GDAL 3.8
11934
 */
11935
bool GDALGroupDeleteGroup(GDALGroupH hGroup, const char *pszSubGroupName,
11936
                          CSLConstList papszOptions)
11937
0
{
11938
0
    VALIDATE_POINTER1(hGroup, __func__, false);
11939
0
    VALIDATE_POINTER1(pszSubGroupName, __func__, false);
11940
0
    return hGroup->m_poImpl->DeleteGroup(std::string(pszSubGroupName),
11941
0
                                         papszOptions);
11942
0
}
11943
11944
/************************************************************************/
11945
/*                      GDALGroupCreateDimension()                      */
11946
/************************************************************************/
11947
11948
/** Create a dimension within a group.
11949
 *
11950
 * This is the same as the C++ method GDALGroup::CreateDimension().
11951
 *
11952
 * @return the dimension, to be freed with GDALDimensionRelease(), or nullptr.
11953
 */
11954
GDALDimensionH GDALGroupCreateDimension(GDALGroupH hGroup, const char *pszName,
11955
                                        const char *pszType,
11956
                                        const char *pszDirection, GUInt64 nSize,
11957
                                        CSLConstList papszOptions)
11958
0
{
11959
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11960
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
11961
0
    auto ret = hGroup->m_poImpl->CreateDimension(
11962
0
        std::string(pszName), std::string(pszType ? pszType : ""),
11963
0
        std::string(pszDirection ? pszDirection : ""), nSize, papszOptions);
11964
0
    if (!ret)
11965
0
        return nullptr;
11966
0
    return new GDALDimensionHS(ret);
11967
0
}
11968
11969
/************************************************************************/
11970
/*                      GDALGroupCreateMDArray()                        */
11971
/************************************************************************/
11972
11973
/** Create a multidimensional array within a group.
11974
 *
11975
 * This is the same as the C++ method GDALGroup::CreateMDArray().
11976
 *
11977
 * @return the array, to be freed with GDALMDArrayRelease(), or nullptr.
11978
 */
11979
GDALMDArrayH GDALGroupCreateMDArray(GDALGroupH hGroup, const char *pszName,
11980
                                    size_t nDimensions,
11981
                                    GDALDimensionH *pahDimensions,
11982
                                    GDALExtendedDataTypeH hEDT,
11983
                                    CSLConstList papszOptions)
11984
0
{
11985
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
11986
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
11987
0
    VALIDATE_POINTER1(hEDT, __func__, nullptr);
11988
0
    std::vector<std::shared_ptr<GDALDimension>> dims;
11989
0
    dims.reserve(nDimensions);
11990
0
    for (size_t i = 0; i < nDimensions; i++)
11991
0
        dims.push_back(pahDimensions[i]->m_poImpl);
11992
0
    auto ret = hGroup->m_poImpl->CreateMDArray(std::string(pszName), dims,
11993
0
                                               *(hEDT->m_poImpl), papszOptions);
11994
0
    if (!ret)
11995
0
        return nullptr;
11996
0
    return new GDALMDArrayHS(ret);
11997
0
}
11998
11999
/************************************************************************/
12000
/*                         GDALGroupDeleteMDArray()                     */
12001
/************************************************************************/
12002
12003
/** Delete an array from a group.
12004
 *
12005
 * After this call, if a previously obtained instance of the deleted object
12006
 * is still alive, no method other than for freeing it should be invoked.
12007
 *
12008
 * This is the same as the C++ method GDALGroup::DeleteMDArray().
12009
 *
12010
 * @return true in case of success.
12011
 * @since GDAL 3.8
12012
 */
12013
bool GDALGroupDeleteMDArray(GDALGroupH hGroup, const char *pszName,
12014
                            CSLConstList papszOptions)
12015
0
{
12016
0
    VALIDATE_POINTER1(hGroup, __func__, false);
12017
0
    VALIDATE_POINTER1(pszName, __func__, false);
12018
0
    return hGroup->m_poImpl->DeleteMDArray(std::string(pszName), papszOptions);
12019
0
}
12020
12021
/************************************************************************/
12022
/*                      GDALGroupCreateAttribute()                      */
12023
/************************************************************************/
12024
12025
/** Create a attribute within a group.
12026
 *
12027
 * This is the same as the C++ method GDALGroup::CreateAttribute().
12028
 *
12029
 * @return the attribute, to be freed with GDALAttributeRelease(), or nullptr.
12030
 */
12031
GDALAttributeH GDALGroupCreateAttribute(GDALGroupH hGroup, const char *pszName,
12032
                                        size_t nDimensions,
12033
                                        const GUInt64 *panDimensions,
12034
                                        GDALExtendedDataTypeH hEDT,
12035
                                        CSLConstList papszOptions)
12036
0
{
12037
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
12038
0
    VALIDATE_POINTER1(hEDT, __func__, nullptr);
12039
0
    std::vector<GUInt64> dims;
12040
0
    dims.reserve(nDimensions);
12041
0
    for (size_t i = 0; i < nDimensions; i++)
12042
0
        dims.push_back(panDimensions[i]);
12043
0
    auto ret = hGroup->m_poImpl->CreateAttribute(
12044
0
        std::string(pszName), dims, *(hEDT->m_poImpl), papszOptions);
12045
0
    if (!ret)
12046
0
        return nullptr;
12047
0
    return new GDALAttributeHS(ret);
12048
0
}
12049
12050
/************************************************************************/
12051
/*                         GDALGroupDeleteAttribute()                   */
12052
/************************************************************************/
12053
12054
/** Delete an attribute from a group.
12055
 *
12056
 * After this call, if a previously obtained instance of the deleted object
12057
 * is still alive, no method other than for freeing it should be invoked.
12058
 *
12059
 * This is the same as the C++ method GDALGroup::DeleteAttribute().
12060
 *
12061
 * @return true in case of success.
12062
 * @since GDAL 3.8
12063
 */
12064
bool GDALGroupDeleteAttribute(GDALGroupH hGroup, const char *pszName,
12065
                              CSLConstList papszOptions)
12066
0
{
12067
0
    VALIDATE_POINTER1(hGroup, __func__, false);
12068
0
    VALIDATE_POINTER1(pszName, __func__, false);
12069
0
    return hGroup->m_poImpl->DeleteAttribute(std::string(pszName),
12070
0
                                             papszOptions);
12071
0
}
12072
12073
/************************************************************************/
12074
/*                          GDALGroupRename()                           */
12075
/************************************************************************/
12076
12077
/** Rename the group.
12078
 *
12079
 * This is not implemented by all drivers.
12080
 *
12081
 * Drivers known to implement it: MEM, netCDF.
12082
 *
12083
 * This is the same as the C++ method GDALGroup::Rename()
12084
 *
12085
 * @return true in case of success
12086
 * @since GDAL 3.8
12087
 */
12088
bool GDALGroupRename(GDALGroupH hGroup, const char *pszNewName)
12089
0
{
12090
0
    VALIDATE_POINTER1(hGroup, __func__, false);
12091
0
    VALIDATE_POINTER1(pszNewName, __func__, false);
12092
0
    return hGroup->m_poImpl->Rename(pszNewName);
12093
0
}
12094
12095
/************************************************************************/
12096
/*                 GDALGroupSubsetDimensionFromSelection()              */
12097
/************************************************************************/
12098
12099
/** Return a virtual group whose one dimension has been subset according to a
12100
 * selection.
12101
 *
12102
 * This is the same as the C++ method GDALGroup::SubsetDimensionFromSelection().
12103
 *
12104
 * @return a virtual group, to be freed with GDALGroupRelease(), or nullptr.
12105
 */
12106
GDALGroupH
12107
GDALGroupSubsetDimensionFromSelection(GDALGroupH hGroup,
12108
                                      const char *pszSelection,
12109
                                      CPL_UNUSED CSLConstList papszOptions)
12110
0
{
12111
0
    VALIDATE_POINTER1(hGroup, __func__, nullptr);
12112
0
    VALIDATE_POINTER1(pszSelection, __func__, nullptr);
12113
0
    auto hNewGroup = hGroup->m_poImpl->SubsetDimensionFromSelection(
12114
0
        std::string(pszSelection));
12115
0
    if (!hNewGroup)
12116
0
        return nullptr;
12117
0
    return new GDALGroupHS(hNewGroup);
12118
0
}
12119
12120
/************************************************************************/
12121
/*                        GDALMDArrayRelease()                          */
12122
/************************************************************************/
12123
12124
/** Release the GDAL in-memory object associated with a GDALMDArray.
12125
 *
12126
 * Note: when applied on a object coming from a driver, this does not
12127
 * destroy the object in the file, database, etc...
12128
 */
12129
void GDALMDArrayRelease(GDALMDArrayH hMDArray)
12130
0
{
12131
0
    delete hMDArray;
12132
0
}
12133
12134
/************************************************************************/
12135
/*                        GDALMDArrayGetName()                          */
12136
/************************************************************************/
12137
12138
/** Return array name.
12139
 *
12140
 * This is the same as the C++ method GDALMDArray::GetName()
12141
 */
12142
const char *GDALMDArrayGetName(GDALMDArrayH hArray)
12143
0
{
12144
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12145
0
    return hArray->m_poImpl->GetName().c_str();
12146
0
}
12147
12148
/************************************************************************/
12149
/*                    GDALMDArrayGetFullName()                          */
12150
/************************************************************************/
12151
12152
/** Return array full name.
12153
 *
12154
 * This is the same as the C++ method GDALMDArray::GetFullName()
12155
 */
12156
const char *GDALMDArrayGetFullName(GDALMDArrayH hArray)
12157
0
{
12158
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12159
0
    return hArray->m_poImpl->GetFullName().c_str();
12160
0
}
12161
12162
/************************************************************************/
12163
/*                        GDALMDArrayGetName()                          */
12164
/************************************************************************/
12165
12166
/** Return the total number of values in the array.
12167
 *
12168
 * This is the same as the C++ method
12169
 * GDALAbstractMDArray::GetTotalElementsCount()
12170
 */
12171
GUInt64 GDALMDArrayGetTotalElementsCount(GDALMDArrayH hArray)
12172
0
{
12173
0
    VALIDATE_POINTER1(hArray, __func__, 0);
12174
0
    return hArray->m_poImpl->GetTotalElementsCount();
12175
0
}
12176
12177
/************************************************************************/
12178
/*                        GDALMDArrayGetDimensionCount()                */
12179
/************************************************************************/
12180
12181
/** Return the number of dimensions.
12182
 *
12183
 * This is the same as the C++ method GDALAbstractMDArray::GetDimensionCount()
12184
 */
12185
size_t GDALMDArrayGetDimensionCount(GDALMDArrayH hArray)
12186
0
{
12187
0
    VALIDATE_POINTER1(hArray, __func__, 0);
12188
0
    return hArray->m_poImpl->GetDimensionCount();
12189
0
}
12190
12191
/************************************************************************/
12192
/*                        GDALMDArrayGetDimensions()                    */
12193
/************************************************************************/
12194
12195
/** Return the dimensions of the array
12196
 *
12197
 * The returned array must be freed with GDALReleaseDimensions(). If only the
12198
 * array itself needs to be freed, CPLFree() should be called (and
12199
 * GDALDimensionRelease() on individual array members).
12200
 *
12201
 * This is the same as the C++ method GDALAbstractMDArray::GetDimensions()
12202
 *
12203
 * @param hArray Array.
12204
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
12205
 *
12206
 * @return an array of *pnCount dimensions.
12207
 */
12208
GDALDimensionH *GDALMDArrayGetDimensions(GDALMDArrayH hArray, size_t *pnCount)
12209
0
{
12210
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12211
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
12212
0
    const auto &dims(hArray->m_poImpl->GetDimensions());
12213
0
    auto ret = static_cast<GDALDimensionH *>(
12214
0
        CPLMalloc(sizeof(GDALDimensionH) * dims.size()));
12215
0
    for (size_t i = 0; i < dims.size(); i++)
12216
0
    {
12217
0
        ret[i] = new GDALDimensionHS(dims[i]);
12218
0
    }
12219
0
    *pnCount = dims.size();
12220
0
    return ret;
12221
0
}
12222
12223
/************************************************************************/
12224
/*                        GDALReleaseDimensions()                       */
12225
/************************************************************************/
12226
12227
/** Free the return of GDALGroupGetDimensions() or GDALMDArrayGetDimensions()
12228
 *
12229
 * @param dims return pointer of above methods
12230
 * @param nCount *pnCount value returned by above methods
12231
 */
12232
void GDALReleaseDimensions(GDALDimensionH *dims, size_t nCount)
12233
0
{
12234
0
    for (size_t i = 0; i < nCount; i++)
12235
0
    {
12236
0
        delete dims[i];
12237
0
    }
12238
0
    CPLFree(dims);
12239
0
}
12240
12241
/************************************************************************/
12242
/*                        GDALMDArrayGetDataType()                     */
12243
/************************************************************************/
12244
12245
/** Return the data type
12246
 *
12247
 * The return must be freed with GDALExtendedDataTypeRelease().
12248
 */
12249
GDALExtendedDataTypeH GDALMDArrayGetDataType(GDALMDArrayH hArray)
12250
0
{
12251
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12252
0
    return new GDALExtendedDataTypeHS(
12253
0
        new GDALExtendedDataType(hArray->m_poImpl->GetDataType()));
12254
0
}
12255
12256
/************************************************************************/
12257
/*                          GDALMDArrayRead()                           */
12258
/************************************************************************/
12259
12260
/** Read part or totality of a multidimensional array.
12261
 *
12262
 * This is the same as the C++ method GDALAbstractMDArray::Read()
12263
 *
12264
 * @return TRUE in case of success.
12265
 */
12266
int GDALMDArrayRead(GDALMDArrayH hArray, const GUInt64 *arrayStartIdx,
12267
                    const size_t *count, const GInt64 *arrayStep,
12268
                    const GPtrDiff_t *bufferStride,
12269
                    GDALExtendedDataTypeH bufferDataType, void *pDstBuffer,
12270
                    const void *pDstBufferAllocStart,
12271
                    size_t nDstBufferAllocSize)
12272
0
{
12273
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12274
0
    if ((arrayStartIdx == nullptr || count == nullptr) &&
12275
0
        hArray->m_poImpl->GetDimensionCount() > 0)
12276
0
    {
12277
0
        VALIDATE_POINTER1(arrayStartIdx, __func__, FALSE);
12278
0
        VALIDATE_POINTER1(count, __func__, FALSE);
12279
0
    }
12280
0
    VALIDATE_POINTER1(bufferDataType, __func__, FALSE);
12281
0
    VALIDATE_POINTER1(pDstBuffer, __func__, FALSE);
12282
0
    return hArray->m_poImpl->Read(arrayStartIdx, count, arrayStep, bufferStride,
12283
0
                                  *(bufferDataType->m_poImpl), pDstBuffer,
12284
0
                                  pDstBufferAllocStart, nDstBufferAllocSize);
12285
0
}
12286
12287
/************************************************************************/
12288
/*                          GDALMDArrayWrite()                           */
12289
/************************************************************************/
12290
12291
/** Write part or totality of a multidimensional array.
12292
 *
12293
 * This is the same as the C++ method GDALAbstractMDArray::Write()
12294
 *
12295
 * @return TRUE in case of success.
12296
 */
12297
int GDALMDArrayWrite(GDALMDArrayH hArray, const GUInt64 *arrayStartIdx,
12298
                     const size_t *count, const GInt64 *arrayStep,
12299
                     const GPtrDiff_t *bufferStride,
12300
                     GDALExtendedDataTypeH bufferDataType,
12301
                     const void *pSrcBuffer, const void *pSrcBufferAllocStart,
12302
                     size_t nSrcBufferAllocSize)
12303
0
{
12304
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12305
0
    if ((arrayStartIdx == nullptr || count == nullptr) &&
12306
0
        hArray->m_poImpl->GetDimensionCount() > 0)
12307
0
    {
12308
0
        VALIDATE_POINTER1(arrayStartIdx, __func__, FALSE);
12309
0
        VALIDATE_POINTER1(count, __func__, FALSE);
12310
0
    }
12311
0
    VALIDATE_POINTER1(bufferDataType, __func__, FALSE);
12312
0
    VALIDATE_POINTER1(pSrcBuffer, __func__, FALSE);
12313
0
    return hArray->m_poImpl->Write(arrayStartIdx, count, arrayStep,
12314
0
                                   bufferStride, *(bufferDataType->m_poImpl),
12315
0
                                   pSrcBuffer, pSrcBufferAllocStart,
12316
0
                                   nSrcBufferAllocSize);
12317
0
}
12318
12319
/************************************************************************/
12320
/*                       GDALMDArrayAdviseRead()                        */
12321
/************************************************************************/
12322
12323
/** Advise driver of upcoming read requests.
12324
 *
12325
 * This is the same as the C++ method GDALMDArray::AdviseRead()
12326
 *
12327
 * @return TRUE in case of success.
12328
 *
12329
 * @since GDAL 3.2
12330
 */
12331
int GDALMDArrayAdviseRead(GDALMDArrayH hArray, const GUInt64 *arrayStartIdx,
12332
                          const size_t *count)
12333
0
{
12334
0
    return GDALMDArrayAdviseReadEx(hArray, arrayStartIdx, count, nullptr);
12335
0
}
12336
12337
/************************************************************************/
12338
/*                      GDALMDArrayAdviseReadEx()                       */
12339
/************************************************************************/
12340
12341
/** Advise driver of upcoming read requests.
12342
 *
12343
 * This is the same as the C++ method GDALMDArray::AdviseRead()
12344
 *
12345
 * @return TRUE in case of success.
12346
 *
12347
 * @since GDAL 3.4
12348
 */
12349
int GDALMDArrayAdviseReadEx(GDALMDArrayH hArray, const GUInt64 *arrayStartIdx,
12350
                            const size_t *count, CSLConstList papszOptions)
12351
0
{
12352
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12353
0
    return hArray->m_poImpl->AdviseRead(arrayStartIdx, count, papszOptions);
12354
0
}
12355
12356
/************************************************************************/
12357
/*                         GDALMDArrayGetAttribute()                    */
12358
/************************************************************************/
12359
12360
/** Return an attribute by its name.
12361
 *
12362
 * This is the same as the C++ method GDALIHasAttribute::GetAttribute()
12363
 *
12364
 * The returned attribute must be freed with GDALAttributeRelease().
12365
 */
12366
GDALAttributeH GDALMDArrayGetAttribute(GDALMDArrayH hArray, const char *pszName)
12367
0
{
12368
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12369
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
12370
0
    auto attr = hArray->m_poImpl->GetAttribute(std::string(pszName));
12371
0
    if (attr)
12372
0
        return new GDALAttributeHS(attr);
12373
0
    return nullptr;
12374
0
}
12375
12376
/************************************************************************/
12377
/*                        GDALMDArrayGetAttributes()                    */
12378
/************************************************************************/
12379
12380
/** Return the list of attributes contained in this array.
12381
 *
12382
 * The returned array must be freed with GDALReleaseAttributes(). If only the
12383
 * array itself needs to be freed, CPLFree() should be called (and
12384
 * GDALAttributeRelease() on individual array members).
12385
 *
12386
 * This is the same as the C++ method GDALMDArray::GetAttributes().
12387
 *
12388
 * @param hArray Array.
12389
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
12390
 * @param papszOptions Driver specific options determining how attributes
12391
 * should be retrieved. Pass nullptr for default behavior.
12392
 *
12393
 * @return an array of *pnCount attributes.
12394
 */
12395
GDALAttributeH *GDALMDArrayGetAttributes(GDALMDArrayH hArray, size_t *pnCount,
12396
                                         CSLConstList papszOptions)
12397
0
{
12398
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12399
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
12400
0
    auto attrs = hArray->m_poImpl->GetAttributes(papszOptions);
12401
0
    auto ret = static_cast<GDALAttributeH *>(
12402
0
        CPLMalloc(sizeof(GDALAttributeH) * attrs.size()));
12403
0
    for (size_t i = 0; i < attrs.size(); i++)
12404
0
    {
12405
0
        ret[i] = new GDALAttributeHS(attrs[i]);
12406
0
    }
12407
0
    *pnCount = attrs.size();
12408
0
    return ret;
12409
0
}
12410
12411
/************************************************************************/
12412
/*                       GDALMDArrayCreateAttribute()                   */
12413
/************************************************************************/
12414
12415
/** Create a attribute within an array.
12416
 *
12417
 * This is the same as the C++ method GDALMDArray::CreateAttribute().
12418
 *
12419
 * @return the attribute, to be freed with GDALAttributeRelease(), or nullptr.
12420
 */
12421
GDALAttributeH GDALMDArrayCreateAttribute(GDALMDArrayH hArray,
12422
                                          const char *pszName,
12423
                                          size_t nDimensions,
12424
                                          const GUInt64 *panDimensions,
12425
                                          GDALExtendedDataTypeH hEDT,
12426
                                          CSLConstList papszOptions)
12427
0
{
12428
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12429
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
12430
0
    VALIDATE_POINTER1(hEDT, __func__, nullptr);
12431
0
    std::vector<GUInt64> dims;
12432
0
    dims.reserve(nDimensions);
12433
0
    for (size_t i = 0; i < nDimensions; i++)
12434
0
        dims.push_back(panDimensions[i]);
12435
0
    auto ret = hArray->m_poImpl->CreateAttribute(
12436
0
        std::string(pszName), dims, *(hEDT->m_poImpl), papszOptions);
12437
0
    if (!ret)
12438
0
        return nullptr;
12439
0
    return new GDALAttributeHS(ret);
12440
0
}
12441
12442
/************************************************************************/
12443
/*                       GDALMDArrayDeleteAttribute()                   */
12444
/************************************************************************/
12445
12446
/** Delete an attribute from an array.
12447
 *
12448
 * After this call, if a previously obtained instance of the deleted object
12449
 * is still alive, no method other than for freeing it should be invoked.
12450
 *
12451
 * This is the same as the C++ method GDALMDArray::DeleteAttribute().
12452
 *
12453
 * @return true in case of success.
12454
 * @since GDAL 3.8
12455
 */
12456
bool GDALMDArrayDeleteAttribute(GDALMDArrayH hArray, const char *pszName,
12457
                                CSLConstList papszOptions)
12458
0
{
12459
0
    VALIDATE_POINTER1(hArray, __func__, false);
12460
0
    VALIDATE_POINTER1(pszName, __func__, false);
12461
0
    return hArray->m_poImpl->DeleteAttribute(std::string(pszName),
12462
0
                                             papszOptions);
12463
0
}
12464
12465
/************************************************************************/
12466
/*                       GDALMDArrayGetRawNoDataValue()                 */
12467
/************************************************************************/
12468
12469
/** Return the nodata value as a "raw" value.
12470
 *
12471
 * The value returned might be nullptr in case of no nodata value. When
12472
 * a nodata value is registered, a non-nullptr will be returned whose size in
12473
 * bytes is GetDataType().GetSize().
12474
 *
12475
 * The returned value should not be modified or freed.
12476
 *
12477
 * This is the same as the ++ method GDALMDArray::GetRawNoDataValue().
12478
 *
12479
 * @return nullptr or a pointer to GetDataType().GetSize() bytes.
12480
 */
12481
const void *GDALMDArrayGetRawNoDataValue(GDALMDArrayH hArray)
12482
0
{
12483
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12484
0
    return hArray->m_poImpl->GetRawNoDataValue();
12485
0
}
12486
12487
/************************************************************************/
12488
/*                      GDALMDArrayGetNoDataValueAsDouble()             */
12489
/************************************************************************/
12490
12491
/** Return the nodata value as a double.
12492
 *
12493
 * The value returned might be nullptr in case of no nodata value. When
12494
 * a nodata value is registered, a non-nullptr will be returned whose size in
12495
 * bytes is GetDataType().GetSize().
12496
 *
12497
 * This is the same as the C++ method GDALMDArray::GetNoDataValueAsDouble().
12498
 *
12499
 * @param hArray Array handle.
12500
 * @param pbHasNoDataValue Pointer to a output boolean that will be set to true
12501
 * if a nodata value exists and can be converted to double. Might be nullptr.
12502
 *
12503
 * @return the nodata value as a double. A 0.0 value might also indicate the
12504
 * absence of a nodata value or an error in the conversion (*pbHasNoDataValue
12505
 * will be set to false then).
12506
 */
12507
double GDALMDArrayGetNoDataValueAsDouble(GDALMDArrayH hArray,
12508
                                         int *pbHasNoDataValue)
12509
0
{
12510
0
    VALIDATE_POINTER1(hArray, __func__, 0);
12511
0
    bool bHasNodataValue = false;
12512
0
    double ret = hArray->m_poImpl->GetNoDataValueAsDouble(&bHasNodataValue);
12513
0
    if (pbHasNoDataValue)
12514
0
        *pbHasNoDataValue = bHasNodataValue;
12515
0
    return ret;
12516
0
}
12517
12518
/************************************************************************/
12519
/*                      GDALMDArrayGetNoDataValueAsInt64()              */
12520
/************************************************************************/
12521
12522
/** Return the nodata value as a Int64.
12523
 *
12524
 * This is the same as the C++ method GDALMDArray::GetNoDataValueAsInt64().
12525
 *
12526
 * @param hArray Array handle.
12527
 * @param pbHasNoDataValue Pointer to a output boolean that will be set to true
12528
 * if a nodata value exists and can be converted to Int64. Might be nullptr.
12529
 *
12530
 * @return the nodata value as a Int64.
12531
 * @since GDAL 3.5
12532
 */
12533
int64_t GDALMDArrayGetNoDataValueAsInt64(GDALMDArrayH hArray,
12534
                                         int *pbHasNoDataValue)
12535
0
{
12536
0
    VALIDATE_POINTER1(hArray, __func__, 0);
12537
0
    bool bHasNodataValue = false;
12538
0
    const auto ret = hArray->m_poImpl->GetNoDataValueAsInt64(&bHasNodataValue);
12539
0
    if (pbHasNoDataValue)
12540
0
        *pbHasNoDataValue = bHasNodataValue;
12541
0
    return ret;
12542
0
}
12543
12544
/************************************************************************/
12545
/*                      GDALMDArrayGetNoDataValueAsUInt64()              */
12546
/************************************************************************/
12547
12548
/** Return the nodata value as a UInt64.
12549
 *
12550
 * This is the same as the C++ method GDALMDArray::GetNoDataValueAsInt64().
12551
 *
12552
 * @param hArray Array handle.
12553
 * @param pbHasNoDataValue Pointer to a output boolean that will be set to true
12554
 * if a nodata value exists and can be converted to UInt64. Might be nullptr.
12555
 *
12556
 * @return the nodata value as a UInt64.
12557
 * @since GDAL 3.5
12558
 */
12559
uint64_t GDALMDArrayGetNoDataValueAsUInt64(GDALMDArrayH hArray,
12560
                                           int *pbHasNoDataValue)
12561
0
{
12562
0
    VALIDATE_POINTER1(hArray, __func__, 0);
12563
0
    bool bHasNodataValue = false;
12564
0
    const auto ret = hArray->m_poImpl->GetNoDataValueAsUInt64(&bHasNodataValue);
12565
0
    if (pbHasNoDataValue)
12566
0
        *pbHasNoDataValue = bHasNodataValue;
12567
0
    return ret;
12568
0
}
12569
12570
/************************************************************************/
12571
/*                     GDALMDArraySetRawNoDataValue()                   */
12572
/************************************************************************/
12573
12574
/** Set the nodata value as a "raw" value.
12575
 *
12576
 * This is the same as the C++ method GDALMDArray::SetRawNoDataValue(const
12577
 * void*).
12578
 *
12579
 * @return TRUE in case of success.
12580
 */
12581
int GDALMDArraySetRawNoDataValue(GDALMDArrayH hArray, const void *pNoData)
12582
0
{
12583
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12584
0
    return hArray->m_poImpl->SetRawNoDataValue(pNoData);
12585
0
}
12586
12587
/************************************************************************/
12588
/*                   GDALMDArraySetNoDataValueAsDouble()                */
12589
/************************************************************************/
12590
12591
/** Set the nodata value as a double.
12592
 *
12593
 * If the natural data type of the attribute/array is not double, type
12594
 * conversion will occur to the type returned by GetDataType().
12595
 *
12596
 * This is the same as the C++ method GDALMDArray::SetNoDataValue(double).
12597
 *
12598
 * @return TRUE in case of success.
12599
 */
12600
int GDALMDArraySetNoDataValueAsDouble(GDALMDArrayH hArray, double dfNoDataValue)
12601
0
{
12602
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12603
0
    return hArray->m_poImpl->SetNoDataValue(dfNoDataValue);
12604
0
}
12605
12606
/************************************************************************/
12607
/*                   GDALMDArraySetNoDataValueAsInt64()                 */
12608
/************************************************************************/
12609
12610
/** Set the nodata value as a Int64.
12611
 *
12612
 * If the natural data type of the attribute/array is not Int64, type conversion
12613
 * will occur to the type returned by GetDataType().
12614
 *
12615
 * This is the same as the C++ method GDALMDArray::SetNoDataValue(int64_t).
12616
 *
12617
 * @return TRUE in case of success.
12618
 * @since GDAL 3.5
12619
 */
12620
int GDALMDArraySetNoDataValueAsInt64(GDALMDArrayH hArray, int64_t nNoDataValue)
12621
0
{
12622
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12623
0
    return hArray->m_poImpl->SetNoDataValue(nNoDataValue);
12624
0
}
12625
12626
/************************************************************************/
12627
/*                   GDALMDArraySetNoDataValueAsUInt64()                */
12628
/************************************************************************/
12629
12630
/** Set the nodata value as a UInt64.
12631
 *
12632
 * If the natural data type of the attribute/array is not UInt64, type
12633
 * conversion will occur to the type returned by GetDataType().
12634
 *
12635
 * This is the same as the C++ method GDALMDArray::SetNoDataValue(uint64_t).
12636
 *
12637
 * @return TRUE in case of success.
12638
 * @since GDAL 3.5
12639
 */
12640
int GDALMDArraySetNoDataValueAsUInt64(GDALMDArrayH hArray,
12641
                                      uint64_t nNoDataValue)
12642
0
{
12643
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12644
0
    return hArray->m_poImpl->SetNoDataValue(nNoDataValue);
12645
0
}
12646
12647
/************************************************************************/
12648
/*                        GDALMDArrayResize()                           */
12649
/************************************************************************/
12650
12651
/** Resize an array to new dimensions.
12652
 *
12653
 * Not all drivers may allow this operation, and with restrictions (e.g.
12654
 * for netCDF, this is limited to growing of "unlimited" dimensions)
12655
 *
12656
 * Resizing a dimension used in other arrays will cause those other arrays
12657
 * to be resized.
12658
 *
12659
 * This is the same as the C++ method GDALMDArray::Resize().
12660
 *
12661
 * @param hArray Array.
12662
 * @param panNewDimSizes Array of GetDimensionCount() values containing the
12663
 *                       new size of each indexing dimension.
12664
 * @param papszOptions Options. (Driver specific)
12665
 * @return true in case of success.
12666
 * @since GDAL 3.7
12667
 */
12668
bool GDALMDArrayResize(GDALMDArrayH hArray, const GUInt64 *panNewDimSizes,
12669
                       CSLConstList papszOptions)
12670
0
{
12671
0
    VALIDATE_POINTER1(hArray, __func__, false);
12672
0
    VALIDATE_POINTER1(panNewDimSizes, __func__, false);
12673
0
    std::vector<GUInt64> anNewDimSizes(hArray->m_poImpl->GetDimensionCount());
12674
0
    for (size_t i = 0; i < anNewDimSizes.size(); ++i)
12675
0
    {
12676
0
        anNewDimSizes[i] = panNewDimSizes[i];
12677
0
    }
12678
0
    return hArray->m_poImpl->Resize(anNewDimSizes, papszOptions);
12679
0
}
12680
12681
/************************************************************************/
12682
/*                          GDALMDArraySetScale()                       */
12683
/************************************************************************/
12684
12685
/** Set the scale value to apply to raw values.
12686
 *
12687
 * unscaled_value = raw_value * GetScale() + GetOffset()
12688
 *
12689
 * This is the same as the C++ method GDALMDArray::SetScale().
12690
 *
12691
 * @return TRUE in case of success.
12692
 */
12693
int GDALMDArraySetScale(GDALMDArrayH hArray, double dfScale)
12694
0
{
12695
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12696
0
    return hArray->m_poImpl->SetScale(dfScale);
12697
0
}
12698
12699
/************************************************************************/
12700
/*                        GDALMDArraySetScaleEx()                       */
12701
/************************************************************************/
12702
12703
/** Set the scale value to apply to raw values.
12704
 *
12705
 * unscaled_value = raw_value * GetScale() + GetOffset()
12706
 *
12707
 * This is the same as the C++ method GDALMDArray::SetScale().
12708
 *
12709
 * @return TRUE in case of success.
12710
 * @since GDAL 3.3
12711
 */
12712
int GDALMDArraySetScaleEx(GDALMDArrayH hArray, double dfScale,
12713
                          GDALDataType eStorageType)
12714
0
{
12715
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12716
0
    return hArray->m_poImpl->SetScale(dfScale, eStorageType);
12717
0
}
12718
12719
/************************************************************************/
12720
/*                          GDALMDArraySetOffset()                       */
12721
/************************************************************************/
12722
12723
/** Set the scale value to apply to raw values.
12724
 *
12725
 * unscaled_value = raw_value * GetScale() + GetOffset()
12726
 *
12727
 * This is the same as the C++ method GDALMDArray::SetOffset().
12728
 *
12729
 * @return TRUE in case of success.
12730
 */
12731
int GDALMDArraySetOffset(GDALMDArrayH hArray, double dfOffset)
12732
0
{
12733
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12734
0
    return hArray->m_poImpl->SetOffset(dfOffset);
12735
0
}
12736
12737
/************************************************************************/
12738
/*                       GDALMDArraySetOffsetEx()                       */
12739
/************************************************************************/
12740
12741
/** Set the scale value to apply to raw values.
12742
 *
12743
 * unscaled_value = raw_value * GetOffset() + GetOffset()
12744
 *
12745
 * This is the same as the C++ method GDALMDArray::SetOffset().
12746
 *
12747
 * @return TRUE in case of success.
12748
 * @since GDAL 3.3
12749
 */
12750
int GDALMDArraySetOffsetEx(GDALMDArrayH hArray, double dfOffset,
12751
                           GDALDataType eStorageType)
12752
0
{
12753
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
12754
0
    return hArray->m_poImpl->SetOffset(dfOffset, eStorageType);
12755
0
}
12756
12757
/************************************************************************/
12758
/*                          GDALMDArrayGetScale()                       */
12759
/************************************************************************/
12760
12761
/** Get the scale value to apply to raw values.
12762
 *
12763
 * unscaled_value = raw_value * GetScale() + GetOffset()
12764
 *
12765
 * This is the same as the C++ method GDALMDArray::GetScale().
12766
 *
12767
 * @return the scale value
12768
 */
12769
double GDALMDArrayGetScale(GDALMDArrayH hArray, int *pbHasValue)
12770
0
{
12771
0
    VALIDATE_POINTER1(hArray, __func__, 0.0);
12772
0
    bool bHasValue = false;
12773
0
    double dfRet = hArray->m_poImpl->GetScale(&bHasValue);
12774
0
    if (pbHasValue)
12775
0
        *pbHasValue = bHasValue;
12776
0
    return dfRet;
12777
0
}
12778
12779
/************************************************************************/
12780
/*                        GDALMDArrayGetScaleEx()                       */
12781
/************************************************************************/
12782
12783
/** Get the scale value to apply to raw values.
12784
 *
12785
 * unscaled_value = raw_value * GetScale() + GetScale()
12786
 *
12787
 * This is the same as the C++ method GDALMDArray::GetScale().
12788
 *
12789
 * @return the scale value
12790
 * @since GDAL 3.3
12791
 */
12792
double GDALMDArrayGetScaleEx(GDALMDArrayH hArray, int *pbHasValue,
12793
                             GDALDataType *peStorageType)
12794
0
{
12795
0
    VALIDATE_POINTER1(hArray, __func__, 0.0);
12796
0
    bool bHasValue = false;
12797
0
    double dfRet = hArray->m_poImpl->GetScale(&bHasValue, peStorageType);
12798
0
    if (pbHasValue)
12799
0
        *pbHasValue = bHasValue;
12800
0
    return dfRet;
12801
0
}
12802
12803
/************************************************************************/
12804
/*                          GDALMDArrayGetOffset()                      */
12805
/************************************************************************/
12806
12807
/** Get the scale value to apply to raw values.
12808
 *
12809
 * unscaled_value = raw_value * GetScale() + GetOffset()
12810
 *
12811
 * This is the same as the C++ method GDALMDArray::GetOffset().
12812
 *
12813
 * @return the scale value
12814
 */
12815
double GDALMDArrayGetOffset(GDALMDArrayH hArray, int *pbHasValue)
12816
0
{
12817
0
    VALIDATE_POINTER1(hArray, __func__, 0.0);
12818
0
    bool bHasValue = false;
12819
0
    double dfRet = hArray->m_poImpl->GetOffset(&bHasValue);
12820
0
    if (pbHasValue)
12821
0
        *pbHasValue = bHasValue;
12822
0
    return dfRet;
12823
0
}
12824
12825
/************************************************************************/
12826
/*                        GDALMDArrayGetOffsetEx()                      */
12827
/************************************************************************/
12828
12829
/** Get the scale value to apply to raw values.
12830
 *
12831
 * unscaled_value = raw_value * GetScale() + GetOffset()
12832
 *
12833
 * This is the same as the C++ method GDALMDArray::GetOffset().
12834
 *
12835
 * @return the scale value
12836
 * @since GDAL 3.3
12837
 */
12838
double GDALMDArrayGetOffsetEx(GDALMDArrayH hArray, int *pbHasValue,
12839
                              GDALDataType *peStorageType)
12840
0
{
12841
0
    VALIDATE_POINTER1(hArray, __func__, 0.0);
12842
0
    bool bHasValue = false;
12843
0
    double dfRet = hArray->m_poImpl->GetOffset(&bHasValue, peStorageType);
12844
0
    if (pbHasValue)
12845
0
        *pbHasValue = bHasValue;
12846
0
    return dfRet;
12847
0
}
12848
12849
/************************************************************************/
12850
/*                      GDALMDArrayGetBlockSize()                       */
12851
/************************************************************************/
12852
12853
/** Return the "natural" block size of the array along all dimensions.
12854
 *
12855
 * Some drivers might organize the array in tiles/blocks and reading/writing
12856
 * aligned on those tile/block boundaries will be more efficient.
12857
 *
12858
 * The returned number of elements in the vector is the same as
12859
 * GetDimensionCount(). A value of 0 should be interpreted as no hint regarding
12860
 * the natural block size along the considered dimension.
12861
 * "Flat" arrays will typically return a vector of values set to 0.
12862
 *
12863
 * The default implementation will return a vector of values set to 0.
12864
 *
12865
 * This method is used by GetProcessingChunkSize().
12866
 *
12867
 * Pedantic note: the returned type is GUInt64, so in the highly unlikeley
12868
 * theoretical case of a 32-bit platform, this might exceed its size_t
12869
 * allocation capabilities.
12870
 *
12871
 * This is the same as the C++ method GDALAbstractMDArray::GetBlockSize().
12872
 *
12873
 * @return the block size, in number of elements along each dimension.
12874
 */
12875
GUInt64 *GDALMDArrayGetBlockSize(GDALMDArrayH hArray, size_t *pnCount)
12876
0
{
12877
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12878
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
12879
0
    auto res = hArray->m_poImpl->GetBlockSize();
12880
0
    auto ret = static_cast<GUInt64 *>(CPLMalloc(sizeof(GUInt64) * res.size()));
12881
0
    for (size_t i = 0; i < res.size(); i++)
12882
0
    {
12883
0
        ret[i] = res[i];
12884
0
    }
12885
0
    *pnCount = res.size();
12886
0
    return ret;
12887
0
}
12888
12889
/***********************************************************************/
12890
/*                   GDALMDArrayGetProcessingChunkSize()               */
12891
/************************************************************************/
12892
12893
/** \brief Return an optimal chunk size for read/write operations, given the
12894
 * natural block size and memory constraints specified.
12895
 *
12896
 * This method will use GetBlockSize() to define a chunk whose dimensions are
12897
 * multiple of those returned by GetBlockSize() (unless the block define by
12898
 * GetBlockSize() is larger than nMaxChunkMemory, in which case it will be
12899
 * returned by this method).
12900
 *
12901
 * This is the same as the C++ method
12902
 * GDALAbstractMDArray::GetProcessingChunkSize().
12903
 *
12904
 * @param hArray Array.
12905
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
12906
 * @param nMaxChunkMemory Maximum amount of memory, in bytes, to use for the
12907
 * chunk.
12908
 *
12909
 * @return the chunk size, in number of elements along each dimension.
12910
 */
12911
12912
size_t *GDALMDArrayGetProcessingChunkSize(GDALMDArrayH hArray, size_t *pnCount,
12913
                                          size_t nMaxChunkMemory)
12914
0
{
12915
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12916
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
12917
0
    auto res = hArray->m_poImpl->GetProcessingChunkSize(nMaxChunkMemory);
12918
0
    auto ret = static_cast<size_t *>(CPLMalloc(sizeof(size_t) * res.size()));
12919
0
    for (size_t i = 0; i < res.size(); i++)
12920
0
    {
12921
0
        ret[i] = res[i];
12922
0
    }
12923
0
    *pnCount = res.size();
12924
0
    return ret;
12925
0
}
12926
12927
/************************************************************************/
12928
/*                     GDALMDArrayGetStructuralInfo()                   */
12929
/************************************************************************/
12930
12931
/** Return structural information on the array.
12932
 *
12933
 * This may be the compression, etc..
12934
 *
12935
 * The return value should not be freed and is valid until GDALMDArray is
12936
 * released or this function called again.
12937
 *
12938
 * This is the same as the C++ method GDALMDArray::GetStructuralInfo().
12939
 */
12940
CSLConstList GDALMDArrayGetStructuralInfo(GDALMDArrayH hArray)
12941
0
{
12942
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12943
0
    return hArray->m_poImpl->GetStructuralInfo();
12944
0
}
12945
12946
/************************************************************************/
12947
/*                        GDALMDArrayGetView()                          */
12948
/************************************************************************/
12949
12950
/** Return a view of the array using slicing or field access.
12951
 *
12952
 * The returned object should be released with GDALMDArrayRelease().
12953
 *
12954
 * This is the same as the C++ method GDALMDArray::GetView().
12955
 */
12956
GDALMDArrayH GDALMDArrayGetView(GDALMDArrayH hArray, const char *pszViewExpr)
12957
0
{
12958
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12959
0
    VALIDATE_POINTER1(pszViewExpr, __func__, nullptr);
12960
0
    auto sliced = hArray->m_poImpl->GetView(std::string(pszViewExpr));
12961
0
    if (!sliced)
12962
0
        return nullptr;
12963
0
    return new GDALMDArrayHS(sliced);
12964
0
}
12965
12966
/************************************************************************/
12967
/*                       GDALMDArrayTranspose()                         */
12968
/************************************************************************/
12969
12970
/** Return a view of the array whose axis have been reordered.
12971
 *
12972
 * The returned object should be released with GDALMDArrayRelease().
12973
 *
12974
 * This is the same as the C++ method GDALMDArray::Transpose().
12975
 */
12976
GDALMDArrayH GDALMDArrayTranspose(GDALMDArrayH hArray, size_t nNewAxisCount,
12977
                                  const int *panMapNewAxisToOldAxis)
12978
0
{
12979
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
12980
0
    std::vector<int> anMapNewAxisToOldAxis(nNewAxisCount);
12981
0
    if (nNewAxisCount)
12982
0
    {
12983
0
        memcpy(&anMapNewAxisToOldAxis[0], panMapNewAxisToOldAxis,
12984
0
               nNewAxisCount * sizeof(int));
12985
0
    }
12986
0
    auto reordered = hArray->m_poImpl->Transpose(anMapNewAxisToOldAxis);
12987
0
    if (!reordered)
12988
0
        return nullptr;
12989
0
    return new GDALMDArrayHS(reordered);
12990
0
}
12991
12992
/************************************************************************/
12993
/*                      GDALMDArrayGetUnscaled()                        */
12994
/************************************************************************/
12995
12996
/** Return an array that is the unscaled version of the current one.
12997
 *
12998
 * That is each value of the unscaled array will be
12999
 * unscaled_value = raw_value * GetScale() + GetOffset()
13000
 *
13001
 * Starting with GDAL 3.3, the Write() method is implemented and will convert
13002
 * from unscaled values to raw values.
13003
 *
13004
 * The returned object should be released with GDALMDArrayRelease().
13005
 *
13006
 * This is the same as the C++ method GDALMDArray::GetUnscaled().
13007
 */
13008
GDALMDArrayH GDALMDArrayGetUnscaled(GDALMDArrayH hArray)
13009
0
{
13010
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
13011
0
    auto unscaled = hArray->m_poImpl->GetUnscaled();
13012
0
    if (!unscaled)
13013
0
        return nullptr;
13014
0
    return new GDALMDArrayHS(unscaled);
13015
0
}
13016
13017
/************************************************************************/
13018
/*                          GDALMDArrayGetMask()                         */
13019
/************************************************************************/
13020
13021
/** Return an array that is a mask for the current array
13022
 *
13023
 * This array will be of type Byte, with values set to 0 to indicate invalid
13024
 * pixels of the current array, and values set to 1 to indicate valid pixels.
13025
 *
13026
 * The returned object should be released with GDALMDArrayRelease().
13027
 *
13028
 * This is the same as the C++ method GDALMDArray::GetMask().
13029
 */
13030
GDALMDArrayH GDALMDArrayGetMask(GDALMDArrayH hArray, CSLConstList papszOptions)
13031
0
{
13032
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
13033
0
    auto unscaled = hArray->m_poImpl->GetMask(papszOptions);
13034
0
    if (!unscaled)
13035
0
        return nullptr;
13036
0
    return new GDALMDArrayHS(unscaled);
13037
0
}
13038
13039
/************************************************************************/
13040
/*                   GDALMDArrayGetResampled()                          */
13041
/************************************************************************/
13042
13043
/** Return an array that is a resampled / reprojected view of the current array
13044
 *
13045
 * This is the same as the C++ method GDALMDArray::GetResampled().
13046
 *
13047
 * Currently this method can only resample along the last 2 dimensions, unless
13048
 * orthorectifying a NASA EMIT dataset.
13049
 *
13050
 * The returned object should be released with GDALMDArrayRelease().
13051
 *
13052
 * @since 3.4
13053
 */
13054
GDALMDArrayH GDALMDArrayGetResampled(GDALMDArrayH hArray, size_t nNewDimCount,
13055
                                     const GDALDimensionH *pahNewDims,
13056
                                     GDALRIOResampleAlg resampleAlg,
13057
                                     OGRSpatialReferenceH hTargetSRS,
13058
                                     CSLConstList papszOptions)
13059
0
{
13060
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
13061
0
    VALIDATE_POINTER1(pahNewDims, __func__, nullptr);
13062
0
    std::vector<std::shared_ptr<GDALDimension>> apoNewDims(nNewDimCount);
13063
0
    for (size_t i = 0; i < nNewDimCount; ++i)
13064
0
    {
13065
0
        if (pahNewDims[i])
13066
0
            apoNewDims[i] = pahNewDims[i]->m_poImpl;
13067
0
    }
13068
0
    auto poNewArray = hArray->m_poImpl->GetResampled(
13069
0
        apoNewDims, resampleAlg, OGRSpatialReference::FromHandle(hTargetSRS),
13070
0
        papszOptions);
13071
0
    if (!poNewArray)
13072
0
        return nullptr;
13073
0
    return new GDALMDArrayHS(poNewArray);
13074
0
}
13075
13076
/************************************************************************/
13077
/*                      GDALMDArraySetUnit()                            */
13078
/************************************************************************/
13079
13080
/** Set the variable unit.
13081
 *
13082
 * Values should conform as much as possible with those allowed by
13083
 * the NetCDF CF conventions:
13084
 * http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.html#units
13085
 * but others might be returned.
13086
 *
13087
 * Few examples are "meter", "degrees", "second", ...
13088
 * Empty value means unknown.
13089
 *
13090
 * This is the same as the C function GDALMDArraySetUnit()
13091
 *
13092
 * @param hArray array.
13093
 * @param pszUnit unit name.
13094
 * @return TRUE in case of success.
13095
 */
13096
int GDALMDArraySetUnit(GDALMDArrayH hArray, const char *pszUnit)
13097
0
{
13098
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
13099
0
    return hArray->m_poImpl->SetUnit(pszUnit ? pszUnit : "");
13100
0
}
13101
13102
/************************************************************************/
13103
/*                      GDALMDArrayGetUnit()                            */
13104
/************************************************************************/
13105
13106
/** Return the array unit.
13107
 *
13108
 * Values should conform as much as possible with those allowed by
13109
 * the NetCDF CF conventions:
13110
 * http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.html#units
13111
 * but others might be returned.
13112
 *
13113
 * Few examples are "meter", "degrees", "second", ...
13114
 * Empty value means unknown.
13115
 *
13116
 * The return value should not be freed and is valid until GDALMDArray is
13117
 * released or this function called again.
13118
 *
13119
 * This is the same as the C++ method GDALMDArray::GetUnit().
13120
 */
13121
const char *GDALMDArrayGetUnit(GDALMDArrayH hArray)
13122
0
{
13123
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
13124
0
    return hArray->m_poImpl->GetUnit().c_str();
13125
0
}
13126
13127
/************************************************************************/
13128
/*                      GDALMDArrayGetSpatialRef()                      */
13129
/************************************************************************/
13130
13131
/** Assign a spatial reference system object to the array.
13132
 *
13133
 * This is the same as the C++ method GDALMDArray::SetSpatialRef().
13134
 * @return TRUE in case of success.
13135
 */
13136
int GDALMDArraySetSpatialRef(GDALMDArrayH hArray, OGRSpatialReferenceH hSRS)
13137
0
{
13138
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
13139
0
    return hArray->m_poImpl->SetSpatialRef(
13140
0
        OGRSpatialReference::FromHandle(hSRS));
13141
0
}
13142
13143
/************************************************************************/
13144
/*                      GDALMDArrayGetSpatialRef()                      */
13145
/************************************************************************/
13146
13147
/** Return the spatial reference system object associated with the array.
13148
 *
13149
 * This is the same as the C++ method GDALMDArray::GetSpatialRef().
13150
 *
13151
 * The returned object must be freed with OSRDestroySpatialReference().
13152
 */
13153
OGRSpatialReferenceH GDALMDArrayGetSpatialRef(GDALMDArrayH hArray)
13154
0
{
13155
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
13156
0
    auto poSRS = hArray->m_poImpl->GetSpatialRef();
13157
0
    return poSRS ? OGRSpatialReference::ToHandle(poSRS->Clone()) : nullptr;
13158
0
}
13159
13160
/************************************************************************/
13161
/*                      GDALMDArrayGetStatistics()                      */
13162
/************************************************************************/
13163
13164
/**
13165
 * \brief Fetch statistics.
13166
 *
13167
 * This is the same as the C++ method GDALMDArray::GetStatistics().
13168
 *
13169
 * @since GDAL 3.2
13170
 */
13171
13172
CPLErr GDALMDArrayGetStatistics(GDALMDArrayH hArray, GDALDatasetH /*hDS*/,
13173
                                int bApproxOK, int bForce, double *pdfMin,
13174
                                double *pdfMax, double *pdfMean,
13175
                                double *pdfStdDev, GUInt64 *pnValidCount,
13176
                                GDALProgressFunc pfnProgress,
13177
                                void *pProgressData)
13178
0
{
13179
0
    VALIDATE_POINTER1(hArray, __func__, CE_Failure);
13180
0
    return hArray->m_poImpl->GetStatistics(
13181
0
        CPL_TO_BOOL(bApproxOK), CPL_TO_BOOL(bForce), pdfMin, pdfMax, pdfMean,
13182
0
        pdfStdDev, pnValidCount, pfnProgress, pProgressData);
13183
0
}
13184
13185
/************************************************************************/
13186
/*                      GDALMDArrayComputeStatistics()                  */
13187
/************************************************************************/
13188
13189
/**
13190
 * \brief Compute statistics.
13191
 *
13192
 * This is the same as the C++ method GDALMDArray::ComputeStatistics().
13193
 *
13194
 * @since GDAL 3.2
13195
 * @see GDALMDArrayComputeStatisticsEx()
13196
 */
13197
13198
int GDALMDArrayComputeStatistics(GDALMDArrayH hArray, GDALDatasetH /* hDS */,
13199
                                 int bApproxOK, double *pdfMin, double *pdfMax,
13200
                                 double *pdfMean, double *pdfStdDev,
13201
                                 GUInt64 *pnValidCount,
13202
                                 GDALProgressFunc pfnProgress,
13203
                                 void *pProgressData)
13204
0
{
13205
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
13206
0
    return hArray->m_poImpl->ComputeStatistics(
13207
0
        CPL_TO_BOOL(bApproxOK), pdfMin, pdfMax, pdfMean, pdfStdDev,
13208
0
        pnValidCount, pfnProgress, pProgressData, nullptr);
13209
0
}
13210
13211
/************************************************************************/
13212
/*                     GDALMDArrayComputeStatisticsEx()                 */
13213
/************************************************************************/
13214
13215
/**
13216
 * \brief Compute statistics.
13217
 *
13218
 * Same as GDALMDArrayComputeStatistics() with extra papszOptions argument.
13219
 *
13220
 * This is the same as the C++ method GDALMDArray::ComputeStatistics().
13221
 *
13222
 * @since GDAL 3.8
13223
 */
13224
13225
int GDALMDArrayComputeStatisticsEx(GDALMDArrayH hArray, GDALDatasetH /* hDS */,
13226
                                   int bApproxOK, double *pdfMin,
13227
                                   double *pdfMax, double *pdfMean,
13228
                                   double *pdfStdDev, GUInt64 *pnValidCount,
13229
                                   GDALProgressFunc pfnProgress,
13230
                                   void *pProgressData,
13231
                                   CSLConstList papszOptions)
13232
0
{
13233
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
13234
0
    return hArray->m_poImpl->ComputeStatistics(
13235
0
        CPL_TO_BOOL(bApproxOK), pdfMin, pdfMax, pdfMean, pdfStdDev,
13236
0
        pnValidCount, pfnProgress, pProgressData, papszOptions);
13237
0
}
13238
13239
/************************************************************************/
13240
/*                 GDALMDArrayGetCoordinateVariables()                  */
13241
/************************************************************************/
13242
13243
/** Return coordinate variables.
13244
 *
13245
 * The returned array must be freed with GDALReleaseArrays(). If only the array
13246
 * itself needs to be freed, CPLFree() should be called (and
13247
 * GDALMDArrayRelease() on individual array members).
13248
 *
13249
 * This is the same as the C++ method GDALMDArray::GetCoordinateVariables()
13250
 *
13251
 * @param hArray Array.
13252
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
13253
 *
13254
 * @return an array of *pnCount arrays.
13255
 * @since 3.4
13256
 */
13257
GDALMDArrayH *GDALMDArrayGetCoordinateVariables(GDALMDArrayH hArray,
13258
                                                size_t *pnCount)
13259
0
{
13260
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
13261
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
13262
0
    const auto coordinates(hArray->m_poImpl->GetCoordinateVariables());
13263
0
    auto ret = static_cast<GDALMDArrayH *>(
13264
0
        CPLMalloc(sizeof(GDALMDArrayH) * coordinates.size()));
13265
0
    for (size_t i = 0; i < coordinates.size(); i++)
13266
0
    {
13267
0
        ret[i] = new GDALMDArrayHS(coordinates[i]);
13268
0
    }
13269
0
    *pnCount = coordinates.size();
13270
0
    return ret;
13271
0
}
13272
13273
/************************************************************************/
13274
/*                     GDALMDArrayGetGridded()                          */
13275
/************************************************************************/
13276
13277
/** Return a gridded array from scattered point data, that is from an array
13278
 * whose last dimension is the indexing variable of X and Y arrays.
13279
 *
13280
 * The returned object should be released with GDALMDArrayRelease().
13281
 *
13282
 * This is the same as the C++ method GDALMDArray::GetGridded().
13283
 *
13284
 * @since GDAL 3.7
13285
 */
13286
GDALMDArrayH GDALMDArrayGetGridded(GDALMDArrayH hArray,
13287
                                   const char *pszGridOptions,
13288
                                   GDALMDArrayH hXArray, GDALMDArrayH hYArray,
13289
                                   CSLConstList papszOptions)
13290
0
{
13291
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
13292
0
    VALIDATE_POINTER1(pszGridOptions, __func__, nullptr);
13293
0
    auto gridded = hArray->m_poImpl->GetGridded(
13294
0
        pszGridOptions, hXArray ? hXArray->m_poImpl : nullptr,
13295
0
        hYArray ? hYArray->m_poImpl : nullptr, papszOptions);
13296
0
    if (!gridded)
13297
0
        return nullptr;
13298
0
    return new GDALMDArrayHS(gridded);
13299
0
}
13300
13301
/************************************************************************/
13302
/*                      GDALMDArrayGetMeshGrid()                        */
13303
/************************************************************************/
13304
13305
/** Return a list of multidimensional arrays from a list of one-dimensional
13306
 * arrays.
13307
 *
13308
 * This is typically used to transform one-dimensional longitude, latitude
13309
 * arrays into 2D ones.
13310
 *
13311
 * More formally, for one-dimensional arrays x1, x2,..., xn with lengths
13312
 * Ni=len(xi), returns (N1, N2, ..., Nn) shaped arrays if indexing="ij" or
13313
 * (N2, N1, ..., Nn) shaped arrays if indexing="xy" with the elements of xi
13314
 * repeated to fill the matrix along the first dimension for x1, the second
13315
 * for x2 and so on.
13316
 *
13317
 * For example, if x = [1, 2], and y = [3, 4, 5],
13318
 * GetMeshGrid([x, y], ["INDEXING=xy"]) will return [xm, ym] such that
13319
 * xm=[[1, 2],[1, 2],[1, 2]] and ym=[[3, 3],[4, 4],[5, 5]],
13320
 * or more generally xm[any index][i] = x[i] and ym[i][any index]=y[i]
13321
 *
13322
 * and
13323
 * GetMeshGrid([x, y], ["INDEXING=ij"]) will return [xm, ym] such that
13324
 * xm=[[1, 1, 1],[2, 2, 2]] and ym=[[3, 4, 5],[3, 4, 5]],
13325
 * or more generally xm[i][any index] = x[i] and ym[any index][i]=y[i]
13326
 *
13327
 * The currently supported options are:
13328
 * <ul>
13329
 * <li>INDEXING=xy/ij: Cartesian ("xy", default) or matrix ("ij") indexing of
13330
 * output.
13331
 * </li>
13332
 * </ul>
13333
 *
13334
 * This is the same as
13335
 * <a href="https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html">numpy.meshgrid()</a>
13336
 * function.
13337
 *
13338
 * The returned array (of arrays) must be freed with GDALReleaseArrays().
13339
 * If only the array itself needs to be freed, CPLFree() should be called
13340
 * (and GDALMDArrayRelease() on individual array members).
13341
 *
13342
 * This is the same as the C++ method GDALMDArray::GetMeshGrid()
13343
 *
13344
 * @param pahInputArrays Input arrays
13345
 * @param nCountInputArrays Number of input arrays
13346
 * @param pnCountOutputArrays Pointer to the number of values returned. Must NOT be NULL.
13347
 * @param papszOptions NULL, or NULL terminated list of options.
13348
 *
13349
 * @return an array of *pnCountOutputArrays arrays.
13350
 * @since 3.10
13351
 */
13352
GDALMDArrayH *GDALMDArrayGetMeshGrid(const GDALMDArrayH *pahInputArrays,
13353
                                     size_t nCountInputArrays,
13354
                                     size_t *pnCountOutputArrays,
13355
                                     CSLConstList papszOptions)
13356
0
{
13357
0
    VALIDATE_POINTER1(pahInputArrays, __func__, nullptr);
13358
0
    VALIDATE_POINTER1(pnCountOutputArrays, __func__, nullptr);
13359
13360
0
    std::vector<std::shared_ptr<GDALMDArray>> apoInputArrays;
13361
0
    for (size_t i = 0; i < nCountInputArrays; ++i)
13362
0
        apoInputArrays.push_back(pahInputArrays[i]->m_poImpl);
13363
13364
0
    const auto apoOutputArrays =
13365
0
        GDALMDArray::GetMeshGrid(apoInputArrays, papszOptions);
13366
0
    auto ret = static_cast<GDALMDArrayH *>(
13367
0
        CPLMalloc(sizeof(GDALMDArrayH) * apoOutputArrays.size()));
13368
0
    for (size_t i = 0; i < apoOutputArrays.size(); i++)
13369
0
    {
13370
0
        ret[i] = new GDALMDArrayHS(apoOutputArrays[i]);
13371
0
    }
13372
0
    *pnCountOutputArrays = apoOutputArrays.size();
13373
0
    return ret;
13374
0
}
13375
13376
/************************************************************************/
13377
/*                        GDALReleaseArrays()                           */
13378
/************************************************************************/
13379
13380
/** Free the return of GDALMDArrayGetCoordinateVariables()
13381
 *
13382
 * @param arrays return pointer of above methods
13383
 * @param nCount *pnCount value returned by above methods
13384
 */
13385
void GDALReleaseArrays(GDALMDArrayH *arrays, size_t nCount)
13386
0
{
13387
0
    for (size_t i = 0; i < nCount; i++)
13388
0
    {
13389
0
        delete arrays[i];
13390
0
    }
13391
0
    CPLFree(arrays);
13392
0
}
13393
13394
/************************************************************************/
13395
/*                           GDALMDArrayCache()                         */
13396
/************************************************************************/
13397
13398
/**
13399
 * \brief Cache the content of the array into an auxiliary filename.
13400
 *
13401
 * This is the same as the C++ method GDALMDArray::Cache().
13402
 *
13403
 * @since GDAL 3.4
13404
 */
13405
13406
int GDALMDArrayCache(GDALMDArrayH hArray, CSLConstList papszOptions)
13407
0
{
13408
0
    VALIDATE_POINTER1(hArray, __func__, FALSE);
13409
0
    return hArray->m_poImpl->Cache(papszOptions);
13410
0
}
13411
13412
/************************************************************************/
13413
/*                       GDALMDArrayRename()                           */
13414
/************************************************************************/
13415
13416
/** Rename the array.
13417
 *
13418
 * This is not implemented by all drivers.
13419
 *
13420
 * Drivers known to implement it: MEM, netCDF, Zarr.
13421
 *
13422
 * This is the same as the C++ method GDALAbstractMDArray::Rename()
13423
 *
13424
 * @return true in case of success
13425
 * @since GDAL 3.8
13426
 */
13427
bool GDALMDArrayRename(GDALMDArrayH hArray, const char *pszNewName)
13428
0
{
13429
0
    VALIDATE_POINTER1(hArray, __func__, false);
13430
0
    VALIDATE_POINTER1(pszNewName, __func__, false);
13431
0
    return hArray->m_poImpl->Rename(pszNewName);
13432
0
}
13433
13434
/************************************************************************/
13435
/*                        GDALAttributeRelease()                        */
13436
/************************************************************************/
13437
13438
/** Release the GDAL in-memory object associated with a GDALAttribute.
13439
 *
13440
 * Note: when applied on a object coming from a driver, this does not
13441
 * destroy the object in the file, database, etc...
13442
 */
13443
void GDALAttributeRelease(GDALAttributeH hAttr)
13444
0
{
13445
0
    delete hAttr;
13446
0
}
13447
13448
/************************************************************************/
13449
/*                        GDALAttributeGetName()                        */
13450
/************************************************************************/
13451
13452
/** Return the name of the attribute.
13453
 *
13454
 * The returned pointer is valid until hAttr is released.
13455
 *
13456
 * This is the same as the C++ method GDALAttribute::GetName().
13457
 */
13458
const char *GDALAttributeGetName(GDALAttributeH hAttr)
13459
0
{
13460
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13461
0
    return hAttr->m_poImpl->GetName().c_str();
13462
0
}
13463
13464
/************************************************************************/
13465
/*                      GDALAttributeGetFullName()                      */
13466
/************************************************************************/
13467
13468
/** Return the full name of the attribute.
13469
 *
13470
 * The returned pointer is valid until hAttr is released.
13471
 *
13472
 * This is the same as the C++ method GDALAttribute::GetFullName().
13473
 */
13474
const char *GDALAttributeGetFullName(GDALAttributeH hAttr)
13475
0
{
13476
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13477
0
    return hAttr->m_poImpl->GetFullName().c_str();
13478
0
}
13479
13480
/************************************************************************/
13481
/*                   GDALAttributeGetTotalElementsCount()               */
13482
/************************************************************************/
13483
13484
/** Return the total number of values in the attribute.
13485
 *
13486
 * This is the same as the C++ method
13487
 * GDALAbstractMDArray::GetTotalElementsCount()
13488
 */
13489
GUInt64 GDALAttributeGetTotalElementsCount(GDALAttributeH hAttr)
13490
0
{
13491
0
    VALIDATE_POINTER1(hAttr, __func__, 0);
13492
0
    return hAttr->m_poImpl->GetTotalElementsCount();
13493
0
}
13494
13495
/************************************************************************/
13496
/*                    GDALAttributeGetDimensionCount()                */
13497
/************************************************************************/
13498
13499
/** Return the number of dimensions.
13500
 *
13501
 * This is the same as the C++ method GDALAbstractMDArray::GetDimensionCount()
13502
 */
13503
size_t GDALAttributeGetDimensionCount(GDALAttributeH hAttr)
13504
0
{
13505
0
    VALIDATE_POINTER1(hAttr, __func__, 0);
13506
0
    return hAttr->m_poImpl->GetDimensionCount();
13507
0
}
13508
13509
/************************************************************************/
13510
/*                       GDALAttributeGetDimensionsSize()                */
13511
/************************************************************************/
13512
13513
/** Return the dimension sizes of the attribute.
13514
 *
13515
 * The returned array must be freed with CPLFree()
13516
 *
13517
 * @param hAttr Attribute.
13518
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
13519
 *
13520
 * @return an array of *pnCount values.
13521
 */
13522
GUInt64 *GDALAttributeGetDimensionsSize(GDALAttributeH hAttr, size_t *pnCount)
13523
0
{
13524
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13525
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
13526
0
    const auto &dims = hAttr->m_poImpl->GetDimensions();
13527
0
    auto ret = static_cast<GUInt64 *>(CPLMalloc(sizeof(GUInt64) * dims.size()));
13528
0
    for (size_t i = 0; i < dims.size(); i++)
13529
0
    {
13530
0
        ret[i] = dims[i]->GetSize();
13531
0
    }
13532
0
    *pnCount = dims.size();
13533
0
    return ret;
13534
0
}
13535
13536
/************************************************************************/
13537
/*                       GDALAttributeGetDataType()                     */
13538
/************************************************************************/
13539
13540
/** Return the data type
13541
 *
13542
 * The return must be freed with GDALExtendedDataTypeRelease().
13543
 */
13544
GDALExtendedDataTypeH GDALAttributeGetDataType(GDALAttributeH hAttr)
13545
0
{
13546
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13547
0
    return new GDALExtendedDataTypeHS(
13548
0
        new GDALExtendedDataType(hAttr->m_poImpl->GetDataType()));
13549
0
}
13550
13551
/************************************************************************/
13552
/*                       GDALAttributeReadAsRaw()                       */
13553
/************************************************************************/
13554
13555
/** Return the raw value of an attribute.
13556
 *
13557
 * This is the same as the C++ method GDALAttribute::ReadAsRaw().
13558
 *
13559
 * The returned buffer must be freed with GDALAttributeFreeRawResult()
13560
 *
13561
 * @param hAttr Attribute.
13562
 * @param pnSize Pointer to the number of bytes returned. Must NOT be NULL.
13563
 *
13564
 * @return a buffer of *pnSize bytes.
13565
 */
13566
GByte *GDALAttributeReadAsRaw(GDALAttributeH hAttr, size_t *pnSize)
13567
0
{
13568
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13569
0
    VALIDATE_POINTER1(pnSize, __func__, nullptr);
13570
0
    auto res(hAttr->m_poImpl->ReadAsRaw());
13571
0
    *pnSize = res.size();
13572
0
    auto ret = res.StealData();
13573
0
    if (!ret)
13574
0
    {
13575
0
        *pnSize = 0;
13576
0
        return nullptr;
13577
0
    }
13578
0
    return ret;
13579
0
}
13580
13581
/************************************************************************/
13582
/*                       GDALAttributeFreeRawResult()                   */
13583
/************************************************************************/
13584
13585
/** Free the return of GDALAttributeAsRaw()
13586
 */
13587
void GDALAttributeFreeRawResult(GDALAttributeH hAttr, GByte *raw,
13588
                                CPL_UNUSED size_t nSize)
13589
0
{
13590
0
    VALIDATE_POINTER0(hAttr, __func__);
13591
0
    if (raw)
13592
0
    {
13593
0
        const auto &dt(hAttr->m_poImpl->GetDataType());
13594
0
        const auto nDTSize(dt.GetSize());
13595
0
        GByte *pabyPtr = raw;
13596
0
        const auto nEltCount(hAttr->m_poImpl->GetTotalElementsCount());
13597
0
        CPLAssert(nSize == nDTSize * nEltCount);
13598
0
        for (size_t i = 0; i < nEltCount; ++i)
13599
0
        {
13600
0
            dt.FreeDynamicMemory(pabyPtr);
13601
0
            pabyPtr += nDTSize;
13602
0
        }
13603
0
        CPLFree(raw);
13604
0
    }
13605
0
}
13606
13607
/************************************************************************/
13608
/*                       GDALAttributeReadAsString()                    */
13609
/************************************************************************/
13610
13611
/** Return the value of an attribute as a string.
13612
 *
13613
 * The returned string should not be freed, and its lifetime does not
13614
 * excess a next call to ReadAsString() on the same object, or the deletion
13615
 * of the object itself.
13616
 *
13617
 * This function will only return the first element if there are several.
13618
 *
13619
 * This is the same as the C++ method GDALAttribute::ReadAsString()
13620
 *
13621
 * @return a string, or nullptr.
13622
 */
13623
const char *GDALAttributeReadAsString(GDALAttributeH hAttr)
13624
0
{
13625
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13626
0
    return hAttr->m_poImpl->ReadAsString();
13627
0
}
13628
13629
/************************************************************************/
13630
/*                      GDALAttributeReadAsInt()                        */
13631
/************************************************************************/
13632
13633
/** Return the value of an attribute as a integer.
13634
 *
13635
 * This function will only return the first element if there are several.
13636
 *
13637
 * It can fail if its value can not be converted to integer.
13638
 *
13639
 * This is the same as the C++ method GDALAttribute::ReadAsInt()
13640
 *
13641
 * @return a integer, or INT_MIN in case of error.
13642
 */
13643
int GDALAttributeReadAsInt(GDALAttributeH hAttr)
13644
0
{
13645
0
    VALIDATE_POINTER1(hAttr, __func__, 0);
13646
0
    return hAttr->m_poImpl->ReadAsInt();
13647
0
}
13648
13649
/************************************************************************/
13650
/*                      GDALAttributeReadAsInt64()                      */
13651
/************************************************************************/
13652
13653
/** Return the value of an attribute as a int64_t.
13654
 *
13655
 * This function will only return the first element if there are several.
13656
 *
13657
 * It can fail if its value can not be converted to integer.
13658
 *
13659
 * This is the same as the C++ method GDALAttribute::ReadAsInt64()
13660
 *
13661
 * @return an int64_t, or INT64_MIN in case of error.
13662
 */
13663
int64_t GDALAttributeReadAsInt64(GDALAttributeH hAttr)
13664
0
{
13665
0
    VALIDATE_POINTER1(hAttr, __func__, 0);
13666
0
    return hAttr->m_poImpl->ReadAsInt64();
13667
0
}
13668
13669
/************************************************************************/
13670
/*                       GDALAttributeReadAsDouble()                    */
13671
/************************************************************************/
13672
13673
/** Return the value of an attribute as a double.
13674
 *
13675
 * This function will only return the first element if there are several.
13676
 *
13677
 * It can fail if its value can not be converted to double.
13678
 *
13679
 * This is the same as the C++ method GDALAttribute::ReadAsDouble()
13680
 *
13681
 * @return a double value.
13682
 */
13683
double GDALAttributeReadAsDouble(GDALAttributeH hAttr)
13684
0
{
13685
0
    VALIDATE_POINTER1(hAttr, __func__, 0);
13686
0
    return hAttr->m_poImpl->ReadAsDouble();
13687
0
}
13688
13689
/************************************************************************/
13690
/*                     GDALAttributeReadAsStringArray()                 */
13691
/************************************************************************/
13692
13693
/** Return the value of an attribute as an array of strings.
13694
 *
13695
 * This is the same as the C++ method GDALAttribute::ReadAsStringArray()
13696
 *
13697
 * The return value must be freed with CSLDestroy().
13698
 */
13699
char **GDALAttributeReadAsStringArray(GDALAttributeH hAttr)
13700
0
{
13701
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13702
0
    return hAttr->m_poImpl->ReadAsStringArray().StealList();
13703
0
}
13704
13705
/************************************************************************/
13706
/*                     GDALAttributeReadAsIntArray()                    */
13707
/************************************************************************/
13708
13709
/** Return the value of an attribute as an array of integers.
13710
 *
13711
 * This is the same as the C++ method GDALAttribute::ReadAsIntArray()
13712
 *
13713
 * @param hAttr Attribute
13714
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
13715
 * @return array to be freed with CPLFree(), or nullptr.
13716
 */
13717
int *GDALAttributeReadAsIntArray(GDALAttributeH hAttr, size_t *pnCount)
13718
0
{
13719
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13720
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
13721
0
    *pnCount = 0;
13722
0
    auto tmp(hAttr->m_poImpl->ReadAsIntArray());
13723
0
    if (tmp.empty())
13724
0
        return nullptr;
13725
0
    auto ret = static_cast<int *>(VSI_MALLOC2_VERBOSE(tmp.size(), sizeof(int)));
13726
0
    if (!ret)
13727
0
        return nullptr;
13728
0
    memcpy(ret, tmp.data(), tmp.size() * sizeof(int));
13729
0
    *pnCount = tmp.size();
13730
0
    return ret;
13731
0
}
13732
13733
/************************************************************************/
13734
/*                     GDALAttributeReadAsInt64Array()                  */
13735
/************************************************************************/
13736
13737
/** Return the value of an attribute as an array of int64_t.
13738
 *
13739
 * This is the same as the C++ method GDALAttribute::ReadAsInt64Array()
13740
 *
13741
 * @param hAttr Attribute
13742
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
13743
 * @return array to be freed with CPLFree(), or nullptr.
13744
 */
13745
int64_t *GDALAttributeReadAsInt64Array(GDALAttributeH hAttr, size_t *pnCount)
13746
0
{
13747
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13748
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
13749
0
    *pnCount = 0;
13750
0
    auto tmp(hAttr->m_poImpl->ReadAsInt64Array());
13751
0
    if (tmp.empty())
13752
0
        return nullptr;
13753
0
    auto ret = static_cast<int64_t *>(
13754
0
        VSI_MALLOC2_VERBOSE(tmp.size(), sizeof(int64_t)));
13755
0
    if (!ret)
13756
0
        return nullptr;
13757
0
    memcpy(ret, tmp.data(), tmp.size() * sizeof(int64_t));
13758
0
    *pnCount = tmp.size();
13759
0
    return ret;
13760
0
}
13761
13762
/************************************************************************/
13763
/*                     GDALAttributeReadAsDoubleArray()                 */
13764
/************************************************************************/
13765
13766
/** Return the value of an attribute as an array of doubles.
13767
 *
13768
 * This is the same as the C++ method GDALAttribute::ReadAsDoubleArray()
13769
 *
13770
 * @param hAttr Attribute
13771
 * @param pnCount Pointer to the number of values returned. Must NOT be NULL.
13772
 * @return array to be freed with CPLFree(), or nullptr.
13773
 */
13774
double *GDALAttributeReadAsDoubleArray(GDALAttributeH hAttr, size_t *pnCount)
13775
0
{
13776
0
    VALIDATE_POINTER1(hAttr, __func__, nullptr);
13777
0
    VALIDATE_POINTER1(pnCount, __func__, nullptr);
13778
0
    *pnCount = 0;
13779
0
    auto tmp(hAttr->m_poImpl->ReadAsDoubleArray());
13780
0
    if (tmp.empty())
13781
0
        return nullptr;
13782
0
    auto ret =
13783
0
        static_cast<double *>(VSI_MALLOC2_VERBOSE(tmp.size(), sizeof(double)));
13784
0
    if (!ret)
13785
0
        return nullptr;
13786
0
    memcpy(ret, tmp.data(), tmp.size() * sizeof(double));
13787
0
    *pnCount = tmp.size();
13788
0
    return ret;
13789
0
}
13790
13791
/************************************************************************/
13792
/*                     GDALAttributeWriteRaw()                          */
13793
/************************************************************************/
13794
13795
/** Write an attribute from raw values expressed in GetDataType()
13796
 *
13797
 * The values should be provided in the type of GetDataType() and there should
13798
 * be exactly GetTotalElementsCount() of them.
13799
 * If GetDataType() is a string, each value should be a char* pointer.
13800
 *
13801
 * This is the same as the C++ method GDALAttribute::Write(const void*, size_t).
13802
 *
13803
 * @param hAttr Attribute
13804
 * @param pabyValue Buffer of nLen bytes.
13805
 * @param nLength Size of pabyValue in bytes. Should be equal to
13806
 *             GetTotalElementsCount() * GetDataType().GetSize()
13807
 * @return TRUE in case of success.
13808
 */
13809
int GDALAttributeWriteRaw(GDALAttributeH hAttr, const void *pabyValue,
13810
                          size_t nLength)
13811
0
{
13812
0
    VALIDATE_POINTER1(hAttr, __func__, FALSE);
13813
0
    return hAttr->m_poImpl->Write(pabyValue, nLength);
13814
0
}
13815
13816
/************************************************************************/
13817
/*                     GDALAttributeWriteString()                       */
13818
/************************************************************************/
13819
13820
/** Write an attribute from a string value.
13821
 *
13822
 * Type conversion will be performed if needed. If the attribute contains
13823
 * multiple values, only the first one will be updated.
13824
 *
13825
 * This is the same as the C++ method GDALAttribute::Write(const char*)
13826
 *
13827
 * @param hAttr Attribute
13828
 * @param pszVal Pointer to a string.
13829
 * @return TRUE in case of success.
13830
 */
13831
int GDALAttributeWriteString(GDALAttributeH hAttr, const char *pszVal)
13832
0
{
13833
0
    VALIDATE_POINTER1(hAttr, __func__, FALSE);
13834
0
    return hAttr->m_poImpl->Write(pszVal);
13835
0
}
13836
13837
/************************************************************************/
13838
/*                        GDALAttributeWriteInt()                       */
13839
/************************************************************************/
13840
13841
/** Write an attribute from a integer value.
13842
 *
13843
 * Type conversion will be performed if needed. If the attribute contains
13844
 * multiple values, only the first one will be updated.
13845
 *
13846
 * This is the same as the C++ method GDALAttribute::WriteInt()
13847
 *
13848
 * @param hAttr Attribute
13849
 * @param nVal Value.
13850
 * @return TRUE in case of success.
13851
 */
13852
int GDALAttributeWriteInt(GDALAttributeH hAttr, int nVal)
13853
0
{
13854
0
    VALIDATE_POINTER1(hAttr, __func__, FALSE);
13855
0
    return hAttr->m_poImpl->WriteInt(nVal);
13856
0
}
13857
13858
/************************************************************************/
13859
/*                        GDALAttributeWriteInt64()                     */
13860
/************************************************************************/
13861
13862
/** Write an attribute from an int64_t value.
13863
 *
13864
 * Type conversion will be performed if needed. If the attribute contains
13865
 * multiple values, only the first one will be updated.
13866
 *
13867
 * This is the same as the C++ method GDALAttribute::WriteLong()
13868
 *
13869
 * @param hAttr Attribute
13870
 * @param nVal Value.
13871
 * @return TRUE in case of success.
13872
 */
13873
int GDALAttributeWriteInt64(GDALAttributeH hAttr, int64_t nVal)
13874
0
{
13875
0
    VALIDATE_POINTER1(hAttr, __func__, FALSE);
13876
0
    return hAttr->m_poImpl->WriteInt64(nVal);
13877
0
}
13878
13879
/************************************************************************/
13880
/*                        GDALAttributeWriteDouble()                    */
13881
/************************************************************************/
13882
13883
/** Write an attribute from a double value.
13884
 *
13885
 * Type conversion will be performed if needed. If the attribute contains
13886
 * multiple values, only the first one will be updated.
13887
 *
13888
 * This is the same as the C++ method GDALAttribute::Write(double);
13889
 *
13890
 * @param hAttr Attribute
13891
 * @param dfVal Value.
13892
 *
13893
 * @return TRUE in case of success.
13894
 */
13895
int GDALAttributeWriteDouble(GDALAttributeH hAttr, double dfVal)
13896
0
{
13897
0
    VALIDATE_POINTER1(hAttr, __func__, FALSE);
13898
0
    return hAttr->m_poImpl->Write(dfVal);
13899
0
}
13900
13901
/************************************************************************/
13902
/*                       GDALAttributeWriteStringArray()                */
13903
/************************************************************************/
13904
13905
/** Write an attribute from an array of strings.
13906
 *
13907
 * Type conversion will be performed if needed.
13908
 *
13909
 * Exactly GetTotalElementsCount() strings must be provided
13910
 *
13911
 * This is the same as the C++ method GDALAttribute::Write(CSLConstList)
13912
 *
13913
 * @param hAttr Attribute
13914
 * @param papszValues Array of strings.
13915
 * @return TRUE in case of success.
13916
 */
13917
int GDALAttributeWriteStringArray(GDALAttributeH hAttr,
13918
                                  CSLConstList papszValues)
13919
0
{
13920
0
    VALIDATE_POINTER1(hAttr, __func__, FALSE);
13921
0
    return hAttr->m_poImpl->Write(papszValues);
13922
0
}
13923
13924
/************************************************************************/
13925
/*                       GDALAttributeWriteIntArray()                */
13926
/************************************************************************/
13927
13928
/** Write an attribute from an array of int.
13929
 *
13930
 * Type conversion will be performed if needed.
13931
 *
13932
 * Exactly GetTotalElementsCount() strings must be provided
13933
 *
13934
 * This is the same as the C++ method GDALAttribute::Write(const int *,
13935
 * size_t)
13936
 *
13937
 * @param hAttr Attribute
13938
 * @param panValues Array of int.
13939
 * @param nCount Should be equal to GetTotalElementsCount().
13940
 * @return TRUE in case of success.
13941
 */
13942
int GDALAttributeWriteIntArray(GDALAttributeH hAttr, const int *panValues,
13943
                               size_t nCount)
13944
0
{
13945
0
    VALIDATE_POINTER1(hAttr, __func__, FALSE);
13946
0
    return hAttr->m_poImpl->Write(panValues, nCount);
13947
0
}
13948
13949
/************************************************************************/
13950
/*                       GDALAttributeWriteInt64Array()                 */
13951
/************************************************************************/
13952
13953
/** Write an attribute from an array of int64_t.
13954
 *
13955
 * Type conversion will be performed if needed.
13956
 *
13957
 * Exactly GetTotalElementsCount() strings must be provided
13958
 *
13959
 * This is the same as the C++ method GDALAttribute::Write(const int64_t *,
13960
 * size_t)
13961
 *
13962
 * @param hAttr Attribute
13963
 * @param panValues Array of int64_t.
13964
 * @param nCount Should be equal to GetTotalElementsCount().
13965
 * @return TRUE in case of success.
13966
 */
13967
int GDALAttributeWriteInt64Array(GDALAttributeH hAttr, const int64_t *panValues,
13968
                                 size_t nCount)
13969
0
{
13970
0
    VALIDATE_POINTER1(hAttr, __func__, FALSE);
13971
0
    return hAttr->m_poImpl->Write(panValues, nCount);
13972
0
}
13973
13974
/************************************************************************/
13975
/*                       GDALAttributeWriteDoubleArray()                */
13976
/************************************************************************/
13977
13978
/** Write an attribute from an array of double.
13979
 *
13980
 * Type conversion will be performed if needed.
13981
 *
13982
 * Exactly GetTotalElementsCount() strings must be provided
13983
 *
13984
 * This is the same as the C++ method GDALAttribute::Write(const double *,
13985
 * size_t)
13986
 *
13987
 * @param hAttr Attribute
13988
 * @param padfValues Array of double.
13989
 * @param nCount Should be equal to GetTotalElementsCount().
13990
 * @return TRUE in case of success.
13991
 */
13992
int GDALAttributeWriteDoubleArray(GDALAttributeH hAttr,
13993
                                  const double *padfValues, size_t nCount)
13994
0
{
13995
0
    VALIDATE_POINTER1(hAttr, __func__, FALSE);
13996
0
    return hAttr->m_poImpl->Write(padfValues, nCount);
13997
0
}
13998
13999
/************************************************************************/
14000
/*                      GDALAttributeRename()                           */
14001
/************************************************************************/
14002
14003
/** Rename the attribute.
14004
 *
14005
 * This is not implemented by all drivers.
14006
 *
14007
 * Drivers known to implement it: MEM, netCDF.
14008
 *
14009
 * This is the same as the C++ method GDALAbstractMDArray::Rename()
14010
 *
14011
 * @return true in case of success
14012
 * @since GDAL 3.8
14013
 */
14014
bool GDALAttributeRename(GDALAttributeH hAttr, const char *pszNewName)
14015
0
{
14016
0
    VALIDATE_POINTER1(hAttr, __func__, false);
14017
0
    VALIDATE_POINTER1(pszNewName, __func__, false);
14018
0
    return hAttr->m_poImpl->Rename(pszNewName);
14019
0
}
14020
14021
/************************************************************************/
14022
/*                        GDALDimensionRelease()                        */
14023
/************************************************************************/
14024
14025
/** Release the GDAL in-memory object associated with a GDALDimension.
14026
 *
14027
 * Note: when applied on a object coming from a driver, this does not
14028
 * destroy the object in the file, database, etc...
14029
 */
14030
void GDALDimensionRelease(GDALDimensionH hDim)
14031
0
{
14032
0
    delete hDim;
14033
0
}
14034
14035
/************************************************************************/
14036
/*                        GDALDimensionGetName()                        */
14037
/************************************************************************/
14038
14039
/** Return dimension name.
14040
 *
14041
 * This is the same as the C++ method GDALDimension::GetName()
14042
 */
14043
const char *GDALDimensionGetName(GDALDimensionH hDim)
14044
0
{
14045
0
    VALIDATE_POINTER1(hDim, __func__, nullptr);
14046
0
    return hDim->m_poImpl->GetName().c_str();
14047
0
}
14048
14049
/************************************************************************/
14050
/*                      GDALDimensionGetFullName()                      */
14051
/************************************************************************/
14052
14053
/** Return dimension full name.
14054
 *
14055
 * This is the same as the C++ method GDALDimension::GetFullName()
14056
 */
14057
const char *GDALDimensionGetFullName(GDALDimensionH hDim)
14058
0
{
14059
0
    VALIDATE_POINTER1(hDim, __func__, nullptr);
14060
0
    return hDim->m_poImpl->GetFullName().c_str();
14061
0
}
14062
14063
/************************************************************************/
14064
/*                        GDALDimensionGetType()                        */
14065
/************************************************************************/
14066
14067
/** Return dimension type.
14068
 *
14069
 * This is the same as the C++ method GDALDimension::GetType()
14070
 */
14071
const char *GDALDimensionGetType(GDALDimensionH hDim)
14072
0
{
14073
0
    VALIDATE_POINTER1(hDim, __func__, nullptr);
14074
0
    return hDim->m_poImpl->GetType().c_str();
14075
0
}
14076
14077
/************************************************************************/
14078
/*                     GDALDimensionGetDirection()                      */
14079
/************************************************************************/
14080
14081
/** Return dimension direction.
14082
 *
14083
 * This is the same as the C++ method GDALDimension::GetDirection()
14084
 */
14085
const char *GDALDimensionGetDirection(GDALDimensionH hDim)
14086
0
{
14087
0
    VALIDATE_POINTER1(hDim, __func__, nullptr);
14088
0
    return hDim->m_poImpl->GetDirection().c_str();
14089
0
}
14090
14091
/************************************************************************/
14092
/*                        GDALDimensionGetSize()                        */
14093
/************************************************************************/
14094
14095
/** Return the size, that is the number of values along the dimension.
14096
 *
14097
 * This is the same as the C++ method GDALDimension::GetSize()
14098
 */
14099
GUInt64 GDALDimensionGetSize(GDALDimensionH hDim)
14100
0
{
14101
0
    VALIDATE_POINTER1(hDim, __func__, 0);
14102
0
    return hDim->m_poImpl->GetSize();
14103
0
}
14104
14105
/************************************************************************/
14106
/*                     GDALDimensionGetIndexingVariable()               */
14107
/************************************************************************/
14108
14109
/** Return the variable that is used to index the dimension (if there is one).
14110
 *
14111
 * This is the array, typically one-dimensional, describing the values taken
14112
 * by the dimension.
14113
 *
14114
 * The returned value should be freed with GDALMDArrayRelease().
14115
 *
14116
 * This is the same as the C++ method GDALDimension::GetIndexingVariable()
14117
 */
14118
GDALMDArrayH GDALDimensionGetIndexingVariable(GDALDimensionH hDim)
14119
0
{
14120
0
    VALIDATE_POINTER1(hDim, __func__, nullptr);
14121
0
    auto var(hDim->m_poImpl->GetIndexingVariable());
14122
0
    if (!var)
14123
0
        return nullptr;
14124
0
    return new GDALMDArrayHS(var);
14125
0
}
14126
14127
/************************************************************************/
14128
/*                      GDALDimensionSetIndexingVariable()              */
14129
/************************************************************************/
14130
14131
/** Set the variable that is used to index the dimension.
14132
 *
14133
 * This is the array, typically one-dimensional, describing the values taken
14134
 * by the dimension.
14135
 *
14136
 * This is the same as the C++ method GDALDimension::SetIndexingVariable()
14137
 *
14138
 * @return TRUE in case of success.
14139
 */
14140
int GDALDimensionSetIndexingVariable(GDALDimensionH hDim, GDALMDArrayH hArray)
14141
0
{
14142
0
    VALIDATE_POINTER1(hDim, __func__, FALSE);
14143
0
    return hDim->m_poImpl->SetIndexingVariable(hArray ? hArray->m_poImpl
14144
0
                                                      : nullptr);
14145
0
}
14146
14147
/************************************************************************/
14148
/*                      GDALDimensionRename()                           */
14149
/************************************************************************/
14150
14151
/** Rename the dimension.
14152
 *
14153
 * This is not implemented by all drivers.
14154
 *
14155
 * Drivers known to implement it: MEM, netCDF.
14156
 *
14157
 * This is the same as the C++ method GDALDimension::Rename()
14158
 *
14159
 * @return true in case of success
14160
 * @since GDAL 3.8
14161
 */
14162
bool GDALDimensionRename(GDALDimensionH hDim, const char *pszNewName)
14163
0
{
14164
0
    VALIDATE_POINTER1(hDim, __func__, false);
14165
0
    VALIDATE_POINTER1(pszNewName, __func__, false);
14166
0
    return hDim->m_poImpl->Rename(pszNewName);
14167
0
}
14168
14169
/************************************************************************/
14170
/*                       GDALDatasetGetRootGroup()                      */
14171
/************************************************************************/
14172
14173
/** Return the root GDALGroup of this dataset.
14174
 *
14175
 * Only valid for multidimensional datasets.
14176
 *
14177
 * The returned value must be freed with GDALGroupRelease().
14178
 *
14179
 * This is the same as the C++ method GDALDataset::GetRootGroup().
14180
 *
14181
 * @since GDAL 3.1
14182
 */
14183
GDALGroupH GDALDatasetGetRootGroup(GDALDatasetH hDS)
14184
0
{
14185
0
    VALIDATE_POINTER1(hDS, __func__, nullptr);
14186
0
    auto poGroup(GDALDataset::FromHandle(hDS)->GetRootGroup());
14187
0
    return poGroup ? new GDALGroupHS(poGroup) : nullptr;
14188
0
}
14189
14190
/************************************************************************/
14191
/*                      GDALRasterBandAsMDArray()                        */
14192
/************************************************************************/
14193
14194
/** Return a view of this raster band as a 2D multidimensional GDALMDArray.
14195
 *
14196
 * The band must be linked to a GDALDataset. If this dataset is not already
14197
 * marked as shared, it will be, so that the returned array holds a reference
14198
 * to it.
14199
 *
14200
 * If the dataset has a geotransform attached, the X and Y dimensions of the
14201
 * returned array will have an associated indexing variable.
14202
 *
14203
 * The returned pointer must be released with GDALMDArrayRelease().
14204
 *
14205
 * This is the same as the C++ method GDALRasterBand::AsMDArray().
14206
 *
14207
 * @return a new array, or NULL.
14208
 *
14209
 * @since GDAL 3.1
14210
 */
14211
GDALMDArrayH GDALRasterBandAsMDArray(GDALRasterBandH hBand)
14212
0
{
14213
0
    VALIDATE_POINTER1(hBand, __func__, nullptr);
14214
0
    auto poArray(GDALRasterBand::FromHandle(hBand)->AsMDArray());
14215
0
    if (!poArray)
14216
0
        return nullptr;
14217
0
    return new GDALMDArrayHS(poArray);
14218
0
}
14219
14220
/************************************************************************/
14221
/*                       GDALMDArrayAsClassicDataset()                  */
14222
/************************************************************************/
14223
14224
/** Return a view of this array as a "classic" GDALDataset (ie 2D)
14225
 *
14226
 * Only 2D or more arrays are supported.
14227
 *
14228
 * In the case of > 2D arrays, additional dimensions will be represented as
14229
 * raster bands.
14230
 *
14231
 * The "reverse" method is GDALRasterBand::AsMDArray().
14232
 *
14233
 * This is the same as the C++ method GDALMDArray::AsClassicDataset().
14234
 *
14235
 * @param hArray Array.
14236
 * @param iXDim Index of the dimension that will be used as the X/width axis.
14237
 * @param iYDim Index of the dimension that will be used as the Y/height axis.
14238
 * @return a new GDALDataset that must be freed with GDALClose(), or nullptr
14239
 */
14240
GDALDatasetH GDALMDArrayAsClassicDataset(GDALMDArrayH hArray, size_t iXDim,
14241
                                         size_t iYDim)
14242
0
{
14243
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
14244
0
    return GDALDataset::ToHandle(
14245
0
        hArray->m_poImpl->AsClassicDataset(iXDim, iYDim));
14246
0
}
14247
14248
/************************************************************************/
14249
/*                     GDALMDArrayAsClassicDatasetEx()                  */
14250
/************************************************************************/
14251
14252
/** Return a view of this array as a "classic" GDALDataset (ie 2D)
14253
 *
14254
 * Only 2D or more arrays are supported.
14255
 *
14256
 * In the case of > 2D arrays, additional dimensions will be represented as
14257
 * raster bands.
14258
 *
14259
 * The "reverse" method is GDALRasterBand::AsMDArray().
14260
 *
14261
 * This is the same as the C++ method GDALMDArray::AsClassicDataset().
14262
 * @param hArray Array.
14263
 * @param iXDim Index of the dimension that will be used as the X/width axis.
14264
 * @param iYDim Index of the dimension that will be used as the Y/height axis.
14265
 *              Ignored if the dimension count is 1.
14266
 * @param hRootGroup Root group, or NULL. Used with the BAND_METADATA and
14267
 *                   BAND_IMAGERY_METADATA option.
14268
 * @param papszOptions Cf GDALMDArray::AsClassicDataset()
14269
 * @return a new GDALDataset that must be freed with GDALClose(), or nullptr
14270
 * @since GDAL 3.8
14271
 */
14272
GDALDatasetH GDALMDArrayAsClassicDatasetEx(GDALMDArrayH hArray, size_t iXDim,
14273
                                           size_t iYDim, GDALGroupH hRootGroup,
14274
                                           CSLConstList papszOptions)
14275
0
{
14276
0
    VALIDATE_POINTER1(hArray, __func__, nullptr);
14277
0
    return GDALDataset::ToHandle(hArray->m_poImpl->AsClassicDataset(
14278
0
        iXDim, iYDim, hRootGroup ? hRootGroup->m_poImpl : nullptr,
14279
0
        papszOptions));
14280
0
}
14281
14282
//! @cond Doxygen_Suppress
14283
14284
GDALAttributeString::GDALAttributeString(const std::string &osParentName,
14285
                                         const std::string &osName,
14286
                                         const std::string &osValue,
14287
                                         GDALExtendedDataTypeSubType eSubType)
14288
0
    : GDALAbstractMDArray(osParentName, osName),
14289
0
      GDALAttribute(osParentName, osName),
14290
0
      m_dt(GDALExtendedDataType::CreateString(0, eSubType)), m_osValue(osValue)
14291
0
{
14292
0
}
Unexecuted instantiation: GDALAttributeString::GDALAttributeString(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, GDALExtendedDataTypeSubType)
Unexecuted instantiation: GDALAttributeString::GDALAttributeString(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, GDALExtendedDataTypeSubType)
14293
14294
const std::vector<std::shared_ptr<GDALDimension>> &
14295
GDALAttributeString::GetDimensions() const
14296
0
{
14297
0
    return m_dims;
14298
0
}
14299
14300
const GDALExtendedDataType &GDALAttributeString::GetDataType() const
14301
0
{
14302
0
    return m_dt;
14303
0
}
14304
14305
bool GDALAttributeString::IRead(const GUInt64 *, const size_t *, const GInt64 *,
14306
                                const GPtrDiff_t *,
14307
                                const GDALExtendedDataType &bufferDataType,
14308
                                void *pDstBuffer) const
14309
0
{
14310
0
    if (bufferDataType.GetClass() != GEDTC_STRING)
14311
0
        return false;
14312
0
    char *pszStr = static_cast<char *>(VSIMalloc(m_osValue.size() + 1));
14313
0
    if (!pszStr)
14314
0
        return false;
14315
0
    memcpy(pszStr, m_osValue.c_str(), m_osValue.size() + 1);
14316
0
    *static_cast<char **>(pDstBuffer) = pszStr;
14317
0
    return true;
14318
0
}
14319
14320
GDALAttributeNumeric::GDALAttributeNumeric(const std::string &osParentName,
14321
                                           const std::string &osName,
14322
                                           double dfValue)
14323
0
    : GDALAbstractMDArray(osParentName, osName),
14324
0
      GDALAttribute(osParentName, osName),
14325
0
      m_dt(GDALExtendedDataType::Create(GDT_Float64)), m_dfValue(dfValue)
14326
0
{
14327
0
}
Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, double)
Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, double)
14328
14329
GDALAttributeNumeric::GDALAttributeNumeric(const std::string &osParentName,
14330
                                           const std::string &osName,
14331
                                           int nValue)
14332
0
    : GDALAbstractMDArray(osParentName, osName),
14333
0
      GDALAttribute(osParentName, osName),
14334
0
      m_dt(GDALExtendedDataType::Create(GDT_Int32)), m_nValue(nValue)
14335
0
{
14336
0
}
Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)
Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)
14337
14338
GDALAttributeNumeric::GDALAttributeNumeric(const std::string &osParentName,
14339
                                           const std::string &osName,
14340
                                           const std::vector<GUInt32> &anValues)
14341
0
    : GDALAbstractMDArray(osParentName, osName),
14342
0
      GDALAttribute(osParentName, osName),
14343
0
      m_dt(GDALExtendedDataType::Create(GDT_UInt32)), m_anValuesUInt32(anValues)
14344
0
{
14345
0
    m_dims.push_back(std::make_shared<GDALDimension>(
14346
0
        std::string(), "dim0", std::string(), std::string(),
14347
0
        m_anValuesUInt32.size()));
14348
0
}
Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&)
Unexecuted instantiation: GDALAttributeNumeric::GDALAttributeNumeric(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&)
14349
14350
const std::vector<std::shared_ptr<GDALDimension>> &
14351
GDALAttributeNumeric::GetDimensions() const
14352
0
{
14353
0
    return m_dims;
14354
0
}
14355
14356
const GDALExtendedDataType &GDALAttributeNumeric::GetDataType() const
14357
0
{
14358
0
    return m_dt;
14359
0
}
14360
14361
bool GDALAttributeNumeric::IRead(const GUInt64 *arrayStartIdx,
14362
                                 const size_t *count, const GInt64 *arrayStep,
14363
                                 const GPtrDiff_t *bufferStride,
14364
                                 const GDALExtendedDataType &bufferDataType,
14365
                                 void *pDstBuffer) const
14366
0
{
14367
0
    if (m_dims.empty())
14368
0
    {
14369
0
        if (m_dt.GetNumericDataType() == GDT_Float64)
14370
0
            GDALExtendedDataType::CopyValue(&m_dfValue, m_dt, pDstBuffer,
14371
0
                                            bufferDataType);
14372
0
        else
14373
0
        {
14374
0
            CPLAssert(m_dt.GetNumericDataType() == GDT_Int32);
14375
0
            GDALExtendedDataType::CopyValue(&m_nValue, m_dt, pDstBuffer,
14376
0
                                            bufferDataType);
14377
0
        }
14378
0
    }
14379
0
    else
14380
0
    {
14381
0
        CPLAssert(m_dt.GetNumericDataType() == GDT_UInt32);
14382
0
        GByte *pabyDstBuffer = static_cast<GByte *>(pDstBuffer);
14383
0
        for (size_t i = 0; i < count[0]; ++i)
14384
0
        {
14385
0
            GDALExtendedDataType::CopyValue(
14386
0
                &m_anValuesUInt32[static_cast<size_t>(arrayStartIdx[0] +
14387
0
                                                      i * arrayStep[0])],
14388
0
                m_dt, pabyDstBuffer, bufferDataType);
14389
0
            pabyDstBuffer += bufferDataType.GetSize() * bufferStride[0];
14390
0
        }
14391
0
    }
14392
0
    return true;
14393
0
}
14394
14395
GDALMDArrayRegularlySpaced::GDALMDArrayRegularlySpaced(
14396
    const std::string &osParentName, const std::string &osName,
14397
    const std::shared_ptr<GDALDimension> &poDim, double dfStart,
14398
    double dfIncrement, double dfOffsetInIncrement)
14399
0
    : GDALAbstractMDArray(osParentName, osName),
14400
0
      GDALMDArray(osParentName, osName), m_dfStart(dfStart),
14401
0
      m_dfIncrement(dfIncrement),
14402
0
      m_dfOffsetInIncrement(dfOffsetInIncrement), m_dims{poDim}
14403
0
{
14404
0
}
Unexecuted instantiation: GDALMDArrayRegularlySpaced::GDALMDArrayRegularlySpaced(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<GDALDimension> const&, double, double, double)
Unexecuted instantiation: GDALMDArrayRegularlySpaced::GDALMDArrayRegularlySpaced(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<GDALDimension> const&, double, double, double)
14405
14406
std::shared_ptr<GDALMDArrayRegularlySpaced> GDALMDArrayRegularlySpaced::Create(
14407
    const std::string &osParentName, const std::string &osName,
14408
    const std::shared_ptr<GDALDimension> &poDim, double dfStart,
14409
    double dfIncrement, double dfOffsetInIncrement)
14410
0
{
14411
0
    auto poArray = std::make_shared<GDALMDArrayRegularlySpaced>(
14412
0
        osParentName, osName, poDim, dfStart, dfIncrement, dfOffsetInIncrement);
14413
0
    poArray->SetSelf(poArray);
14414
0
    return poArray;
14415
0
}
14416
14417
const std::vector<std::shared_ptr<GDALDimension>> &
14418
GDALMDArrayRegularlySpaced::GetDimensions() const
14419
0
{
14420
0
    return m_dims;
14421
0
}
14422
14423
const GDALExtendedDataType &GDALMDArrayRegularlySpaced::GetDataType() const
14424
0
{
14425
0
    return m_dt;
14426
0
}
14427
14428
std::vector<std::shared_ptr<GDALAttribute>>
14429
GDALMDArrayRegularlySpaced::GetAttributes(CSLConstList) const
14430
0
{
14431
0
    return m_attributes;
14432
0
}
14433
14434
void GDALMDArrayRegularlySpaced::AddAttribute(
14435
    const std::shared_ptr<GDALAttribute> &poAttr)
14436
0
{
14437
0
    m_attributes.emplace_back(poAttr);
14438
0
}
14439
14440
bool GDALMDArrayRegularlySpaced::IRead(
14441
    const GUInt64 *arrayStartIdx, const size_t *count, const GInt64 *arrayStep,
14442
    const GPtrDiff_t *bufferStride, const GDALExtendedDataType &bufferDataType,
14443
    void *pDstBuffer) const
14444
0
{
14445
0
    GByte *pabyDstBuffer = static_cast<GByte *>(pDstBuffer);
14446
0
    for (size_t i = 0; i < count[0]; i++)
14447
0
    {
14448
0
        const double dfVal = m_dfStart + (arrayStartIdx[0] + i * arrayStep[0] +
14449
0
                                          m_dfOffsetInIncrement) *
14450
0
                                             m_dfIncrement;
14451
0
        GDALExtendedDataType::CopyValue(&dfVal, m_dt, pabyDstBuffer,
14452
0
                                        bufferDataType);
14453
0
        pabyDstBuffer += bufferStride[0] * bufferDataType.GetSize();
14454
0
    }
14455
0
    return true;
14456
0
}
14457
14458
GDALDimensionWeakIndexingVar::GDALDimensionWeakIndexingVar(
14459
    const std::string &osParentName, const std::string &osName,
14460
    const std::string &osType, const std::string &osDirection, GUInt64 nSize)
14461
0
    : GDALDimension(osParentName, osName, osType, osDirection, nSize)
14462
0
{
14463
0
}
14464
14465
std::shared_ptr<GDALMDArray>
14466
GDALDimensionWeakIndexingVar::GetIndexingVariable() const
14467
0
{
14468
0
    return m_poIndexingVariable.lock();
14469
0
}
14470
14471
// cppcheck-suppress passedByValue
14472
bool GDALDimensionWeakIndexingVar::SetIndexingVariable(
14473
    std::shared_ptr<GDALMDArray> poIndexingVariable)
14474
0
{
14475
0
    m_poIndexingVariable = poIndexingVariable;
14476
0
    return true;
14477
0
}
14478
14479
void GDALDimensionWeakIndexingVar::SetSize(GUInt64 nNewSize)
14480
0
{
14481
0
    m_nSize = nNewSize;
14482
0
}
14483
14484
/************************************************************************/
14485
/*                       GDALPamMultiDim::Private                       */
14486
/************************************************************************/
14487
14488
struct GDALPamMultiDim::Private
14489
{
14490
    std::string m_osFilename{};
14491
    std::string m_osPamFilename{};
14492
14493
    struct Statistics
14494
    {
14495
        bool bHasStats = false;
14496
        bool bApproxStats = false;
14497
        double dfMin = 0;
14498
        double dfMax = 0;
14499
        double dfMean = 0;
14500
        double dfStdDev = 0;
14501
        GUInt64 nValidCount = 0;
14502
    };
14503
14504
    struct ArrayInfo
14505
    {
14506
        std::shared_ptr<OGRSpatialReference> poSRS{};
14507
        // cppcheck-suppress unusedStructMember
14508
        Statistics stats{};
14509
    };
14510
14511
    typedef std::pair<std::string, std::string> NameContext;
14512
    std::map<NameContext, ArrayInfo> m_oMapArray{};
14513
    std::vector<CPLXMLTreeCloser> m_apoOtherNodes{};
14514
    bool m_bDirty = false;
14515
    bool m_bLoaded = false;
14516
};
14517
14518
/************************************************************************/
14519
/*                          GDALPamMultiDim                             */
14520
/************************************************************************/
14521
14522
GDALPamMultiDim::GDALPamMultiDim(const std::string &osFilename)
14523
0
    : d(new Private())
14524
0
{
14525
0
    d->m_osFilename = osFilename;
14526
0
}
14527
14528
/************************************************************************/
14529
/*                   GDALPamMultiDim::~GDALPamMultiDim()                */
14530
/************************************************************************/
14531
14532
GDALPamMultiDim::~GDALPamMultiDim()
14533
0
{
14534
0
    if (d->m_bDirty)
14535
0
        Save();
14536
0
}
14537
14538
/************************************************************************/
14539
/*                          GDALPamMultiDim::Load()                     */
14540
/************************************************************************/
14541
14542
void GDALPamMultiDim::Load()
14543
0
{
14544
0
    if (d->m_bLoaded)
14545
0
        return;
14546
0
    d->m_bLoaded = true;
14547
14548
0
    const char *pszProxyPam = PamGetProxy(d->m_osFilename.c_str());
14549
0
    d->m_osPamFilename =
14550
0
        pszProxyPam ? std::string(pszProxyPam) : d->m_osFilename + ".aux.xml";
14551
0
    CPLXMLTreeCloser oTree(nullptr);
14552
0
    {
14553
0
        CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
14554
0
        oTree.reset(CPLParseXMLFile(d->m_osPamFilename.c_str()));
14555
0
    }
14556
0
    if (!oTree)
14557
0
    {
14558
0
        return;
14559
0
    }
14560
0
    const auto poPAMMultiDim = CPLGetXMLNode(oTree.get(), "=PAMDataset");
14561
0
    if (!poPAMMultiDim)
14562
0
        return;
14563
0
    for (CPLXMLNode *psIter = poPAMMultiDim->psChild; psIter;
14564
0
         psIter = psIter->psNext)
14565
0
    {
14566
0
        if (psIter->eType == CXT_Element &&
14567
0
            strcmp(psIter->pszValue, "Array") == 0)
14568
0
        {
14569
0
            const char *pszName = CPLGetXMLValue(psIter, "name", nullptr);
14570
0
            if (!pszName)
14571
0
                continue;
14572
0
            const char *pszContext = CPLGetXMLValue(psIter, "context", "");
14573
0
            const auto oKey =
14574
0
                std::pair<std::string, std::string>(pszName, pszContext);
14575
14576
            /* --------------------------------------------------------------------
14577
             */
14578
            /*      Check for an SRS node. */
14579
            /* --------------------------------------------------------------------
14580
             */
14581
0
            const CPLXMLNode *psSRSNode = CPLGetXMLNode(psIter, "SRS");
14582
0
            if (psSRSNode)
14583
0
            {
14584
0
                std::shared_ptr<OGRSpatialReference> poSRS =
14585
0
                    std::make_shared<OGRSpatialReference>();
14586
0
                poSRS->SetFromUserInput(
14587
0
                    CPLGetXMLValue(psSRSNode, nullptr, ""),
14588
0
                    OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS);
14589
0
                const char *pszMapping = CPLGetXMLValue(
14590
0
                    psSRSNode, "dataAxisToSRSAxisMapping", nullptr);
14591
0
                if (pszMapping)
14592
0
                {
14593
0
                    char **papszTokens =
14594
0
                        CSLTokenizeStringComplex(pszMapping, ",", FALSE, FALSE);
14595
0
                    std::vector<int> anMapping;
14596
0
                    for (int i = 0; papszTokens && papszTokens[i]; i++)
14597
0
                    {
14598
0
                        anMapping.push_back(atoi(papszTokens[i]));
14599
0
                    }
14600
0
                    CSLDestroy(papszTokens);
14601
0
                    poSRS->SetDataAxisToSRSAxisMapping(anMapping);
14602
0
                }
14603
0
                else
14604
0
                {
14605
0
                    poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
14606
0
                }
14607
14608
0
                const char *pszCoordinateEpoch =
14609
0
                    CPLGetXMLValue(psSRSNode, "coordinateEpoch", nullptr);
14610
0
                if (pszCoordinateEpoch)
14611
0
                    poSRS->SetCoordinateEpoch(CPLAtof(pszCoordinateEpoch));
14612
14613
0
                d->m_oMapArray[oKey].poSRS = std::move(poSRS);
14614
0
            }
14615
14616
0
            const CPLXMLNode *psStatistics =
14617
0
                CPLGetXMLNode(psIter, "Statistics");
14618
0
            if (psStatistics)
14619
0
            {
14620
0
                Private::Statistics sStats;
14621
0
                sStats.bHasStats = true;
14622
0
                sStats.bApproxStats = CPLTestBool(
14623
0
                    CPLGetXMLValue(psStatistics, "ApproxStats", "false"));
14624
0
                sStats.dfMin =
14625
0
                    CPLAtofM(CPLGetXMLValue(psStatistics, "Minimum", "0"));
14626
0
                sStats.dfMax =
14627
0
                    CPLAtofM(CPLGetXMLValue(psStatistics, "Maximum", "0"));
14628
0
                sStats.dfMean =
14629
0
                    CPLAtofM(CPLGetXMLValue(psStatistics, "Mean", "0"));
14630
0
                sStats.dfStdDev =
14631
0
                    CPLAtofM(CPLGetXMLValue(psStatistics, "StdDev", "0"));
14632
0
                sStats.nValidCount = static_cast<GUInt64>(CPLAtoGIntBig(
14633
0
                    CPLGetXMLValue(psStatistics, "ValidSampleCount", "0")));
14634
0
                d->m_oMapArray[oKey].stats = sStats;
14635
0
            }
14636
0
        }
14637
0
        else
14638
0
        {
14639
0
            CPLXMLNode *psNextBackup = psIter->psNext;
14640
0
            psIter->psNext = nullptr;
14641
0
            d->m_apoOtherNodes.emplace_back(
14642
0
                CPLXMLTreeCloser(CPLCloneXMLTree(psIter)));
14643
0
            psIter->psNext = psNextBackup;
14644
0
        }
14645
0
    }
14646
0
}
14647
14648
/************************************************************************/
14649
/*                          GDALPamMultiDim::Save()                     */
14650
/************************************************************************/
14651
14652
void GDALPamMultiDim::Save()
14653
0
{
14654
0
    CPLXMLTreeCloser oTree(
14655
0
        CPLCreateXMLNode(nullptr, CXT_Element, "PAMDataset"));
14656
0
    for (const auto &poOtherNode : d->m_apoOtherNodes)
14657
0
    {
14658
0
        CPLAddXMLChild(oTree.get(), CPLCloneXMLTree(poOtherNode.get()));
14659
0
    }
14660
0
    for (const auto &kv : d->m_oMapArray)
14661
0
    {
14662
0
        CPLXMLNode *psArrayNode =
14663
0
            CPLCreateXMLNode(oTree.get(), CXT_Element, "Array");
14664
0
        CPLAddXMLAttributeAndValue(psArrayNode, "name", kv.first.first.c_str());
14665
0
        if (!kv.first.second.empty())
14666
0
        {
14667
0
            CPLAddXMLAttributeAndValue(psArrayNode, "context",
14668
0
                                       kv.first.second.c_str());
14669
0
        }
14670
0
        if (kv.second.poSRS)
14671
0
        {
14672
0
            char *pszWKT = nullptr;
14673
0
            {
14674
0
                CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
14675
0
                const char *const apszOptions[] = {"FORMAT=WKT2", nullptr};
14676
0
                kv.second.poSRS->exportToWkt(&pszWKT, apszOptions);
14677
0
            }
14678
0
            CPLXMLNode *psSRSNode =
14679
0
                CPLCreateXMLElementAndValue(psArrayNode, "SRS", pszWKT);
14680
0
            CPLFree(pszWKT);
14681
0
            const auto &mapping =
14682
0
                kv.second.poSRS->GetDataAxisToSRSAxisMapping();
14683
0
            CPLString osMapping;
14684
0
            for (size_t i = 0; i < mapping.size(); ++i)
14685
0
            {
14686
0
                if (!osMapping.empty())
14687
0
                    osMapping += ",";
14688
0
                osMapping += CPLSPrintf("%d", mapping[i]);
14689
0
            }
14690
0
            CPLAddXMLAttributeAndValue(psSRSNode, "dataAxisToSRSAxisMapping",
14691
0
                                       osMapping.c_str());
14692
14693
0
            const double dfCoordinateEpoch =
14694
0
                kv.second.poSRS->GetCoordinateEpoch();
14695
0
            if (dfCoordinateEpoch > 0)
14696
0
            {
14697
0
                std::string osCoordinateEpoch =
14698
0
                    CPLSPrintf("%f", dfCoordinateEpoch);
14699
0
                if (osCoordinateEpoch.find('.') != std::string::npos)
14700
0
                {
14701
0
                    while (osCoordinateEpoch.back() == '0')
14702
0
                        osCoordinateEpoch.resize(osCoordinateEpoch.size() - 1);
14703
0
                }
14704
0
                CPLAddXMLAttributeAndValue(psSRSNode, "coordinateEpoch",
14705
0
                                           osCoordinateEpoch.c_str());
14706
0
            }
14707
0
        }
14708
14709
0
        if (kv.second.stats.bHasStats)
14710
0
        {
14711
0
            CPLXMLNode *psMDArray =
14712
0
                CPLCreateXMLNode(psArrayNode, CXT_Element, "Statistics");
14713
0
            CPLCreateXMLElementAndValue(psMDArray, "ApproxStats",
14714
0
                                        kv.second.stats.bApproxStats ? "1"
14715
0
                                                                     : "0");
14716
0
            CPLCreateXMLElementAndValue(
14717
0
                psMDArray, "Minimum",
14718
0
                CPLSPrintf("%.17g", kv.second.stats.dfMin));
14719
0
            CPLCreateXMLElementAndValue(
14720
0
                psMDArray, "Maximum",
14721
0
                CPLSPrintf("%.17g", kv.second.stats.dfMax));
14722
0
            CPLCreateXMLElementAndValue(
14723
0
                psMDArray, "Mean", CPLSPrintf("%.17g", kv.second.stats.dfMean));
14724
0
            CPLCreateXMLElementAndValue(
14725
0
                psMDArray, "StdDev",
14726
0
                CPLSPrintf("%.17g", kv.second.stats.dfStdDev));
14727
0
            CPLCreateXMLElementAndValue(
14728
0
                psMDArray, "ValidSampleCount",
14729
0
                CPLSPrintf(CPL_FRMT_GUIB, kv.second.stats.nValidCount));
14730
0
        }
14731
0
    }
14732
14733
0
    int bSaved;
14734
0
    CPLErrorAccumulator oErrorAccumulator;
14735
0
    {
14736
0
        auto oAccumulator = oErrorAccumulator.InstallForCurrentScope();
14737
0
        CPL_IGNORE_RET_VAL(oAccumulator);
14738
0
        bSaved =
14739
0
            CPLSerializeXMLTreeToFile(oTree.get(), d->m_osPamFilename.c_str());
14740
0
    }
14741
14742
0
    const char *pszNewPam = nullptr;
14743
0
    if (!bSaved && PamGetProxy(d->m_osFilename.c_str()) == nullptr &&
14744
0
        ((pszNewPam = PamAllocateProxy(d->m_osFilename.c_str())) != nullptr))
14745
0
    {
14746
0
        CPLErrorReset();
14747
0
        CPLSerializeXMLTreeToFile(oTree.get(), pszNewPam);
14748
0
    }
14749
0
    else
14750
0
    {
14751
0
        oErrorAccumulator.ReplayErrors();
14752
0
    }
14753
0
}
14754
14755
/************************************************************************/
14756
/*                    GDALPamMultiDim::GetSpatialRef()                  */
14757
/************************************************************************/
14758
14759
std::shared_ptr<OGRSpatialReference>
14760
GDALPamMultiDim::GetSpatialRef(const std::string &osArrayFullName,
14761
                               const std::string &osContext)
14762
0
{
14763
0
    Load();
14764
0
    auto oIter =
14765
0
        d->m_oMapArray.find(std::make_pair(osArrayFullName, osContext));
14766
0
    if (oIter != d->m_oMapArray.end())
14767
0
        return oIter->second.poSRS;
14768
0
    return nullptr;
14769
0
}
14770
14771
/************************************************************************/
14772
/*                    GDALPamMultiDim::SetSpatialRef()                  */
14773
/************************************************************************/
14774
14775
void GDALPamMultiDim::SetSpatialRef(const std::string &osArrayFullName,
14776
                                    const std::string &osContext,
14777
                                    const OGRSpatialReference *poSRS)
14778
0
{
14779
0
    Load();
14780
0
    d->m_bDirty = true;
14781
0
    if (poSRS && !poSRS->IsEmpty())
14782
0
        d->m_oMapArray[std::make_pair(osArrayFullName, osContext)].poSRS.reset(
14783
0
            poSRS->Clone());
14784
0
    else
14785
0
        d->m_oMapArray[std::make_pair(osArrayFullName, osContext)]
14786
0
            .poSRS.reset();
14787
0
}
14788
14789
/************************************************************************/
14790
/*                           GetStatistics()                            */
14791
/************************************************************************/
14792
14793
CPLErr GDALPamMultiDim::GetStatistics(const std::string &osArrayFullName,
14794
                                      const std::string &osContext,
14795
                                      bool bApproxOK, double *pdfMin,
14796
                                      double *pdfMax, double *pdfMean,
14797
                                      double *pdfStdDev, GUInt64 *pnValidCount)
14798
0
{
14799
0
    Load();
14800
0
    auto oIter =
14801
0
        d->m_oMapArray.find(std::make_pair(osArrayFullName, osContext));
14802
0
    if (oIter == d->m_oMapArray.end())
14803
0
        return CE_Failure;
14804
0
    const auto &stats = oIter->second.stats;
14805
0
    if (!stats.bHasStats)
14806
0
        return CE_Failure;
14807
0
    if (!bApproxOK && stats.bApproxStats)
14808
0
        return CE_Failure;
14809
0
    if (pdfMin)
14810
0
        *pdfMin = stats.dfMin;
14811
0
    if (pdfMax)
14812
0
        *pdfMax = stats.dfMax;
14813
0
    if (pdfMean)
14814
0
        *pdfMean = stats.dfMean;
14815
0
    if (pdfStdDev)
14816
0
        *pdfStdDev = stats.dfStdDev;
14817
0
    if (pnValidCount)
14818
0
        *pnValidCount = stats.nValidCount;
14819
0
    return CE_None;
14820
0
}
14821
14822
/************************************************************************/
14823
/*                           SetStatistics()                            */
14824
/************************************************************************/
14825
14826
void GDALPamMultiDim::SetStatistics(const std::string &osArrayFullName,
14827
                                    const std::string &osContext,
14828
                                    bool bApproxStats, double dfMin,
14829
                                    double dfMax, double dfMean,
14830
                                    double dfStdDev, GUInt64 nValidCount)
14831
0
{
14832
0
    Load();
14833
0
    d->m_bDirty = true;
14834
0
    auto &stats =
14835
0
        d->m_oMapArray[std::make_pair(osArrayFullName, osContext)].stats;
14836
0
    stats.bHasStats = true;
14837
0
    stats.bApproxStats = bApproxStats;
14838
0
    stats.dfMin = dfMin;
14839
0
    stats.dfMax = dfMax;
14840
0
    stats.dfMean = dfMean;
14841
0
    stats.dfStdDev = dfStdDev;
14842
0
    stats.nValidCount = nValidCount;
14843
0
}
14844
14845
/************************************************************************/
14846
/*                           ClearStatistics()                          */
14847
/************************************************************************/
14848
14849
void GDALPamMultiDim::ClearStatistics(const std::string &osArrayFullName,
14850
                                      const std::string &osContext)
14851
0
{
14852
0
    Load();
14853
0
    d->m_bDirty = true;
14854
0
    d->m_oMapArray[std::make_pair(osArrayFullName, osContext)].stats.bHasStats =
14855
0
        false;
14856
0
}
14857
14858
/************************************************************************/
14859
/*                           ClearStatistics()                          */
14860
/************************************************************************/
14861
14862
void GDALPamMultiDim::ClearStatistics()
14863
0
{
14864
0
    Load();
14865
0
    d->m_bDirty = true;
14866
0
    for (auto &kv : d->m_oMapArray)
14867
0
        kv.second.stats.bHasStats = false;
14868
0
}
14869
14870
/************************************************************************/
14871
/*                             GetPAM()                                 */
14872
/************************************************************************/
14873
14874
/*static*/ std::shared_ptr<GDALPamMultiDim>
14875
GDALPamMultiDim::GetPAM(const std::shared_ptr<GDALMDArray> &poParent)
14876
0
{
14877
0
    auto poPamArray = dynamic_cast<GDALPamMDArray *>(poParent.get());
14878
0
    if (poPamArray)
14879
0
        return poPamArray->GetPAM();
14880
0
    return nullptr;
14881
0
}
14882
14883
/************************************************************************/
14884
/*                           GDALPamMDArray                             */
14885
/************************************************************************/
14886
14887
GDALPamMDArray::GDALPamMDArray(const std::string &osParentName,
14888
                               const std::string &osName,
14889
                               const std::shared_ptr<GDALPamMultiDim> &poPam,
14890
                               const std::string &osContext)
14891
    :
14892
#if !defined(COMPILER_WARNS_ABOUT_ABSTRACT_VBASE_INIT)
14893
      GDALAbstractMDArray(osParentName, osName),
14894
#endif
14895
0
      GDALMDArray(osParentName, osName, osContext), m_poPam(poPam)
14896
0
{
14897
0
}
14898
14899
/************************************************************************/
14900
/*                    GDALPamMDArray::SetSpatialRef()                   */
14901
/************************************************************************/
14902
14903
bool GDALPamMDArray::SetSpatialRef(const OGRSpatialReference *poSRS)
14904
0
{
14905
0
    if (!m_poPam)
14906
0
        return false;
14907
0
    m_poPam->SetSpatialRef(GetFullName(), GetContext(), poSRS);
14908
0
    return true;
14909
0
}
14910
14911
/************************************************************************/
14912
/*                    GDALPamMDArray::GetSpatialRef()                   */
14913
/************************************************************************/
14914
14915
std::shared_ptr<OGRSpatialReference> GDALPamMDArray::GetSpatialRef() const
14916
0
{
14917
0
    if (!m_poPam)
14918
0
        return nullptr;
14919
0
    return m_poPam->GetSpatialRef(GetFullName(), GetContext());
14920
0
}
14921
14922
/************************************************************************/
14923
/*                           GetStatistics()                            */
14924
/************************************************************************/
14925
14926
CPLErr GDALPamMDArray::GetStatistics(bool bApproxOK, bool bForce,
14927
                                     double *pdfMin, double *pdfMax,
14928
                                     double *pdfMean, double *pdfStdDev,
14929
                                     GUInt64 *pnValidCount,
14930
                                     GDALProgressFunc pfnProgress,
14931
                                     void *pProgressData)
14932
0
{
14933
0
    if (m_poPam && m_poPam->GetStatistics(GetFullName(), GetContext(),
14934
0
                                          bApproxOK, pdfMin, pdfMax, pdfMean,
14935
0
                                          pdfStdDev, pnValidCount) == CE_None)
14936
0
    {
14937
0
        return CE_None;
14938
0
    }
14939
0
    if (!bForce)
14940
0
        return CE_Warning;
14941
14942
0
    return GDALMDArray::GetStatistics(bApproxOK, bForce, pdfMin, pdfMax,
14943
0
                                      pdfMean, pdfStdDev, pnValidCount,
14944
0
                                      pfnProgress, pProgressData);
14945
0
}
14946
14947
/************************************************************************/
14948
/*                           SetStatistics()                            */
14949
/************************************************************************/
14950
14951
bool GDALPamMDArray::SetStatistics(bool bApproxStats, double dfMin,
14952
                                   double dfMax, double dfMean, double dfStdDev,
14953
                                   GUInt64 nValidCount,
14954
                                   CSLConstList /* papszOptions */)
14955
0
{
14956
0
    if (!m_poPam)
14957
0
        return false;
14958
0
    m_poPam->SetStatistics(GetFullName(), GetContext(), bApproxStats, dfMin,
14959
0
                           dfMax, dfMean, dfStdDev, nValidCount);
14960
0
    return true;
14961
0
}
14962
14963
/************************************************************************/
14964
/*                           ClearStatistics()                          */
14965
/************************************************************************/
14966
14967
void GDALPamMDArray::ClearStatistics()
14968
0
{
14969
0
    if (!m_poPam)
14970
0
        return;
14971
0
    m_poPam->ClearStatistics(GetFullName(), GetContext());
14972
0
}
14973
14974
//! @endcond