-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathgdaldataset.cpp
10442 lines (8898 loc) · 375 KB
/
gdaldataset.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
/******************************************************************************
*
* Project: GDAL Core
* Purpose: Base class for raster file formats.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1998, 2003, Frank Warmerdam
* Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include "gdal.h"
#include "gdal_priv.h"
#include <climits>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <mutex>
#include <new>
#include <set>
#include <string>
#include <utility>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_hash_set.h"
#include "cpl_multiproc.h"
#include "cpl_progress.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "cpl_vsi_error.h"
#include "gdal_alg.h"
#include "ogr_api.h"
#include "ogr_attrind.h"
#include "ogr_core.h"
#include "ogr_feature.h"
#include "ogr_featurestyle.h"
#include "ogr_gensql.h"
#include "ogr_geometry.h"
#include "ogr_p.h"
#include "ogr_spatialref.h"
#include "ogr_srs_api.h"
#include "ograpispy.h"
#include "ogrsf_frmts.h"
#include "ogrunionlayer.h"
#include "ogr_swq.h"
#include "../frmts/derived/derivedlist.h"
#ifdef SQLITE_ENABLED
#include "../sqlite/ogrsqliteexecutesql.h"
#endif
extern const swq_field_type SpecialFieldTypes[SPECIAL_FIELD_COUNT];
CPL_C_START
GDALAsyncReader *GDALGetDefaultAsyncReader(GDALDataset *poDS, int nXOff,
int nYOff, int nXSize, int nYSize,
void *pBuf, int nBufXSize,
int nBufYSize, GDALDataType eBufType,
int nBandCount, int *panBandMap,
int nPixelSpace, int nLineSpace,
int nBandSpace, char **papszOptions);
CPL_C_END
enum class GDALAllowReadWriteMutexState
{
RW_MUTEX_STATE_UNKNOWN,
RW_MUTEX_STATE_ALLOWED,
RW_MUTEX_STATE_DISABLED
};
const GIntBig TOTAL_FEATURES_NOT_INIT = -2;
const GIntBig TOTAL_FEATURES_UNKNOWN = -1;
class GDALDataset::Private
{
CPL_DISALLOW_COPY_ASSIGN(Private)
public:
CPLMutex *hMutex = nullptr;
std::map<GIntBig, int> oMapThreadToMutexTakenCount{};
#ifdef DEBUG_EXTRA
std::map<GIntBig, int> oMapThreadToMutexTakenCountSaved{};
#endif
GDALAllowReadWriteMutexState eStateReadWriteMutex =
GDALAllowReadWriteMutexState::RW_MUTEX_STATE_UNKNOWN;
int nCurrentLayerIdx = 0;
int nLayerCount = -1;
GIntBig nFeatureReadInLayer = 0;
GIntBig nFeatureReadInDataset = 0;
GIntBig nTotalFeaturesInLayer = TOTAL_FEATURES_NOT_INIT;
GIntBig nTotalFeatures = TOTAL_FEATURES_NOT_INIT;
OGRLayer *poCurrentLayer = nullptr;
std::mutex m_oMutexWKT{};
char *m_pszWKTCached = nullptr;
OGRSpatialReference *m_poSRSCached = nullptr;
char *m_pszWKTGCPCached = nullptr;
OGRSpatialReference *m_poSRSGCPCached = nullptr;
GDALDataset *poParentDataset = nullptr;
bool m_bOverviewsEnabled = true;
std::vector<int>
m_anBandMap{}; // used by RasterIO(). Values are 1, 2, etc.
Private() = default;
};
struct SharedDatasetCtxt
{
// PID of the thread that mark the dataset as shared
// This may not be the actual PID, but the responsiblePID.
GIntBig nPID;
char *pszDescription;
char *pszConcatenatedOpenOptions;
int nOpenFlags;
GDALDataset *poDS;
};
// Set of datasets opened as shared datasets (with GDALOpenShared)
// The values in the set are of type SharedDatasetCtxt.
static CPLHashSet *phSharedDatasetSet = nullptr;
// Set of all datasets created in the constructor of GDALDataset.
// In the case of a shared dataset, memorize the PID of the thread
// that marked the dataset as shared, so that we can remove it from
// the phSharedDatasetSet in the destructor of the dataset, even
// if GDALClose is called from a different thread.
static std::map<GDALDataset *, GIntBig> *poAllDatasetMap = nullptr;
static CPLMutex *hDLMutex = nullptr;
// Static array of all datasets. Used by GDALGetOpenDatasets.
// Not thread-safe. See GDALGetOpenDatasets.
static GDALDataset **ppDatasets = nullptr;
static unsigned long GDALSharedDatasetHashFunc(const void *elt)
{
const SharedDatasetCtxt *psStruct =
static_cast<const SharedDatasetCtxt *>(elt);
return static_cast<unsigned long>(
CPLHashSetHashStr(psStruct->pszDescription) ^
CPLHashSetHashStr(psStruct->pszConcatenatedOpenOptions) ^
psStruct->nOpenFlags ^ psStruct->nPID);
}
static int GDALSharedDatasetEqualFunc(const void *elt1, const void *elt2)
{
const SharedDatasetCtxt *psStruct1 =
static_cast<const SharedDatasetCtxt *>(elt1);
const SharedDatasetCtxt *psStruct2 =
static_cast<const SharedDatasetCtxt *>(elt2);
return strcmp(psStruct1->pszDescription, psStruct2->pszDescription) == 0 &&
strcmp(psStruct1->pszConcatenatedOpenOptions,
psStruct2->pszConcatenatedOpenOptions) == 0 &&
psStruct1->nPID == psStruct2->nPID &&
psStruct1->nOpenFlags == psStruct2->nOpenFlags;
}
static void GDALSharedDatasetFreeFunc(void *elt)
{
SharedDatasetCtxt *psStruct = static_cast<SharedDatasetCtxt *>(elt);
CPLFree(psStruct->pszDescription);
CPLFree(psStruct->pszConcatenatedOpenOptions);
CPLFree(psStruct);
}
static std::string
GDALSharedDatasetConcatenateOpenOptions(CSLConstList papszOpenOptions)
{
std::string osStr;
for (const char *pszOption : cpl::Iterate(papszOpenOptions))
osStr += pszOption;
return osStr;
}
/************************************************************************/
/* Functions shared between gdalproxypool.cpp and gdaldataset.cpp */
/************************************************************************/
// The open-shared mutex must be used by the ProxyPool too.
CPLMutex **GDALGetphDLMutex()
{
return &hDLMutex;
}
// The current thread will act in the behalf of the thread of PID
// responsiblePID.
void GDALSetResponsiblePIDForCurrentThread(GIntBig responsiblePID)
{
GIntBig *pResponsiblePID =
static_cast<GIntBig *>(CPLGetTLS(CTLS_RESPONSIBLEPID));
if (pResponsiblePID == nullptr)
{
pResponsiblePID = static_cast<GIntBig *>(CPLMalloc(sizeof(GIntBig)));
CPLSetTLS(CTLS_RESPONSIBLEPID, pResponsiblePID, TRUE);
}
*pResponsiblePID = responsiblePID;
}
// Get the PID of the thread that the current thread will act in the behalf of
// By default : the current thread acts in the behalf of itself.
GIntBig GDALGetResponsiblePIDForCurrentThread()
{
GIntBig *pResponsiblePID =
static_cast<GIntBig *>(CPLGetTLS(CTLS_RESPONSIBLEPID));
if (pResponsiblePID == nullptr)
return CPLGetPID();
return *pResponsiblePID;
}
/************************************************************************/
/* ==================================================================== */
/* GDALDataset */
/* ==================================================================== */
/************************************************************************/
/**
* \class GDALDataset "gdal_priv.h"
*
* A dataset encapsulating one or more raster bands. Details are further
* discussed in the <a href="https://gdal.org/user/raster_data_model.html">GDAL
* Raster Data Model</a>.
*
* Use GDALOpen() or GDALOpenShared() to create a GDALDataset for a named file,
* or GDALDriver::Create() or GDALDriver::CreateCopy() to create a new
* dataset.
*/
/************************************************************************/
/* GDALDataset() */
/************************************************************************/
//! @cond Doxygen_Suppress
GDALDataset::GDALDataset()
: GDALDataset(CPLTestBool(CPLGetConfigOption("GDAL_FORCE_CACHING", "NO")))
{
}
GDALDataset::GDALDataset(int bForceCachedIOIn)
: bForceCachedIO(CPL_TO_BOOL(bForceCachedIOIn)),
m_poPrivate(new(std::nothrow) GDALDataset::Private)
{
}
//! @endcond
/************************************************************************/
/* ~GDALDataset() */
/************************************************************************/
/**
* \brief Destroy an open GDALDataset.
*
* This is the accepted method of closing a GDAL dataset and deallocating
* all resources associated with it.
*
* Equivalent of the C callable GDALClose(). Except that GDALClose() first
* decrements the reference count, and then closes only if it has dropped to
* zero.
*
* For Windows users, it is not recommended to use the delete operator on the
* dataset object because of known issues when allocating and freeing memory
* across module boundaries. Calling GDALClose() is then a better option.
*/
GDALDataset::~GDALDataset()
{
// we don't want to report destruction of datasets that
// were never really open or meant as internal
if (!bIsInternal && (nBands != 0 || !EQUAL(GetDescription(), "")))
{
if (CPLGetPID() != GDALGetResponsiblePIDForCurrentThread())
CPLDebug("GDAL",
"GDALClose(%s, this=%p) (pid=%d, responsiblePID=%d)",
GetDescription(), this, static_cast<int>(CPLGetPID()),
static_cast<int>(GDALGetResponsiblePIDForCurrentThread()));
else
CPLDebug("GDAL", "GDALClose(%s, this=%p)", GetDescription(), this);
}
if (IsMarkedSuppressOnClose())
{
if (poDriver == nullptr ||
// Someone issuing Create("foo.tif") on a
// memory driver doesn't expect files with those names to be deleted
// on a file system...
// This is somewhat messy. Ideally there should be a way for the
// driver to overload the default behavior
(!EQUAL(poDriver->GetDescription(), "MEM") &&
!EQUAL(poDriver->GetDescription(), "Memory")))
{
VSIUnlink(GetDescription());
}
}
/* -------------------------------------------------------------------- */
/* Remove dataset from the "open" dataset list. */
/* -------------------------------------------------------------------- */
if (!bIsInternal)
{
CPLMutexHolderD(&hDLMutex);
if (poAllDatasetMap)
{
std::map<GDALDataset *, GIntBig>::iterator oIter =
poAllDatasetMap->find(this);
CPLAssert(oIter != poAllDatasetMap->end());
UnregisterFromSharedDataset();
poAllDatasetMap->erase(oIter);
if (poAllDatasetMap->empty())
{
delete poAllDatasetMap;
poAllDatasetMap = nullptr;
if (phSharedDatasetSet)
{
CPLHashSetDestroy(phSharedDatasetSet);
}
phSharedDatasetSet = nullptr;
CPLFree(ppDatasets);
ppDatasets = nullptr;
}
}
}
/* -------------------------------------------------------------------- */
/* Destroy the raster bands if they exist. */
/* -------------------------------------------------------------------- */
for (int i = 0; i < nBands && papoBands != nullptr; ++i)
{
if (papoBands[i] != nullptr)
delete papoBands[i];
papoBands[i] = nullptr;
}
CPLFree(papoBands);
if (m_poStyleTable)
{
delete m_poStyleTable;
m_poStyleTable = nullptr;
}
if (m_poPrivate != nullptr)
{
if (m_poPrivate->hMutex != nullptr)
CPLDestroyMutex(m_poPrivate->hMutex);
// coverity[missing_lock]
CPLFree(m_poPrivate->m_pszWKTCached);
if (m_poPrivate->m_poSRSCached)
{
m_poPrivate->m_poSRSCached->Release();
}
// coverity[missing_lock]
CPLFree(m_poPrivate->m_pszWKTGCPCached);
if (m_poPrivate->m_poSRSGCPCached)
{
m_poPrivate->m_poSRSGCPCached->Release();
}
}
delete m_poPrivate;
CSLDestroy(papszOpenOptions);
}
/************************************************************************/
/* Close() */
/************************************************************************/
/** Do final cleanup before a dataset is destroyed.
*
* This method is typically called by GDALClose() or the destructor of a
* GDALDataset subclass. It might also be called by C++ users before
* destroying a dataset. It should not be called on a shared dataset whose
* reference count is greater than one.
*
* It gives a last chance to the closing process to return an error code if
* something goes wrong, in particular in creation / update scenarios where
* file write or network communication might occur when finalizing the dataset.
*
* Implementations should be robust to this method to be called several times
* (on subsequent calls, it should do nothing and return CE_None).
* Once it has been called, no other method than Close() or the dataset
* destructor should be called. RasterBand or OGRLayer owned by the dataset
* should be assumed as no longer being valid.
*
* If a driver implements this method, it must also call it from its
* dataset destructor.
*
* A typical implementation might look as the following
* \code{.cpp}
*
* MyDataset::~MyDataset()
* {
* try
* {
* MyDataset::Close();
* }
* catch (const std::exception &exc)
* {
* // If Close() can throw exception
* CPLError(CE_Failure, CPLE_AppDefined,
* "Exception thrown in MyDataset::Close(): %s",
* exc.what());
* }
* catch (...)
* {
* // If Close() can throw exception
* CPLError(CE_Failure, CPLE_AppDefined,
* "Exception thrown in MyDataset::Close()");
* }
* }
*
* CPLErr MyDataset::Close()
* {
* CPLErr eErr = CE_None;
* if( nOpenFlags != OPEN_FLAGS_CLOSED )
* {
* if( MyDataset::FlushCache(true) != CE_None )
* eErr = CE_Failure;
*
* // Do something driver specific
* if (m_fpImage)
* {
* if( VSIFCloseL(m_fpImage) != 0 )
* {
* CPLError(CE_Failure, CPLE_FileIO, "VSIFCloseL() failed");
* eErr = CE_Failure;
* }
* }
*
* // Call parent Close() implementation.
* if( MyParentDatasetClass::Close() != CE_None )
* eErr = CE_Failure;
* }
* return eErr;
* }
* \endcode
*
* @since GDAL 3.7
*/
CPLErr GDALDataset::Close()
{
// Call UnregisterFromSharedDataset() before altering nOpenFlags
UnregisterFromSharedDataset();
nOpenFlags = OPEN_FLAGS_CLOSED;
return CE_None;
}
/************************************************************************/
/* UnregisterFromSharedDataset() */
/************************************************************************/
void GDALDataset::UnregisterFromSharedDataset()
{
if (!(!bIsInternal && bShared && poAllDatasetMap && phSharedDatasetSet))
return;
CPLMutexHolderD(&hDLMutex);
std::map<GDALDataset *, GIntBig>::iterator oIter =
poAllDatasetMap->find(this);
CPLAssert(oIter != poAllDatasetMap->end());
const GIntBig nPIDCreatorForShared = oIter->second;
bShared = false;
SharedDatasetCtxt sStruct;
sStruct.nPID = nPIDCreatorForShared;
sStruct.nOpenFlags = nOpenFlags & ~GDAL_OF_SHARED;
sStruct.pszDescription = const_cast<char *>(GetDescription());
std::string osConcatenatedOpenOptions =
GDALSharedDatasetConcatenateOpenOptions(papszOpenOptions);
sStruct.pszConcatenatedOpenOptions = &osConcatenatedOpenOptions[0];
sStruct.poDS = nullptr;
SharedDatasetCtxt *psStruct = static_cast<SharedDatasetCtxt *>(
CPLHashSetLookup(phSharedDatasetSet, &sStruct));
if (psStruct && psStruct->poDS == this)
{
CPLHashSetRemove(phSharedDatasetSet, psStruct);
}
else
{
CPLDebug("GDAL",
"Should not happen. Cannot find %s, "
"this=%p in phSharedDatasetSet",
GetDescription(), this);
}
}
/************************************************************************/
/* AddToDatasetOpenList() */
/************************************************************************/
void GDALDataset::AddToDatasetOpenList()
{
/* -------------------------------------------------------------------- */
/* Add this dataset to the open dataset list. */
/* -------------------------------------------------------------------- */
bIsInternal = false;
CPLMutexHolderD(&hDLMutex);
if (poAllDatasetMap == nullptr)
poAllDatasetMap = new std::map<GDALDataset *, GIntBig>;
(*poAllDatasetMap)[this] = -1;
}
/************************************************************************/
/* FlushCache() */
/************************************************************************/
/**
* \brief Flush all write cached data to disk.
*
* Any raster (or other GDAL) data written via GDAL calls, but buffered
* internally will be written to disk.
*
* The default implementation of this method just calls the FlushCache() method
* on each of the raster bands and the SyncToDisk() method
* on each of the layers. Conceptually, calling FlushCache() on a dataset
* should include any work that might be accomplished by calling SyncToDisk()
* on layers in that dataset.
*
* Using this method does not prevent use from calling GDALClose()
* to properly close a dataset and ensure that important data not addressed
* by FlushCache() is written in the file.
*
* This method is the same as the C function GDALFlushCache().
*
* @param bAtClosing Whether this is called from a GDALDataset destructor
* @return CE_None in case of success (note: return value added in GDAL 3.7)
*/
CPLErr GDALDataset::FlushCache(bool bAtClosing)
{
CPLErr eErr = CE_None;
// This sometimes happens if a dataset is destroyed before completely
// built.
if (papoBands)
{
for (int i = 0; i < nBands; ++i)
{
if (papoBands[i])
{
if (papoBands[i]->FlushCache(bAtClosing) != CE_None)
eErr = CE_Failure;
}
}
}
const int nLayers = GetLayerCount();
// cppcheck-suppress knownConditionTrueFalse
if (nLayers > 0)
{
CPLMutexHolderD(m_poPrivate ? &(m_poPrivate->hMutex) : nullptr);
for (int i = 0; i < nLayers; ++i)
{
OGRLayer *poLayer = GetLayer(i);
if (poLayer)
{
if (poLayer->SyncToDisk() != OGRERR_NONE)
eErr = CE_Failure;
}
}
}
return eErr;
}
/************************************************************************/
/* GDALFlushCache() */
/************************************************************************/
/**
* \brief Flush all write cached data to disk.
*
* @see GDALDataset::FlushCache().
* @return CE_None in case of success (note: return value added in GDAL 3.7)
*/
CPLErr CPL_STDCALL GDALFlushCache(GDALDatasetH hDS)
{
VALIDATE_POINTER1(hDS, "GDALFlushCache", CE_Failure);
return GDALDataset::FromHandle(hDS)->FlushCache(false);
}
/************************************************************************/
/* DropCache() */
/************************************************************************/
/**
* \brief Drop all write cached data
*
* This method is the same as the C function GDALDropCache().
*
* @return CE_None in case of success
* @since 3.9
*/
CPLErr GDALDataset::DropCache()
{
CPLErr eErr = CE_None;
if (papoBands)
{
for (int i = 0; i < nBands; ++i)
{
if (papoBands[i])
{
if (papoBands[i]->DropCache() != CE_None)
eErr = CE_Failure;
}
}
}
return eErr;
}
/************************************************************************/
/* GDALDropCache() */
/************************************************************************/
/**
* \brief Drop all write cached data
*
* @see GDALDataset::DropCache().
* @return CE_None in case of success
* @since 3.9
*/
CPLErr CPL_STDCALL GDALDropCache(GDALDatasetH hDS)
{
VALIDATE_POINTER1(hDS, "GDALDropCache", CE_Failure);
return GDALDataset::FromHandle(hDS)->DropCache();
}
/************************************************************************/
/* GetEstimatedRAMUsage() */
/************************************************************************/
/**
* \brief Return the intrinsic RAM usage of this dataset.
*
* The returned value should take into account caches in the underlying driver
* and decoding library, but not the usage related to the GDAL block cache.
*
* At time of writing, this method is only implemented in the JP2OpenJPEG
* driver. For single-tiled JPEG2000 images, the decoding of the image,
* even partially, involves allocating at least
* width * height * number_of_bands * sizeof(uint32_t) bytes inside the libopenjp2
* library.
*
* This method is used by the GDALDatasetPool class, itself used by the GDAL VRT
* driver, to determine how long a dataset in the pool must be kept open, given
* the RAM usage of the dataset with respect to the usable total RAM.
*
* @since GDAL 3.7
* @return RAM usage in bytes, or -1 if unknown (the default implementation
* returns -1)
*/
GIntBig GDALDataset::GetEstimatedRAMUsage()
{
return -1;
}
/************************************************************************/
/* BlockBasedFlushCache() */
/* */
/* This helper method can be called by the */
/* GDALDataset::FlushCache() for particular drivers to ensure */
/* that buffers will be flushed in a manner suitable for pixel */
/* interleaved (by block) IO. That is, if all the bands have */
/* the same size blocks then a given block will be flushed for */
/* all bands before proceeding to the next block. */
/************************************************************************/
//! @cond Doxygen_Suppress
CPLErr GDALDataset::BlockBasedFlushCache(bool bAtClosing)
{
GDALRasterBand *poBand1 = GetRasterBand(1);
if (poBand1 == nullptr || (IsMarkedSuppressOnClose() && bAtClosing))
{
return GDALDataset::FlushCache(bAtClosing);
}
int nBlockXSize = 0;
int nBlockYSize = 0;
poBand1->GetBlockSize(&nBlockXSize, &nBlockYSize);
/* -------------------------------------------------------------------- */
/* Verify that all bands match. */
/* -------------------------------------------------------------------- */
for (int iBand = 1; iBand < nBands; ++iBand)
{
GDALRasterBand *poBand = GetRasterBand(iBand + 1);
int nThisBlockXSize, nThisBlockYSize;
poBand->GetBlockSize(&nThisBlockXSize, &nThisBlockYSize);
if (nThisBlockXSize != nBlockXSize && nThisBlockYSize != nBlockYSize)
{
return GDALDataset::FlushCache(bAtClosing);
}
}
/* -------------------------------------------------------------------- */
/* Now flush writable data. */
/* -------------------------------------------------------------------- */
for (int iY = 0; iY < poBand1->nBlocksPerColumn; ++iY)
{
for (int iX = 0; iX < poBand1->nBlocksPerRow; ++iX)
{
for (int iBand = 0; iBand < nBands; ++iBand)
{
const CPLErr eErr = papoBands[iBand]->FlushBlock(iX, iY);
if (eErr != CE_None)
return CE_Failure;
}
}
}
return CE_None;
}
/************************************************************************/
/* RasterInitialize() */
/* */
/* Initialize raster size */
/************************************************************************/
void GDALDataset::RasterInitialize(int nXSize, int nYSize)
{
CPLAssert(nXSize > 0 && nYSize > 0);
nRasterXSize = nXSize;
nRasterYSize = nYSize;
}
//! @endcond
/************************************************************************/
/* AddBand() */
/************************************************************************/
/**
* \fn GDALDataset::AddBand(GDALDataType, char**)
* \brief Add a band to a dataset.
*
* This method will add a new band to the dataset if the underlying format
* supports this action. Most formats do not.
*
* Note that the new GDALRasterBand is not returned. It may be fetched
* after successful completion of the method by calling
* GDALDataset::GetRasterBand(GDALDataset::GetRasterCount()) as the newest
* band will always be the last band.
*
* @param eType the data type of the pixels in the new band.
*
* @param papszOptions a list of NAME=VALUE option strings. The supported
* options are format specific. NULL may be passed by default.
*
* @return CE_None on success or CE_Failure on failure.
*/
CPLErr GDALDataset::AddBand(CPL_UNUSED GDALDataType eType,
CPL_UNUSED char **papszOptions)
{
ReportError(CE_Failure, CPLE_NotSupported,
"Dataset does not support the AddBand() method.");
return CE_Failure;
}
/************************************************************************/
/* GDALAddBand() */
/************************************************************************/
/**
* \brief Add a band to a dataset.
*
* @see GDALDataset::AddBand().
*/
CPLErr CPL_STDCALL GDALAddBand(GDALDatasetH hDataset, GDALDataType eType,
CSLConstList papszOptions)
{
VALIDATE_POINTER1(hDataset, "GDALAddBand", CE_Failure);
return GDALDataset::FromHandle(hDataset)->AddBand(
eType, const_cast<char **>(papszOptions));
}
/************************************************************************/
/* SetBand() */
/************************************************************************/
//! @cond Doxygen_Suppress
/** Set a band in the band array, updating the band count, and array size
* appropriately.
*
* @param nNewBand new band number (indexing starts at 1)
* @param poBand band object.
*/
void GDALDataset::SetBand(int nNewBand, GDALRasterBand *poBand)
{
/* -------------------------------------------------------------------- */
/* Do we need to grow the bands list? */
/* -------------------------------------------------------------------- */
if (nBands < nNewBand || papoBands == nullptr)
{
GDALRasterBand **papoNewBands = nullptr;
if (papoBands == nullptr)
papoNewBands = static_cast<GDALRasterBand **>(VSICalloc(
sizeof(GDALRasterBand *), std::max(nNewBand, nBands)));
else
papoNewBands = static_cast<GDALRasterBand **>(
VSIRealloc(papoBands, sizeof(GDALRasterBand *) *
std::max(nNewBand, nBands)));
if (papoNewBands == nullptr)
{
ReportError(CE_Failure, CPLE_OutOfMemory,
"Cannot allocate band array");
return;
}
papoBands = papoNewBands;
for (int i = nBands; i < nNewBand; ++i)
papoBands[i] = nullptr;
nBands = std::max(nBands, nNewBand);
if (m_poPrivate)
{
for (int i = static_cast<int>(m_poPrivate->m_anBandMap.size());
i < nBands; ++i)
{
m_poPrivate->m_anBandMap.push_back(i + 1);
}
}
}
/* -------------------------------------------------------------------- */
/* Set the band. Resetting the band is currently not permitted. */
/* -------------------------------------------------------------------- */
if (papoBands[nNewBand - 1] != nullptr)
{
ReportError(CE_Failure, CPLE_NotSupported,
"Cannot set band %d as it is already set", nNewBand);
return;
}
papoBands[nNewBand - 1] = poBand;
/* -------------------------------------------------------------------- */
/* Set back reference information on the raster band. Note */
/* that the GDALDataset is a friend of the GDALRasterBand */
/* specifically to allow this. */
/* -------------------------------------------------------------------- */
poBand->nBand = nNewBand;
poBand->poDS = this;
poBand->nRasterXSize = nRasterXSize;
poBand->nRasterYSize = nRasterYSize;
poBand->eAccess = eAccess; // Default access to be same as dataset.
}
//! @endcond
/************************************************************************/
/* SetBand() */
/************************************************************************/
//! @cond Doxygen_Suppress
/** Set a band in the band array, updating the band count, and array size
* appropriately.
*
* @param nNewBand new band number (indexing starts at 1)
* @param poBand band object.
*/
void GDALDataset::SetBand(int nNewBand, std::unique_ptr<GDALRasterBand> poBand)
{
SetBand(nNewBand, poBand.release());
}
//! @endcond
/************************************************************************/
/* GetRasterXSize() */
/************************************************************************/
/**
\brief Fetch raster width in pixels.
Equivalent of the C function GDALGetRasterXSize().
@return the width in pixels of raster bands in this GDALDataset.
*/
int GDALDataset::GetRasterXSize() const
{
return nRasterXSize;
}
/************************************************************************/
/* GDALGetRasterXSize() */
/************************************************************************/
/**
* \brief Fetch raster width in pixels.
*
* @see GDALDataset::GetRasterXSize().
*/
int CPL_STDCALL GDALGetRasterXSize(GDALDatasetH hDataset)
{
VALIDATE_POINTER1(hDataset, "GDALGetRasterXSize", 0);
return GDALDataset::FromHandle(hDataset)->GetRasterXSize();
}
/************************************************************************/
/* GetRasterYSize() */
/************************************************************************/
/**
\brief Fetch raster height in pixels.
Equivalent of the C function GDALGetRasterYSize().
@return the height in pixels of raster bands in this GDALDataset.
*/
int GDALDataset::GetRasterYSize() const
{
return nRasterYSize;
}
/************************************************************************/
/* GDALGetRasterYSize() */
/************************************************************************/
/**
* \brief Fetch raster height in pixels.
*
* @see GDALDataset::GetRasterYSize().
*/
int CPL_STDCALL GDALGetRasterYSize(GDALDatasetH hDataset)
{
VALIDATE_POINTER1(hDataset, "GDALGetRasterYSize", 0);
return GDALDataset::FromHandle(hDataset)->GetRasterYSize();
}
/************************************************************************/
/* GetRasterBand() */
/************************************************************************/
/**
\brief Fetch a band object for a dataset.