-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
gdalmultidim.cpp
12003 lines (10931 loc) · 448 KB
/
gdalmultidim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* $Id$
*
* Name: gdalmultidim.cpp
* Project: GDAL Core
* Purpose: GDAL Core C++/Private implementation for multidimensional support
* Author: Even Rouault <even.rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2019, Even Rouault <even.rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include <assert.h>
#include <algorithm>
#include <limits>
#include <queue>
#include <set>
#include <ctype.h> // isalnum
#include "cpl_error_internal.h"
#include "gdal_priv.h"
#include "gdal_pam.h"
#include "gdal_utils.h"
#include "cpl_safemaths.hpp"
#include "ogrsf_frmts.h"
#if defined(__clang__) || defined(_MSC_VER)
#define COMPILER_WARNS_ABOUT_ABSTRACT_VBASE_INIT
#endif
/************************************************************************/
/* GetPAM() */
/************************************************************************/
static std::shared_ptr<GDALPamMultiDim> GetPAM(const std::shared_ptr<GDALMDArray>& poParent)
{
auto poPamArray = dynamic_cast<GDALPamMDArray*>(poParent.get());
if( poPamArray )
return poPamArray->GetPAM();
return nullptr;
}
/************************************************************************/
/* GDALMDArrayUnscaled */
/************************************************************************/
class GDALMDArrayUnscaled final: public GDALPamMDArray
{
private:
std::shared_ptr<GDALMDArray> m_poParent{};
GDALExtendedDataType m_dt;
bool m_bHasNoData;
double m_adfNoData[2]{ std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN() };
protected:
explicit GDALMDArrayUnscaled(const std::shared_ptr<GDALMDArray>& poParent):
GDALAbstractMDArray(std::string(), "Unscaled view of " + poParent->GetFullName()),
GDALPamMDArray(std::string(), "Unscaled view of " + poParent->GetFullName(), ::GetPAM(poParent)),
m_poParent(std::move(poParent)),
m_dt(GDALExtendedDataType::Create(GDALDataTypeIsComplex(
m_poParent->GetDataType().GetNumericDataType()) ?
GDT_CFloat64 : GDT_Float64)),
m_bHasNoData( m_poParent->GetRawNoDataValue() != nullptr )
{
}
bool IRead(const GUInt64* arrayStartIdx,
const size_t* count,
const GInt64* arrayStep,
const GPtrDiff_t* bufferStride,
const GDALExtendedDataType& bufferDataType,
void* pDstBuffer) const override;
bool IWrite(const GUInt64* arrayStartIdx,
const size_t* count,
const GInt64* arrayStep,
const GPtrDiff_t* bufferStride,
const GDALExtendedDataType& bufferDataType,
const void* pSrcBuffer) override;
bool IAdviseRead(const GUInt64* arrayStartIdx,
const size_t* count,
CSLConstList papszOptions) const override
{ return m_poParent->AdviseRead(arrayStartIdx, count, papszOptions); }
public:
static std::shared_ptr<GDALMDArrayUnscaled> Create(
const std::shared_ptr<GDALMDArray>& poParent)
{
auto newAr(std::shared_ptr<GDALMDArrayUnscaled>(new GDALMDArrayUnscaled(
poParent)));
newAr->SetSelf(newAr);
return newAr;
}
bool IsWritable() const override { return m_poParent->IsWritable(); }
const std::string& GetFilename() const override { return m_poParent->GetFilename(); }
const std::vector<std::shared_ptr<GDALDimension>>& GetDimensions() const override { return m_poParent->GetDimensions(); }
const GDALExtendedDataType &GetDataType() const override { return m_dt; }
const std::string& GetUnit() const override { return m_poParent->GetUnit(); }
std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override { return m_poParent->GetSpatialRef(); }
const void* GetRawNoDataValue() const override { return m_bHasNoData ? &m_adfNoData[0] : nullptr; }
bool SetRawNoDataValue(const void* pRawNoData) override {
m_bHasNoData = true; memcpy(m_adfNoData, pRawNoData, m_dt.GetSize()); return true; }
std::vector<GUInt64> GetBlockSize() const override { return m_poParent->GetBlockSize(); }
std::shared_ptr<GDALAttribute> GetAttribute(const std::string& osName) const override
{ return m_poParent->GetAttribute(osName); }
std::vector<std::shared_ptr<GDALAttribute>> GetAttributes(CSLConstList papszOptions = nullptr) const override
{ return m_poParent->GetAttributes(papszOptions); }
bool SetUnit(const std::string& osUnit) override { return m_poParent->SetUnit(osUnit); }
bool SetSpatialRef(const OGRSpatialReference* poSRS) override { return m_poParent->SetSpatialRef(poSRS); }
std::shared_ptr<GDALAttribute> CreateAttribute(
const std::string& osName,
const std::vector<GUInt64>& anDimensions,
const GDALExtendedDataType& oDataType,
CSLConstList papszOptions = nullptr) override {return m_poParent->CreateAttribute(osName, anDimensions, oDataType, papszOptions); }
};
/************************************************************************/
/* ~GDALIHasAttribute() */
/************************************************************************/
GDALIHasAttribute::~GDALIHasAttribute() = default;
/************************************************************************/
/* GetAttribute() */
/************************************************************************/
/** Return an attribute by its name.
*
* If the attribute does not exist, nullptr should be silently returned.
*
* @note Driver implementation: this method will fallback to
* GetAttributeFromAttributes() is not explicitly implemented
*
* Drivers known to implement it for groups and arrays: MEM, netCDF.
*
* This is the same as the C function GDALGroupGetAttribute() or
* GDALMDArrayGetAttribute().
*
* @param osName Attribute name
* @return the attribute, or nullptr if it does not exist or an error occurred.
*/
std::shared_ptr<GDALAttribute> GDALIHasAttribute::GetAttribute(
const std::string& osName) const
{
return GetAttributeFromAttributes(osName);
}
/************************************************************************/
/* GetAttributeFromAttributes() */
/************************************************************************/
/** Possible fallback implementation for GetAttribute() using GetAttributes().
*/
std::shared_ptr<GDALAttribute> GDALIHasAttribute::GetAttributeFromAttributes(
const std::string& osName) const
{
auto attrs(GetAttributes());
for( const auto& attr: attrs )
{
if( attr->GetName() == osName )
return attr;
}
return nullptr;
}
/************************************************************************/
/* GetAttributes() */
/************************************************************************/
/** Return the list of attributes contained in a GDALMDArray or GDALGroup.
*
* If the attribute does not exist, nullptr should be silently returned.
*
* @note Driver implementation: optionally implemented. If implemented,
* GetAttribute() should also be implemented.
*
* Drivers known to implement it for groups and arrays: MEM, netCDF.
*
* This is the same as the C function GDALGroupGetAttributes() or
* GDALMDArrayGetAttributes().
* @param papszOptions Driver specific options determining how attributes
* should be retrieved. Pass nullptr for default behavior.
*
* @return the attributes.
*/
std::vector<std::shared_ptr<GDALAttribute>>
GDALIHasAttribute::GetAttributes(CPL_UNUSED CSLConstList papszOptions) const
{
return {};
}
/************************************************************************/
/* CreateAttribute() */
/************************************************************************/
/** Create an attribute within a GDALMDArray or GDALGroup.
*
* The attribute might not be "physically" created until a value is written
* into it.
*
* Optionally implemented.
*
* Drivers known to implement it: MEM, netCDF
*
* This is the same as the C function GDALGroupCreateAttribute() or
* GDALMDArrayCreateAttribute()
*
* @param osName Attribute name.
* @param anDimensions List of dimension sizes, ordered from the slowest varying
* dimension first to the fastest varying dimension last.
* Empty for a scalar attribute (common case)
* @param oDataType Attribute data type.
* @param papszOptions Driver specific options determining how the attribute.
* should be created.
*
* @return the new attribute, or nullptr if case of error
*/
std::shared_ptr<GDALAttribute> GDALIHasAttribute::CreateAttribute(
CPL_UNUSED const std::string& osName,
CPL_UNUSED const std::vector<GUInt64>& anDimensions,
CPL_UNUSED const GDALExtendedDataType& oDataType,
CPL_UNUSED CSLConstList papszOptions)
{
CPLError(CE_Failure, CPLE_NotSupported, "CreateAttribute() not implemented");
return nullptr;
}
/************************************************************************/
/* GDALGroup() */
/************************************************************************/
//! @cond Doxygen_Suppress
GDALGroup::GDALGroup(const std::string& osParentName, const std::string& osName):
m_osName(osParentName.empty() ? "/" : osName),
m_osFullName(!osParentName.empty() ? ((osParentName == "/" ? "/" : osParentName + "/") + osName) : "/")
{}
//! @endcond
/************************************************************************/
/* ~GDALGroup() */
/************************************************************************/
GDALGroup::~GDALGroup() = default;
/************************************************************************/
/* GetMDArrayNames() */
/************************************************************************/
/** Return the list of multidimensional array names contained in this group.
*
* @note Driver implementation: optionally implemented. If implemented,
* OpenMDArray() should also be implemented.
*
* Drivers known to implement it: MEM, netCDF.
*
* This is the same as the C function GDALGroupGetMDArrayNames().
*
* @param papszOptions Driver specific options determining how arrays
* should be retrieved. Pass nullptr for default behavior.
*
* @return the array names.
*/
std::vector<std::string>
GDALGroup::GetMDArrayNames(CPL_UNUSED CSLConstList papszOptions) const
{
return {};
}
/************************************************************************/
/* OpenMDArray() */
/************************************************************************/
/** Open and return a multidimensional array.
*
* @note Driver implementation: optionally implemented. If implemented,
* GetMDArrayNames() should also be implemented.
*
* Drivers known to implement it: MEM, netCDF.
*
* This is the same as the C function GDALGroupOpenMDArray().
*
* @param osName Array name.
* @param papszOptions Driver specific options determining how the array should
* be opened. Pass nullptr for default behavior.
*
* @return the array, or nullptr.
*/
std::shared_ptr<GDALMDArray> GDALGroup::OpenMDArray(CPL_UNUSED const std::string& osName,
CPL_UNUSED CSLConstList papszOptions) const
{
return nullptr;
}
/************************************************************************/
/* GetGroupNames() */
/************************************************************************/
/** Return the list of sub-groups contained in this group.
*
* @note Driver implementation: optionally implemented. If implemented,
* OpenGroup() should also be implemented.
*
* Drivers known to implement it: MEM, netCDF.
*
* This is the same as the C function GDALGroupGetGroupNames().
*
* @param papszOptions Driver specific options determining how groups
* should be retrieved. Pass nullptr for default behavior.
*
* @return the group names.
*/
std::vector<std::string> GDALGroup::GetGroupNames(CPL_UNUSED CSLConstList papszOptions) const
{
return {};
}
/************************************************************************/
/* OpenGroup() */
/************************************************************************/
/** Open and return a sub-group.
*
* @note Driver implementation: optionally implemented. If implemented,
* GetGroupNames() should also be implemented.
*
* Drivers known to implement it: MEM, netCDF.
*
* This is the same as the C function GDALGroupOpenGroup().
*
* @param osName Sub-group name.
* @param papszOptions Driver specific options determining how the sub-group should
* be opened. Pass nullptr for default behavior.
*
* @return the group, or nullptr.
*/
std::shared_ptr<GDALGroup> GDALGroup::OpenGroup(CPL_UNUSED const std::string& osName,
CPL_UNUSED CSLConstList papszOptions) const
{
return nullptr;
}
/************************************************************************/
/* GetVectorLayerNames() */
/************************************************************************/
/** Return the list of layer names contained in this group.
*
* @note Driver implementation: optionally implemented. If implemented,
* OpenVectorLayer() should also be implemented.
*
* Drivers known to implement it: OpenFileGDB, FileGDB
*
* Other drivers will return an empty list. GDALDataset::GetLayerCount() and
* GDALDataset::GetLayer() should then be used.
*
* This is the same as the C function GDALGroupGetVectorLayerNames().
*
* @param papszOptions Driver specific options determining how layers
* should be retrieved. Pass nullptr for default behavior.
*
* @return the vector layer names.
* @since GDAL 3.4
*/
std::vector<std::string> GDALGroup::GetVectorLayerNames(CPL_UNUSED CSLConstList papszOptions) const
{
return {};
}
/************************************************************************/
/* OpenVectorLayer() */
/************************************************************************/
/** Open and return a vector layer.
*
* Due to the historical ownership of OGRLayer* by GDALDataset*, the
* lifetime of the returned OGRLayer* is linked to the one of the owner
* dataset (contrary to the general design of this class where objects can be
* used independently of the object that returned them)
*
* @note Driver implementation: optionally implemented. If implemented,
* GetVectorLayerNames() should also be implemented.
*
* Drivers known to implement it: MEM, netCDF.
*
* This is the same as the C function GDALGroupOpenVectorLayer().
*
* @param osName Vector layer name.
* @param papszOptions Driver specific options determining how the layer should
* be opened. Pass nullptr for default behavior.
*
* @return the group, or nullptr.
*/
OGRLayer* GDALGroup::OpenVectorLayer(CPL_UNUSED const std::string& osName,
CPL_UNUSED CSLConstList papszOptions) const
{
return nullptr;
}
/************************************************************************/
/* GetDimensions() */
/************************************************************************/
/** Return the list of dimensions contained in this group and used by its
* arrays.
*
* This is for dimensions that can potentially be used by several arrays.
* Not all drivers might implement this. To retrieve the dimensions used by
* a specific array, use GDALMDArray::GetDimensions().
*
* Drivers known to implement it: MEM, netCDF
*
* This is the same as the C function GDALGroupGetDimensions().
*
* @param papszOptions Driver specific options determining how groups
* should be retrieved. Pass nullptr for default behavior.
*
* @return the dimensions.
*/
std::vector<std::shared_ptr<GDALDimension>> GDALGroup::GetDimensions(
CPL_UNUSED CSLConstList papszOptions) const
{
return {};
}
/************************************************************************/
/* GetStructuralInfo() */
/************************************************************************/
/** Return structural information on the group.
*
* This may be the compression, etc..
*
* The return value should not be freed and is valid until GDALGroup is
* released or this function called again.
*
* This is the same as the C function GDALGroupGetStructuralInfo().
*/
CSLConstList GDALGroup::GetStructuralInfo() const
{
return nullptr;
}
/************************************************************************/
/* CreateGroup() */
/************************************************************************/
/** Create a sub-group within a group.
*
* Optionally implemented by drivers.
*
* Drivers known to implement it: MEM, netCDF
*
* This is the same as the C function GDALGroupCreateGroup().
*
* @param osName Sub-group name.
* @param papszOptions Driver specific options determining how the sub-group
* should be created.
*
* @return the new sub-group, or nullptr in case of error.
*/
std::shared_ptr<GDALGroup> GDALGroup::CreateGroup(CPL_UNUSED const std::string& osName,
CPL_UNUSED CSLConstList papszOptions)
{
CPLError(CE_Failure, CPLE_NotSupported, "CreateGroup() not implemented");
return nullptr;
}
/************************************************************************/
/* CreateDimension() */
/************************************************************************/
/** Create a dimension within a group.
*
* @note Driver implementation: drivers supporting CreateDimension() should implement
* this method, but do not have necessarily to implement GDALGroup::GetDimensions().
*
* Drivers known to implement it: MEM, netCDF
*
* This is the same as the C function GDALGroupCreateDimension().
*
* @param osName Dimension name.
* @param osType Dimension type (might be empty, and ignored by drivers)
* @param osDirection Dimension direction (might be empty, and ignored by drivers)
* @param nSize Number of values indexed by this dimension. Should be > 0.
* @param papszOptions Driver specific options determining how the dimension
* should be created.
*
* @return the new dimension, or nullptr if case of error
*/
std::shared_ptr<GDALDimension> GDALGroup::CreateDimension(CPL_UNUSED const std::string& osName,
CPL_UNUSED const std::string& osType,
CPL_UNUSED const std::string& osDirection,
CPL_UNUSED GUInt64 nSize,
CPL_UNUSED CSLConstList papszOptions)
{
CPLError(CE_Failure, CPLE_NotSupported, "CreateDimension() not implemented");
return nullptr;
}
/************************************************************************/
/* CreateMDArray() */
/************************************************************************/
/** Create a multidimensional array within a group.
*
* It is recommended that the GDALDimension objects passed in aoDimensions
* belong to this group, either by retrieving them with GetDimensions()
* or creating a new one with CreateDimension().
*
* Optionally implemented.
*
* Drivers known to implement it: MEM, netCDF
*
* This is the same as the C function GDALGroupCreateMDArray().
*
* @note Driver implementation: drivers should take into account the possibility
* that GDALDimension object passed in aoDimensions might belong to a different
* group / dataset / driver and act accordingly.
*
* @param osName Array name.
* @param aoDimensions List of dimensions, ordered from the slowest varying
* dimension first to the fastest varying dimension last.
* Might be empty for a scalar array (if supported by driver)
* @param oDataType Array data type.
* @param papszOptions Driver specific options determining how the array
* should be created.
*
* @return the new array, or nullptr if case of error
*/
std::shared_ptr<GDALMDArray> GDALGroup::CreateMDArray(
CPL_UNUSED const std::string& osName,
CPL_UNUSED const std::vector<std::shared_ptr<GDALDimension>>& aoDimensions,
CPL_UNUSED const GDALExtendedDataType& oDataType,
CPL_UNUSED CSLConstList papszOptions)
{
CPLError(CE_Failure, CPLE_NotSupported, "CreateMDArray() not implemented");
return nullptr;
}
/************************************************************************/
/* GetTotalCopyCost() */
/************************************************************************/
/** Return a total "cost" to copy the group.
*
* Used as a parameter for CopFrom()
*/
GUInt64 GDALGroup::GetTotalCopyCost() const
{
GUInt64 nCost = COPY_COST;
nCost += GetAttributes().size() * GDALAttribute::COPY_COST;
auto groupNames = GetGroupNames();
for( const auto& name: groupNames )
{
auto subGroup = OpenGroup(name);
if( subGroup )
{
nCost += subGroup->GetTotalCopyCost();
}
}
auto arrayNames = GetMDArrayNames();
for( const auto& name: arrayNames )
{
auto array = OpenMDArray(name);
if( array )
{
nCost += array->GetTotalCopyCost();
}
}
return nCost;
}
/************************************************************************/
/* CopyFrom() */
/************************************************************************/
/** Copy the content of a group into a new (generally empty) group.
*
* @param poDstRootGroup Destination root group. Must NOT be nullptr.
* @param poSrcDS Source dataset. Might be nullptr (but for correct behavior
* of some output drivers this is not recommended)
* @param poSrcGroup Source group. Must NOT be nullptr.
* @param bStrict Whether to enable stict mode. In strict mode, any error will
* stop the copy. In relaxed mode, the copy will be attempted to
* be pursued.
* @param nCurCost Should be provided as a variable initially set to 0.
* @param nTotalCost Total cost from GetTotalCopyCost().
* @param pfnProgress Progress callback, or nullptr.
* @param pProgressData Progress user data, or nulptr.
* @param papszOptions Creation options. Currently, only array creation
* options are supported. They must be prefixed with "ARRAY:" .
* The scope may be further restricted to arrays of a certain
* dimension by adding "IF(DIM={ndims}):" after "ARRAY:".
* For example, "ARRAY:IF(DIM=2):BLOCKSIZE=256,256" will
* restrict BLOCKSIZE=256,256 to arrays of dimension 2.
* Restriction to arrays of a given name is done with adding
* "IF(NAME={name}):" after "ARRAY:". {name} can also be
* a full qualified name.
* A non-driver specific ARRAY option, "AUTOSCALE=YES" can be
* used to ask (non indexing) variables of type Float32 or Float64
* to be scaled to UInt16 with scale and offset values
* being computed from the minimum and maximum of the source array.
* The integer data type used can be set with
* AUTOSCALE_DATA_TYPE=Byte/UInt16/Int16/UInt32/Int32.
*
* @return true in case of success (or partial success if bStrict == false).
*/
bool GDALGroup::CopyFrom( const std::shared_ptr<GDALGroup>& poDstRootGroup,
GDALDataset* poSrcDS,
const std::shared_ptr<GDALGroup>& poSrcGroup,
bool bStrict,
GUInt64& nCurCost,
const GUInt64 nTotalCost,
GDALProgressFunc pfnProgress,
void * pProgressData,
CSLConstList papszOptions )
{
if( pfnProgress == nullptr )
pfnProgress = GDALDummyProgress;
#define EXIT_OR_CONTINUE_IF_NULL(x) \
if( !(x) ) \
{ \
if( bStrict ) \
return false; \
continue; \
} \
(void)0
try
{
nCurCost += GDALGroup::COPY_COST;
const auto srcDims = poSrcGroup->GetDimensions();
std::map<std::string, std::shared_ptr<GDALDimension>> mapExistingDstDims;
std::map<std::string, std::string> mapSrcVariableNameToIndexedDimName;
for( const auto& dim: srcDims )
{
auto dstDim = CreateDimension(dim->GetName(),
dim->GetType(),
dim->GetDirection(),
dim->GetSize());
EXIT_OR_CONTINUE_IF_NULL(dstDim);
mapExistingDstDims[dim->GetName()] = dstDim;
auto poIndexingVarSrc(dim->GetIndexingVariable());
if( poIndexingVarSrc )
{
mapSrcVariableNameToIndexedDimName[poIndexingVarSrc->GetName()] = dim->GetName();
}
}
auto attrs = poSrcGroup->GetAttributes();
for( const auto& attr: attrs )
{
auto dstAttr = CreateAttribute(attr->GetName(),
attr->GetDimensionsSize(),
attr->GetDataType());
EXIT_OR_CONTINUE_IF_NULL(dstAttr);
auto raw(attr->ReadAsRaw());
if( !dstAttr->Write(raw.data(), raw.size()) && bStrict )
return false;
}
if( !attrs.empty() )
{
nCurCost += attrs.size() * GDALAttribute::COPY_COST;
if( !pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData) )
return false;
}
auto arrayNames = poSrcGroup->GetMDArrayNames();
for( const auto& name: arrayNames )
{
auto srcArray = poSrcGroup->OpenMDArray(name);
EXIT_OR_CONTINUE_IF_NULL(srcArray);
// Map source dimensions to target dimensions
std::vector<std::shared_ptr<GDALDimension>> dstArrayDims;
const auto& srcArrayDims(srcArray->GetDimensions());
for( const auto& dim : srcArrayDims )
{
auto dstDim = poDstRootGroup->OpenDimensionFromFullname(
dim->GetFullName());
if( dstDim && dstDim->GetSize() == dim->GetSize() )
{
dstArrayDims.emplace_back(dstDim);
}
else
{
auto oIter = mapExistingDstDims.find(dim->GetName());
if( oIter != mapExistingDstDims.end() &&
oIter->second->GetSize() == dim->GetSize() )
{
dstArrayDims.emplace_back(oIter->second);
}
else
{
std::string newDimName;
if( oIter == mapExistingDstDims.end() )
{
newDimName = dim->GetName();
}
else
{
std::string newDimNamePrefix(name + '_' + dim->GetName());
newDimName = newDimNamePrefix;
int nIterCount = 2;
while( mapExistingDstDims.find(newDimName) != mapExistingDstDims.end() )
{
newDimName = newDimNamePrefix + CPLSPrintf("_%d", nIterCount);
nIterCount ++;
}
}
dstDim = CreateDimension(newDimName,
dim->GetType(),
dim->GetDirection(),
dim->GetSize());
if( !dstDim )
return false;
mapExistingDstDims[newDimName] = dstDim;
dstArrayDims.emplace_back(dstDim);
}
}
}
CPLStringList aosArrayCO;
bool bAutoScale = false;
GDALDataType eAutoScaleType = GDT_UInt16;
for( CSLConstList papszIter = papszOptions;
papszIter && *papszIter; ++papszIter )
{
if( STARTS_WITH_CI(*papszIter, "ARRAY:") )
{
const char* pszOption = *papszIter + strlen("ARRAY:");
if( STARTS_WITH_CI(pszOption, "IF(DIM=") )
{
const char* pszNext = strchr(pszOption, ':');
if( pszNext != nullptr )
{
int nDim = atoi(pszOption + strlen("IF(DIM="));
if( static_cast<size_t>(nDim) == dstArrayDims.size() )
{
pszOption = pszNext + 1;
}
else
{
pszOption = nullptr;
}
}
}
else if( STARTS_WITH_CI(pszOption, "IF(NAME=") )
{
const char* pszName = pszOption + strlen("IF(NAME=");
const char* pszNext = strchr(pszName, ':');
if( pszNext != nullptr && pszNext > pszName &&
pszNext[-1] == ')' )
{
CPLString osName;
osName.assign(pszName, pszNext - pszName - 1);
if( osName == srcArray->GetName() ||
osName == srcArray->GetFullName() )
{
pszOption = pszNext + 1;
}
else
{
pszOption = nullptr;
}
}
}
if( pszOption )
{
if( STARTS_WITH_CI(pszOption, "AUTOSCALE=") )
{
bAutoScale = CPLTestBool(pszOption + strlen("AUTOSCALE="));
}
else if( STARTS_WITH_CI(pszOption, "AUTOSCALE_DATA_TYPE=") )
{
const char* pszDataType = pszOption + strlen("AUTOSCALE_DATA_TYPE=");
eAutoScaleType = GDALGetDataTypeByName(pszDataType);
if( eAutoScaleType != GDT_Byte &&
eAutoScaleType != GDT_UInt16 &&
eAutoScaleType != GDT_Int16 &&
eAutoScaleType != GDT_UInt32 &&
eAutoScaleType != GDT_Int32 &&
eAutoScaleType != GDT_UInt64 &&
eAutoScaleType != GDT_Int64 )
{
CPLError(CE_Failure, CPLE_NotSupported,
"Unsupported value for AUTOSCALE_DATA_TYPE");
return false;
}
}
else
{
aosArrayCO.AddString(pszOption);
}
}
}
}
auto oIterDimName = mapSrcVariableNameToIndexedDimName.find(srcArray->GetName());
const auto& srcArrayType = srcArray->GetDataType();
std::shared_ptr<GDALMDArray> dstArray;
// Only autoscale non-indexing variables
bool bHasOffset = false;
bool bHasScale = false;
if( bAutoScale &&
srcArrayType.GetClass() == GEDTC_NUMERIC &&
(srcArrayType.GetNumericDataType() == GDT_Float32 ||
srcArrayType.GetNumericDataType() == GDT_Float64 ) &&
srcArray->GetOffset(&bHasOffset) == 0.0 &&
!bHasOffset &&
srcArray->GetScale(&bHasScale) == 1.0 &&
!bHasScale &&
oIterDimName == mapSrcVariableNameToIndexedDimName.end() )
{
constexpr bool bApproxOK = false;
constexpr bool bForce = true;
double dfMin = 0.0;
double dfMax = 0.0;
if( srcArray->GetStatistics(bApproxOK, bForce,
&dfMin, &dfMax, nullptr, nullptr,
nullptr,
nullptr, nullptr) != CE_None )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Could not retrieve statistics for array %s",
srcArray->GetName().c_str());
return false;
}
double dfDTMin = 0;
double dfDTMax = 0;
#define setDTMinMax(ctype) do \
{ dfDTMin = static_cast<double>(std::numeric_limits<ctype>::min()); \
dfDTMax = static_cast<double>(std::numeric_limits<ctype>::max()); } while(0)
switch( eAutoScaleType )
{
case GDT_Byte: setDTMinMax(GByte); break;
case GDT_UInt16: setDTMinMax(GUInt16); break;
case GDT_Int16: setDTMinMax(GInt16); break;
case GDT_UInt32: setDTMinMax(GUInt32); break;
case GDT_Int32: setDTMinMax(GInt32); break;
case GDT_UInt64: setDTMinMax(std::uint64_t); break;
case GDT_Int64: setDTMinMax(std::int64_t); break;
default:
CPLAssert(false);
}
dstArray = CreateMDArray(srcArray->GetName(),
dstArrayDims,
GDALExtendedDataType::Create(eAutoScaleType),
aosArrayCO.List());
EXIT_OR_CONTINUE_IF_NULL(dstArray);
if( srcArray->GetRawNoDataValue() != nullptr )
{
// If there's a nodata value in the source array, reserve
// DTMax for that purpose in the target scaled array
if( !dstArray->SetNoDataValue(dfDTMax) )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot set nodata value");
return false;
}
dfDTMax --;
}
const double dfScale = dfMax > dfMin ?
(dfMax - dfMin) / (dfDTMax - dfDTMin) : 1.0;
const double dfOffset = dfMin - dfDTMin * dfScale;
if( !dstArray->SetOffset(dfOffset) ||
!dstArray->SetScale(dfScale) )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot set scale/offset");
return false;
}
auto poUnscaled = dstArray->GetUnscaled();
if( srcArray->GetRawNoDataValue() != nullptr )
{
poUnscaled->SetNoDataValue(
srcArray->GetNoDataValueAsDouble() );
}
// Copy source array into unscaled array
if( !poUnscaled->CopyFrom(poSrcDS,
srcArray.get(), bStrict,
nCurCost, nTotalCost,
pfnProgress, pProgressData) )
{
return false;
}
}
else
{
dstArray = CreateMDArray(srcArray->GetName(),
dstArrayDims,
srcArrayType,
aosArrayCO.List());
EXIT_OR_CONTINUE_IF_NULL(dstArray);
if( !dstArray->CopyFrom(poSrcDS,
srcArray.get(), bStrict,
nCurCost, nTotalCost,
pfnProgress, pProgressData) )
{
return false;
}
}
// If this array is the indexing variable of a dimension, link them
// together.
if( oIterDimName != mapSrcVariableNameToIndexedDimName.end() )
{
auto oCorrespondingDimIter = mapExistingDstDims.find(oIterDimName->second);
if( oCorrespondingDimIter != mapExistingDstDims.end() )
{
CPLErrorHandlerPusher oHandlerPusher(CPLQuietErrorHandler);
CPLErrorStateBackuper oErrorStateBackuper;
oCorrespondingDimIter->second->SetIndexingVariable(dstArray);
}
}
}
auto groupNames = poSrcGroup->GetGroupNames();
for( const auto& name: groupNames )
{
auto srcSubGroup = poSrcGroup->OpenGroup(name);
EXIT_OR_CONTINUE_IF_NULL(srcSubGroup);
auto dstSubGroup = CreateGroup(name);
EXIT_OR_CONTINUE_IF_NULL(dstSubGroup);
if( !dstSubGroup->CopyFrom(poDstRootGroup, poSrcDS,
srcSubGroup, bStrict,
nCurCost, nTotalCost,
pfnProgress, pProgressData,
papszOptions) )
return false;
}
if( !pfnProgress(double(nCurCost) / nTotalCost, "", pProgressData) )
return false;
return true;
}
catch( const std::exception& e )
{
CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
return false;
}
}
/************************************************************************/
/* GetInnerMostGroup() */
/************************************************************************/
//! @cond Doxygen_Suppress
const GDALGroup* GDALGroup::GetInnerMostGroup(