-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathzarr.h
1512 lines (1188 loc) · 48.3 KB
/
zarr.h
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
/******************************************************************************
*
* Project: GDAL
* Purpose: Zarr driver
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2021, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#ifndef ZARR_H
#define ZARR_H
#include "cpl_compressor.h"
#include "cpl_json.h"
#include "gdal_priv.h"
#include "gdal_pam.h"
#include "memmultidim.h"
#include <array>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#define ZARR_DEBUG_KEY "ZARR"
#define CRS_ATTRIBUTE_NAME "_CRS"
/************************************************************************/
/* ZarrDataset */
/************************************************************************/
class ZarrDataset final : public GDALDataset
{
friend class ZarrRasterBand;
std::shared_ptr<GDALGroup> m_poRootGroup{};
CPLStringList m_aosSubdatasets{};
std::array<double, 6> m_adfGeoTransform{{0.0, 1.0, 0.0, 0.0, 0.0, 1.0}};
bool m_bHasGT = false;
std::shared_ptr<GDALDimension> m_poDimX{};
std::shared_ptr<GDALDimension> m_poDimY{};
std::shared_ptr<GDALMDArray> m_poSingleArray{};
static GDALDataset *OpenMultidim(const char *pszFilename, bool bUpdateMode,
CSLConstList papszOpenOptions);
public:
explicit ZarrDataset(const std::shared_ptr<GDALGroup> &poRootGroup);
~ZarrDataset() override;
CPLErr FlushCache(bool bAtClosing = false) override;
static GDALDataset *Open(GDALOpenInfo *poOpenInfo);
static GDALDataset *
CreateMultiDimensional(const char *pszFilename,
CSLConstList /*papszRootGroupOptions*/,
CSLConstList /*papszOptions*/);
static GDALDataset *Create(const char *pszName, int nXSize, int nYSize,
int nBands, GDALDataType eType,
char **papszOptions);
const char *GetMetadataItem(const char *pszName,
const char *pszDomain) override;
char **GetMetadata(const char *pszDomain) override;
CPLErr SetMetadata(char **papszMetadata, const char *pszDomain) override;
const OGRSpatialReference *GetSpatialRef() const override;
CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
CPLErr GetGeoTransform(double *padfTransform) override;
CPLErr SetGeoTransform(double *padfTransform) override;
std::shared_ptr<GDALGroup> GetRootGroup() const override
{
return m_poRootGroup;
}
};
/************************************************************************/
/* ZarrRasterBand */
/************************************************************************/
class ZarrRasterBand final : public GDALRasterBand
{
friend class ZarrDataset;
std::shared_ptr<GDALMDArray> m_poArray;
GDALColorInterp m_eColorInterp = GCI_Undefined;
protected:
CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pData) override;
CPLErr IWriteBlock(int nBlockXOff, int nBlockYOff, void *pData) override;
CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
int nYSize, void *pData, int nBufXSize, int nBufYSize,
GDALDataType eBufType, GSpacing nPixelSpaceBuf,
GSpacing nLineSpaceBuf,
GDALRasterIOExtraArg *psExtraArg) override;
public:
explicit ZarrRasterBand(const std::shared_ptr<GDALMDArray> &poArray);
double GetNoDataValue(int *pbHasNoData) override;
int64_t GetNoDataValueAsInt64(int *pbHasNoData) override;
uint64_t GetNoDataValueAsUInt64(int *pbHasNoData) override;
CPLErr SetNoDataValue(double dfNoData) override;
CPLErr SetNoDataValueAsInt64(int64_t nNoData) override;
CPLErr SetNoDataValueAsUInt64(uint64_t nNoData) override;
double GetOffset(int *pbSuccess = nullptr) override;
CPLErr SetOffset(double dfNewOffset) override;
double GetScale(int *pbSuccess = nullptr) override;
CPLErr SetScale(double dfNewScale) override;
const char *GetUnitType() override;
CPLErr SetUnitType(const char *pszNewValue) override;
GDALColorInterp GetColorInterpretation() override;
CPLErr SetColorInterpretation(GDALColorInterp eColorInterp) override;
};
/************************************************************************/
/* ZarrAttributeGroup() */
/************************************************************************/
class ZarrAttributeGroup
{
// Use a MEMGroup as a convenient container for attributes.
const bool m_bContainerIsGroup;
std::shared_ptr<MEMGroup> m_poGroup;
bool m_bModified = false;
public:
explicit ZarrAttributeGroup(const std::string &osParentName,
bool bContainerIsGroup);
void Init(const CPLJSONObject &obj, bool bUpdatable);
std::shared_ptr<GDALAttribute> GetAttribute(const std::string &osName) const
{
return m_poGroup->GetAttribute(osName);
}
std::vector<std::shared_ptr<GDALAttribute>>
GetAttributes(CSLConstList papszOptions = nullptr) const
{
return m_poGroup->GetAttributes(papszOptions);
}
std::shared_ptr<GDALAttribute>
CreateAttribute(const std::string &osName,
const std::vector<GUInt64> &anDimensions,
const GDALExtendedDataType &oDataType,
CSLConstList /* papszOptions */ = nullptr)
{
auto poAttr = m_poGroup->CreateAttribute(osName, anDimensions,
oDataType, nullptr);
if (poAttr)
{
m_bModified = true;
}
return poAttr;
}
bool DeleteAttribute(const std::string &osName)
{
const bool bOK = m_poGroup->DeleteAttribute(osName, nullptr);
if (bOK)
{
m_bModified = true;
}
return bOK;
}
void SetUpdatable(bool bUpdatable)
{
auto attrs = m_poGroup->GetAttributes(nullptr);
for (auto &attr : attrs)
{
auto memAttr = std::dynamic_pointer_cast<MEMAttribute>(attr);
if (memAttr)
memAttr->SetWritable(bUpdatable);
}
}
void UnsetModified()
{
m_bModified = false;
auto attrs = m_poGroup->GetAttributes(nullptr);
for (auto &attr : attrs)
{
auto memAttr = std::dynamic_pointer_cast<MEMAttribute>(attr);
if (memAttr)
memAttr->SetModified(false);
}
}
bool IsModified() const
{
if (m_bModified)
return true;
const auto attrs = m_poGroup->GetAttributes(nullptr);
for (const auto &attr : attrs)
{
const auto memAttr = std::dynamic_pointer_cast<MEMAttribute>(attr);
if (memAttr && memAttr->IsModified())
return true;
}
return false;
}
CPLJSONObject Serialize() const;
void ParentRenamed(const std::string &osNewParentFullName);
void ParentDeleted();
};
/************************************************************************/
/* ZarrSharedResource */
/************************************************************************/
class ZarrGroupBase;
class ZarrSharedResource
: public std::enable_shared_from_this<ZarrSharedResource>
{
bool m_bUpdatable = false;
std::string m_osRootDirectoryName{};
bool m_bZMetadataEnabled = false;
CPLJSONObject m_oObj{}; // For .zmetadata
bool m_bZMetadataModified = false;
std::shared_ptr<GDALPamMultiDim> m_poPAM{};
CPLStringList m_aosOpenOptions{};
std::weak_ptr<ZarrGroupBase> m_poWeakRootGroup{};
std::set<std::string> m_oSetArrayInLoading{};
explicit ZarrSharedResource(const std::string &osRootDirectoryName,
bool bUpdatable);
std::shared_ptr<ZarrGroupBase> OpenRootGroup();
public:
static std::shared_ptr<ZarrSharedResource>
Create(const std::string &osRootDirectoryName, bool bUpdatable);
~ZarrSharedResource();
bool IsUpdatable() const
{
return m_bUpdatable;
}
void EnableZMetadata()
{
m_bZMetadataEnabled = true;
}
void SetZMetadataItem(const std::string &osFilename,
const CPLJSONObject &obj);
void DeleteZMetadataItemRecursive(const std::string &osFilename);
void RenameZMetadataRecursive(const std::string &osOldFilename,
const std::string &osNewFilename);
const std::shared_ptr<GDALPamMultiDim> &GetPAM()
{
return m_poPAM;
}
const CPLStringList &GetOpenOptions() const
{
return m_aosOpenOptions;
}
void SetOpenOptions(CSLConstList papszOpenOptions)
{
m_aosOpenOptions = papszOpenOptions;
}
void
UpdateDimensionSize(const std::shared_ptr<GDALDimension> &poUpdatedDim);
std::shared_ptr<ZarrGroupBase> GetRootGroup()
{
auto poRootGroup = m_poWeakRootGroup.lock();
if (poRootGroup)
return poRootGroup;
poRootGroup = OpenRootGroup();
m_poWeakRootGroup = poRootGroup;
return poRootGroup;
}
void SetRootGroup(const std::shared_ptr<ZarrGroupBase> &poRootGroup)
{
m_poWeakRootGroup = poRootGroup;
}
bool AddArrayInLoading(const std::string &osZarrayFilename);
void RemoveArrayInLoading(const std::string &osZarrayFilename);
struct SetFilenameAdder
{
std::shared_ptr<ZarrSharedResource> m_poSharedResource;
const std::string m_osFilename;
const bool m_bOK;
SetFilenameAdder(
const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osFilename)
: m_poSharedResource(poSharedResource), m_osFilename(osFilename),
m_bOK(m_poSharedResource->AddArrayInLoading(m_osFilename))
{
}
~SetFilenameAdder()
{
if (m_bOK)
m_poSharedResource->RemoveArrayInLoading(m_osFilename);
}
bool ok() const
{
return m_bOK;
}
};
};
/************************************************************************/
/* ZarrGroup */
/************************************************************************/
class ZarrArray;
class ZarrDimension;
class ZarrGroupBase CPL_NON_FINAL : public GDALGroup
{
protected:
friend class ZarrV2Group;
friend class ZarrV3Group;
// For ZarrV2, this is the directory of the group
// For ZarrV3, this is the root directory of the dataset
std::shared_ptr<ZarrSharedResource> m_poSharedResource;
std::string m_osDirectoryName{};
std::weak_ptr<ZarrGroupBase>
m_poParent{}; // weak reference to owning parent
std::shared_ptr<ZarrGroupBase>
m_poParentStrongRef{}; // strong reference, used only when opening from
// a subgroup
mutable std::map<CPLString, std::shared_ptr<ZarrGroupBase>> m_oMapGroups{};
mutable std::map<CPLString, std::shared_ptr<ZarrArray>> m_oMapMDArrays{};
mutable std::map<CPLString, std::shared_ptr<ZarrDimension>>
m_oMapDimensions{};
mutable bool m_bDirectoryExplored = false;
mutable std::vector<std::string> m_aosGroups{};
mutable std::vector<std::string> m_aosArrays{};
mutable ZarrAttributeGroup m_oAttrGroup;
mutable bool m_bAttributesLoaded = false;
bool m_bReadFromZMetadata = false;
mutable bool m_bDimensionsInstantiated = false;
bool m_bUpdatable = false;
bool m_bDimSizeInUpdate = false;
virtual void ExploreDirectory() const = 0;
virtual void LoadAttributes() const = 0;
ZarrGroupBase(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName)
: GDALGroup(osParentName, osName), m_poSharedResource(poSharedResource),
m_oAttrGroup(m_osFullName, /*bContainerIsGroup=*/true)
{
}
protected:
friend class ZarrDimension;
bool RenameDimension(const std::string &osOldName,
const std::string &osNewName);
void NotifyChildrenOfRenaming() override;
void NotifyChildrenOfDeletion() override;
public:
~ZarrGroupBase() override;
std::shared_ptr<GDALAttribute>
GetAttribute(const std::string &osName) const override
{
LoadAttributes();
return m_oAttrGroup.GetAttribute(osName);
}
std::vector<std::shared_ptr<GDALAttribute>>
GetAttributes(CSLConstList papszOptions = nullptr) const override
{
LoadAttributes();
return m_oAttrGroup.GetAttributes(papszOptions);
}
std::shared_ptr<GDALAttribute>
CreateAttribute(const std::string &osName,
const std::vector<GUInt64> &anDimensions,
const GDALExtendedDataType &oDataType,
CSLConstList papszOptions = nullptr) override;
bool DeleteAttribute(const std::string &osName,
CSLConstList papszOptions = nullptr) override;
std::vector<std::shared_ptr<GDALDimension>>
GetDimensions(CSLConstList papszOptions = nullptr) const override;
std::shared_ptr<GDALDimension>
CreateDimension(const std::string &osName, const std::string &osType,
const std::string &osDirection, GUInt64 nSize,
CSLConstList papszOptions = nullptr) override;
std::vector<std::string>
GetMDArrayNames(CSLConstList papszOptions = nullptr) const override;
std::vector<std::string>
GetGroupNames(CSLConstList papszOptions = nullptr) const override;
virtual std::shared_ptr<ZarrGroupBase>
OpenZarrGroup(const std::string &osName,
CSLConstList papszOptions = nullptr) const = 0;
std::shared_ptr<GDALGroup>
OpenGroup(const std::string &osName,
CSLConstList papszOptions = nullptr) const override
{
return std::static_pointer_cast<GDALGroup>(
OpenZarrGroup(osName, papszOptions));
}
bool DeleteGroup(const std::string &osName,
CSLConstList papszOptions = nullptr) override;
std::shared_ptr<GDALMDArray>
OpenMDArray(const std::string &osName,
CSLConstList papszOptions = nullptr) const override
{
return std::static_pointer_cast<GDALMDArray>(
OpenZarrArray(osName, papszOptions));
}
bool DeleteMDArray(const std::string &osName,
CSLConstList papszOptions = nullptr) override;
virtual std::shared_ptr<ZarrArray>
OpenZarrArray(const std::string &osName,
CSLConstList papszOptions = nullptr) const = 0;
void SetDirectoryName(const std::string &osDirectoryName)
{
m_osDirectoryName = osDirectoryName;
}
const std::string &GetDirectoryName() const
{
return m_osDirectoryName;
}
void RegisterArray(const std::shared_ptr<ZarrArray> &array) const;
void SetUpdatable(bool bUpdatable)
{
m_bUpdatable = bUpdatable;
}
void UpdateDimensionSize(const std::shared_ptr<GDALDimension> &poDim);
static bool IsValidObjectName(const std::string &osName);
bool Rename(const std::string &osNewName) override;
//! Returns false in case of error
bool
CheckArrayOrGroupWithSameNameDoesNotExist(const std::string &osName) const;
void ParentRenamed(const std::string &osNewParentFullName) override;
void NotifyArrayRenamed(const std::string &osOldName,
const std::string &osNewName);
};
/************************************************************************/
/* ZarrV2Group */
/************************************************************************/
class ZarrV2Group final : public ZarrGroupBase
{
void ExploreDirectory() const override;
void LoadAttributes() const override;
std::shared_ptr<ZarrV2Group>
GetOrCreateSubGroup(const std::string &osSubGroupFullname);
ZarrV2Group(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName)
: ZarrGroupBase(poSharedResource, osParentName, osName)
{
}
public:
static std::shared_ptr<ZarrV2Group>
Create(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName);
~ZarrV2Group() override;
static std::shared_ptr<ZarrV2Group>
CreateOnDisk(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName,
const std::string &osDirectoryName);
std::shared_ptr<ZarrArray>
OpenZarrArray(const std::string &osName,
CSLConstList papszOptions = nullptr) const override;
std::shared_ptr<ZarrGroupBase>
OpenZarrGroup(const std::string &osName,
CSLConstList papszOptions = nullptr) const override;
std::shared_ptr<GDALGroup>
CreateGroup(const std::string &osName,
CSLConstList papszOptions = nullptr) override;
std::shared_ptr<ZarrArray>
LoadArray(const std::string &osArrayName,
const std::string &osZarrayFilename, const CPLJSONObject &oRoot,
bool bLoadedFromZMetadata,
const CPLJSONObject &oAttributes) const;
std::shared_ptr<GDALMDArray> CreateMDArray(
const std::string &osName,
const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
const GDALExtendedDataType &oDataType,
CSLConstList papszOptions = nullptr) override;
void InitFromZMetadata(const CPLJSONObject &oRoot);
bool InitFromZGroup(const CPLJSONObject &oRoot);
};
/************************************************************************/
/* ZarrV3Group */
/************************************************************************/
class ZarrV3Group final : public ZarrGroupBase
{
void ExploreDirectory() const override;
void LoadAttributes() const override;
ZarrV3Group(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName,
const std::string &osDirectoryName);
public:
~ZarrV3Group() override;
static std::shared_ptr<ZarrV3Group>
Create(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName,
const std::string &osDirectoryName);
std::shared_ptr<ZarrArray>
OpenZarrArray(const std::string &osName,
CSLConstList papszOptions = nullptr) const override;
std::shared_ptr<ZarrGroupBase>
OpenZarrGroup(const std::string &osName,
CSLConstList papszOptions = nullptr) const override;
static std::shared_ptr<ZarrV3Group>
CreateOnDisk(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentFullName, const std::string &osName,
const std::string &osDirectoryName);
std::shared_ptr<GDALGroup>
CreateGroup(const std::string &osName,
CSLConstList papszOptions = nullptr) override;
std::shared_ptr<ZarrArray> LoadArray(const std::string &osArrayName,
const std::string &osZarrayFilename,
const CPLJSONObject &oRoot) const;
std::shared_ptr<GDALMDArray> CreateMDArray(
const std::string &osName,
const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
const GDALExtendedDataType &oDataType,
CSLConstList papszOptions = nullptr) override;
void SetExplored()
{
m_bDirectoryExplored = true;
}
};
/************************************************************************/
/* ZarrDimension */
/************************************************************************/
class ZarrDimension final : public GDALDimensionWeakIndexingVar
{
const bool m_bUpdatable;
std::weak_ptr<ZarrGroupBase> m_poParentGroup;
bool m_bModified = false;
bool m_bXArrayDim = false;
public:
ZarrDimension(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::weak_ptr<ZarrGroupBase> &poParentGroup,
const std::string &osParentName, const std::string &osName,
const std::string &osType, const std::string &osDirection,
GUInt64 nSize)
: GDALDimensionWeakIndexingVar(osParentName, osName, osType,
osDirection, nSize),
m_bUpdatable(poSharedResource->IsUpdatable()),
m_poParentGroup(poParentGroup)
{
}
bool Rename(const std::string &osNewName) override;
bool IsModified() const
{
return m_bModified;
}
void SetXArrayDimension()
{
m_bXArrayDim = true;
}
bool IsXArrayDimension() const
{
return m_bXArrayDim;
}
};
/************************************************************************/
/* DtypeElt() */
/************************************************************************/
struct DtypeElt
{
enum class NativeType
{
BOOLEAN,
UNSIGNED_INT,
SIGNED_INT,
IEEEFP,
COMPLEX_IEEEFP,
STRING_ASCII,
STRING_UNICODE
};
NativeType nativeType = NativeType::BOOLEAN;
size_t nativeOffset = 0;
size_t nativeSize = 0;
bool needByteSwapping = false;
bool gdalTypeIsApproxOfNative = false;
GDALExtendedDataType gdalType = GDALExtendedDataType::Create(GDT_Unknown);
size_t gdalOffset = 0;
size_t gdalSize = 0;
};
/************************************************************************/
/* ZarrByteVectorQuickResize */
/************************************************************************/
/* std::vector<GByte> with quick resizing (ie that doesn't zero out when
* growing back to a previously reached greater size).
*/
class ZarrByteVectorQuickResize
{
std::vector<GByte> m_oVec{};
size_t m_nSize = 0;
public:
ZarrByteVectorQuickResize() = default;
ZarrByteVectorQuickResize(const ZarrByteVectorQuickResize &) = delete;
ZarrByteVectorQuickResize &
operator=(const ZarrByteVectorQuickResize &) = delete;
ZarrByteVectorQuickResize(ZarrByteVectorQuickResize &&) = default;
ZarrByteVectorQuickResize &
operator=(ZarrByteVectorQuickResize &&) = default;
void resize(size_t nNewSize)
{
if (nNewSize > m_oVec.size())
m_oVec.resize(nNewSize);
m_nSize = nNewSize;
}
inline bool empty() const
{
return m_nSize == 0;
}
inline size_t size() const
{
return m_nSize;
}
inline size_t capacity() const
{
// Not a typo: the capacity of this object is the size
// of the underlying std::vector
return m_oVec.size();
}
inline GByte *data()
{
return m_oVec.data();
}
inline const GByte *data() const
{
return m_oVec.data();
}
inline GByte operator[](size_t idx) const
{
return m_oVec[idx];
}
inline GByte &operator[](size_t idx)
{
return m_oVec[idx];
}
};
/************************************************************************/
/* ZarrArray */
/************************************************************************/
class ZarrArray CPL_NON_FINAL : public GDALPamMDArray
{
protected:
std::shared_ptr<ZarrSharedResource> m_poSharedResource;
const std::vector<std::shared_ptr<GDALDimension>> m_aoDims;
const GDALExtendedDataType m_oType;
const std::vector<DtypeElt> m_aoDtypeElts;
const std::vector<GUInt64> m_anBlockSize;
CPLJSONObject m_dtype{};
GByte *m_pabyNoData = nullptr;
std::string m_osDimSeparator{"."};
std::string m_osFilename{};
size_t m_nTileSize = 0;
mutable ZarrByteVectorQuickResize m_abyRawTileData{};
mutable ZarrByteVectorQuickResize m_abyDecodedTileData{};
mutable std::vector<uint64_t> m_anCachedTiledIndices{};
mutable bool m_bCachedTiledValid = false;
mutable bool m_bCachedTiledEmpty = false;
mutable bool m_bDirtyTile = false;
bool m_bUseOptimizedCodePaths = true;
mutable ZarrAttributeGroup m_oAttrGroup;
mutable std::shared_ptr<OGRSpatialReference> m_poSRS{};
mutable bool m_bAllocateWorkingBuffersDone = false;
mutable bool m_bWorkingBuffersOK = false;
bool m_bUpdatable = false;
bool m_bDefinitionModified = false;
bool m_bSRSModified = false;
bool m_bNew = false;
std::string m_osUnit{};
bool m_bUnitModified = false;
double m_dfOffset = 0.0;
bool m_bHasOffset = false;
bool m_bOffsetModified = false;
double m_dfScale = 1.0;
bool m_bHasScale = false;
bool m_bScaleModified = false;
std::weak_ptr<ZarrGroupBase> m_poGroupWeak{};
uint64_t m_nTotalTileCount = 0;
mutable bool m_bHasTriedCacheTilePresenceArray = false;
mutable std::shared_ptr<GDALMDArray> m_poCacheTilePresenceArray{};
mutable std::mutex m_oMutex{};
struct CachedTile
{
ZarrByteVectorQuickResize abyDecoded{};
};
mutable std::map<uint64_t, CachedTile> m_oMapTileIndexToCachedTile{};
static uint64_t
ComputeTileCount(const std::string &osName,
const std::vector<std::shared_ptr<GDALDimension>> &aoDims,
const std::vector<GUInt64> &anBlockSize);
ZarrArray(const std::shared_ptr<ZarrSharedResource> &poSharedResource,
const std::string &osParentName, const std::string &osName,
const std::vector<std::shared_ptr<GDALDimension>> &aoDims,
const GDALExtendedDataType &oType,
const std::vector<DtypeElt> &aoDtypeElts,
const std::vector<GUInt64> &anBlockSize);
virtual bool LoadTileData(const uint64_t *tileIndices,
bool &bMissingTileOut) const = 0;
void BlockTranspose(const ZarrByteVectorQuickResize &abySrc,
ZarrByteVectorQuickResize &abyDst, bool bDecode) const;
virtual bool AllocateWorkingBuffers() const = 0;
void SerializeNumericNoData(CPLJSONObject &oRoot) const;
void DeallocateDecodedTileData();
virtual std::string GetDataDirectory() const = 0;
virtual CPLStringList
GetTileIndicesFromFilename(const char *pszFilename) const = 0;
virtual bool FlushDirtyTile() const = 0;
std::shared_ptr<GDALMDArray> OpenTilePresenceCache(bool bCanCreate) const;
void NotifyChildrenOfRenaming() override;
void NotifyChildrenOfDeletion() override;
static void EncodeElt(const std::vector<DtypeElt> &elts, const GByte *pSrc,
GByte *pDst);
// Disable copy constructor and assignment operator
ZarrArray(const ZarrArray &) = delete;
ZarrArray &operator=(const ZarrArray &) = delete;
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 IsEmptyTile(const ZarrByteVectorQuickResize &abyTile) const;
bool IAdviseReadCommon(const GUInt64 *arrayStartIdx, const size_t *count,
CSLConstList papszOptions,
std::vector<uint64_t> &anIndicesCur,
int &nThreadsMax,
std::vector<uint64_t> &anReqTilesIndices,
size_t &nReqTiles) const;
CPLJSONObject SerializeSpecialAttributes();
virtual std::string
BuildTileFilename(const uint64_t *tileIndices) const = 0;
bool SetStatistics(bool bApproxStats, double dfMin, double dfMax,
double dfMean, double dfStdDev, GUInt64 nValidCount,
CSLConstList papszOptions) override;
public:
~ZarrArray() override;
static bool ParseChunkSize(const CPLJSONArray &oChunks,
const GDALExtendedDataType &oType,
std::vector<GUInt64> &anBlockSize);
static bool FillBlockSize(
const std::vector<std::shared_ptr<GDALDimension>> &aoDimensions,
const GDALExtendedDataType &oDataType,
std::vector<GUInt64> &anBlockSize, CSLConstList papszOptions);
bool IsWritable() const override
{
return m_bUpdatable;
}
const std::string &GetFilename() const override
{
return m_osFilename;
}
const std::vector<std::shared_ptr<GDALDimension>> &
GetDimensions() const override
{
return m_aoDims;
}
const GDALExtendedDataType &GetDataType() const override
{
return m_oType;
}
std::vector<GUInt64> GetBlockSize() const override
{
return m_anBlockSize;
}
const void *GetRawNoDataValue() const override
{
return m_pabyNoData;
}
const std::string &GetUnit() const override
{
return m_osUnit;
}
bool SetUnit(const std::string &osUnit) override;
void RegisterUnit(const std::string &osUnit)
{
m_osUnit = osUnit;
}
void RegisterGroup(const std::weak_ptr<ZarrGroupBase> &group)
{
m_poGroupWeak = group;
}
double GetOffset(bool *pbHasOffset,
GDALDataType *peStorageType) const override;
double GetScale(bool *pbHasScale,
GDALDataType *peStorageType) const override;
bool SetOffset(double dfOffset, GDALDataType eStorageType) override;
bool SetScale(double dfScale, GDALDataType eStorageType) override;
std::vector<std::shared_ptr<GDALMDArray>>
GetCoordinateVariables() const override;
bool Resize(const std::vector<GUInt64> &anNewDimSizes,
CSLConstList) override;
void RegisterOffset(double dfOffset)
{
m_bHasOffset = true;
m_dfOffset = dfOffset;
}
void RegisterScale(double dfScale)
{
m_bHasScale = true;
m_dfScale = dfScale;
}
bool SetRawNoDataValue(const void *pRawNoData) override;
void RegisterNoDataValue(const void *);
void SetFilename(const std::string &osFilename)
{
m_osFilename = osFilename;
}
void SetDimSeparator(const std::string &osDimSeparator)
{
m_osDimSeparator = osDimSeparator;
}
void ParseSpecialAttributes(const std::shared_ptr<GDALGroup> &poGroup,
CPLJSONObject &oAttributes);
void SetAttributes(const CPLJSONObject &attrs)
{
m_oAttrGroup.Init(attrs, m_bUpdatable);
}
void SetSRS(const std::shared_ptr<OGRSpatialReference> &srs)
{
m_poSRS = srs;
}
std::shared_ptr<GDALAttribute>
GetAttribute(const std::string &osName) const override
{
return m_oAttrGroup.GetAttribute(osName);
}
std::vector<std::shared_ptr<GDALAttribute>>
GetAttributes(CSLConstList papszOptions) const override
{
return m_oAttrGroup.GetAttributes(papszOptions);
}
std::shared_ptr<GDALAttribute>
CreateAttribute(const std::string &osName,
const std::vector<GUInt64> &anDimensions,
const GDALExtendedDataType &oDataType,
CSLConstList papszOptions = nullptr) override;
bool DeleteAttribute(const std::string &osName,
CSLConstList papszOptions = nullptr) override;
std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override;