Coverage Report

Created: 2025-11-16 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/gcore/gdaldataset.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL Core
4
 * Purpose:  Base class for raster file formats.
5
 * Author:   Frank Warmerdam, warmerdam@pobox.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 1998, 2003, Frank Warmerdam
9
 * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "cpl_port.h"
15
16
#include <array>
17
#include <cassert>
18
#include <climits>
19
#include <cstdarg>
20
#include <cstdio>
21
#include <cstdlib>
22
#include <cstring>
23
#include <algorithm>
24
#include <map>
25
#include <mutex>
26
#include <new>
27
#include <set>
28
#include <string>
29
#include <utility>
30
31
#include "cpl_conv.h"
32
#include "cpl_error.h"
33
#include "cpl_hash_set.h"
34
#include "cpl_multiproc.h"
35
#include "cpl_progress.h"
36
#include "cpl_string.h"
37
#include "cpl_vsi.h"
38
#include "cpl_vsi_error.h"
39
40
#include "gdal.h"
41
#include "gdal_alg.h"
42
#include "gdal_abstractbandblockcache.h"
43
#include "gdalantirecursion.h"
44
#include "gdal_dataset.h"
45
#include "gdalsubdatasetinfo.h"
46
47
#include "ogr_api.h"
48
#include "ogr_attrind.h"
49
#include "ogr_core.h"
50
#include "ogr_feature.h"
51
#include "ogr_featurestyle.h"
52
#include "ogr_gensql.h"
53
#include "ogr_geometry.h"
54
#include "ogr_p.h"
55
#include "ogr_spatialref.h"
56
#include "ogr_srs_api.h"
57
#include "ograpispy.h"
58
#include "ogrsf_frmts.h"
59
#include "ogrunionlayer.h"
60
#include "ogr_swq.h"
61
#include "memmultidim.h"
62
#include "gdalmultidim_priv.h"
63
64
#include "../frmts/derived/derivedlist.h"
65
66
#ifdef SQLITE_ENABLED
67
#include "../sqlite/ogrsqliteexecutesql.h"
68
#endif
69
70
extern const swq_field_type SpecialFieldTypes[SPECIAL_FIELD_COUNT];
71
72
CPL_C_START
73
GDALAsyncReader *GDALGetDefaultAsyncReader(GDALDataset *poDS, int nXOff,
74
                                           int nYOff, int nXSize, int nYSize,
75
                                           void *pBuf, int nBufXSize,
76
                                           int nBufYSize, GDALDataType eBufType,
77
                                           int nBandCount, int *panBandMap,
78
                                           int nPixelSpace, int nLineSpace,
79
                                           int nBandSpace, char **papszOptions);
80
CPL_C_END
81
82
enum class GDALAllowReadWriteMutexState
83
{
84
    RW_MUTEX_STATE_UNKNOWN,
85
    RW_MUTEX_STATE_ALLOWED,
86
    RW_MUTEX_STATE_DISABLED
87
};
88
89
const GIntBig TOTAL_FEATURES_NOT_INIT = -2;
90
const GIntBig TOTAL_FEATURES_UNKNOWN = -1;
91
92
class GDALDataset::Private
93
{
94
    CPL_DISALLOW_COPY_ASSIGN(Private)
95
96
  public:
97
    CPLMutex *hMutex = nullptr;
98
    std::map<GIntBig, int> oMapThreadToMutexTakenCount{};
99
#ifdef DEBUG_EXTRA
100
    std::map<GIntBig, int> oMapThreadToMutexTakenCountSaved{};
101
#endif
102
    GDALAllowReadWriteMutexState eStateReadWriteMutex =
103
        GDALAllowReadWriteMutexState::RW_MUTEX_STATE_UNKNOWN;
104
    int nCurrentLayerIdx = 0;
105
    int nLayerCount = -1;
106
    GIntBig nFeatureReadInLayer = 0;
107
    GIntBig nFeatureReadInDataset = 0;
108
    GIntBig nTotalFeaturesInLayer = TOTAL_FEATURES_NOT_INIT;
109
    GIntBig nTotalFeatures = TOTAL_FEATURES_NOT_INIT;
110
    OGRLayer *poCurrentLayer = nullptr;
111
112
    std::mutex m_oMutexWKT{};
113
114
    char *m_pszWKTCached = nullptr;
115
    OGRSpatialReference *m_poSRSCached = nullptr;
116
    char *m_pszWKTGCPCached = nullptr;
117
    OGRSpatialReference *m_poSRSGCPCached = nullptr;
118
119
    GDALDataset *poParentDataset = nullptr;
120
121
    bool m_bOverviewsEnabled = true;
122
123
    std::vector<int>
124
        m_anBandMap{};  // used by RasterIO(). Values are 1, 2, etc.
125
126
0
    Private() = default;
127
};
128
129
struct SharedDatasetCtxt
130
{
131
    // PID of the thread that mark the dataset as shared
132
    // This may not be the actual PID, but the responsiblePID.
133
    GIntBig nPID;
134
    char *pszDescription;
135
    char *pszConcatenatedOpenOptions;
136
    int nOpenFlags;
137
138
    GDALDataset *poDS;
139
};
140
141
// Set of datasets opened as shared datasets (with GDALOpenShared)
142
// The values in the set are of type SharedDatasetCtxt.
143
static CPLHashSet *phSharedDatasetSet = nullptr;
144
145
// Set of all datasets created in the constructor of GDALDataset.
146
// In the case of a shared dataset, memorize the PID of the thread
147
// that marked the dataset as shared, so that we can remove it from
148
// the phSharedDatasetSet in the destructor of the dataset, even
149
// if GDALClose is called from a different thread.
150
static std::map<GDALDataset *, GIntBig> *poAllDatasetMap = nullptr;
151
152
static CPLMutex *hDLMutex = nullptr;
153
154
// Static array of all datasets. Used by GDALGetOpenDatasets.
155
// Not thread-safe. See GDALGetOpenDatasets.
156
static GDALDataset **ppDatasets = nullptr;
157
158
static unsigned long GDALSharedDatasetHashFunc(const void *elt)
159
0
{
160
0
    const SharedDatasetCtxt *psStruct =
161
0
        static_cast<const SharedDatasetCtxt *>(elt);
162
0
    return static_cast<unsigned long>(
163
0
        CPLHashSetHashStr(psStruct->pszDescription) ^
164
0
        CPLHashSetHashStr(psStruct->pszConcatenatedOpenOptions) ^
165
0
        psStruct->nOpenFlags ^ psStruct->nPID);
166
0
}
167
168
static int GDALSharedDatasetEqualFunc(const void *elt1, const void *elt2)
169
0
{
170
0
    const SharedDatasetCtxt *psStruct1 =
171
0
        static_cast<const SharedDatasetCtxt *>(elt1);
172
0
    const SharedDatasetCtxt *psStruct2 =
173
0
        static_cast<const SharedDatasetCtxt *>(elt2);
174
0
    return strcmp(psStruct1->pszDescription, psStruct2->pszDescription) == 0 &&
175
0
           strcmp(psStruct1->pszConcatenatedOpenOptions,
176
0
                  psStruct2->pszConcatenatedOpenOptions) == 0 &&
177
0
           psStruct1->nPID == psStruct2->nPID &&
178
0
           psStruct1->nOpenFlags == psStruct2->nOpenFlags;
179
0
}
180
181
static void GDALSharedDatasetFreeFunc(void *elt)
182
0
{
183
0
    SharedDatasetCtxt *psStruct = static_cast<SharedDatasetCtxt *>(elt);
184
0
    CPLFree(psStruct->pszDescription);
185
0
    CPLFree(psStruct->pszConcatenatedOpenOptions);
186
0
    CPLFree(psStruct);
187
0
}
188
189
static std::string
190
GDALSharedDatasetConcatenateOpenOptions(CSLConstList papszOpenOptions)
191
0
{
192
0
    std::string osStr;
193
0
    for (const char *pszOption : cpl::Iterate(papszOpenOptions))
194
0
        osStr += pszOption;
195
0
    return osStr;
196
0
}
197
198
/************************************************************************/
199
/* Functions shared between gdalproxypool.cpp and gdaldataset.cpp */
200
/************************************************************************/
201
202
// The open-shared mutex must be used by the ProxyPool too.
203
CPLMutex **GDALGetphDLMutex()
204
0
{
205
0
    return &hDLMutex;
206
0
}
207
208
// The current thread will act in the behalf of the thread of PID
209
// responsiblePID.
210
void GDALSetResponsiblePIDForCurrentThread(GIntBig responsiblePID)
211
0
{
212
0
    GIntBig *pResponsiblePID =
213
0
        static_cast<GIntBig *>(CPLGetTLS(CTLS_RESPONSIBLEPID));
214
0
    if (pResponsiblePID == nullptr)
215
0
    {
216
0
        pResponsiblePID = static_cast<GIntBig *>(CPLMalloc(sizeof(GIntBig)));
217
0
        CPLSetTLS(CTLS_RESPONSIBLEPID, pResponsiblePID, TRUE);
218
0
    }
219
0
    *pResponsiblePID = responsiblePID;
220
0
}
221
222
// Get the PID of the thread that the current thread will act in the behalf of
223
// By default : the current thread acts in the behalf of itself.
224
GIntBig GDALGetResponsiblePIDForCurrentThread()
225
0
{
226
0
    GIntBig *pResponsiblePID =
227
0
        static_cast<GIntBig *>(CPLGetTLS(CTLS_RESPONSIBLEPID));
228
0
    if (pResponsiblePID == nullptr)
229
0
        return CPLGetPID();
230
0
    return *pResponsiblePID;
231
0
}
232
233
/************************************************************************/
234
/* ==================================================================== */
235
/*                             GDALDataset                              */
236
/* ==================================================================== */
237
/************************************************************************/
238
239
/**
240
 * \class GDALDataset "gdal_priv.h"
241
 *
242
 * A dataset encapsulating one or more raster bands.  Details are further
243
 * discussed in the <a href="https://gdal.org/user/raster_data_model.html">GDAL
244
 * Raster Data Model</a>.
245
 *
246
 * Use GDALOpen() or GDALOpenShared() to create a GDALDataset for a named file,
247
 * or GDALDriver::Create() or GDALDriver::CreateCopy() to create a new
248
 * dataset.
249
 */
250
251
/************************************************************************/
252
/*                            GDALDataset()                             */
253
/************************************************************************/
254
255
//! @cond Doxygen_Suppress
256
GDALDataset::GDALDataset()
257
0
    : GDALDataset(CPLTestBool(CPLGetConfigOption("GDAL_FORCE_CACHING", "NO")))
258
0
{
259
0
}
260
261
GDALDataset::GDALDataset(int bForceCachedIOIn)
262
0
    : bForceCachedIO(CPL_TO_BOOL(bForceCachedIOIn)),
263
0
      m_poPrivate(new(std::nothrow) GDALDataset::Private)
264
0
{
265
0
}
266
267
//! @endcond
268
269
/************************************************************************/
270
/*                            ~GDALDataset()                            */
271
/************************************************************************/
272
273
/**
274
 * \brief Destroy an open GDALDataset.
275
 *
276
 * This is the accepted method of closing a GDAL dataset and deallocating
277
 * all resources associated with it.
278
 *
279
 * Equivalent of the C callable GDALClose().  Except that GDALClose() first
280
 * decrements the reference count, and then closes only if it has dropped to
281
 * zero.
282
 *
283
 * For Windows users, it is not recommended to use the delete operator on the
284
 * dataset object because of known issues when allocating and freeing memory
285
 * across module boundaries. Calling GDALClose() is then a better option.
286
 */
287
288
GDALDataset::~GDALDataset()
289
290
0
{
291
    // we don't want to report destruction of datasets that
292
    // were never really open or meant as internal
293
0
    if (!bIsInternal && (nBands != 0 || !EQUAL(GetDescription(), "")))
294
0
    {
295
0
        if (CPLGetPID() != GDALGetResponsiblePIDForCurrentThread())
296
0
            CPLDebug("GDAL",
297
0
                     "GDALClose(%s, this=%p) (pid=%d, responsiblePID=%d)",
298
0
                     GetDescription(), this, static_cast<int>(CPLGetPID()),
299
0
                     static_cast<int>(GDALGetResponsiblePIDForCurrentThread()));
300
0
        else
301
0
            CPLDebug("GDAL", "GDALClose(%s, this=%p)", GetDescription(), this);
302
0
    }
303
304
0
    GDALDataset::Close();
305
306
    /* -------------------------------------------------------------------- */
307
    /*      Remove dataset from the "open" dataset list.                    */
308
    /* -------------------------------------------------------------------- */
309
0
    if (!bIsInternal)
310
0
    {
311
0
        CPLMutexHolderD(&hDLMutex);
312
0
        if (poAllDatasetMap)
313
0
        {
314
0
            std::map<GDALDataset *, GIntBig>::iterator oIter =
315
0
                poAllDatasetMap->find(this);
316
0
            CPLAssert(oIter != poAllDatasetMap->end());
317
318
0
            UnregisterFromSharedDataset();
319
320
0
            poAllDatasetMap->erase(oIter);
321
322
0
            if (poAllDatasetMap->empty())
323
0
            {
324
0
                delete poAllDatasetMap;
325
0
                poAllDatasetMap = nullptr;
326
0
                if (phSharedDatasetSet)
327
0
                {
328
0
                    CPLHashSetDestroy(phSharedDatasetSet);
329
0
                }
330
0
                phSharedDatasetSet = nullptr;
331
0
                CPLFree(ppDatasets);
332
0
                ppDatasets = nullptr;
333
0
            }
334
0
        }
335
0
    }
336
337
    /* -------------------------------------------------------------------- */
338
    /*      Destroy the raster bands if they exist.                         */
339
    /* -------------------------------------------------------------------- */
340
0
    for (int i = 0; i < nBands && papoBands != nullptr; ++i)
341
0
    {
342
0
        if (papoBands[i] != nullptr)
343
0
            delete papoBands[i];
344
0
        papoBands[i] = nullptr;
345
0
    }
346
347
0
    CPLFree(papoBands);
348
349
0
    if (m_poStyleTable)
350
0
    {
351
0
        delete m_poStyleTable;
352
0
        m_poStyleTable = nullptr;
353
0
    }
354
355
0
    if (m_poPrivate != nullptr)
356
0
    {
357
0
        if (m_poPrivate->hMutex != nullptr)
358
0
            CPLDestroyMutex(m_poPrivate->hMutex);
359
360
0
#if defined(__COVERITY__) || defined(DEBUG)
361
        // Not needed since at destruction there is no risk of concurrent use.
362
0
        std::lock_guard oLock(m_poPrivate->m_oMutexWKT);
363
0
#endif
364
0
        CPLFree(m_poPrivate->m_pszWKTCached);
365
0
        if (m_poPrivate->m_poSRSCached)
366
0
        {
367
0
            m_poPrivate->m_poSRSCached->Release();
368
0
        }
369
0
        CPLFree(m_poPrivate->m_pszWKTGCPCached);
370
0
        if (m_poPrivate->m_poSRSGCPCached)
371
0
        {
372
0
            m_poPrivate->m_poSRSGCPCached->Release();
373
0
        }
374
0
    }
375
376
0
    delete m_poPrivate;
377
378
0
    CSLDestroy(papszOpenOptions);
379
0
}
380
381
/************************************************************************/
382
/*                             Close()                                  */
383
/************************************************************************/
384
385
/** Do final cleanup before a dataset is destroyed.
386
 *
387
 * This method is typically called by GDALClose() or the destructor of a
388
 * GDALDataset subclass. It might also be called by C++ users before
389
 * destroying a dataset. It should not be called on a shared dataset whose
390
 * reference count is greater than one.
391
 *
392
 * It gives a last chance to the closing process to return an error code if
393
 * something goes wrong, in particular in creation / update scenarios where
394
 * file write or network communication might occur when finalizing the dataset.
395
 *
396
 * Implementations should be robust to this method to be called several times
397
 * (on subsequent calls, it should do nothing and return CE_None).
398
 * Once it has been called, no other method than Close() or the dataset
399
 * destructor should be called. RasterBand or OGRLayer owned by the dataset
400
 * should be assumed as no longer being valid.
401
 *
402
 * If a driver implements this method, it must also call it from its
403
 * dataset destructor.
404
 *
405
 * This is the equivalent of C function GDALDatasetRunCloseWithoutDestroying().
406
 *
407
 * A typical implementation might look as the following
408
 * \code{.cpp}
409
 *
410
 *  MyDataset::~MyDataset()
411
 *  {
412
 *     try
413
 *     {
414
 *         MyDataset::Close();
415
 *     }
416
 *     catch (const std::exception &exc)
417
 *     {
418
 *         // If Close() can throw exception
419
 *         CPLError(CE_Failure, CPLE_AppDefined,
420
 *                  "Exception thrown in MyDataset::Close(): %s",
421
 *                  exc.what());
422
 *     }
423
 *     catch (...)
424
 *     {
425
 *         // If Close() can throw exception
426
 *         CPLError(CE_Failure, CPLE_AppDefined,
427
 *                  "Exception thrown in MyDataset::Close()");
428
 *     }
429
 *  }
430
 *
431
 *  CPLErr MyDataset::Close()
432
 *  {
433
 *      CPLErr eErr = CE_None;
434
 *      if( nOpenFlags != OPEN_FLAGS_CLOSED )
435
 *      {
436
 *          eErr = MyDataset::FlushCache(true);
437
 *
438
 *          // Do something driver specific
439
 *          if (m_fpImage)
440
 *          {
441
 *              if( VSIFCloseL(m_fpImage) != 0 )
442
 *              {
443
 *                  CPLError(CE_Failure, CPLE_FileIO, "VSIFCloseL() failed");
444
 *                  eErr = CE_Failure;
445
 *              }
446
 *          }
447
 *
448
 *          // Call parent Close() implementation.
449
 *          eErr = GDAL::Combine(eErr, MyParentDatasetClass::Close());
450
 *      }
451
 *      return eErr;
452
 *  }
453
 * \endcode
454
 *
455
 * @since GDAL 3.7
456
 */
457
CPLErr GDALDataset::Close()
458
0
{
459
0
    if (nOpenFlags != OPEN_FLAGS_CLOSED)
460
0
    {
461
        // Call UnregisterFromSharedDataset() before altering nOpenFlags
462
0
        UnregisterFromSharedDataset();
463
464
0
        nOpenFlags = OPEN_FLAGS_CLOSED;
465
0
    }
466
467
0
    if (IsMarkedSuppressOnClose())
468
0
    {
469
0
        if (poDriver == nullptr ||
470
            // Someone issuing Create("foo.tif") on a
471
            // memory driver doesn't expect files with those names to be deleted
472
            // on a file system...
473
            // This is somewhat messy. Ideally there should be a way for the
474
            // driver to overload the default behavior
475
0
            (!EQUAL(poDriver->GetDescription(), "MEM") &&
476
0
             !EQUAL(poDriver->GetDescription(), "Memory")))
477
0
        {
478
0
            if (VSIUnlink(GetDescription()) == 0)
479
0
                UnMarkSuppressOnClose();
480
0
        }
481
0
    }
482
483
0
    return CE_None;
484
0
}
485
486
/************************************************************************/
487
/*                   GDALDatasetRunCloseWithoutDestroying()             */
488
/************************************************************************/
489
490
/** Run the Close() method, without running destruction of the object.
491
 *
492
 * This ensures that content that should be written to file is written and
493
 * that all file descriptors are closed.
494
 *
495
 * Note that this is different from GDALClose() which also destroys
496
 * the underlying C++ object. GDALClose() or GDALReleaseDataset() are actually
497
 * the only functions that can be safely called on the dataset handle after
498
 * this function has been called.
499
 *
500
 * Most users want to use GDALClose() or GDALReleaseDataset() rather than
501
 * this function.
502
 *
503
 * This function is equivalent to the C++ method GDALDataset:Close()
504
 *
505
 * @param hDS dataset handle.
506
 * @return CE_None if no error
507
 *
508
 * @since GDAL 3.12
509
 * @see GDALClose()
510
 */
511
CPLErr GDALDatasetRunCloseWithoutDestroying(GDALDatasetH hDS)
512
0
{
513
0
    VALIDATE_POINTER1(hDS, __func__, CE_Failure);
514
0
    return GDALDataset::FromHandle(hDS)->Close();
515
0
}
516
517
/************************************************************************/
518
/*                UnregisterFromSharedDataset()                         */
519
/************************************************************************/
520
521
void GDALDataset::UnregisterFromSharedDataset()
522
0
{
523
0
    if (!(!bIsInternal && bShared && poAllDatasetMap && phSharedDatasetSet))
524
0
        return;
525
526
0
    CPLMutexHolderD(&hDLMutex);
527
528
0
    std::map<GDALDataset *, GIntBig>::iterator oIter =
529
0
        poAllDatasetMap->find(this);
530
0
    CPLAssert(oIter != poAllDatasetMap->end());
531
0
    const GIntBig nPIDCreatorForShared = oIter->second;
532
0
    bShared = false;
533
0
    SharedDatasetCtxt sStruct;
534
0
    sStruct.nPID = nPIDCreatorForShared;
535
0
    sStruct.nOpenFlags = nOpenFlags & ~GDAL_OF_SHARED;
536
0
    sStruct.pszDescription = const_cast<char *>(GetDescription());
537
0
    std::string osConcatenatedOpenOptions =
538
0
        GDALSharedDatasetConcatenateOpenOptions(papszOpenOptions);
539
0
    sStruct.pszConcatenatedOpenOptions = &osConcatenatedOpenOptions[0];
540
0
    sStruct.poDS = nullptr;
541
0
    SharedDatasetCtxt *psStruct = static_cast<SharedDatasetCtxt *>(
542
0
        CPLHashSetLookup(phSharedDatasetSet, &sStruct));
543
0
    if (psStruct && psStruct->poDS == this)
544
0
    {
545
0
        CPLHashSetRemove(phSharedDatasetSet, psStruct);
546
0
    }
547
0
    else
548
0
    {
549
0
        CPLDebug("GDAL",
550
0
                 "Should not happen. Cannot find %s, "
551
0
                 "this=%p in phSharedDatasetSet",
552
0
                 GetDescription(), this);
553
0
    }
554
0
}
555
556
/************************************************************************/
557
/*                      AddToDatasetOpenList()                          */
558
/************************************************************************/
559
560
void GDALDataset::AddToDatasetOpenList()
561
0
{
562
    /* -------------------------------------------------------------------- */
563
    /*      Add this dataset to the open dataset list.                      */
564
    /* -------------------------------------------------------------------- */
565
0
    bIsInternal = false;
566
567
0
    CPLMutexHolderD(&hDLMutex);
568
569
0
    if (poAllDatasetMap == nullptr)
570
0
        poAllDatasetMap = new std::map<GDALDataset *, GIntBig>;
571
0
    (*poAllDatasetMap)[this] = -1;
572
0
}
573
574
/************************************************************************/
575
/*                             FlushCache()                             */
576
/************************************************************************/
577
578
/**
579
 * \brief Flush all write cached data to disk.
580
 *
581
 * Any raster (or other GDAL) data written via GDAL calls, but buffered
582
 * internally will be written to disk.
583
 *
584
 * The default implementation of this method just calls the FlushCache() method
585
 * on each of the raster bands and the SyncToDisk() method
586
 * on each of the layers.  Conceptually, calling FlushCache() on a dataset
587
 * should include any work that might be accomplished by calling SyncToDisk()
588
 * on layers in that dataset.
589
 *
590
 * Using this method does not prevent use from calling GDALClose()
591
 * to properly close a dataset and ensure that important data not addressed
592
 * by FlushCache() is written in the file.
593
 *
594
 * This method is the same as the C function GDALFlushCache().
595
 *
596
 * @param bAtClosing Whether this is called from a GDALDataset destructor
597
 * @return CE_None in case of success (note: return value added in GDAL 3.7)
598
 */
599
600
CPLErr GDALDataset::FlushCache(bool bAtClosing)
601
602
0
{
603
0
    CPLErr eErr = CE_None;
604
    // This sometimes happens if a dataset is destroyed before completely
605
    // built.
606
607
0
    if (papoBands)
608
0
    {
609
0
        for (int i = 0; i < nBands; ++i)
610
0
        {
611
0
            if (papoBands[i])
612
0
            {
613
0
                if (papoBands[i]->FlushCache(bAtClosing) != CE_None)
614
0
                    eErr = CE_Failure;
615
0
            }
616
0
        }
617
0
    }
618
619
0
    const int nLayers = GetLayerCount();
620
    // cppcheck-suppress knownConditionTrueFalse
621
0
    if (nLayers > 0)
622
0
    {
623
0
        CPLMutexHolderD(m_poPrivate ? &(m_poPrivate->hMutex) : nullptr);
624
0
        for (int i = 0; i < nLayers; ++i)
625
0
        {
626
0
            OGRLayer *poLayer = GetLayer(i);
627
628
0
            if (poLayer)
629
0
            {
630
0
                if (poLayer->SyncToDisk() != OGRERR_NONE)
631
0
                    eErr = CE_Failure;
632
0
            }
633
0
        }
634
0
    }
635
636
0
    return eErr;
637
0
}
638
639
/************************************************************************/
640
/*                           GDALFlushCache()                           */
641
/************************************************************************/
642
643
/**
644
 * \brief Flush all write cached data to disk.
645
 *
646
 * @see GDALDataset::FlushCache().
647
 * @return CE_None in case of success (note: return value added in GDAL 3.7)
648
 */
649
650
CPLErr CPL_STDCALL GDALFlushCache(GDALDatasetH hDS)
651
652
0
{
653
0
    VALIDATE_POINTER1(hDS, "GDALFlushCache", CE_Failure);
654
655
0
    return GDALDataset::FromHandle(hDS)->FlushCache(false);
656
0
}
657
658
/************************************************************************/
659
/*                             DropCache()                              */
660
/************************************************************************/
661
662
/**
663
* \brief Drop all write cached data
664
*
665
* This method is the same as the C function GDALDropCache().
666
*
667
* @return CE_None in case of success
668
* @since 3.9
669
*/
670
671
CPLErr GDALDataset::DropCache()
672
673
0
{
674
0
    CPLErr eErr = CE_None;
675
676
0
    if (papoBands)
677
0
    {
678
0
        for (int i = 0; i < nBands; ++i)
679
0
        {
680
0
            if (papoBands[i])
681
0
            {
682
0
                if (papoBands[i]->DropCache() != CE_None)
683
0
                    eErr = CE_Failure;
684
0
            }
685
0
        }
686
0
    }
687
688
0
    return eErr;
689
0
}
690
691
/************************************************************************/
692
/*                           GDALDropCache()                           */
693
/************************************************************************/
694
695
/**
696
* \brief Drop all write cached data
697
*
698
* @see GDALDataset::DropCache().
699
* @return CE_None in case of success
700
* @since 3.9
701
*/
702
703
CPLErr CPL_STDCALL GDALDropCache(GDALDatasetH hDS)
704
705
0
{
706
0
    VALIDATE_POINTER1(hDS, "GDALDropCache", CE_Failure);
707
708
0
    return GDALDataset::FromHandle(hDS)->DropCache();
709
0
}
710
711
/************************************************************************/
712
/*                      GetEstimatedRAMUsage()                          */
713
/************************************************************************/
714
715
/**
716
 * \brief Return the intrinsic RAM usage of this dataset.
717
 *
718
 * The returned value should take into account caches in the underlying driver
719
 * and decoding library, but not the usage related to the GDAL block cache.
720
 *
721
 * At time of writing, this method is only implemented in the JP2OpenJPEG
722
 * driver. For single-tiled JPEG2000 images, the decoding of the image,
723
 * even partially, involves allocating at least
724
 * width * height * number_of_bands * sizeof(uint32_t) bytes inside the libopenjp2
725
 * library.
726
 *
727
 * This method is used by the GDALDatasetPool class, itself used by the GDAL VRT
728
 * driver, to determine how long a dataset in the pool must be kept open, given
729
 * the RAM usage of the dataset with respect to the usable total RAM.
730
 *
731
 * @since GDAL 3.7
732
 * @return RAM usage in bytes, or -1 if unknown (the default implementation
733
 * returns -1)
734
 */
735
736
GIntBig GDALDataset::GetEstimatedRAMUsage()
737
0
{
738
0
    return -1;
739
0
}
740
741
/************************************************************************/
742
/*                        BlockBasedFlushCache()                        */
743
/*                                                                      */
744
/*      This helper method can be called by the                         */
745
/*      GDALDataset::FlushCache() for particular drivers to ensure      */
746
/*      that buffers will be flushed in a manner suitable for pixel     */
747
/*      interleaved (by block) IO.  That is, if all the bands have      */
748
/*      the same size blocks then a given block will be flushed for     */
749
/*      all bands before proceeding to the next block.                  */
750
/************************************************************************/
751
752
//! @cond Doxygen_Suppress
753
CPLErr GDALDataset::BlockBasedFlushCache(bool bAtClosing)
754
755
0
{
756
0
    GDALRasterBand *poBand1 = GetRasterBand(1);
757
0
    if (poBand1 == nullptr || (IsMarkedSuppressOnClose() && bAtClosing))
758
0
    {
759
0
        return GDALDataset::FlushCache(bAtClosing);
760
0
    }
761
762
0
    int nBlockXSize = 0;
763
0
    int nBlockYSize = 0;
764
0
    poBand1->GetBlockSize(&nBlockXSize, &nBlockYSize);
765
766
    /* -------------------------------------------------------------------- */
767
    /*      Verify that all bands match.                                    */
768
    /* -------------------------------------------------------------------- */
769
0
    for (int iBand = 1; iBand < nBands; ++iBand)
770
0
    {
771
0
        GDALRasterBand *poBand = GetRasterBand(iBand + 1);
772
773
0
        int nThisBlockXSize, nThisBlockYSize;
774
0
        poBand->GetBlockSize(&nThisBlockXSize, &nThisBlockYSize);
775
0
        if (nThisBlockXSize != nBlockXSize && nThisBlockYSize != nBlockYSize)
776
0
        {
777
0
            return GDALDataset::FlushCache(bAtClosing);
778
0
        }
779
0
    }
780
781
    /* -------------------------------------------------------------------- */
782
    /*      Now flush writable data.                                        */
783
    /* -------------------------------------------------------------------- */
784
0
    for (int iY = 0; iY < poBand1->nBlocksPerColumn; ++iY)
785
0
    {
786
0
        for (int iX = 0; iX < poBand1->nBlocksPerRow; ++iX)
787
0
        {
788
0
            for (int iBand = 0; iBand < nBands; ++iBand)
789
0
            {
790
0
                const CPLErr eErr = papoBands[iBand]->FlushBlock(iX, iY);
791
792
0
                if (eErr != CE_None)
793
0
                    return CE_Failure;
794
0
            }
795
0
        }
796
0
    }
797
0
    return CE_None;
798
0
}
799
800
/************************************************************************/
801
/*                          RasterInitialize()                          */
802
/*                                                                      */
803
/*      Initialize raster size                                          */
804
/************************************************************************/
805
806
void GDALDataset::RasterInitialize(int nXSize, int nYSize)
807
808
0
{
809
0
    CPLAssert(nXSize > 0 && nYSize > 0);
810
811
0
    nRasterXSize = nXSize;
812
0
    nRasterYSize = nYSize;
813
0
}
814
815
//! @endcond
816
817
/************************************************************************/
818
/*                              AddBand()                               */
819
/************************************************************************/
820
821
/**
822
 * \fn GDALDataset::AddBand(GDALDataType, char**)
823
 * \brief Add a band to a dataset.
824
 *
825
 * This method will add a new band to the dataset if the underlying format
826
 * supports this action.  Most formats do not.
827
 *
828
 * Note that the new GDALRasterBand is not returned.  It may be fetched
829
 * after successful completion of the method by calling
830
 * GDALDataset::GetRasterBand(GDALDataset::GetRasterCount()) as the newest
831
 * band will always be the last band.
832
 *
833
 * @param eType the data type of the pixels in the new band.
834
 *
835
 * @param papszOptions a list of NAME=VALUE option strings.  The supported
836
 * options are format specific.  NULL may be passed by default.
837
 *
838
 * @return CE_None on success or CE_Failure on failure.
839
 */
840
841
CPLErr GDALDataset::AddBand(CPL_UNUSED GDALDataType eType,
842
                            CPL_UNUSED char **papszOptions)
843
844
0
{
845
0
    ReportError(CE_Failure, CPLE_NotSupported,
846
0
                "Dataset does not support the AddBand() method.");
847
848
0
    return CE_Failure;
849
0
}
850
851
/************************************************************************/
852
/*                            GDALAddBand()                             */
853
/************************************************************************/
854
855
/**
856
 * \brief Add a band to a dataset.
857
 *
858
 * @see GDALDataset::AddBand().
859
 */
860
861
CPLErr CPL_STDCALL GDALAddBand(GDALDatasetH hDataset, GDALDataType eType,
862
                               CSLConstList papszOptions)
863
864
0
{
865
0
    VALIDATE_POINTER1(hDataset, "GDALAddBand", CE_Failure);
866
867
0
    return GDALDataset::FromHandle(hDataset)->AddBand(
868
0
        eType, const_cast<char **>(papszOptions));
869
0
}
870
871
/************************************************************************/
872
/*                              SetBand()                               */
873
/************************************************************************/
874
875
//! @cond Doxygen_Suppress
876
/**  Set a band in the band array, updating the band count, and array size
877
 * appropriately.
878
 *
879
 * @param nNewBand new band number (indexing starts at 1)
880
 * @param poBand band object.
881
 */
882
883
void GDALDataset::SetBand(int nNewBand, GDALRasterBand *poBand)
884
885
0
{
886
    /* -------------------------------------------------------------------- */
887
    /*      Do we need to grow the bands list?                              */
888
    /* -------------------------------------------------------------------- */
889
0
    if (nBands < nNewBand || papoBands == nullptr)
890
0
    {
891
0
        GDALRasterBand **papoNewBands = nullptr;
892
893
0
        if (papoBands == nullptr)
894
0
            papoNewBands = static_cast<GDALRasterBand **>(VSICalloc(
895
0
                sizeof(GDALRasterBand *), std::max(nNewBand, nBands)));
896
0
        else
897
0
            papoNewBands = static_cast<GDALRasterBand **>(
898
0
                VSIRealloc(papoBands, sizeof(GDALRasterBand *) *
899
0
                                          std::max(nNewBand, nBands)));
900
0
        if (papoNewBands == nullptr)
901
0
        {
902
0
            ReportError(CE_Failure, CPLE_OutOfMemory,
903
0
                        "Cannot allocate band array");
904
0
            return;
905
0
        }
906
907
0
        papoBands = papoNewBands;
908
909
0
        for (int i = nBands; i < nNewBand; ++i)
910
0
            papoBands[i] = nullptr;
911
912
0
        nBands = std::max(nBands, nNewBand);
913
914
0
        if (m_poPrivate)
915
0
        {
916
0
            for (int i = static_cast<int>(m_poPrivate->m_anBandMap.size());
917
0
                 i < nBands; ++i)
918
0
            {
919
0
                m_poPrivate->m_anBandMap.push_back(i + 1);
920
0
            }
921
0
        }
922
0
    }
923
924
    /* -------------------------------------------------------------------- */
925
    /*      Set the band.  Resetting the band is currently not permitted.   */
926
    /* -------------------------------------------------------------------- */
927
0
    if (papoBands[nNewBand - 1] != nullptr)
928
0
    {
929
0
        ReportError(CE_Failure, CPLE_NotSupported,
930
0
                    "Cannot set band %d as it is already set", nNewBand);
931
0
        return;
932
0
    }
933
934
0
    papoBands[nNewBand - 1] = poBand;
935
936
    /* -------------------------------------------------------------------- */
937
    /*      Set back reference information on the raster band.  Note        */
938
    /*      that the GDALDataset is a friend of the GDALRasterBand          */
939
    /*      specifically to allow this.                                     */
940
    /* -------------------------------------------------------------------- */
941
0
    poBand->nBand = nNewBand;
942
0
    poBand->poDS = this;
943
0
    poBand->nRasterXSize = nRasterXSize;
944
0
    poBand->nRasterYSize = nRasterYSize;
945
0
    poBand->eAccess = eAccess;  // Default access to be same as dataset.
946
0
}
947
948
//! @endcond
949
950
/************************************************************************/
951
/*                              SetBand()                               */
952
/************************************************************************/
953
954
//! @cond Doxygen_Suppress
955
/**  Set a band in the band array, updating the band count, and array size
956
 * appropriately.
957
 *
958
 * @param nNewBand new band number (indexing starts at 1)
959
 * @param poBand band object.
960
 */
961
962
void GDALDataset::SetBand(int nNewBand, std::unique_ptr<GDALRasterBand> poBand)
963
0
{
964
0
    SetBand(nNewBand, poBand.release());
965
0
}
966
967
//! @endcond
968
969
/************************************************************************/
970
/*                           GetRasterXSize()                           */
971
/************************************************************************/
972
973
/**
974
975
 \brief Fetch raster width in pixels.
976
977
 Equivalent of the C function GDALGetRasterXSize().
978
979
 @return the width in pixels of raster bands in this GDALDataset.
980
981
*/
982
983
int GDALDataset::GetRasterXSize() const
984
0
{
985
0
    return nRasterXSize;
986
0
}
987
988
/************************************************************************/
989
/*                         GDALGetRasterXSize()                         */
990
/************************************************************************/
991
992
/**
993
 * \brief Fetch raster width in pixels.
994
 *
995
 * @see GDALDataset::GetRasterXSize().
996
 */
997
998
int CPL_STDCALL GDALGetRasterXSize(GDALDatasetH hDataset)
999
1000
0
{
1001
0
    VALIDATE_POINTER1(hDataset, "GDALGetRasterXSize", 0);
1002
1003
0
    return GDALDataset::FromHandle(hDataset)->GetRasterXSize();
1004
0
}
1005
1006
/************************************************************************/
1007
/*                           GetRasterYSize()                           */
1008
/************************************************************************/
1009
1010
/**
1011
1012
 \brief Fetch raster height in pixels.
1013
1014
 Equivalent of the C function GDALGetRasterYSize().
1015
1016
 @return the height in pixels of raster bands in this GDALDataset.
1017
1018
*/
1019
1020
int GDALDataset::GetRasterYSize() const
1021
0
{
1022
0
    return nRasterYSize;
1023
0
}
1024
1025
/************************************************************************/
1026
/*                         GDALGetRasterYSize()                         */
1027
/************************************************************************/
1028
1029
/**
1030
 * \brief Fetch raster height in pixels.
1031
 *
1032
 * @see GDALDataset::GetRasterYSize().
1033
 */
1034
1035
int CPL_STDCALL GDALGetRasterYSize(GDALDatasetH hDataset)
1036
1037
0
{
1038
0
    VALIDATE_POINTER1(hDataset, "GDALGetRasterYSize", 0);
1039
1040
0
    return GDALDataset::FromHandle(hDataset)->GetRasterYSize();
1041
0
}
1042
1043
/************************************************************************/
1044
/*                           GetRasterBand()                            */
1045
/************************************************************************/
1046
1047
/**
1048
1049
 \brief Fetch a band object for a dataset.
1050
1051
 See GetBands() for a C++ iterator version of this method.
1052
1053
 Equivalent of the C function GDALGetRasterBand().
1054
1055
 @param nBandId the index number of the band to fetch, from 1 to
1056
                GetRasterCount().
1057
1058
 @return the nBandId th band object
1059
1060
*/
1061
1062
GDALRasterBand *GDALDataset::GetRasterBand(int nBandId)
1063
1064
0
{
1065
0
    if (papoBands)
1066
0
    {
1067
0
        if (nBandId < 1 || nBandId > nBands)
1068
0
        {
1069
0
            ReportError(CE_Failure, CPLE_IllegalArg,
1070
0
                        "GDALDataset::GetRasterBand(%d) - Illegal band #\n",
1071
0
                        nBandId);
1072
0
            return nullptr;
1073
0
        }
1074
1075
0
        return papoBands[nBandId - 1];
1076
0
    }
1077
0
    return nullptr;
1078
0
}
1079
1080
/************************************************************************/
1081
/*                           GetRasterBand()                            */
1082
/************************************************************************/
1083
1084
/**
1085
1086
 \brief Fetch a band object for a dataset.
1087
1088
 See GetBands() for a C++ iterator version of this method.
1089
1090
 Equivalent of the C function GDALGetRasterBand().
1091
1092
 @param nBandId the index number of the band to fetch, from 1 to
1093
                GetRasterCount().
1094
1095
 @return the nBandId th band object
1096
1097
*/
1098
1099
const GDALRasterBand *GDALDataset::GetRasterBand(int nBandId) const
1100
1101
0
{
1102
0
    if (papoBands)
1103
0
    {
1104
0
        if (nBandId < 1 || nBandId > nBands)
1105
0
        {
1106
0
            ReportError(CE_Failure, CPLE_IllegalArg,
1107
0
                        "GDALDataset::GetRasterBand(%d) - Illegal band #\n",
1108
0
                        nBandId);
1109
0
            return nullptr;
1110
0
        }
1111
1112
0
        return papoBands[nBandId - 1];
1113
0
    }
1114
0
    return nullptr;
1115
0
}
1116
1117
/************************************************************************/
1118
/*                         GDALGetRasterBand()                          */
1119
/************************************************************************/
1120
1121
/**
1122
 * \brief Fetch a band object for a dataset.
1123
 * @see GDALDataset::GetRasterBand().
1124
 */
1125
1126
GDALRasterBandH CPL_STDCALL GDALGetRasterBand(GDALDatasetH hDS, int nBandId)
1127
1128
0
{
1129
0
    VALIDATE_POINTER1(hDS, "GDALGetRasterBand", nullptr);
1130
1131
0
    return GDALRasterBand::ToHandle(
1132
0
        GDALDataset::FromHandle(hDS)->GetRasterBand(nBandId));
1133
0
}
1134
1135
/************************************************************************/
1136
/*                           GetRasterCount()                           */
1137
/************************************************************************/
1138
1139
/**
1140
 * \brief Fetch the number of raster bands on this dataset.
1141
 *
1142
 * Same as the C function GDALGetRasterCount().
1143
 *
1144
 * @return the number of raster bands.
1145
 */
1146
1147
int GDALDataset::GetRasterCount() const
1148
0
{
1149
0
    return papoBands ? nBands : 0;
1150
0
}
1151
1152
/************************************************************************/
1153
/*                         GDALGetRasterCount()                         */
1154
/************************************************************************/
1155
1156
/**
1157
 * \brief Fetch the number of raster bands on this dataset.
1158
 *
1159
 * @see GDALDataset::GetRasterCount().
1160
 */
1161
1162
int CPL_STDCALL GDALGetRasterCount(GDALDatasetH hDS)
1163
1164
0
{
1165
0
    VALIDATE_POINTER1(hDS, "GDALGetRasterCount", 0);
1166
1167
0
    return GDALDataset::FromHandle(hDS)->GetRasterCount();
1168
0
}
1169
1170
/************************************************************************/
1171
/*                          GetProjectionRef()                          */
1172
/************************************************************************/
1173
1174
/**
1175
 * \brief Fetch the projection definition string for this dataset.
1176
 *
1177
 * Same as the C function GDALGetProjectionRef().
1178
 *
1179
 * The returned string defines the projection coordinate system of the
1180
 * image in OpenGIS WKT format.  It should be suitable for use with the
1181
 * OGRSpatialReference class.
1182
 *
1183
 * When a projection definition is not available an empty (but not NULL)
1184
 * string is returned.
1185
 *
1186
 * \note Starting with GDAL 3.0, this is a compatibility layer around
1187
 * GetSpatialRef()
1188
 *
1189
 * @return a pointer to an internal projection reference string.  It should
1190
 * not be altered, freed or expected to last for long.
1191
 *
1192
 * @see https://gdal.org/tutorials/osr_api_tut.html
1193
 */
1194
1195
const char *GDALDataset::GetProjectionRef() const
1196
0
{
1197
0
    const auto poSRS = GetSpatialRef();
1198
0
    if (!poSRS || !m_poPrivate)
1199
0
    {
1200
0
        return "";
1201
0
    }
1202
0
    char *pszWKT = nullptr;
1203
0
    poSRS->exportToWkt(&pszWKT);
1204
0
    if (!pszWKT)
1205
0
    {
1206
0
        return "";
1207
0
    }
1208
1209
    // If called on a thread-safe dataset, we might be called by several
1210
    // threads, so make sure our accesses to m_pszWKTCached are protected
1211
    // by a mutex.
1212
0
    std::lock_guard oLock(m_poPrivate->m_oMutexWKT);
1213
0
    if (m_poPrivate->m_pszWKTCached &&
1214
0
        strcmp(pszWKT, m_poPrivate->m_pszWKTCached) == 0)
1215
0
    {
1216
0
        CPLFree(pszWKT);
1217
0
        return m_poPrivate->m_pszWKTCached;
1218
0
    }
1219
0
    CPLFree(m_poPrivate->m_pszWKTCached);
1220
0
    m_poPrivate->m_pszWKTCached = pszWKT;
1221
0
    return m_poPrivate->m_pszWKTCached;
1222
0
}
1223
1224
/************************************************************************/
1225
/*                           GetSpatialRef()                            */
1226
/************************************************************************/
1227
1228
static thread_local int tlsEnableLayersInGetSpatialRefCounter = 0;
1229
1230
/**
1231
 * \brief Fetch the spatial reference for this dataset.
1232
 *
1233
 * Same as the C function GDALGetSpatialRef().
1234
 *
1235
 * When a projection definition is not available, null is returned. If used on
1236
 * a dataset where there are GCPs and not a geotransform, this method returns
1237
 * null. Use GetGCPSpatialRef() instead.
1238
 *
1239
 * Since GDAL 3.12, the default implementation of this method will iterate over
1240
 * vector layers and return their SRS if all geometry columns of all layers use
1241
 * the same SRS, or nullptr otherwise.
1242
 *
1243
 * @since GDAL 3.0
1244
 *
1245
 * @return a pointer to an internal object. It should not be altered or freed.
1246
 * Its lifetime will be the one of the dataset object.
1247
 *
1248
 * @see https://gdal.org/tutorials/osr_api_tut.html
1249
 */
1250
1251
const OGRSpatialReference *GDALDataset::GetSpatialRef() const
1252
0
{
1253
0
    if (tlsEnableLayersInGetSpatialRefCounter == 0)
1254
0
        return GetSpatialRefVectorOnly();
1255
0
    return nullptr;
1256
0
}
1257
1258
/************************************************************************/
1259
/*                       GetSpatialRefVectorOnly()                      */
1260
/************************************************************************/
1261
1262
/**
1263
 * \brief Fetch the spatial reference for this dataset (only for vector layers)
1264
 *
1265
 * The default implementation of this method will iterate over
1266
 * vector layers and return their SRS if all geometry columns of all layers use
1267
 * the same SRS, or nullptr otherwise.
1268
 *
1269
 * @since GDAL 3.12
1270
 *
1271
 * @return a pointer to an internal object. It should not be altered or freed.
1272
 * Its lifetime will be the one of the dataset object.
1273
 */
1274
1275
const OGRSpatialReference *GDALDataset::GetSpatialRefVectorOnly() const
1276
0
{
1277
0
    bool bInit = false;
1278
0
    const OGRSpatialReference *poGlobalSRS = nullptr;
1279
0
    for (const OGRLayer *poLayer : GetLayers())
1280
0
    {
1281
0
        for (const auto *poGeomFieldDefn :
1282
0
             poLayer->GetLayerDefn()->GetGeomFields())
1283
0
        {
1284
0
            const auto *poSRS = poGeomFieldDefn->GetSpatialRef();
1285
0
            if (!bInit)
1286
0
            {
1287
0
                bInit = true;
1288
0
                poGlobalSRS = poSRS;
1289
0
            }
1290
0
            else if ((poSRS && !poGlobalSRS) || (!poSRS && poGlobalSRS) ||
1291
0
                     (poSRS && poGlobalSRS && !poSRS->IsSame(poGlobalSRS)))
1292
0
            {
1293
0
                CPLDebug("GDAL",
1294
0
                         "Not all geometry fields or layers have the same CRS");
1295
0
                return nullptr;
1296
0
            }
1297
0
        }
1298
0
    }
1299
0
    return poGlobalSRS;
1300
0
}
1301
1302
/************************************************************************/
1303
/*                       GetSpatialRefRasterOnly()                      */
1304
/************************************************************************/
1305
1306
/**
1307
 * \brief Fetch the spatial reference for this dataset (ignoring vector layers)
1308
 *
1309
 * @since GDAL 3.12
1310
 *
1311
 * @return a pointer to an internal object. It should not be altered or freed.
1312
 * Its lifetime will be the one of the dataset object.
1313
 */
1314
1315
const OGRSpatialReference *GDALDataset::GetSpatialRefRasterOnly() const
1316
0
{
1317
0
    ++tlsEnableLayersInGetSpatialRefCounter;
1318
0
    const auto poRet = GetSpatialRef();
1319
0
    --tlsEnableLayersInGetSpatialRefCounter;
1320
0
    return poRet;
1321
0
}
1322
1323
/************************************************************************/
1324
/*                        GDALGetSpatialRef()                           */
1325
/************************************************************************/
1326
1327
/**
1328
 * \brief Fetch the spatial reference for this dataset.
1329
 *
1330
 * Same as the C++ method GDALDataset::GetSpatialRef()
1331
 *
1332
 * @since GDAL 3.0
1333
 *
1334
 * @see GDALDataset::GetSpatialRef()
1335
 */
1336
1337
OGRSpatialReferenceH GDALGetSpatialRef(GDALDatasetH hDS)
1338
1339
0
{
1340
0
    VALIDATE_POINTER1(hDS, "GDALGetSpatialRef", nullptr);
1341
1342
0
    return OGRSpatialReference::ToHandle(const_cast<OGRSpatialReference *>(
1343
0
        GDALDataset::FromHandle(hDS)->GetSpatialRef()));
1344
0
}
1345
1346
/************************************************************************/
1347
/*                        GDALGetProjectionRef()                        */
1348
/************************************************************************/
1349
1350
/**
1351
 * \brief Fetch the projection definition string for this dataset.
1352
 *
1353
 * @see GDALDataset::GetProjectionRef()
1354
 */
1355
1356
const char *CPL_STDCALL GDALGetProjectionRef(GDALDatasetH hDS)
1357
1358
0
{
1359
0
    VALIDATE_POINTER1(hDS, "GDALGetProjectionRef", nullptr);
1360
1361
0
    return GDALDataset::FromHandle(hDS)->GetProjectionRef();
1362
0
}
1363
1364
/************************************************************************/
1365
/*                           SetProjection()                            */
1366
/************************************************************************/
1367
1368
/**
1369
 * \brief Set the projection reference string for this dataset.
1370
 *
1371
 * The string should be in OGC WKT or PROJ.4 format.  An error may occur
1372
 * because of incorrectly specified projection strings, because the dataset
1373
 * is not writable, or because the dataset does not support the indicated
1374
 * projection.  Many formats do not support writing projections.
1375
 *
1376
 * This method is the same as the C GDALSetProjection() function.
1377
 *
1378
 * \note Startig with GDAL 3.0, this is a compatibility layer around
1379
 * SetSpatialRef()
1380
1381
 * @param pszProjection projection reference string.
1382
 *
1383
 * @return CE_Failure if an error occurs, otherwise CE_None.
1384
 */
1385
1386
CPLErr GDALDataset::SetProjection(const char *pszProjection)
1387
0
{
1388
0
    if (pszProjection && pszProjection[0] != '\0')
1389
0
    {
1390
0
        OGRSpatialReference oSRS;
1391
0
        oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1392
0
        if (oSRS.SetFromUserInput(pszProjection) != OGRERR_NONE)
1393
0
        {
1394
0
            return CE_Failure;
1395
0
        }
1396
0
        return SetSpatialRef(&oSRS);
1397
0
    }
1398
0
    else
1399
0
    {
1400
0
        return SetSpatialRef(nullptr);
1401
0
    }
1402
0
}
1403
1404
/************************************************************************/
1405
/*                           SetSpatialRef()                            */
1406
/************************************************************************/
1407
1408
/**
1409
 * \brief Set the spatial reference system for this dataset.
1410
 *
1411
 * An error may occur because the dataset
1412
 * is not writable, or because the dataset does not support the indicated
1413
 * projection. Many formats do not support writing projections.
1414
 *
1415
 * This method is the same as the C GDALSetSpatialRef() function.
1416
 *
1417
 * @since GDAL 3.0
1418
1419
 * @param poSRS spatial reference system object. nullptr can potentially be
1420
 * passed for drivers that support unsetting the SRS.
1421
 *
1422
 * @return CE_Failure if an error occurs, otherwise CE_None.
1423
 */
1424
1425
CPLErr GDALDataset::SetSpatialRef(CPL_UNUSED const OGRSpatialReference *poSRS)
1426
0
{
1427
0
    if (!(GetMOFlags() & GMO_IGNORE_UNIMPLEMENTED))
1428
0
        ReportError(CE_Failure, CPLE_NotSupported,
1429
0
                    "Dataset does not support the SetSpatialRef() method.");
1430
0
    return CE_Failure;
1431
0
}
1432
1433
/************************************************************************/
1434
/*                         GDALSetSpatialRef()                          */
1435
/************************************************************************/
1436
1437
/**
1438
 * \brief Set the spatial reference system for this dataset.
1439
 *
1440
 * @since GDAL 3.0
1441
 *
1442
 * @see GDALDataset::SetSpatialRef()
1443
 */
1444
1445
CPLErr GDALSetSpatialRef(GDALDatasetH hDS, OGRSpatialReferenceH hSRS)
1446
1447
0
{
1448
0
    VALIDATE_POINTER1(hDS, "GDALSetSpatialRef", CE_Failure);
1449
1450
0
    return GDALDataset::FromHandle(hDS)->SetSpatialRef(
1451
0
        OGRSpatialReference::FromHandle(hSRS));
1452
0
}
1453
1454
/************************************************************************/
1455
/*                         GDALSetProjection()                          */
1456
/************************************************************************/
1457
1458
/**
1459
 * \brief Set the projection reference string for this dataset.
1460
 *
1461
 * @see GDALDataset::SetProjection()
1462
 */
1463
1464
CPLErr CPL_STDCALL GDALSetProjection(GDALDatasetH hDS,
1465
                                     const char *pszProjection)
1466
1467
0
{
1468
0
    VALIDATE_POINTER1(hDS, "GDALSetProjection", CE_Failure);
1469
1470
0
    return GDALDataset::FromHandle(hDS)->SetProjection(pszProjection);
1471
0
}
1472
1473
/************************************************************************/
1474
/*                          GetGeoTransform()                           */
1475
/************************************************************************/
1476
1477
/**
1478
 * \brief Fetch the affine transformation coefficients.
1479
 *
1480
 * Fetches the coefficients for transforming between pixel/line (P,L) raster
1481
 * space, and projection coordinates (Xp,Yp) space.
1482
 *
1483
 * \code
1484
 *   Xp = gt[0] + P*gt[1] + L*gt[2];
1485
 *   Yp = gt[3] + P*padfTransform[4] + L*gt[5];
1486
 * \endcode
1487
 *
1488
 * In a north up image, gt[1] is the pixel width, and
1489
 * gt[5] is the pixel height.  The upper left corner of the
1490
 * upper left pixel is at position (gt[0],gt[3]).
1491
 *
1492
 * The default transform is (0,1,0,0,0,1) and should be returned even when
1493
 * a CE_Failure error is returned, such as for formats that don't support
1494
 * transformation to projection coordinates.
1495
 *
1496
 * This method does the same thing as the C GDALGetGeoTransform() function.
1497
 *
1498
 * @param gt an existing six double buffer into which the
1499
 * transformation will be placed.
1500
 *
1501
 * @return CE_None on success, or CE_Failure if no transform can be fetched.
1502
 *
1503
 * @since 3.12
1504
 */
1505
1506
CPLErr GDALDataset::GetGeoTransform(GDALGeoTransform &gt) const
1507
1508
0
{
1509
0
    gt = GDALGeoTransform();
1510
1511
0
    return CE_Failure;
1512
0
}
1513
1514
/************************************************************************/
1515
/*                          GetGeoTransform()                           */
1516
/************************************************************************/
1517
1518
/**
1519
 * \brief Fetch the affine transformation coefficients.
1520
 *
1521
 * Fetches the coefficients for transforming between pixel/line (P,L) raster
1522
 * space, and projection coordinates (Xp,Yp) space.
1523
 *
1524
 * \code
1525
 *   Xp = padfTransform[0] + P*padfTransform[1] + L*padfTransform[2];
1526
 *   Yp = padfTransform[3] + P*padfTransform[4] + L*padfTransform[5];
1527
 * \endcode
1528
 *
1529
 * In a north up image, padfTransform[1] is the pixel width, and
1530
 * padfTransform[5] is the pixel height.  The upper left corner of the
1531
 * upper left pixel is at position (padfTransform[0],padfTransform[3]).
1532
 *
1533
 * The default transform is (0,1,0,0,0,1) and should be returned even when
1534
 * a CE_Failure error is returned, such as for formats that don't support
1535
 * transformation to projection coordinates.
1536
 *
1537
 * This method does the same thing as the C GDALGetGeoTransform() function.
1538
 *
1539
 * @param padfTransform an existing six double buffer into which the
1540
 * transformation will be placed.
1541
 *
1542
 * @return CE_None on success, or CE_Failure if no transform can be fetched.
1543
 *
1544
 * @deprecated since 3.12. Use GetGeoTransform(GDALGeoTransform&) instead
1545
 */
1546
1547
CPLErr GDALDataset::GetGeoTransform(double *padfTransform) const
1548
1549
0
{
1550
0
    return GetGeoTransform(
1551
0
        *reinterpret_cast<GDALGeoTransform *>(padfTransform));
1552
0
}
1553
1554
/************************************************************************/
1555
/*                        GDALGetGeoTransform()                         */
1556
/************************************************************************/
1557
1558
/**
1559
 * \brief Fetch the affine transformation coefficients.
1560
 *
1561
 * @see GDALDataset::GetGeoTransform()
1562
 */
1563
1564
CPLErr CPL_STDCALL GDALGetGeoTransform(GDALDatasetH hDS, double *padfTransform)
1565
1566
0
{
1567
0
    VALIDATE_POINTER1(hDS, "GDALGetGeoTransform", CE_Failure);
1568
1569
0
    return GDALDataset::FromHandle(hDS)->GetGeoTransform(
1570
0
        *reinterpret_cast<GDALGeoTransform *>(padfTransform));
1571
0
}
1572
1573
/************************************************************************/
1574
/*                          SetGeoTransform()                           */
1575
/************************************************************************/
1576
1577
/**
1578
 * \fn GDALDataset::SetGeoTransform(const GDALGeoTransform&)
1579
 * \brief Set the affine transformation coefficients.
1580
 *
1581
 * See GetGeoTransform() for details on the meaning of the padfTransform
1582
 * coefficients.
1583
 *
1584
 * This method does the same thing as the C GDALSetGeoTransform() function.
1585
 *
1586
 * @param gt the transformation coefficients to be written with the dataset.
1587
 *
1588
 * @return CE_None on success, or CE_Failure if this transform cannot be
1589
 * written.
1590
 *
1591
 * @since 3.12
1592
 */
1593
1594
CPLErr GDALDataset::SetGeoTransform(CPL_UNUSED const GDALGeoTransform &gt)
1595
1596
0
{
1597
0
    if (!(GetMOFlags() & GMO_IGNORE_UNIMPLEMENTED))
1598
0
        ReportError(CE_Failure, CPLE_NotSupported,
1599
0
                    "SetGeoTransform() not supported for this dataset.");
1600
1601
0
    return CE_Failure;
1602
0
}
1603
1604
/************************************************************************/
1605
/*                          SetGeoTransform()                           */
1606
/************************************************************************/
1607
1608
/**
1609
 * \brief Set the affine transformation coefficients.
1610
 *
1611
 * See GetGeoTransform() for details on the meaning of the padfTransform
1612
 * coefficients.
1613
 *
1614
 * This method does the same thing as the C GDALSetGeoTransform() function.
1615
 *
1616
 * @param padfTransform a six double buffer containing the transformation
1617
 * coefficients to be written with the dataset.
1618
 *
1619
 * @return CE_None on success, or CE_Failure if this transform cannot be
1620
 * written.
1621
 *
1622
 * @deprecated since 3.12. Use SetGeoTransform(const GDALGeoTransform&) instead
1623
 */
1624
CPLErr GDALDataset::SetGeoTransform(const double *padfTransform)
1625
1626
0
{
1627
0
    return SetGeoTransform(
1628
0
        *reinterpret_cast<const GDALGeoTransform *>(padfTransform));
1629
0
}
1630
1631
/************************************************************************/
1632
/*                        GDALSetGeoTransform()                         */
1633
/************************************************************************/
1634
1635
/**
1636
 * \brief Set the affine transformation coefficients.
1637
 *
1638
 * @see GDALDataset::SetGeoTransform()
1639
 */
1640
1641
CPLErr CPL_STDCALL GDALSetGeoTransform(GDALDatasetH hDS,
1642
                                       const double *padfTransform)
1643
1644
0
{
1645
0
    VALIDATE_POINTER1(hDS, "GDALSetGeoTransform", CE_Failure);
1646
0
    VALIDATE_POINTER1(padfTransform, "GDALSetGeoTransform", CE_Failure);
1647
1648
0
    return GDALDataset::FromHandle(hDS)->SetGeoTransform(
1649
0
        *reinterpret_cast<const GDALGeoTransform *>(padfTransform));
1650
0
}
1651
1652
/************************************************************************/
1653
/*                         GetInternalHandle()                          */
1654
/************************************************************************/
1655
1656
/**
1657
 * \fn GDALDataset::GetInternalHandle(const char*)
1658
 * \brief Fetch a format specific internally meaningful handle.
1659
 *
1660
 * This method is the same as the C GDALGetInternalHandle() method.
1661
 *
1662
 * @param pszHandleName the handle name desired.  The meaningful names
1663
 * will be specific to the file format.
1664
 *
1665
 * @return the desired handle value, or NULL if not recognized/supported.
1666
 */
1667
1668
void *GDALDataset::GetInternalHandle(CPL_UNUSED const char *pszHandleName)
1669
1670
0
{
1671
0
    return nullptr;
1672
0
}
1673
1674
/************************************************************************/
1675
/*                       GDALGetInternalHandle()                        */
1676
/************************************************************************/
1677
1678
/**
1679
 * \brief Fetch a format specific internally meaningful handle.
1680
 *
1681
 * @see GDALDataset::GetInternalHandle()
1682
 */
1683
1684
void *CPL_STDCALL GDALGetInternalHandle(GDALDatasetH hDS,
1685
                                        const char *pszRequest)
1686
1687
0
{
1688
0
    VALIDATE_POINTER1(hDS, "GDALGetInternalHandle", nullptr);
1689
1690
0
    return GDALDataset::FromHandle(hDS)->GetInternalHandle(pszRequest);
1691
0
}
1692
1693
/************************************************************************/
1694
/*                             GetDriver()                              */
1695
/************************************************************************/
1696
1697
/**
1698
 * \brief Fetch the driver to which this dataset relates.
1699
 *
1700
 * This method is the same as the C GDALGetDatasetDriver() function.
1701
 *
1702
 * @return the driver on which the dataset was created with GDALOpen() or
1703
 * GDALCreate().
1704
 */
1705
1706
GDALDriver *GDALDataset::GetDriver()
1707
0
{
1708
0
    return poDriver;
1709
0
}
1710
1711
/************************************************************************/
1712
/*                        GDALGetDatasetDriver()                        */
1713
/************************************************************************/
1714
1715
/**
1716
 * \brief Fetch the driver to which this dataset relates.
1717
 *
1718
 * @see GDALDataset::GetDriver()
1719
 */
1720
1721
GDALDriverH CPL_STDCALL GDALGetDatasetDriver(GDALDatasetH hDataset)
1722
1723
0
{
1724
0
    VALIDATE_POINTER1(hDataset, "GDALGetDatasetDriver", nullptr);
1725
1726
0
    return static_cast<GDALDriverH>(
1727
0
        GDALDataset::FromHandle(hDataset)->GetDriver());
1728
0
}
1729
1730
/************************************************************************/
1731
/*                             Reference()                              */
1732
/************************************************************************/
1733
1734
/**
1735
 * \brief Add one to dataset reference count.
1736
 *
1737
 * The reference is one after instantiation.
1738
 *
1739
 * This method is the same as the C GDALReferenceDataset() function.
1740
 *
1741
 * @return the post-increment reference count.
1742
 */
1743
1744
int GDALDataset::Reference()
1745
0
{
1746
0
    return ++nRefCount;
1747
0
}
1748
1749
/************************************************************************/
1750
/*                        GDALReferenceDataset()                        */
1751
/************************************************************************/
1752
1753
/**
1754
 * \brief Add one to dataset reference count.
1755
 *
1756
 * @see GDALDataset::Reference()
1757
 */
1758
1759
int CPL_STDCALL GDALReferenceDataset(GDALDatasetH hDataset)
1760
1761
0
{
1762
0
    VALIDATE_POINTER1(hDataset, "GDALReferenceDataset", 0);
1763
1764
0
    return GDALDataset::FromHandle(hDataset)->Reference();
1765
0
}
1766
1767
/************************************************************************/
1768
/*                            Dereference()                             */
1769
/************************************************************************/
1770
1771
/**
1772
 * \brief Subtract one from dataset reference count.
1773
 *
1774
 * The reference is one after instantiation.  Generally when the reference
1775
 * count has dropped to zero the dataset may be safely deleted (closed).
1776
 *
1777
 * This method is the same as the C GDALDereferenceDataset() function.
1778
 *
1779
 * @return the post-decrement reference count.
1780
 */
1781
1782
int GDALDataset::Dereference()
1783
0
{
1784
0
    return --nRefCount;
1785
0
}
1786
1787
/************************************************************************/
1788
/*                       GDALDereferenceDataset()                       */
1789
/************************************************************************/
1790
1791
/**
1792
 * \brief Subtract one from dataset reference count.
1793
 *
1794
 * @see GDALDataset::Dereference()
1795
 */
1796
1797
int CPL_STDCALL GDALDereferenceDataset(GDALDatasetH hDataset)
1798
1799
0
{
1800
0
    VALIDATE_POINTER1(hDataset, "GDALDereferenceDataset", 0);
1801
1802
0
    return GDALDataset::FromHandle(hDataset)->Dereference();
1803
0
}
1804
1805
/************************************************************************/
1806
/*                            ReleaseRef()                              */
1807
/************************************************************************/
1808
1809
/**
1810
 * \brief Drop a reference to this object, and destroy if no longer referenced.
1811
 * @return TRUE if the object has been destroyed.
1812
 */
1813
1814
int GDALDataset::ReleaseRef()
1815
1816
0
{
1817
0
    if (Dereference() <= 0)
1818
0
    {
1819
0
        nRefCount = 1;
1820
0
        delete this;
1821
0
        return TRUE;
1822
0
    }
1823
0
    return FALSE;
1824
0
}
1825
1826
/************************************************************************/
1827
/*                        GDALReleaseDataset()                          */
1828
/************************************************************************/
1829
1830
/**
1831
 * \brief Drop a reference to this object, and destroy if no longer referenced.
1832
 *
1833
 * @see GDALDataset::ReleaseRef()
1834
 */
1835
1836
int CPL_STDCALL GDALReleaseDataset(GDALDatasetH hDataset)
1837
1838
0
{
1839
0
    VALIDATE_POINTER1(hDataset, "GDALReleaseDataset", 0);
1840
1841
0
    return GDALDataset::FromHandle(hDataset)->ReleaseRef();
1842
0
}
1843
1844
/************************************************************************/
1845
/*                             GetShared()                              */
1846
/************************************************************************/
1847
1848
/**
1849
 * \brief Returns shared flag.
1850
 *
1851
 * @return TRUE if the GDALDataset is available for sharing, or FALSE if not.
1852
 */
1853
1854
int GDALDataset::GetShared() const
1855
0
{
1856
0
    return bShared;
1857
0
}
1858
1859
/************************************************************************/
1860
/*                            MarkAsShared()                            */
1861
/************************************************************************/
1862
1863
/**
1864
 * \brief Mark this dataset as available for sharing.
1865
 */
1866
1867
void GDALDataset::MarkAsShared()
1868
1869
0
{
1870
0
    CPLAssert(!bShared);
1871
1872
0
    bShared = true;
1873
0
    if (bIsInternal)
1874
0
        return;
1875
1876
0
    GIntBig nPID = GDALGetResponsiblePIDForCurrentThread();
1877
1878
    // Insert the dataset in the set of shared opened datasets.
1879
0
    CPLMutexHolderD(&hDLMutex);
1880
0
    if (phSharedDatasetSet == nullptr)
1881
0
        phSharedDatasetSet =
1882
0
            CPLHashSetNew(GDALSharedDatasetHashFunc, GDALSharedDatasetEqualFunc,
1883
0
                          GDALSharedDatasetFreeFunc);
1884
1885
0
    SharedDatasetCtxt *psStruct =
1886
0
        static_cast<SharedDatasetCtxt *>(CPLMalloc(sizeof(SharedDatasetCtxt)));
1887
0
    psStruct->poDS = this;
1888
0
    psStruct->nPID = nPID;
1889
0
    psStruct->nOpenFlags = nOpenFlags & ~GDAL_OF_SHARED;
1890
0
    psStruct->pszDescription = CPLStrdup(GetDescription());
1891
0
    std::string osConcatenatedOpenOptions =
1892
0
        GDALSharedDatasetConcatenateOpenOptions(papszOpenOptions);
1893
0
    psStruct->pszConcatenatedOpenOptions =
1894
0
        CPLStrdup(osConcatenatedOpenOptions.c_str());
1895
0
    if (CPLHashSetLookup(phSharedDatasetSet, psStruct) != nullptr)
1896
0
    {
1897
0
        GDALSharedDatasetFreeFunc(psStruct);
1898
0
        ReportError(CE_Failure, CPLE_AppDefined,
1899
0
                    "An existing shared dataset already has this description. "
1900
0
                    "This should not happen.");
1901
0
    }
1902
0
    else
1903
0
    {
1904
0
        CPLHashSetInsert(phSharedDatasetSet, psStruct);
1905
1906
0
        (*poAllDatasetMap)[this] = nPID;
1907
0
    }
1908
0
}
1909
1910
/************************************************************************/
1911
/*                        MarkSuppressOnClose()                         */
1912
/************************************************************************/
1913
1914
/** Set that the dataset must be deleted on close.
1915
 *
1916
 * This is the same as C function GDALDatasetMarkSuppressOnClose()
1917
 */
1918
void GDALDataset::MarkSuppressOnClose()
1919
0
{
1920
0
    bSuppressOnClose = true;
1921
0
}
1922
1923
/************************************************************************/
1924
/*                   GDALDatasetMarkSuppressOnClose()                   */
1925
/************************************************************************/
1926
1927
/** Set that the dataset must be deleted on close.
1928
 *
1929
 * This is the same as C++ method GDALDataset::MarkSuppressOnClose()
1930
 *
1931
 * @since GDAL 3.12
1932
 */
1933
1934
void GDALDatasetMarkSuppressOnClose(GDALDatasetH hDS)
1935
0
{
1936
0
    VALIDATE_POINTER0(hDS, "GDALDatasetMarkSuppressOnClose");
1937
1938
0
    return GDALDataset::FromHandle(hDS)->MarkSuppressOnClose();
1939
0
}
1940
1941
/************************************************************************/
1942
/*                       UnMarkSuppressOnClose()                        */
1943
/************************************************************************/
1944
1945
/** Remove the flag requesting the dataset to be deleted on close. */
1946
void GDALDataset::UnMarkSuppressOnClose()
1947
0
{
1948
0
    bSuppressOnClose = false;
1949
0
}
1950
1951
/************************************************************************/
1952
/*                        CleanupPostFileClosing()                      */
1953
/************************************************************************/
1954
1955
/** This method should be called by driver implementations in their destructor,
1956
 * after having closed all files, but before having freed resources that
1957
 * are needed for their GetFileList() implementation.
1958
 * This is used to implement MarkSuppressOnClose behavior.
1959
 */
1960
void GDALDataset::CleanupPostFileClosing()
1961
0
{
1962
0
    if (IsMarkedSuppressOnClose())
1963
0
    {
1964
0
        char **papszFileList = GetFileList();
1965
0
        for (int i = 0; papszFileList && papszFileList[i]; ++i)
1966
0
            VSIUnlink(papszFileList[i]);
1967
0
        CSLDestroy(papszFileList);
1968
0
    }
1969
0
}
1970
1971
/************************************************************************/
1972
/*                            GetGCPCount()                             */
1973
/************************************************************************/
1974
1975
/**
1976
 * \brief Get number of GCPs.
1977
 *
1978
 * This method is the same as the C function GDALGetGCPCount().
1979
 *
1980
 * @return number of GCPs for this dataset.  Zero if there are none.
1981
 */
1982
1983
int GDALDataset::GetGCPCount()
1984
0
{
1985
0
    return 0;
1986
0
}
1987
1988
/************************************************************************/
1989
/*                          GDALGetGCPCount()                           */
1990
/************************************************************************/
1991
1992
/**
1993
 * \brief Get number of GCPs.
1994
 *
1995
 * @see GDALDataset::GetGCPCount()
1996
 */
1997
1998
int CPL_STDCALL GDALGetGCPCount(GDALDatasetH hDS)
1999
2000
0
{
2001
0
    VALIDATE_POINTER1(hDS, "GDALGetGCPCount", 0);
2002
2003
0
    return GDALDataset::FromHandle(hDS)->GetGCPCount();
2004
0
}
2005
2006
/************************************************************************/
2007
/*                          GetGCPProjection()                          */
2008
/************************************************************************/
2009
2010
/**
2011
 * \brief Get output projection for GCPs.
2012
 *
2013
 * This method is the same as the C function GDALGetGCPProjection().
2014
 *
2015
 * The projection string follows the normal rules from GetProjectionRef().
2016
 *
2017
 * \note Starting with GDAL 3.0, this is a compatibility layer around
2018
 * GetGCPSpatialRef()
2019
 *
2020
 * @return internal projection string or "" if there are no GCPs.
2021
 *  It should not be altered, freed or expected to last for long.
2022
 */
2023
2024
const char *GDALDataset::GetGCPProjection() const
2025
0
{
2026
0
    const auto poSRS = GetGCPSpatialRef();
2027
0
    if (!poSRS || !m_poPrivate)
2028
0
    {
2029
0
        return "";
2030
0
    }
2031
0
    char *pszWKT = nullptr;
2032
0
    poSRS->exportToWkt(&pszWKT);
2033
0
    if (!pszWKT)
2034
0
    {
2035
0
        return "";
2036
0
    }
2037
2038
    // If called on a thread-safe dataset, we might be called by several
2039
    // threads, so make sure our accesses to m_pszWKTCached are protected
2040
    // by a mutex.
2041
0
    std::lock_guard oLock(m_poPrivate->m_oMutexWKT);
2042
0
    if (m_poPrivate->m_pszWKTGCPCached &&
2043
0
        strcmp(pszWKT, m_poPrivate->m_pszWKTGCPCached) == 0)
2044
0
    {
2045
0
        CPLFree(pszWKT);
2046
0
        return m_poPrivate->m_pszWKTGCPCached;
2047
0
    }
2048
0
    CPLFree(m_poPrivate->m_pszWKTGCPCached);
2049
0
    m_poPrivate->m_pszWKTGCPCached = pszWKT;
2050
0
    return m_poPrivate->m_pszWKTGCPCached;
2051
0
}
2052
2053
/************************************************************************/
2054
/*                          GetGCPSpatialRef()                          */
2055
/************************************************************************/
2056
2057
/**
2058
 * \brief Get output spatial reference system for GCPs.
2059
 *
2060
 * Same as the C function GDALGetGCPSpatialRef().
2061
 *
2062
 * When a SRS is not available, null is returned. If used on
2063
 * a dataset where there is a geotransform, and not GCPs, this method returns
2064
 * null. Use GetSpatialRef() instead.
2065
 *
2066
 * @since GDAL 3.0
2067
 *
2068
 * @return a pointer to an internal object. It should not be altered or freed.
2069
 * Its lifetime will be the one of the dataset object, or until the next
2070
 * call to this method.
2071
 */
2072
2073
const OGRSpatialReference *GDALDataset::GetGCPSpatialRef() const
2074
0
{
2075
0
    return nullptr;
2076
0
}
2077
2078
/************************************************************************/
2079
/*                       GDALGetGCPSpatialRef()                         */
2080
/************************************************************************/
2081
2082
/**
2083
 * \brief Get output spatial reference system for GCPs.
2084
 *
2085
 * @since GDAL 3.0
2086
 *
2087
 * @see GDALDataset::GetGCPSpatialRef()
2088
 */
2089
2090
OGRSpatialReferenceH GDALGetGCPSpatialRef(GDALDatasetH hDS)
2091
2092
0
{
2093
0
    VALIDATE_POINTER1(hDS, "GDALGetGCPSpatialRef", nullptr);
2094
2095
0
    return OGRSpatialReference::ToHandle(const_cast<OGRSpatialReference *>(
2096
0
        GDALDataset::FromHandle(hDS)->GetGCPSpatialRef()));
2097
0
}
2098
2099
/************************************************************************/
2100
/*                        GDALGetGCPProjection()                        */
2101
/************************************************************************/
2102
2103
/**
2104
 * \brief Get output projection for GCPs.
2105
 *
2106
 * @see GDALDataset::GetGCPProjection()
2107
 */
2108
2109
const char *CPL_STDCALL GDALGetGCPProjection(GDALDatasetH hDS)
2110
2111
0
{
2112
0
    VALIDATE_POINTER1(hDS, "GDALGetGCPProjection", nullptr);
2113
2114
0
    return GDALDataset::FromHandle(hDS)->GetGCPProjection();
2115
0
}
2116
2117
/************************************************************************/
2118
/*                               GetGCPs()                              */
2119
/************************************************************************/
2120
2121
/**
2122
 * \brief Fetch GCPs.
2123
 *
2124
 * This method is the same as the C function GDALGetGCPs().
2125
 *
2126
 * @return pointer to internal GCP structure list.  It should not be modified,
2127
 * and may change on the next GDAL call.
2128
 */
2129
2130
const GDAL_GCP *GDALDataset::GetGCPs()
2131
0
{
2132
0
    return nullptr;
2133
0
}
2134
2135
/************************************************************************/
2136
/*                            GDALGetGCPs()                             */
2137
/************************************************************************/
2138
2139
/**
2140
 * \brief Fetch GCPs.
2141
 *
2142
 * @see GDALDataset::GetGCPs()
2143
 */
2144
2145
const GDAL_GCP *CPL_STDCALL GDALGetGCPs(GDALDatasetH hDS)
2146
2147
0
{
2148
0
    VALIDATE_POINTER1(hDS, "GDALGetGCPs", nullptr);
2149
2150
0
    return GDALDataset::FromHandle(hDS)->GetGCPs();
2151
0
}
2152
2153
/************************************************************************/
2154
/*                              SetGCPs()                               */
2155
/************************************************************************/
2156
2157
/**
2158
 * \brief Assign GCPs.
2159
 *
2160
 * This method is the same as the C function GDALSetGCPs().
2161
 *
2162
 * This method assigns the passed set of GCPs to this dataset, as well as
2163
 * setting their coordinate system.  Internally copies are made of the
2164
 * coordinate system and list of points, so the caller remains responsible for
2165
 * deallocating these arguments if appropriate.
2166
 *
2167
 * Most formats do not support setting of GCPs, even formats that can
2168
 * handle GCPs.  These formats will return CE_Failure.
2169
 *
2170
 * \note Startig with GDAL 3.0, this is a compatibility layer around
2171
 * SetGCPs(int, const GDAL_GCP*, const char*)
2172
 *
2173
 * @param nGCPCount number of GCPs being assigned.
2174
 *
2175
 * @param pasGCPList array of GCP structures being assign (nGCPCount in array).
2176
 *
2177
 * @param pszGCPProjection the new OGC WKT coordinate system to assign for the
2178
 * GCP output coordinates.  This parameter should be "" if no output coordinate
2179
 * system is known.
2180
 *
2181
 * @return CE_None on success, CE_Failure on failure (including if action is
2182
 * not supported for this format).
2183
 */
2184
2185
CPLErr GDALDataset::SetGCPs(int nGCPCount, const GDAL_GCP *pasGCPList,
2186
                            const char *pszGCPProjection)
2187
2188
0
{
2189
0
    if (pszGCPProjection && pszGCPProjection[0] != '\0')
2190
0
    {
2191
0
        OGRSpatialReference oSRS;
2192
0
        oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
2193
0
        if (oSRS.importFromWkt(pszGCPProjection) != OGRERR_NONE)
2194
0
        {
2195
0
            return CE_Failure;
2196
0
        }
2197
0
        return SetGCPs(nGCPCount, pasGCPList, &oSRS);
2198
0
    }
2199
0
    else
2200
0
    {
2201
0
        return SetGCPs(nGCPCount, pasGCPList,
2202
0
                       static_cast<const OGRSpatialReference *>(nullptr));
2203
0
    }
2204
0
}
2205
2206
/************************************************************************/
2207
/*                              SetGCPs()                               */
2208
/************************************************************************/
2209
2210
/**
2211
 * \brief Assign GCPs.
2212
 *
2213
 * This method is the same as the C function GDALSetGCPs().
2214
 *
2215
 * This method assigns the passed set of GCPs to this dataset, as well as
2216
 * setting their coordinate system.  Internally copies are made of the
2217
 * coordinate system and list of points, so the caller remains responsible for
2218
 * deallocating these arguments if appropriate.
2219
 *
2220
 * Most formats do not support setting of GCPs, even formats that can
2221
 * handle GCPs.  These formats will return CE_Failure.
2222
 *
2223
 * @since GDAL 3.0
2224
 *
2225
 * @param nGCPCount number of GCPs being assigned.
2226
 *
2227
 * @param pasGCPList array of GCP structures being assign (nGCPCount in array).
2228
 *
2229
 * @param poGCP_SRS the new coordinate reference system to assign for the
2230
 * GCP output coordinates.  This parameter should be null if no output
2231
 * coordinate system is known.
2232
 *
2233
 * @return CE_None on success, CE_Failure on failure (including if action is
2234
 * not supported for this format).
2235
 */
2236
2237
CPLErr GDALDataset::SetGCPs(CPL_UNUSED int nGCPCount,
2238
                            CPL_UNUSED const GDAL_GCP *pasGCPList,
2239
                            CPL_UNUSED const OGRSpatialReference *poGCP_SRS)
2240
2241
0
{
2242
0
    if (!(GetMOFlags() & GMO_IGNORE_UNIMPLEMENTED))
2243
0
        ReportError(CE_Failure, CPLE_NotSupported,
2244
0
                    "Dataset does not support the SetGCPs() method.");
2245
2246
0
    return CE_Failure;
2247
0
}
2248
2249
/************************************************************************/
2250
/*                            GDALSetGCPs()                             */
2251
/************************************************************************/
2252
2253
/**
2254
 * \brief Assign GCPs.
2255
 *
2256
 * @see GDALDataset::SetGCPs(int, const GDAL_GCP*, const char*)
2257
 */
2258
2259
CPLErr CPL_STDCALL GDALSetGCPs(GDALDatasetH hDS, int nGCPCount,
2260
                               const GDAL_GCP *pasGCPList,
2261
                               const char *pszGCPProjection)
2262
2263
0
{
2264
0
    VALIDATE_POINTER1(hDS, "GDALSetGCPs", CE_Failure);
2265
2266
0
    return GDALDataset::FromHandle(hDS)->SetGCPs(nGCPCount, pasGCPList,
2267
0
                                                 pszGCPProjection);
2268
0
}
2269
2270
/************************************************************************/
2271
/*                           GDALSetGCPs2()                             */
2272
/************************************************************************/
2273
2274
/**
2275
 * \brief Assign GCPs.
2276
 *
2277
 * @since GDAL 3.0
2278
 * @see GDALDataset::SetGCPs(int, const GDAL_GCP*, const OGRSpatialReference*)
2279
 */
2280
2281
CPLErr GDALSetGCPs2(GDALDatasetH hDS, int nGCPCount, const GDAL_GCP *pasGCPList,
2282
                    OGRSpatialReferenceH hSRS)
2283
2284
0
{
2285
0
    VALIDATE_POINTER1(hDS, "GDALSetGCPs2", CE_Failure);
2286
2287
0
    return GDALDataset::FromHandle(hDS)->SetGCPs(
2288
0
        nGCPCount, pasGCPList, OGRSpatialReference::FromHandle(hSRS));
2289
0
}
2290
2291
/************************************************************************/
2292
/*                           BuildOverviews()                           */
2293
/************************************************************************/
2294
2295
/**
2296
 * \brief Build raster overview(s)
2297
 *
2298
 * If the operation is not supported for the indicated dataset, then
2299
 * CE_Failure is returned, and CPLGetLastErrorNo() will return
2300
 * CPLE_NotSupported.
2301
 *
2302
 * Depending on the actual file format, all overviews level can be also
2303
 * deleted by specifying nOverviews == 0. This works at least for external
2304
 * overviews (.ovr), TIFF internal overviews, etc.
2305
 *
2306
 * Starting with GDAL 3.2, the GDAL_NUM_THREADS configuration option can be set
2307
 * to "ALL_CPUS" or a integer value to specify the number of threads to use for
2308
 * overview computation.
2309
 *
2310
 * This method is the same as the C function GDALBuildOverviewsEx().
2311
 *
2312
 * @param pszResampling one of "AVERAGE", "AVERAGE_MAGPHASE", "RMS",
2313
 * "BILINEAR", "CUBIC", "CUBICSPLINE", "GAUSS", "LANCZOS", "MODE", "NEAREST",
2314
 * or "NONE" controlling the downsampling method applied.
2315
 * @param nOverviews number of overviews to build, or 0 to clean overviews.
2316
 * @param panOverviewList the list of overview decimation factors (positive
2317
 *                        integers, normally larger or equal to 2) to build, or
2318
 *                        NULL if nOverviews == 0.
2319
 * @param nListBands number of bands to build overviews for in panBandList.
2320
 * Build for all bands if this is 0.
2321
 * @param panBandList list of band numbers.
2322
 * @param pfnProgress a function to call to report progress, or NULL.
2323
 * @param pProgressData application data to pass to the progress function.
2324
 * @param papszOptions (GDAL >= 3.6) NULL terminated list of options as
2325
 *                     key=value pairs, or NULL.
2326
 *                     Possible keys are the ones returned by
2327
 *                     GetDriver()->GetMetadataItem(GDAL_DMD_OVERVIEW_CREATIONOPTIONLIST)
2328
 *
2329
 * @return CE_None on success or CE_Failure if the operation doesn't work.
2330
 *
2331
 * For example, to build overview level 2, 4 and 8 on all bands the following
2332
 * call could be made:
2333
 * \code{.cpp}
2334
 *   int       anOverviewList[3] = { 2, 4, 8 };
2335
 *
2336
 *   poDataset->BuildOverviews( "NEAREST", 3, anOverviewList, 0, nullptr,
2337
 *                              GDALDummyProgress, nullptr );
2338
 * \endcode
2339
 *
2340
 * @see GDALRegenerateOverviewsEx()
2341
 */
2342
2343
CPLErr GDALDataset::BuildOverviews(const char *pszResampling, int nOverviews,
2344
                                   const int *panOverviewList, int nListBands,
2345
                                   const int *panBandList,
2346
                                   GDALProgressFunc pfnProgress,
2347
                                   void *pProgressData,
2348
                                   CSLConstList papszOptions)
2349
0
{
2350
0
    int *panAllBandList = nullptr;
2351
2352
0
    CPLStringList aosOptions(papszOptions);
2353
0
    if (poDriver && !aosOptions.empty())
2354
0
    {
2355
0
        const char *pszOptionList =
2356
0
            poDriver->GetMetadataItem(GDAL_DMD_OVERVIEW_CREATIONOPTIONLIST);
2357
0
        if (pszOptionList)
2358
0
        {
2359
            // For backwards compatibility
2360
0
            if (const char *opt = aosOptions.FetchNameValue("USE_RRD"))
2361
0
            {
2362
0
                if (strstr(pszOptionList, "<Value>RRD</Value>") &&
2363
0
                    aosOptions.FetchNameValue("LOCATION") == nullptr)
2364
0
                {
2365
0
                    if (CPLTestBool(opt))
2366
0
                        aosOptions.SetNameValue("LOCATION", "RRD");
2367
0
                    aosOptions.SetNameValue("USE_RRD", nullptr);
2368
0
                }
2369
0
            }
2370
0
            if (const char *opt =
2371
0
                    aosOptions.FetchNameValue("VRT_VIRTUAL_OVERVIEWS"))
2372
0
            {
2373
0
                if (strstr(pszOptionList, "VIRTUAL"))
2374
0
                {
2375
0
                    aosOptions.SetNameValue("VIRTUAL", opt);
2376
0
                    aosOptions.SetNameValue("VRT_VIRTUAL_OVERVIEWS", nullptr);
2377
0
                }
2378
0
            }
2379
2380
0
            for (const auto &[pszKey, pszValue] :
2381
0
                 cpl::IterateNameValue(papszOptions))
2382
0
            {
2383
0
                if (cpl::ends_with(std::string_view(pszKey), "_OVERVIEW"))
2384
0
                {
2385
0
                    aosOptions.SetNameValue(
2386
0
                        std::string(pszKey)
2387
0
                            .substr(0, strlen(pszKey) - strlen("_OVERVIEW"))
2388
0
                            .c_str(),
2389
0
                        pszValue);
2390
0
                    aosOptions.SetNameValue(pszKey, nullptr);
2391
0
                }
2392
0
            }
2393
2394
0
            CPLString osDriver;
2395
0
            osDriver.Printf("driver %s", poDriver->GetDescription());
2396
0
            GDALValidateOptions(pszOptionList, aosOptions.List(),
2397
0
                                "overview creation option", osDriver);
2398
0
        }
2399
0
    }
2400
2401
0
    if (nListBands == 0)
2402
0
    {
2403
0
        nListBands = GetRasterCount();
2404
0
        panAllBandList =
2405
0
            static_cast<int *>(CPLMalloc(sizeof(int) * nListBands));
2406
0
        for (int i = 0; i < nListBands; ++i)
2407
0
            panAllBandList[i] = i + 1;
2408
2409
0
        panBandList = panAllBandList;
2410
0
    }
2411
2412
0
    if (pfnProgress == nullptr)
2413
0
        pfnProgress = GDALDummyProgress;
2414
2415
0
    for (int i = 0; i < nOverviews; ++i)
2416
0
    {
2417
0
        if (panOverviewList[i] <= 0)
2418
0
        {
2419
0
            CPLError(CE_Failure, CPLE_IllegalArg,
2420
0
                     "panOverviewList[%d] = %d is invalid. It must be a "
2421
0
                     "positive value",
2422
0
                     i, panOverviewList[i]);
2423
0
            CPLFree(panAllBandList);
2424
0
            return CE_Failure;
2425
0
        }
2426
0
    }
2427
2428
0
    const CPLErr eErr = IBuildOverviews(
2429
0
        pszResampling, nOverviews, panOverviewList, nListBands, panBandList,
2430
0
        pfnProgress, pProgressData, aosOptions.List());
2431
2432
0
    if (panAllBandList != nullptr)
2433
0
        CPLFree(panAllBandList);
2434
2435
0
    return eErr;
2436
0
}
2437
2438
/************************************************************************/
2439
/*                         GDALBuildOverviews()                         */
2440
/************************************************************************/
2441
2442
/**
2443
 * \brief Build raster overview(s)
2444
 *
2445
 * @see GDALDataset::BuildOverviews() and GDALBuildOverviews()
2446
 */
2447
2448
CPLErr CPL_STDCALL GDALBuildOverviews(GDALDatasetH hDataset,
2449
                                      const char *pszResampling, int nOverviews,
2450
                                      const int *panOverviewList,
2451
                                      int nListBands, const int *panBandList,
2452
                                      GDALProgressFunc pfnProgress,
2453
                                      void *pProgressData)
2454
2455
0
{
2456
0
    VALIDATE_POINTER1(hDataset, "GDALBuildOverviews", CE_Failure);
2457
2458
0
    return GDALDataset::FromHandle(hDataset)->BuildOverviews(
2459
0
        pszResampling, nOverviews, panOverviewList, nListBands, panBandList,
2460
0
        pfnProgress, pProgressData, nullptr);
2461
0
}
2462
2463
/************************************************************************/
2464
/*                         GDALBuildOverviews()                         */
2465
/************************************************************************/
2466
2467
/**
2468
 * \brief Build raster overview(s)
2469
 *
2470
 * @see GDALDataset::BuildOverviews()
2471
 * @since GDAL 3.6
2472
 */
2473
2474
CPLErr CPL_STDCALL
2475
GDALBuildOverviewsEx(GDALDatasetH hDataset, const char *pszResampling,
2476
                     int nOverviews, const int *panOverviewList, int nListBands,
2477
                     const int *panBandList, GDALProgressFunc pfnProgress,
2478
                     void *pProgressData, CSLConstList papszOptions)
2479
2480
0
{
2481
0
    VALIDATE_POINTER1(hDataset, "GDALBuildOverviews", CE_Failure);
2482
2483
0
    return GDALDataset::FromHandle(hDataset)->BuildOverviews(
2484
0
        pszResampling, nOverviews, panOverviewList, nListBands, panBandList,
2485
0
        pfnProgress, pProgressData, papszOptions);
2486
0
}
2487
2488
/************************************************************************/
2489
/*                          IBuildOverviews()                           */
2490
/*                                                                      */
2491
/*      Default implementation.                                         */
2492
/************************************************************************/
2493
2494
//! @cond Doxygen_Suppress
2495
CPLErr GDALDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
2496
                                    const int *panOverviewList, int nListBands,
2497
                                    const int *panBandList,
2498
                                    GDALProgressFunc pfnProgress,
2499
                                    void *pProgressData,
2500
                                    CSLConstList papszOptions)
2501
2502
0
{
2503
0
    if (oOvManager.IsInitialized())
2504
0
        return oOvManager.BuildOverviews(
2505
0
            nullptr, pszResampling, nOverviews, panOverviewList, nListBands,
2506
0
            panBandList, pfnProgress, pProgressData, papszOptions);
2507
0
    else
2508
0
    {
2509
0
        ReportError(CE_Failure, CPLE_NotSupported,
2510
0
                    "BuildOverviews() not supported for this dataset.");
2511
2512
0
        return CE_Failure;
2513
0
    }
2514
0
}
2515
2516
//! @endcond
2517
2518
/************************************************************************/
2519
/*                            AddOverviews()                            */
2520
/*                                                                      */
2521
/*      Default implementation.                                         */
2522
/************************************************************************/
2523
2524
/**
2525
 * \brief Add overview from existing dataset(s)
2526
 *
2527
 * This function creates new overview levels or refresh existing one from
2528
 * the list of provided overview datasets.
2529
 * Source overviews may come from any GDAL supported format, provided they
2530
 * have the same number of bands and geospatial extent than the target
2531
 * dataset.
2532
 *
2533
 * If the operation is not supported for the indicated dataset, then
2534
 * CE_Failure is returned, and CPLGetLastErrorNo() will return
2535
 * CPLE_NotSupported.
2536
 *
2537
 * At time of writing, this method is only implemented for internal overviews
2538
 * of GeoTIFF datasets and external overviews in GeoTIFF format.
2539
 *
2540
 * @param apoSrcOvrDS Vector of source overviews.
2541
 * @param pfnProgress a function to call to report progress, or NULL.
2542
 * @param pProgressData application data to pass to the progress function.
2543
 * @param papszOptions NULL terminated list of options as
2544
 *                     key=value pairs, or NULL. Possible keys are the
2545
 *                     ones returned by
2546
 *                     GetDriver()->GetMetadataItem(GDAL_DMD_OVERVIEW_CREATIONOPTIONLIST)
2547
 *
2548
 * @return CE_None on success or CE_Failure if the operation doesn't work.
2549
 * @since 3.12
2550
 */
2551
CPLErr GDALDataset::AddOverviews(const std::vector<GDALDataset *> &apoSrcOvrDS,
2552
                                 GDALProgressFunc pfnProgress,
2553
                                 void *pProgressData, CSLConstList papszOptions)
2554
0
{
2555
0
    if (oOvManager.IsInitialized())
2556
0
    {
2557
0
        return oOvManager.AddOverviews(nullptr, apoSrcOvrDS, pfnProgress,
2558
0
                                       pProgressData, papszOptions);
2559
0
    }
2560
0
    else
2561
0
    {
2562
0
        ReportError(CE_Failure, CPLE_NotSupported,
2563
0
                    "AddOverviews() not supported for this dataset.");
2564
0
        return CE_Failure;
2565
0
    }
2566
0
}
2567
2568
/************************************************************************/
2569
/*                             IRasterIO()                              */
2570
/*                                                                      */
2571
/*      The default implementation of IRasterIO() is, in the general    */
2572
/*      case to pass the request off to each band objects rasterio      */
2573
/*      methods with appropriate arguments. In some cases, it might     */
2574
/*      choose instead the BlockBasedRasterIO() implementation.         */
2575
/************************************************************************/
2576
2577
//! @cond Doxygen_Suppress
2578
CPLErr GDALDataset::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
2579
                              int nXSize, int nYSize, void *pData,
2580
                              int nBufXSize, int nBufYSize,
2581
                              GDALDataType eBufType, int nBandCount,
2582
                              BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
2583
                              GSpacing nLineSpace, GSpacing nBandSpace,
2584
                              GDALRasterIOExtraArg *psExtraArg)
2585
2586
0
{
2587
0
    const char *pszInterleave = nullptr;
2588
2589
0
    CPLAssert(nullptr != pData);
2590
2591
0
    const bool bHasSubpixelShift =
2592
0
        psExtraArg->bFloatingPointWindowValidity &&
2593
0
        psExtraArg->eResampleAlg != GRIORA_NearestNeighbour &&
2594
0
        (nXOff != psExtraArg->dfXOff || nYOff != psExtraArg->dfYOff);
2595
2596
0
    if (!bHasSubpixelShift && nXSize == nBufXSize && nYSize == nBufYSize &&
2597
0
        nBandCount > 1 &&
2598
0
        (pszInterleave = GetMetadataItem("INTERLEAVE", "IMAGE_STRUCTURE")) !=
2599
0
            nullptr &&
2600
0
        EQUAL(pszInterleave, "PIXEL"))
2601
0
    {
2602
0
        return BlockBasedRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData,
2603
0
                                  nBufXSize, nBufYSize, eBufType, nBandCount,
2604
0
                                  panBandMap, nPixelSpace, nLineSpace,
2605
0
                                  nBandSpace, psExtraArg);
2606
0
    }
2607
2608
0
    if (eRWFlag == GF_Read &&
2609
0
        (psExtraArg->eResampleAlg == GRIORA_Cubic ||
2610
0
         psExtraArg->eResampleAlg == GRIORA_CubicSpline ||
2611
0
         psExtraArg->eResampleAlg == GRIORA_Bilinear ||
2612
0
         psExtraArg->eResampleAlg == GRIORA_Lanczos) &&
2613
0
        !(nXSize == nBufXSize && nYSize == nBufYSize) && nBandCount > 1)
2614
0
    {
2615
0
        if (nBufXSize < nXSize && nBufYSize < nYSize && AreOverviewsEnabled())
2616
0
        {
2617
0
            int bTried = FALSE;
2618
0
            const CPLErr eErr = TryOverviewRasterIO(
2619
0
                eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize,
2620
0
                nBufYSize, eBufType, nBandCount, panBandMap, nPixelSpace,
2621
0
                nLineSpace, nBandSpace, psExtraArg, &bTried);
2622
0
            if (bTried)
2623
0
                return eErr;
2624
0
        }
2625
2626
0
        GDALDataType eFirstBandDT = GDT_Unknown;
2627
0
        int nFirstMaskFlags = 0;
2628
0
        GDALRasterBand *poFirstMaskBand = nullptr;
2629
0
        int nOKBands = 0;
2630
2631
        // Check if bands share the same mask band
2632
0
        for (int i = 0; i < nBandCount; ++i)
2633
0
        {
2634
0
            GDALRasterBand *poBand = GetRasterBand(panBandMap[i]);
2635
0
            if ((nBufXSize < nXSize || nBufYSize < nYSize) &&
2636
0
                poBand->GetOverviewCount())
2637
0
            {
2638
                // Could be improved to select the appropriate overview.
2639
0
                break;
2640
0
            }
2641
0
            if (poBand->GetColorTable() != nullptr)
2642
0
            {
2643
0
                break;
2644
0
            }
2645
0
            const GDALDataType eDT = poBand->GetRasterDataType();
2646
0
            if (GDALDataTypeIsComplex(eDT))
2647
0
            {
2648
0
                break;
2649
0
            }
2650
0
            if (i == 0)
2651
0
            {
2652
0
                eFirstBandDT = eDT;
2653
0
                nFirstMaskFlags = poBand->GetMaskFlags();
2654
0
                if (nFirstMaskFlags == GMF_NODATA)
2655
0
                {
2656
                    // The dataset-level resampling code is not ready for nodata
2657
                    // Fallback to band-level resampling
2658
0
                    break;
2659
0
                }
2660
0
                poFirstMaskBand = poBand->GetMaskBand();
2661
0
            }
2662
0
            else
2663
0
            {
2664
0
                if (eDT != eFirstBandDT)
2665
0
                {
2666
0
                    break;
2667
0
                }
2668
0
                int nMaskFlags = poBand->GetMaskFlags();
2669
0
                if (nMaskFlags == GMF_NODATA)
2670
0
                {
2671
                    // The dataset-level resampling code is not ready for nodata
2672
                    // Fallback to band-level resampling
2673
0
                    break;
2674
0
                }
2675
0
                GDALRasterBand *poMaskBand = poBand->GetMaskBand();
2676
0
                if (nFirstMaskFlags == GMF_ALL_VALID &&
2677
0
                    nMaskFlags == GMF_ALL_VALID)
2678
0
                {
2679
                    // Ok.
2680
0
                }
2681
0
                else if (poFirstMaskBand == poMaskBand)
2682
0
                {
2683
                    // Ok.
2684
0
                }
2685
0
                else
2686
0
                {
2687
0
                    break;
2688
0
                }
2689
0
            }
2690
2691
0
            ++nOKBands;
2692
0
        }
2693
2694
0
        GDALProgressFunc pfnProgressGlobal = psExtraArg->pfnProgress;
2695
0
        void *pProgressDataGlobal = psExtraArg->pProgressData;
2696
2697
0
        CPLErr eErr = CE_None;
2698
0
        if (nOKBands > 0)
2699
0
        {
2700
0
            if (nOKBands < nBandCount)
2701
0
            {
2702
0
                psExtraArg->pfnProgress = GDALScaledProgress;
2703
0
                psExtraArg->pProgressData = GDALCreateScaledProgress(
2704
0
                    0.0, static_cast<double>(nOKBands) / nBandCount,
2705
0
                    pfnProgressGlobal, pProgressDataGlobal);
2706
0
                if (psExtraArg->pProgressData == nullptr)
2707
0
                    psExtraArg->pfnProgress = nullptr;
2708
0
            }
2709
2710
0
            eErr = RasterIOResampled(eRWFlag, nXOff, nYOff, nXSize, nYSize,
2711
0
                                     pData, nBufXSize, nBufYSize, eBufType,
2712
0
                                     nOKBands, panBandMap, nPixelSpace,
2713
0
                                     nLineSpace, nBandSpace, psExtraArg);
2714
2715
0
            if (nOKBands < nBandCount)
2716
0
            {
2717
0
                GDALDestroyScaledProgress(psExtraArg->pProgressData);
2718
0
            }
2719
0
        }
2720
0
        if (eErr == CE_None && nOKBands < nBandCount)
2721
0
        {
2722
0
            if (nOKBands > 0)
2723
0
            {
2724
0
                psExtraArg->pfnProgress = GDALScaledProgress;
2725
0
                psExtraArg->pProgressData = GDALCreateScaledProgress(
2726
0
                    static_cast<double>(nOKBands) / nBandCount, 1.0,
2727
0
                    pfnProgressGlobal, pProgressDataGlobal);
2728
0
                if (psExtraArg->pProgressData == nullptr)
2729
0
                    psExtraArg->pfnProgress = nullptr;
2730
0
            }
2731
0
            eErr = BandBasedRasterIO(
2732
0
                eRWFlag, nXOff, nYOff, nXSize, nYSize,
2733
0
                static_cast<GByte *>(pData) + nBandSpace * nOKBands, nBufXSize,
2734
0
                nBufYSize, eBufType, nBandCount - nOKBands,
2735
0
                panBandMap + nOKBands, nPixelSpace, nLineSpace, nBandSpace,
2736
0
                psExtraArg);
2737
0
            if (nOKBands > 0)
2738
0
            {
2739
0
                GDALDestroyScaledProgress(psExtraArg->pProgressData);
2740
0
            }
2741
0
        }
2742
2743
0
        psExtraArg->pfnProgress = pfnProgressGlobal;
2744
0
        psExtraArg->pProgressData = pProgressDataGlobal;
2745
2746
0
        return eErr;
2747
0
    }
2748
2749
0
    return BandBasedRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData,
2750
0
                             nBufXSize, nBufYSize, eBufType, nBandCount,
2751
0
                             panBandMap, nPixelSpace, nLineSpace, nBandSpace,
2752
0
                             psExtraArg);
2753
0
}
2754
2755
//! @endcond
2756
2757
/************************************************************************/
2758
/*                         BandBasedRasterIO()                          */
2759
/*                                                                      */
2760
/*      Pass the request off to each band objects rasterio methods with */
2761
/*      appropriate arguments.                                          */
2762
/************************************************************************/
2763
2764
//! @cond Doxygen_Suppress
2765
CPLErr GDALDataset::BandBasedRasterIO(
2766
    GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
2767
    void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
2768
    int nBandCount, const int *panBandMap, GSpacing nPixelSpace,
2769
    GSpacing nLineSpace, GSpacing nBandSpace, GDALRasterIOExtraArg *psExtraArg)
2770
2771
0
{
2772
0
    int iBandIndex;
2773
0
    CPLErr eErr = CE_None;
2774
2775
0
    GDALProgressFunc pfnProgressGlobal = psExtraArg->pfnProgress;
2776
0
    void *pProgressDataGlobal = psExtraArg->pProgressData;
2777
2778
0
    for (iBandIndex = 0; iBandIndex < nBandCount && eErr == CE_None;
2779
0
         ++iBandIndex)
2780
0
    {
2781
0
        GDALRasterBand *poBand = GetRasterBand(panBandMap[iBandIndex]);
2782
2783
0
        if (poBand == nullptr)
2784
0
        {
2785
0
            eErr = CE_Failure;
2786
0
            break;
2787
0
        }
2788
2789
0
        GByte *pabyBandData =
2790
0
            static_cast<GByte *>(pData) + iBandIndex * nBandSpace;
2791
2792
0
        if (nBandCount > 1)
2793
0
        {
2794
0
            psExtraArg->pfnProgress = GDALScaledProgress;
2795
0
            psExtraArg->pProgressData = GDALCreateScaledProgress(
2796
0
                1.0 * iBandIndex / nBandCount,
2797
0
                1.0 * (iBandIndex + 1) / nBandCount, pfnProgressGlobal,
2798
0
                pProgressDataGlobal);
2799
0
            if (psExtraArg->pProgressData == nullptr)
2800
0
                psExtraArg->pfnProgress = nullptr;
2801
0
        }
2802
2803
0
        eErr = poBand->IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
2804
0
                                 pabyBandData, nBufXSize, nBufYSize, eBufType,
2805
0
                                 nPixelSpace, nLineSpace, psExtraArg);
2806
2807
0
        if (nBandCount > 1)
2808
0
            GDALDestroyScaledProgress(psExtraArg->pProgressData);
2809
0
    }
2810
2811
0
    psExtraArg->pfnProgress = pfnProgressGlobal;
2812
0
    psExtraArg->pProgressData = pProgressDataGlobal;
2813
2814
0
    return eErr;
2815
0
}
2816
2817
//! @endcond
2818
2819
/************************************************************************/
2820
/*               ValidateRasterIOOrAdviseReadParameters()               */
2821
/************************************************************************/
2822
2823
//! @cond Doxygen_Suppress
2824
CPLErr GDALDataset::ValidateRasterIOOrAdviseReadParameters(
2825
    const char *pszCallingFunc, int *pbStopProcessingOnCENone, int nXOff,
2826
    int nYOff, int nXSize, int nYSize, int nBufXSize, int nBufYSize,
2827
    int nBandCount, const int *panBandMap)
2828
0
{
2829
2830
    /* -------------------------------------------------------------------- */
2831
    /*      Some size values are "noop".  Lets just return to avoid         */
2832
    /*      stressing lower level functions.                                */
2833
    /* -------------------------------------------------------------------- */
2834
0
    if (nXSize < 1 || nYSize < 1 || nBufXSize < 1 || nBufYSize < 1)
2835
0
    {
2836
0
        CPLDebug("GDAL",
2837
0
                 "%s skipped for odd window or buffer size.\n"
2838
0
                 "  Window = (%d,%d)x%dx%d\n"
2839
0
                 "  Buffer = %dx%d",
2840
0
                 pszCallingFunc, nXOff, nYOff, nXSize, nYSize, nBufXSize,
2841
0
                 nBufYSize);
2842
2843
0
        *pbStopProcessingOnCENone = TRUE;
2844
0
        return CE_None;
2845
0
    }
2846
2847
0
    CPLErr eErr = CE_None;
2848
0
    *pbStopProcessingOnCENone = FALSE;
2849
2850
0
    if (nXOff < 0 || nXOff > INT_MAX - nXSize ||
2851
0
        nXOff + nXSize > nRasterXSize || nYOff < 0 ||
2852
0
        nYOff > INT_MAX - nYSize || nYOff + nYSize > nRasterYSize)
2853
0
    {
2854
0
        ReportError(CE_Failure, CPLE_IllegalArg,
2855
0
                    "Access window out of range in %s.  Requested "
2856
0
                    "(%d,%d) of size %dx%d on raster of %dx%d.",
2857
0
                    pszCallingFunc, nXOff, nYOff, nXSize, nYSize, nRasterXSize,
2858
0
                    nRasterYSize);
2859
0
        eErr = CE_Failure;
2860
0
    }
2861
2862
0
    if (panBandMap == nullptr && nBandCount > GetRasterCount())
2863
0
    {
2864
0
        ReportError(CE_Failure, CPLE_IllegalArg,
2865
0
                    "%s: nBandCount cannot be greater than %d", pszCallingFunc,
2866
0
                    GetRasterCount());
2867
0
        eErr = CE_Failure;
2868
0
    }
2869
2870
0
    for (int i = 0; i < nBandCount && eErr == CE_None; ++i)
2871
0
    {
2872
0
        int iBand = (panBandMap != nullptr) ? panBandMap[i] : i + 1;
2873
0
        if (iBand < 1 || iBand > GetRasterCount())
2874
0
        {
2875
0
            ReportError(
2876
0
                CE_Failure, CPLE_IllegalArg,
2877
0
                "%s: panBandMap[%d] = %d, this band does not exist on dataset.",
2878
0
                pszCallingFunc, i, iBand);
2879
0
            eErr = CE_Failure;
2880
0
        }
2881
2882
0
        if (eErr == CE_None && GetRasterBand(iBand) == nullptr)
2883
0
        {
2884
0
            ReportError(
2885
0
                CE_Failure, CPLE_IllegalArg,
2886
0
                "%s: panBandMap[%d]=%d, this band should exist but is NULL!",
2887
0
                pszCallingFunc, i, iBand);
2888
0
            eErr = CE_Failure;
2889
0
        }
2890
0
    }
2891
2892
0
    return eErr;
2893
0
}
2894
2895
//! @endcond
2896
2897
/************************************************************************/
2898
/*                              RasterIO()                              */
2899
/************************************************************************/
2900
2901
/**
2902
 * \brief Read/write a region of image data from multiple bands.
2903
 *
2904
 * This method allows reading a region of one or more GDALRasterBands from
2905
 * this dataset into a buffer,  or writing data from a buffer into a region
2906
 * of the GDALRasterBands.  It automatically takes care of data type
2907
 * translation if the data type (eBufType) of the buffer is different than
2908
 * that of the GDALRasterBand.
2909
 * The method also takes care of image decimation / replication if the
2910
 * buffer size (nBufXSize x nBufYSize) is different than the size of the
2911
 * region being accessed (nXSize x nYSize).
2912
 *
2913
 * The window of interest expressed by (nXOff, nYOff, nXSize, nYSize) should be
2914
 * fully within the raster space, that is nXOff >= 0, nYOff >= 0,
2915
 * nXOff + nXSize <= GetRasterXSize() and nYOff + nYSize <= GetRasterYSize().
2916
 * If reads larger than the raster space are wished, GDALTranslate() might be used.
2917
 * Or use nLineSpace and a possibly shifted pData value.
2918
 *
2919
 * The nPixelSpace, nLineSpace and nBandSpace parameters allow reading into or
2920
 * writing from various organization of buffers.
2921
 *
2922
 * Some formats may efficiently implement decimation into a buffer by
2923
 * reading from lower resolution overview images. The logic of the default
2924
 * implementation in the base class GDALRasterBand is the following one. It
2925
 * computes a target_downscaling_factor from the window of interest and buffer
2926
 * size which is min(nXSize/nBufXSize, nYSize/nBufYSize).
2927
 * It then walks through overviews and will select the first one whose
2928
 * downscaling factor is greater than target_downscaling_factor / 1.2.
2929
 *
2930
 * Let's assume we have overviews at downscaling factors 2, 4 and 8.
2931
 * The relationship between target_downscaling_factor and the select overview
2932
 * level is the following one:
2933
 *
2934
 * target_downscaling_factor  | selected_overview
2935
 * -------------------------  | -----------------
2936
 * ]0,       2 / 1.2]         | full resolution band
2937
 * ]2 / 1.2, 4 / 1.2]         | 2x downsampled band
2938
 * ]4 / 1.2, 8 / 1.2]         | 4x downsampled band
2939
 * ]8 / 1.2, infinity[        | 8x downsampled band
2940
 *
2941
 * Note that starting with GDAL 3.9, this 1.2 oversampling factor can be
2942
 * modified by setting the GDAL_OVERVIEW_OVERSAMPLING_THRESHOLD configuration
2943
 * option. Also note that starting with GDAL 3.9, when the resampling algorithm
2944
 * specified in psExtraArg->eResampleAlg is different from GRIORA_NearestNeighbour,
2945
 * this oversampling threshold defaults to 1. Consequently if there are overviews
2946
 * of downscaling factor 2, 4 and 8, and the desired downscaling factor is
2947
 * 7.99, the overview of factor 4 will be selected for a non nearest resampling.
2948
 *
2949
 * For highest performance full resolution data access, read and write
2950
 * on "block boundaries" as returned by GetBlockSize(), or use the
2951
 * ReadBlock() and WriteBlock() methods.
2952
 *
2953
 * This method is the same as the C GDALDatasetRasterIO() or
2954
 * GDALDatasetRasterIOEx() functions.
2955
 *
2956
 * @param eRWFlag Either GF_Read to read a region of data, or GF_Write to
2957
 * write a region of data.
2958
 *
2959
 * @param nXOff The pixel offset to the top left corner of the region
2960
 * of the band to be accessed.  This would be zero to start from the left side.
2961
 *
2962
 * @param nYOff The line offset to the top left corner of the region
2963
 * of the band to be accessed.  This would be zero to start from the top.
2964
 *
2965
 * @param nXSize The width of the region of the band to be accessed in pixels.
2966
 *
2967
 * @param nYSize The height of the region of the band to be accessed in lines.
2968
 *
2969
 * @param pData The buffer into which the data should be read, or from which
2970
 * it should be written.  This buffer must contain at least
2971
 * nBufXSize * nBufYSize * nBandCount words of type eBufType.  It is organized
2972
 * in left to right,top to bottom pixel order.  Spacing is controlled by the
2973
 * nPixelSpace, and nLineSpace parameters.
2974
 * Note that even with eRWFlag==GF_Write, the content of the buffer might be
2975
 * temporarily modified during the execution of this method (and eventually
2976
 * restored back to its original content), so it is not safe to use a buffer
2977
 * stored in a read-only section of the calling program.
2978
 *
2979
 * @param nBufXSize the width of the buffer image into which the desired region
2980
 * is to be read, or from which it is to be written.
2981
 *
2982
 * @param nBufYSize the height of the buffer image into which the desired
2983
 * region is to be read, or from which it is to be written.
2984
 *
2985
 * @param eBufType the type of the pixel values in the pData data buffer. The
2986
 * pixel values will automatically be translated to/from the GDALRasterBand
2987
 * data type as needed. Most driver implementations will use GDALCopyWords64()
2988
 * to perform data type translation.
2989
 *
2990
 * @param nBandCount the number of bands being read or written.
2991
 *
2992
 * @param panBandMap the list of nBandCount band numbers being read/written.
2993
 * Note band numbers are 1 based. This may be NULL to select the first
2994
 * nBandCount bands. (Note: before GDAL 3.10, argument type was "int*", and
2995
 * not "const int*")
2996
 *
2997
 * @param nPixelSpace The byte offset from the start of one pixel value in
2998
 * pData to the start of the next pixel value within a scanline. If defaulted
2999
 * (0) the size of the datatype eBufType is used.
3000
 *
3001
 * @param nLineSpace The byte offset from the start of one scanline in
3002
 * pData to the start of the next. If defaulted (0) the size of the datatype
3003
 * eBufType * nBufXSize is used.
3004
 *
3005
 * @param nBandSpace the byte offset from the start of one bands data to the
3006
 * start of the next. If defaulted (0) the value will be
3007
 * nLineSpace * nBufYSize implying band sequential organization
3008
 * of the data buffer.
3009
 *
3010
 * @param psExtraArg pointer to a GDALRasterIOExtraArg
3011
 * structure with additional arguments to specify resampling and progress
3012
 * callback, or NULL for default behavior. The GDAL_RASTERIO_RESAMPLING
3013
 * configuration option can also be defined to override the default resampling
3014
 * to one of BILINEAR, CUBIC, CUBICSPLINE, LANCZOS, AVERAGE or MODE.
3015
 *
3016
 * @return CE_Failure if the access fails, otherwise CE_None.
3017
 */
3018
3019
CPLErr GDALDataset::RasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
3020
                             int nXSize, int nYSize, void *pData, int nBufXSize,
3021
                             int nBufYSize, GDALDataType eBufType,
3022
                             int nBandCount, const int *panBandMap,
3023
                             GSpacing nPixelSpace, GSpacing nLineSpace,
3024
                             GSpacing nBandSpace,
3025
                             GDALRasterIOExtraArg *psExtraArg)
3026
3027
0
{
3028
0
    GDALRasterIOExtraArg sExtraArg;
3029
0
    if (psExtraArg == nullptr)
3030
0
    {
3031
0
        INIT_RASTERIO_EXTRA_ARG(sExtraArg);
3032
3033
        // 4 below inits are not strictly needed but make Coverity Scan
3034
        // happy
3035
0
        sExtraArg.dfXOff = nXOff;
3036
0
        sExtraArg.dfYOff = nYOff;
3037
0
        sExtraArg.dfXSize = nXSize;
3038
0
        sExtraArg.dfYSize = nYSize;
3039
3040
0
        psExtraArg = &sExtraArg;
3041
0
    }
3042
0
    else if (CPL_UNLIKELY(psExtraArg->nVersion >
3043
0
                          RASTERIO_EXTRA_ARG_CURRENT_VERSION))
3044
0
    {
3045
0
        ReportError(CE_Failure, CPLE_AppDefined,
3046
0
                    "Unhandled version of GDALRasterIOExtraArg");
3047
0
        return CE_Failure;
3048
0
    }
3049
3050
0
    GDALRasterIOExtraArgSetResampleAlg(psExtraArg, nXSize, nYSize, nBufXSize,
3051
0
                                       nBufYSize);
3052
3053
0
    if (CPL_UNLIKELY(nullptr == pData))
3054
0
    {
3055
0
        ReportError(CE_Failure, CPLE_AppDefined,
3056
0
                    "The buffer into which the data should be read is null");
3057
0
        return CE_Failure;
3058
0
    }
3059
3060
    /* -------------------------------------------------------------------- */
3061
    /*      Do some validation of parameters.                               */
3062
    /* -------------------------------------------------------------------- */
3063
3064
0
    if (CPL_UNLIKELY(eRWFlag != GF_Read && eRWFlag != GF_Write))
3065
0
    {
3066
0
        ReportError(
3067
0
            CE_Failure, CPLE_IllegalArg,
3068
0
            "eRWFlag = %d, only GF_Read (0) and GF_Write (1) are legal.",
3069
0
            eRWFlag);
3070
0
        return CE_Failure;
3071
0
    }
3072
3073
0
    if (eRWFlag == GF_Write)
3074
0
    {
3075
0
        if (CPL_UNLIKELY(eAccess != GA_Update))
3076
0
        {
3077
0
            ReportError(CE_Failure, CPLE_AppDefined,
3078
0
                        "Write operation not permitted on dataset opened "
3079
0
                        "in read-only mode");
3080
0
            return CE_Failure;
3081
0
        }
3082
0
    }
3083
3084
0
    int bStopProcessing = FALSE;
3085
0
    CPLErr eErr = ValidateRasterIOOrAdviseReadParameters(
3086
0
        "RasterIO()", &bStopProcessing, nXOff, nYOff, nXSize, nYSize, nBufXSize,
3087
0
        nBufYSize, nBandCount, panBandMap);
3088
0
    if (eErr != CE_None || bStopProcessing)
3089
0
        return eErr;
3090
0
    if (CPL_UNLIKELY(eBufType == GDT_Unknown || eBufType == GDT_TypeCount))
3091
0
    {
3092
0
        ReportError(CE_Failure, CPLE_AppDefined,
3093
0
                    "Illegal GDT_Unknown/GDT_TypeCount argument");
3094
0
        return CE_Failure;
3095
0
    }
3096
3097
    /* -------------------------------------------------------------------- */
3098
    /*      If pixel and line spacing are defaulted assign reasonable      */
3099
    /*      value assuming a packed buffer.                                 */
3100
    /* -------------------------------------------------------------------- */
3101
0
    if (nPixelSpace == 0)
3102
0
        nPixelSpace = GDALGetDataTypeSizeBytes(eBufType);
3103
3104
0
    if (nLineSpace == 0)
3105
0
    {
3106
0
        nLineSpace = nPixelSpace * nBufXSize;
3107
0
    }
3108
3109
0
    if (nBandSpace == 0 && nBandCount > 1)
3110
0
    {
3111
0
        nBandSpace = nLineSpace * nBufYSize;
3112
0
    }
3113
3114
0
    if (panBandMap == nullptr)
3115
0
    {
3116
0
        if (!m_poPrivate)
3117
0
            return CE_Failure;
3118
0
        CPLAssert(static_cast<int>(m_poPrivate->m_anBandMap.size()) == nBands);
3119
0
        panBandMap = m_poPrivate->m_anBandMap.data();
3120
0
    }
3121
3122
0
    int bCallLeaveReadWrite = EnterReadWrite(eRWFlag);
3123
3124
    /* -------------------------------------------------------------------- */
3125
    /*      We are being forced to use cached IO instead of a driver        */
3126
    /*      specific implementation.                                        */
3127
    /* -------------------------------------------------------------------- */
3128
0
    if (bForceCachedIO)
3129
0
    {
3130
0
        eErr = BlockBasedRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData,
3131
0
                                  nBufXSize, nBufYSize, eBufType, nBandCount,
3132
0
                                  panBandMap, nPixelSpace, nLineSpace,
3133
0
                                  nBandSpace, psExtraArg);
3134
0
    }
3135
3136
    /* -------------------------------------------------------------------- */
3137
    /*      Call the format specific function.                              */
3138
    /* -------------------------------------------------------------------- */
3139
0
    else
3140
0
    {
3141
0
        eErr = IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData,
3142
0
                         nBufXSize, nBufYSize, eBufType, nBandCount,
3143
                         // TODO: remove this const_cast once IRasterIO()
3144
                         // takes a const int*
3145
0
                         const_cast<int *>(panBandMap), nPixelSpace, nLineSpace,
3146
0
                         nBandSpace, psExtraArg);
3147
0
    }
3148
3149
0
    if (bCallLeaveReadWrite)
3150
0
        LeaveReadWrite();
3151
3152
0
    return eErr;
3153
0
}
3154
3155
/************************************************************************/
3156
/*                        GDALDatasetRasterIO()                         */
3157
/************************************************************************/
3158
3159
/**
3160
 * \brief Read/write a region of image data from multiple bands.
3161
 *
3162
 * Use GDALDatasetRasterIOEx() if 64 bit spacings or extra arguments (resampling
3163
 * resolution, progress callback, etc. are needed)
3164
 *
3165
 * Note: before GDAL 3.10, panBandMap type was "int*", and not "const int*"
3166
 *
3167
 * @see GDALDataset::RasterIO()
3168
 */
3169
3170
CPLErr CPL_STDCALL GDALDatasetRasterIO(GDALDatasetH hDS, GDALRWFlag eRWFlag,
3171
                                       int nXOff, int nYOff, int nXSize,
3172
                                       int nYSize, void *pData, int nBufXSize,
3173
                                       int nBufYSize, GDALDataType eBufType,
3174
                                       int nBandCount, const int *panBandMap,
3175
                                       int nPixelSpace, int nLineSpace,
3176
                                       int nBandSpace)
3177
3178
0
{
3179
0
    VALIDATE_POINTER1(hDS, "GDALDatasetRasterIO", CE_Failure);
3180
3181
0
    GDALDataset *poDS = GDALDataset::FromHandle(hDS);
3182
3183
0
    return poDS->RasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData,
3184
0
                          nBufXSize, nBufYSize, eBufType, nBandCount,
3185
0
                          panBandMap, nPixelSpace, nLineSpace, nBandSpace,
3186
0
                          nullptr);
3187
0
}
3188
3189
/************************************************************************/
3190
/*                       GDALDatasetRasterIOEx()                        */
3191
/************************************************************************/
3192
3193
/**
3194
 * \brief Read/write a region of image data from multiple bands.
3195
 *
3196
 * Note: before GDAL 3.10, panBandMap type was "int*", and not "const int*"
3197
 *
3198
 * @see GDALDataset::RasterIO()
3199
 */
3200
3201
CPLErr CPL_STDCALL GDALDatasetRasterIOEx(
3202
    GDALDatasetH hDS, GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize,
3203
    int nYSize, void *pData, int nBufXSize, int nBufYSize,
3204
    GDALDataType eBufType, int nBandCount, const int *panBandMap,
3205
    GSpacing nPixelSpace, GSpacing nLineSpace, GSpacing nBandSpace,
3206
    GDALRasterIOExtraArg *psExtraArg)
3207
3208
0
{
3209
0
    VALIDATE_POINTER1(hDS, "GDALDatasetRasterIOEx", CE_Failure);
3210
3211
0
    GDALDataset *poDS = GDALDataset::FromHandle(hDS);
3212
3213
0
    return poDS->RasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData,
3214
0
                          nBufXSize, nBufYSize, eBufType, nBandCount,
3215
0
                          panBandMap, nPixelSpace, nLineSpace, nBandSpace,
3216
0
                          psExtraArg);
3217
0
}
3218
3219
/************************************************************************/
3220
/*                          GetOpenDatasets()                           */
3221
/************************************************************************/
3222
3223
/**
3224
 * \brief Fetch all open GDAL dataset handles.
3225
 *
3226
 * This method is the same as the C function GDALGetOpenDatasets().
3227
 *
3228
 * NOTE: This method is not thread safe.  The returned list may change
3229
 * at any time and it should not be freed.
3230
 *
3231
 * @param pnCount integer into which to place the count of dataset pointers
3232
 * being returned.
3233
 *
3234
 * @return a pointer to an array of dataset handles.
3235
 */
3236
3237
GDALDataset **GDALDataset::GetOpenDatasets(int *pnCount)
3238
3239
0
{
3240
0
    CPLMutexHolderD(&hDLMutex);
3241
3242
0
    if (poAllDatasetMap == nullptr)
3243
0
    {
3244
0
        *pnCount = 0;
3245
0
        return nullptr;
3246
0
    }
3247
3248
0
    *pnCount = static_cast<int>(poAllDatasetMap->size());
3249
0
    ppDatasets = static_cast<GDALDataset **>(
3250
0
        CPLRealloc(ppDatasets, (*pnCount) * sizeof(GDALDataset *)));
3251
0
    std::map<GDALDataset *, GIntBig>::iterator oIter = poAllDatasetMap->begin();
3252
0
    for (int i = 0; oIter != poAllDatasetMap->end(); ++oIter, ++i)
3253
0
        ppDatasets[i] = oIter->first;
3254
0
    return ppDatasets;
3255
0
}
3256
3257
/************************************************************************/
3258
/*                        GDALGetOpenDatasets()                         */
3259
/************************************************************************/
3260
3261
/**
3262
 * \brief Fetch all open GDAL dataset handles.
3263
 *
3264
 * @see GDALDataset::GetOpenDatasets()
3265
 */
3266
3267
void CPL_STDCALL GDALGetOpenDatasets(GDALDatasetH **ppahDSList, int *pnCount)
3268
3269
0
{
3270
0
    VALIDATE_POINTER0(ppahDSList, "GDALGetOpenDatasets");
3271
0
    VALIDATE_POINTER0(pnCount, "GDALGetOpenDatasets");
3272
3273
0
    *ppahDSList =
3274
0
        reinterpret_cast<GDALDatasetH *>(GDALDataset::GetOpenDatasets(pnCount));
3275
0
}
3276
3277
/************************************************************************/
3278
/*                        GDALCleanOpenDatasetsList()                   */
3279
/************************************************************************/
3280
3281
// Useful when called from the child of a fork(), to avoid closing
3282
// the datasets of the parent at the child termination.
3283
void GDALNullifyOpenDatasetsList()
3284
0
{
3285
0
    poAllDatasetMap = nullptr;
3286
0
    phSharedDatasetSet = nullptr;
3287
0
    ppDatasets = nullptr;
3288
0
    hDLMutex = nullptr;
3289
0
}
3290
3291
/************************************************************************/
3292
/*                             GDALGetAccess()                          */
3293
/************************************************************************/
3294
3295
/**
3296
 * \brief Return access flag
3297
 *
3298
 * @see GDALDataset::GetAccess()
3299
 */
3300
3301
int CPL_STDCALL GDALGetAccess(GDALDatasetH hDS)
3302
0
{
3303
0
    VALIDATE_POINTER1(hDS, "GDALGetAccess", 0);
3304
3305
0
    return GDALDataset::FromHandle(hDS)->GetAccess();
3306
0
}
3307
3308
/************************************************************************/
3309
/*                             AdviseRead()                             */
3310
/************************************************************************/
3311
3312
/**
3313
 * \brief Advise driver of upcoming read requests.
3314
 *
3315
 * Some GDAL drivers operate more efficiently if they know in advance what
3316
 * set of upcoming read requests will be made.  The AdviseRead() method allows
3317
 * an application to notify the driver of the region and bands of interest,
3318
 * and at what resolution the region will be read.
3319
 *
3320
 * Many drivers just ignore the AdviseRead() call, but it can dramatically
3321
 * accelerate access via some drivers.
3322
 *
3323
 * Depending on call paths, drivers might receive several calls to
3324
 * AdviseRead() with the same parameters.
3325
 *
3326
 * @param nXOff The pixel offset to the top left corner of the region
3327
 * of the band to be accessed.  This would be zero to start from the left side.
3328
 *
3329
 * @param nYOff The line offset to the top left corner of the region
3330
 * of the band to be accessed.  This would be zero to start from the top.
3331
 *
3332
 * @param nXSize The width of the region of the band to be accessed in pixels.
3333
 *
3334
 * @param nYSize The height of the region of the band to be accessed in lines.
3335
 *
3336
 * @param nBufXSize the width of the buffer image into which the desired region
3337
 * is to be read, or from which it is to be written.
3338
 *
3339
 * @param nBufYSize the height of the buffer image into which the desired
3340
 * region is to be read, or from which it is to be written.
3341
 *
3342
 * @param eBufType the type of the pixel values in the pData data buffer.  The
3343
 * pixel values will automatically be translated to/from the GDALRasterBand
3344
 * data type as needed.
3345
 *
3346
 * @param nBandCount the number of bands being read or written.
3347
 *
3348
 * @param panBandMap the list of nBandCount band numbers being read/written.
3349
 * Note band numbers are 1 based.   This may be NULL to select the first
3350
 * nBandCount bands.
3351
 *
3352
 * @param papszOptions a list of name=value strings with special control
3353
 * options.  Normally this is NULL.
3354
 *
3355
 * @return CE_Failure if the request is invalid and CE_None if it works or
3356
 * is ignored.
3357
 */
3358
3359
CPLErr GDALDataset::AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
3360
                               int nBufXSize, int nBufYSize,
3361
                               GDALDataType eBufType, int nBandCount,
3362
                               int *panBandMap, char **papszOptions)
3363
3364
0
{
3365
    /* -------------------------------------------------------------------- */
3366
    /*      Do some validation of parameters.                               */
3367
    /* -------------------------------------------------------------------- */
3368
0
    int bStopProcessing = FALSE;
3369
0
    CPLErr eErr = ValidateRasterIOOrAdviseReadParameters(
3370
0
        "AdviseRead()", &bStopProcessing, nXOff, nYOff, nXSize, nYSize,
3371
0
        nBufXSize, nBufYSize, nBandCount, panBandMap);
3372
0
    if (eErr != CE_None || bStopProcessing)
3373
0
        return eErr;
3374
3375
0
    for (int iBand = 0; iBand < nBandCount; ++iBand)
3376
0
    {
3377
0
        GDALRasterBand *poBand = nullptr;
3378
3379
0
        if (panBandMap == nullptr)
3380
0
            poBand = GetRasterBand(iBand + 1);
3381
0
        else
3382
0
            poBand = GetRasterBand(panBandMap[iBand]);
3383
3384
0
        if (poBand == nullptr)
3385
0
            return CE_Failure;
3386
3387
0
        eErr = poBand->AdviseRead(nXOff, nYOff, nXSize, nYSize, nBufXSize,
3388
0
                                  nBufYSize, eBufType, papszOptions);
3389
3390
0
        if (eErr != CE_None)
3391
0
            return eErr;
3392
0
    }
3393
3394
0
    return CE_None;
3395
0
}
3396
3397
/************************************************************************/
3398
/*                       GDALDatasetAdviseRead()                        */
3399
/************************************************************************/
3400
3401
/**
3402
 * \brief Advise driver of upcoming read requests.
3403
 *
3404
 * @see GDALDataset::AdviseRead()
3405
 */
3406
CPLErr CPL_STDCALL GDALDatasetAdviseRead(GDALDatasetH hDS, int nXOff, int nYOff,
3407
                                         int nXSize, int nYSize, int nBufXSize,
3408
                                         int nBufYSize, GDALDataType eDT,
3409
                                         int nBandCount, int *panBandMap,
3410
                                         CSLConstList papszOptions)
3411
3412
0
{
3413
0
    VALIDATE_POINTER1(hDS, "GDALDatasetAdviseRead", CE_Failure);
3414
3415
0
    return GDALDataset::FromHandle(hDS)->AdviseRead(
3416
0
        nXOff, nYOff, nXSize, nYSize, nBufXSize, nBufYSize, eDT, nBandCount,
3417
0
        panBandMap, const_cast<char **>(papszOptions));
3418
0
}
3419
3420
/************************************************************************/
3421
/*                         GDALAntiRecursionStruct                      */
3422
/************************************************************************/
3423
3424
// Prevent infinite recursion.
3425
struct GDALAntiRecursionStruct
3426
{
3427
    struct DatasetContext
3428
    {
3429
        std::string osFilename;
3430
        int nOpenFlags;
3431
        std::string osAllowedDrivers;
3432
3433
        DatasetContext(const std::string &osFilenameIn, int nOpenFlagsIn,
3434
                       const std::string &osAllowedDriversIn)
3435
0
            : osFilename(osFilenameIn), nOpenFlags(nOpenFlagsIn),
3436
0
              osAllowedDrivers(osAllowedDriversIn)
3437
0
        {
3438
0
        }
3439
    };
3440
3441
    struct DatasetContextCompare
3442
    {
3443
        bool operator()(const DatasetContext &lhs,
3444
                        const DatasetContext &rhs) const
3445
0
        {
3446
0
            return lhs.osFilename < rhs.osFilename ||
3447
0
                   (lhs.osFilename == rhs.osFilename &&
3448
0
                    (lhs.nOpenFlags < rhs.nOpenFlags ||
3449
0
                     (lhs.nOpenFlags == rhs.nOpenFlags &&
3450
0
                      lhs.osAllowedDrivers < rhs.osAllowedDrivers)));
3451
0
        }
3452
    };
3453
3454
    ~GDALAntiRecursionStruct()
3455
0
    {
3456
0
        CPLAssert(aosDatasetNamesWithFlags.empty());
3457
0
        CPLAssert(nRecLevel == 0);
3458
0
        CPLAssert(m_oMapDepth.empty());
3459
0
    }
3460
3461
    std::set<DatasetContext, DatasetContextCompare> aosDatasetNamesWithFlags{};
3462
    int nRecLevel = 0;
3463
    std::map<std::string, int> m_oMapDepth{};
3464
};
3465
3466
#ifdef _WIN32
3467
// Currently thread_local and C++ objects don't work well with DLL on Windows
3468
static void FreeAntiRecursionOpen(void *pData)
3469
{
3470
    delete static_cast<GDALAntiRecursionStruct *>(pData);
3471
}
3472
3473
static GDALAntiRecursionStruct &GetAntiRecursionOpen()
3474
{
3475
    static GDALAntiRecursionStruct dummy;
3476
    int bMemoryErrorOccurred = false;
3477
    void *pData =
3478
        CPLGetTLSEx(CTLS_GDALOPEN_ANTIRECURSION, &bMemoryErrorOccurred);
3479
    if (bMemoryErrorOccurred)
3480
    {
3481
        return dummy;
3482
    }
3483
    if (pData == nullptr)
3484
    {
3485
        auto pAntiRecursion = new GDALAntiRecursionStruct();
3486
        CPLSetTLSWithFreeFuncEx(CTLS_GDALOPEN_ANTIRECURSION, pAntiRecursion,
3487
                                FreeAntiRecursionOpen, &bMemoryErrorOccurred);
3488
        if (bMemoryErrorOccurred)
3489
        {
3490
            delete pAntiRecursion;
3491
            return dummy;
3492
        }
3493
        return *pAntiRecursion;
3494
    }
3495
    return *static_cast<GDALAntiRecursionStruct *>(pData);
3496
}
3497
#else
3498
static thread_local GDALAntiRecursionStruct g_tls_antiRecursion;
3499
3500
static GDALAntiRecursionStruct &GetAntiRecursionOpen()
3501
0
{
3502
0
    return g_tls_antiRecursion;
3503
0
}
3504
#endif
3505
3506
//! @cond Doxygen_Suppress
3507
GDALAntiRecursionGuard::GDALAntiRecursionGuard(const std::string &osIdentifier)
3508
0
    : m_psAntiRecursionStruct(&GetAntiRecursionOpen()),
3509
0
      m_osIdentifier(osIdentifier),
3510
0
      m_nDepth(++m_psAntiRecursionStruct->m_oMapDepth[m_osIdentifier])
3511
0
{
3512
0
    CPLAssert(!osIdentifier.empty());
3513
0
}
3514
3515
GDALAntiRecursionGuard::GDALAntiRecursionGuard(
3516
    const GDALAntiRecursionGuard &other, const std::string &osIdentifier)
3517
0
    : m_psAntiRecursionStruct(other.m_psAntiRecursionStruct),
3518
0
      m_osIdentifier(osIdentifier.empty()
3519
0
                         ? osIdentifier
3520
0
                         : other.m_osIdentifier + osIdentifier),
3521
0
      m_nDepth(m_osIdentifier.empty()
3522
0
                   ? 0
3523
0
                   : ++m_psAntiRecursionStruct->m_oMapDepth[m_osIdentifier])
3524
0
{
3525
0
}
3526
3527
GDALAntiRecursionGuard::~GDALAntiRecursionGuard()
3528
0
{
3529
0
    if (!m_osIdentifier.empty())
3530
0
    {
3531
0
        auto oIter = m_psAntiRecursionStruct->m_oMapDepth.find(m_osIdentifier);
3532
0
        CPLAssert(oIter != m_psAntiRecursionStruct->m_oMapDepth.end());
3533
0
        if (--(oIter->second) == 0)
3534
0
            m_psAntiRecursionStruct->m_oMapDepth.erase(oIter);
3535
0
    }
3536
0
}
3537
3538
//! @endcond
3539
3540
/************************************************************************/
3541
/*                            GetFileList()                             */
3542
/************************************************************************/
3543
3544
/**
3545
 * \brief Fetch files forming dataset.
3546
 *
3547
 * Returns a list of files believed to be part of this dataset.  If it returns
3548
 * an empty list of files it means there is believed to be no local file
3549
 * system files associated with the dataset (for instance a virtual dataset).
3550
 * The returned file list is owned by the caller and should be deallocated
3551
 * with CSLDestroy().
3552
 *
3553
 * The returned filenames will normally be relative or absolute paths
3554
 * depending on the path used to originally open the dataset.  The strings
3555
 * will be UTF-8 encoded.
3556
 *
3557
 * This method is the same as the C GDALGetFileList() function.
3558
 *
3559
 * @return NULL or a NULL terminated array of file names.
3560
 */
3561
3562
char **GDALDataset::GetFileList()
3563
3564
0
{
3565
0
    CPLString osMainFilename = GetDescription();
3566
0
    VSIStatBufL sStat;
3567
3568
0
    GDALAntiRecursionStruct &sAntiRecursion = GetAntiRecursionOpen();
3569
0
    GDALAntiRecursionStruct::DatasetContext datasetCtxt(osMainFilename, 0,
3570
0
                                                        std::string());
3571
0
    auto &aosDatasetList = sAntiRecursion.aosDatasetNamesWithFlags;
3572
0
    if (cpl::contains(aosDatasetList, datasetCtxt))
3573
0
        return nullptr;
3574
3575
    /* -------------------------------------------------------------------- */
3576
    /*      Is the main filename even a real filesystem object?             */
3577
    /* -------------------------------------------------------------------- */
3578
0
    int bMainFileReal =
3579
0
        VSIStatExL(osMainFilename, &sStat, VSI_STAT_EXISTS_FLAG) == 0;
3580
3581
    /* -------------------------------------------------------------------- */
3582
    /*      Form new list.                                                  */
3583
    /* -------------------------------------------------------------------- */
3584
0
    char **papszList = nullptr;
3585
3586
0
    if (bMainFileReal)
3587
0
        papszList = CSLAddString(papszList, osMainFilename);
3588
3589
0
    if (sAntiRecursion.nRecLevel == 100)
3590
0
    {
3591
0
        CPLError(CE_Failure, CPLE_AppDefined,
3592
0
                 "GetFileList() called with too many recursion levels");
3593
0
        return papszList;
3594
0
    }
3595
0
    ++sAntiRecursion.nRecLevel;
3596
3597
    /* -------------------------------------------------------------------- */
3598
    /*      Do we have a known overview file?                               */
3599
    /* -------------------------------------------------------------------- */
3600
0
    if (oOvManager.IsInitialized() && oOvManager.poODS != nullptr)
3601
0
    {
3602
0
        auto iter = aosDatasetList.insert(datasetCtxt).first;
3603
0
        char **papszOvrList = oOvManager.poODS->GetFileList();
3604
0
        papszList = CSLInsertStrings(papszList, -1, papszOvrList);
3605
0
        CSLDestroy(papszOvrList);
3606
0
        aosDatasetList.erase(iter);
3607
0
    }
3608
3609
    /* -------------------------------------------------------------------- */
3610
    /*      Do we have a known mask file?                                   */
3611
    /* -------------------------------------------------------------------- */
3612
0
    if (oOvManager.HaveMaskFile())
3613
0
    {
3614
0
        auto iter = aosDatasetList.insert(std::move(datasetCtxt)).first;
3615
0
        for (const char *pszFile :
3616
0
             CPLStringList(oOvManager.poMaskDS->GetFileList()))
3617
0
        {
3618
0
            if (CSLFindString(papszList, pszFile) < 0)
3619
0
                papszList = CSLAddString(papszList, pszFile);
3620
0
        }
3621
0
        aosDatasetList.erase(iter);
3622
0
    }
3623
3624
0
    --sAntiRecursion.nRecLevel;
3625
3626
0
    return papszList;
3627
0
}
3628
3629
/************************************************************************/
3630
/*                          GDALGetFileList()                           */
3631
/************************************************************************/
3632
3633
/**
3634
 * \brief Fetch files forming dataset.
3635
 *
3636
 * @see GDALDataset::GetFileList()
3637
 */
3638
3639
char **CPL_STDCALL GDALGetFileList(GDALDatasetH hDS)
3640
3641
0
{
3642
0
    VALIDATE_POINTER1(hDS, "GDALGetFileList", nullptr);
3643
3644
0
    return GDALDataset::FromHandle(hDS)->GetFileList();
3645
0
}
3646
3647
/************************************************************************/
3648
/*                           CreateMaskBand()                           */
3649
/************************************************************************/
3650
3651
/**
3652
 * \brief Adds a mask band to the dataset
3653
 *
3654
 * The default implementation of the CreateMaskBand() method is implemented
3655
 * based on similar rules to the .ovr handling implemented using the
3656
 * GDALDefaultOverviews object. A TIFF file with the extension .msk will
3657
 * be created with the same basename as the original file, and it will have
3658
 * one band.
3659
 * The mask images will be deflate compressed tiled images with the same
3660
 * block size as the original image if possible.
3661
 * It will have INTERNAL_MASK_FLAGS_xx metadata items set at the dataset
3662
 * level, where xx matches the band number of a band of the main dataset. The
3663
 * value of those items will be the one of the nFlagsIn parameter.
3664
 *
3665
 * Note that if you got a mask band with a previous call to GetMaskBand(), it
3666
 * might be invalidated by CreateMaskBand(). So you have to call GetMaskBand()
3667
 * again.
3668
 *
3669
 *
3670
 * @param nFlagsIn 0 or combination of GMF_PER_DATASET / GMF_ALPHA.
3671
 *                 GMF_PER_DATASET will be always set, even if not explicitly
3672
 *                 specified.
3673
 * @return CE_None on success or CE_Failure on an error.
3674
 *
3675
 * @see https://gdal.org/development/rfc/rfc15_nodatabitmask.html
3676
 * @see GDALRasterBand::CreateMaskBand()
3677
 *
3678
 */
3679
CPLErr GDALDataset::CreateMaskBand(int nFlagsIn)
3680
3681
0
{
3682
0
    if (oOvManager.IsInitialized())
3683
0
    {
3684
0
        CPLErr eErr = oOvManager.CreateMaskBand(nFlagsIn, -1);
3685
0
        if (eErr != CE_None)
3686
0
            return eErr;
3687
3688
        // Invalidate existing raster band masks.
3689
0
        for (int i = 0; i < nBands; ++i)
3690
0
        {
3691
0
            GDALRasterBand *poBand = papoBands[i];
3692
0
            poBand->poMask.reset();
3693
0
        }
3694
3695
0
        return CE_None;
3696
0
    }
3697
3698
0
    ReportError(CE_Failure, CPLE_NotSupported,
3699
0
                "CreateMaskBand() not supported for this dataset.");
3700
3701
0
    return CE_Failure;
3702
0
}
3703
3704
/************************************************************************/
3705
/*                     GDALCreateDatasetMaskBand()                      */
3706
/************************************************************************/
3707
3708
/**
3709
 * \brief Adds a mask band to the dataset
3710
 * @see GDALDataset::CreateMaskBand()
3711
 */
3712
CPLErr CPL_STDCALL GDALCreateDatasetMaskBand(GDALDatasetH hDS, int nFlags)
3713
3714
0
{
3715
0
    VALIDATE_POINTER1(hDS, "GDALCreateDatasetMaskBand", CE_Failure);
3716
3717
0
    return GDALDataset::FromHandle(hDS)->CreateMaskBand(nFlags);
3718
0
}
3719
3720
/************************************************************************/
3721
/*                              GDALOpen()                              */
3722
/************************************************************************/
3723
3724
/**
3725
 * \brief Open a raster file as a GDALDataset.
3726
 *
3727
 * This function will try to open the passed file, or virtual dataset
3728
 * name by invoking the Open method of each registered GDALDriver in turn.
3729
 * The first successful open will result in a returned dataset.  If all
3730
 * drivers fail then NULL is returned and an error is issued.
3731
 *
3732
 * Several recommendations :
3733
 * <ul>
3734
 * <li>If you open a dataset object with GA_Update access, it is not recommended
3735
 * to open a new dataset on the same underlying file.</li>
3736
 * <li>The returned dataset should only be accessed by one thread at a time. If
3737
 * you want to use it from different threads, you must add all necessary code
3738
 * (mutexes, etc.)  to avoid concurrent use of the object. (Some drivers, such
3739
 * as GeoTIFF, maintain internal state variables that are updated each time a
3740
 * new block is read, thus preventing concurrent use.) </li>
3741
 * </ul>
3742
 *
3743
 * For drivers supporting the VSI virtual file API, it is possible to open a
3744
 * file in a .zip archive (see VSIInstallZipFileHandler()), in a
3745
 * .tar/.tar.gz/.tgz archive (see VSIInstallTarFileHandler()) or on a HTTP / FTP
3746
 * server (see VSIInstallCurlFileHandler())
3747
 *
3748
 * \sa GDALOpenShared()
3749
 * \sa GDALOpenEx()
3750
 *
3751
 * @param pszFilename the name of the file to access.  In the case of
3752
 * exotic drivers this may not refer to a physical file, but instead contain
3753
 * information for the driver on how to access a dataset.  It should be in UTF-8
3754
 * encoding.
3755
 *
3756
 * @param eAccess the desired access, either GA_Update or GA_ReadOnly.  Many
3757
 * drivers support only read only access.
3758
 *
3759
 * @return A GDALDatasetH handle or NULL on failure.  For C++ applications
3760
 * this handle can be cast to a GDALDataset *.
3761
 */
3762
3763
GDALDatasetH CPL_STDCALL GDALOpen(const char *pszFilename, GDALAccess eAccess)
3764
3765
0
{
3766
0
    const int nUpdateFlag = eAccess == GA_Update ? GDAL_OF_UPDATE : 0;
3767
0
    const int nOpenFlags = GDAL_OF_RASTER | nUpdateFlag | GDAL_OF_VERBOSE_ERROR;
3768
0
    GDALDatasetH hDataset =
3769
0
        GDALOpenEx(pszFilename, nOpenFlags, nullptr, nullptr, nullptr);
3770
0
    return hDataset;
3771
0
}
3772
3773
/************************************************************************/
3774
/*                             GetSharedDS()                            */
3775
/************************************************************************/
3776
3777
static GDALDataset *GetSharedDS(const char *pszFilename,
3778
                                unsigned int nOpenFlags,
3779
                                const char *const *papszOpenOptions)
3780
0
{
3781
0
    CPLMutexHolderD(&hDLMutex);
3782
3783
0
    if (phSharedDatasetSet != nullptr)
3784
0
    {
3785
0
        const GIntBig nThisPID = GDALGetResponsiblePIDForCurrentThread();
3786
0
        SharedDatasetCtxt sStruct;
3787
3788
0
        sStruct.nPID = nThisPID;
3789
0
        sStruct.pszDescription = const_cast<char *>(pszFilename);
3790
0
        sStruct.nOpenFlags = nOpenFlags & ~GDAL_OF_SHARED;
3791
0
        std::string osConcatenatedOpenOptions =
3792
0
            GDALSharedDatasetConcatenateOpenOptions(papszOpenOptions);
3793
0
        sStruct.pszConcatenatedOpenOptions = &osConcatenatedOpenOptions[0];
3794
0
        sStruct.poDS = nullptr;
3795
0
        SharedDatasetCtxt *psStruct = static_cast<SharedDatasetCtxt *>(
3796
0
            CPLHashSetLookup(phSharedDatasetSet, &sStruct));
3797
0
        if (psStruct == nullptr && (nOpenFlags & GDAL_OF_UPDATE) == 0)
3798
0
        {
3799
0
            sStruct.nOpenFlags |= GDAL_OF_UPDATE;
3800
0
            psStruct = static_cast<SharedDatasetCtxt *>(
3801
0
                CPLHashSetLookup(phSharedDatasetSet, &sStruct));
3802
0
        }
3803
0
        if (psStruct)
3804
0
        {
3805
0
            return psStruct->poDS;
3806
0
        }
3807
0
    }
3808
0
    return nullptr;
3809
0
}
3810
3811
/************************************************************************/
3812
/*                             GDALOpenEx()                             */
3813
/************************************************************************/
3814
3815
/**
3816
 * \brief Open a raster or vector file as a GDALDataset.
3817
 *
3818
 * This function will try to open the passed file, or virtual dataset
3819
 * name by invoking the Open method of each registered GDALDriver in turn.
3820
 * The first successful open will result in a returned dataset.  If all
3821
 * drivers fail then NULL is returned and an error is issued.
3822
 *
3823
 * Several recommendations :
3824
 * <ul>
3825
 * <li>If you open a dataset object with GDAL_OF_UPDATE access, it is not
3826
 * recommended to open a new dataset on the same underlying file.</li>
3827
 * <li>The returned dataset should only be accessed by one thread at a time. If
3828
 * you want to use it from different threads, you must add all necessary code
3829
 * (mutexes, etc.)  to avoid concurrent use of the object. (Some drivers, such
3830
 * as GeoTIFF, maintain internal state variables that are updated each time a
3831
 * new block is read, thus preventing concurrent use.) </li>
3832
 * </ul>
3833
 *
3834
 * For drivers supporting the VSI virtual file API, it is possible to open a
3835
 * file in a .zip archive (see VSIInstallZipFileHandler()), in a
3836
 * .tar/.tar.gz/.tgz archive (see VSIInstallTarFileHandler()) or on a HTTP / FTP
3837
 * server (see VSIInstallCurlFileHandler())
3838
 *
3839
 * In order to reduce the need for searches through the operating system
3840
 * file system machinery, it is possible to give an optional list of files with
3841
 * the papszSiblingFiles parameter.
3842
 * This is the list of all files at the same level in the file system as the
3843
 * target file, including the target file. The filenames must not include any
3844
 * path components, are essentially just the output of VSIReadDir() on the
3845
 * parent directory. If the target object does not have filesystem semantics
3846
 * then the file list should be NULL.
3847
 *
3848
 * @param pszFilename the name of the file to access.  In the case of
3849
 * exotic drivers this may not refer to a physical file, but instead contain
3850
 * information for the driver on how to access a dataset.  It should be in UTF-8
3851
 * encoding.
3852
 *
3853
 * @param nOpenFlags a combination of GDAL_OF_ flags that may be combined
3854
 * through logical or operator.
3855
 * <ul>
3856
 * <li>Driver kind:
3857
 *   <ul>
3858
 *     <li>GDAL_OF_RASTER for raster drivers,</li>
3859
 *     <li>GDAL_OF_MULTIDIM_RASTER for multidimensional raster drivers,</li>
3860
 *     <li>GDAL_OF_VECTOR for vector drivers,</li>
3861
 *     <li>GDAL_OF_GNM for Geographic Network Model drivers.</li>
3862
 *    </ul>
3863
 * GDAL_OF_RASTER and GDAL_OF_MULTIDIM_RASTER are generally mutually
3864
 * exclusive. If none of the value is specified, GDAL_OF_RASTER | GDAL_OF_VECTOR
3865
 * | GDAL_OF_GNM is implied.
3866
 * </li>
3867
 * <li>Access mode: GDAL_OF_READONLY (exclusive)or GDAL_OF_UPDATE.
3868
 * </li>
3869
 * <li>Shared mode: GDAL_OF_SHARED. If set,
3870
 * it allows the sharing of GDALDataset handles for a dataset with other callers
3871
 * that have set GDAL_OF_SHARED. In particular, GDALOpenEx() will first consult
3872
 * its list of currently open and shared GDALDataset's, and if the
3873
 * GetDescription() name for one exactly matches the pszFilename passed to
3874
 * GDALOpenEx() it will be referenced and returned, if GDALOpenEx() is called
3875
 * from the same thread.
3876
 * </li>
3877
 * <li>Thread safe mode: GDAL_OF_THREAD_SAFE (added in 3.10).
3878
 * This must be use in combination with GDAL_OF_RASTER, and is mutually
3879
 * exclusive with GDAL_OF_UPDATE, GDAL_OF_VECTOR, GDAL_OF_MULTIDIM_RASTER or
3880
 * GDAL_OF_GNM.
3881
 * </li>
3882
 * <li>Verbose error: GDAL_OF_VERBOSE_ERROR. If set,
3883
 * a failed attempt to open the file will lead to an error message to be
3884
 * reported.
3885
 * </li>
3886
 * </ul>
3887
 *
3888
 * @param papszAllowedDrivers NULL to consider all candidate drivers, or a NULL
3889
 * terminated list of strings with the driver short names that must be
3890
 * considered.
3891
 *
3892
 * @param papszOpenOptions NULL, or a NULL terminated list of strings with open
3893
 * options passed to candidate drivers. An option exists for all drivers,
3894
 * OVERVIEW_LEVEL=level, to select a particular overview level of a dataset.
3895
 * The level index starts at 0. The level number can be suffixed by "only" to
3896
 * specify that only this overview level must be visible, and not sub-levels.
3897
 * Open options are validated by default, and a warning is emitted in case the
3898
 * option is not recognized. In some scenarios, it might be not desirable (e.g.
3899
 * when not knowing which driver will open the file), so the special open option
3900
 * VALIDATE_OPEN_OPTIONS can be set to NO to avoid such warnings. Alternatively,
3901
 * that it may not cause a warning if the driver doesn't declare this option.
3902
 * Starting with GDAL 3.3, OVERVIEW_LEVEL=NONE is supported to indicate that
3903
 * no overviews should be exposed.
3904
 *
3905
 * @param papszSiblingFiles NULL, or a NULL terminated list of strings that are
3906
 * filenames that are auxiliary to the main filename. If NULL is passed, a
3907
 * probing of the file system will be done.
3908
 *
3909
 * @return A GDALDatasetH handle or NULL on failure.  For C++ applications
3910
 * this handle can be cast to a GDALDataset *.
3911
 *
3912
 */
3913
3914
GDALDatasetH CPL_STDCALL GDALOpenEx(const char *pszFilename,
3915
                                    unsigned int nOpenFlags,
3916
                                    const char *const *papszAllowedDrivers,
3917
                                    const char *const *papszOpenOptions,
3918
                                    const char *const *papszSiblingFiles)
3919
0
{
3920
0
    VALIDATE_POINTER1(pszFilename, "GDALOpen", nullptr);
3921
3922
    // Hack for the ZARR driver. We translate the CACHE_KERCHUNK_JSON
3923
    // into VSIKERCHUNK_USE_CACHE config option
3924
0
    std::unique_ptr<CPLConfigOptionSetter> poVSIKERCHUNK_USE_CACHESetter;
3925
0
    if (CPLFetchBool(papszOpenOptions, "CACHE_KERCHUNK_JSON", false))
3926
0
    {
3927
0
        poVSIKERCHUNK_USE_CACHESetter = std::make_unique<CPLConfigOptionSetter>(
3928
0
            "VSIKERCHUNK_USE_CACHE", "YES", false);
3929
0
    }
3930
3931
    // Do some sanity checks on incompatible flags with thread-safe mode.
3932
0
    if ((nOpenFlags & GDAL_OF_THREAD_SAFE) != 0)
3933
0
    {
3934
0
        const struct
3935
0
        {
3936
0
            int nFlag;
3937
0
            const char *pszFlagName;
3938
0
        } asFlags[] = {
3939
0
            {GDAL_OF_UPDATE, "GDAL_OF_UPDATE"},
3940
0
            {GDAL_OF_VECTOR, "GDAL_OF_VECTOR"},
3941
0
            {GDAL_OF_MULTIDIM_RASTER, "GDAL_OF_MULTIDIM_RASTER"},
3942
0
            {GDAL_OF_GNM, "GDAL_OF_GNM"},
3943
0
        };
3944
3945
0
        for (const auto &asFlag : asFlags)
3946
0
        {
3947
0
            if ((nOpenFlags & asFlag.nFlag) != 0)
3948
0
            {
3949
0
                CPLError(CE_Failure, CPLE_IllegalArg,
3950
0
                         "GDAL_OF_THREAD_SAFE and %s are mutually "
3951
0
                         "exclusive",
3952
0
                         asFlag.pszFlagName);
3953
0
                return nullptr;
3954
0
            }
3955
0
        }
3956
0
    }
3957
3958
    // If no driver kind is specified, assume all are to be probed.
3959
0
    if ((nOpenFlags & GDAL_OF_KIND_MASK) == 0)
3960
0
        nOpenFlags |= GDAL_OF_KIND_MASK & ~GDAL_OF_MULTIDIM_RASTER;
3961
3962
    /* -------------------------------------------------------------------- */
3963
    /*      In case of shared dataset, first scan the existing list to see  */
3964
    /*      if it could already contain the requested dataset.              */
3965
    /* -------------------------------------------------------------------- */
3966
0
    if (nOpenFlags & GDAL_OF_SHARED)
3967
0
    {
3968
0
        if (nOpenFlags & GDAL_OF_INTERNAL)
3969
0
        {
3970
0
            CPLError(CE_Failure, CPLE_IllegalArg,
3971
0
                     "GDAL_OF_SHARED and GDAL_OF_INTERNAL are exclusive");
3972
0
            return nullptr;
3973
0
        }
3974
3975
0
        auto poSharedDS =
3976
0
            GetSharedDS(pszFilename, nOpenFlags, papszOpenOptions);
3977
0
        if (poSharedDS)
3978
0
        {
3979
0
            poSharedDS->Reference();
3980
0
            return poSharedDS;
3981
0
        }
3982
0
    }
3983
3984
0
    GDALDriverManager *poDM = GetGDALDriverManager();
3985
    // CPLLocaleC  oLocaleForcer;
3986
3987
0
    CPLErrorReset();
3988
0
    VSIErrorReset();
3989
0
    CPLAssert(nullptr != poDM);
3990
3991
    // Build GDALOpenInfo just now to avoid useless file stat'ing if a
3992
    // shared dataset was asked before.
3993
0
    GDALOpenInfo oOpenInfo(pszFilename, nOpenFlags,
3994
0
                           const_cast<char **>(papszSiblingFiles));
3995
0
    oOpenInfo.papszAllowedDrivers = papszAllowedDrivers;
3996
3997
0
    GDALAntiRecursionStruct &sAntiRecursion = GetAntiRecursionOpen();
3998
0
    if (sAntiRecursion.nRecLevel == 100)
3999
0
    {
4000
0
        CPLError(CE_Failure, CPLE_AppDefined,
4001
0
                 "GDALOpen() called with too many recursion levels");
4002
0
        return nullptr;
4003
0
    }
4004
4005
0
    std::string osAllowedDrivers;
4006
0
    for (const char *pszDriverName : cpl::Iterate(papszAllowedDrivers))
4007
0
        osAllowedDrivers += pszDriverName;
4008
0
    auto dsCtxt = GDALAntiRecursionStruct::DatasetContext(
4009
0
        std::string(pszFilename), nOpenFlags, osAllowedDrivers);
4010
0
    if (cpl::contains(sAntiRecursion.aosDatasetNamesWithFlags, dsCtxt))
4011
0
    {
4012
0
        CPLError(CE_Failure, CPLE_AppDefined,
4013
0
                 "GDALOpen() called on %s recursively", pszFilename);
4014
0
        return nullptr;
4015
0
    }
4016
4017
    // Remove leading @ if present.
4018
0
    char **papszOpenOptionsCleaned =
4019
0
        CSLDuplicate(const_cast<char **>(papszOpenOptions));
4020
0
    for (char **papszIter = papszOpenOptionsCleaned; papszIter && *papszIter;
4021
0
         ++papszIter)
4022
0
    {
4023
0
        char *pszOption = *papszIter;
4024
0
        if (pszOption[0] == '@')
4025
0
            memmove(pszOption, pszOption + 1, strlen(pszOption + 1) + 1);
4026
0
    }
4027
4028
0
    oOpenInfo.papszOpenOptions = papszOpenOptionsCleaned;
4029
0
    oOpenInfo.nOpenFlags |= GDAL_OF_FROM_GDALOPEN;
4030
4031
0
#ifdef OGRAPISPY_ENABLED
4032
0
    const bool bUpdate = (nOpenFlags & GDAL_OF_UPDATE) != 0;
4033
0
    const int iSnapshot =
4034
0
        (nOpenFlags & GDAL_OF_VECTOR) != 0 && (nOpenFlags & GDAL_OF_RASTER) == 0
4035
0
            ? OGRAPISpyOpenTakeSnapshot(pszFilename, bUpdate)
4036
0
            : INT_MIN;
4037
0
#endif
4038
4039
0
    const int nDriverCount = poDM->GetDriverCount(/*bIncludeHidden=*/true);
4040
0
    GDALDriver *poMissingPluginDriver = nullptr;
4041
0
    std::vector<GDALDriver *> apoSecondPassDrivers;
4042
4043
    // Lookup of matching driver for dataset can involve up to 2 passes:
4044
    // - in the first pass, all drivers that are compabile of the request mode
4045
    //   (raster/vector/etc.) are probed using their Identify() method if it
4046
    //   exists. If the Identify() method returns FALSE, the driver is skipped.
4047
    //   If the Identify() methods returns GDAL_IDENTIFY_UNKNOWN and that the
4048
    //   driver is a deferred-loading plugin, it is added to the
4049
    //   apoSecondPassDrivers list for potential later probing, and execution
4050
    //   continues to the next driver in the list.
4051
    //   Otherwise if Identify() returns non-FALSE, the Open() method is used.
4052
    //   If Open() returns a non-NULL dataset, the loop stops and it is
4053
    //   returned. Otherwise looping over remaining drivers continues.
4054
    // - the second pass is optional, only if at least one driver was added
4055
    //   into apoSecondPassDrivers during the first pass. It is similar
4056
    //   to the first pass except it runs only on apoSecondPassDrivers drivers.
4057
    //   And the Open() method of such drivers is used, causing them to be
4058
    //   loaded for real.
4059
0
    int iPass = 1;
4060
0
retry:
4061
0
    for (int iDriver = 0;
4062
0
         iDriver < (iPass == 1 ? nDriverCount
4063
0
                               : static_cast<int>(apoSecondPassDrivers.size()));
4064
0
         ++iDriver)
4065
0
    {
4066
0
        GDALDriver *poDriver =
4067
0
            iPass == 1 ? poDM->GetDriver(iDriver, /*bIncludeHidden=*/true)
4068
0
                       : apoSecondPassDrivers[iDriver];
4069
0
        if (papszAllowedDrivers != nullptr &&
4070
0
            CSLFindString(papszAllowedDrivers,
4071
0
                          GDALGetDriverShortName(poDriver)) == -1)
4072
0
        {
4073
0
            continue;
4074
0
        }
4075
4076
0
        if (poDriver->GetMetadataItem(GDAL_DCAP_OPEN) == nullptr)
4077
0
            continue;
4078
4079
0
        if ((nOpenFlags & GDAL_OF_RASTER) != 0 &&
4080
0
            (nOpenFlags & GDAL_OF_VECTOR) == 0 &&
4081
0
            poDriver->GetMetadataItem(GDAL_DCAP_RASTER) == nullptr)
4082
0
            continue;
4083
0
        if ((nOpenFlags & GDAL_OF_VECTOR) != 0 &&
4084
0
            (nOpenFlags & GDAL_OF_RASTER) == 0 &&
4085
0
            poDriver->GetMetadataItem(GDAL_DCAP_VECTOR) == nullptr)
4086
0
            continue;
4087
0
        if ((nOpenFlags & GDAL_OF_MULTIDIM_RASTER) != 0 &&
4088
0
            (nOpenFlags & GDAL_OF_RASTER) == 0 &&
4089
0
            poDriver->GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER) == nullptr)
4090
0
            continue;
4091
4092
        // Remove general OVERVIEW_LEVEL open options from list before passing
4093
        // it to the driver, if it isn't a driver specific option already.
4094
0
        char **papszTmpOpenOptions = nullptr;
4095
0
        char **papszTmpOpenOptionsToValidate = nullptr;
4096
0
        char **papszOptionsToValidate = const_cast<char **>(papszOpenOptions);
4097
0
        if (CSLFetchNameValue(papszOpenOptionsCleaned, "OVERVIEW_LEVEL") !=
4098
0
                nullptr &&
4099
0
            !poDriver->HasOpenOption("OVERVIEW_LEVEL"))
4100
0
        {
4101
0
            papszTmpOpenOptions = CSLDuplicate(papszOpenOptionsCleaned);
4102
0
            papszTmpOpenOptions =
4103
0
                CSLSetNameValue(papszTmpOpenOptions, "OVERVIEW_LEVEL", nullptr);
4104
0
            oOpenInfo.papszOpenOptions = papszTmpOpenOptions;
4105
4106
0
            papszOptionsToValidate = CSLDuplicate(papszOptionsToValidate);
4107
0
            papszOptionsToValidate = CSLSetNameValue(papszOptionsToValidate,
4108
0
                                                     "OVERVIEW_LEVEL", nullptr);
4109
0
            papszTmpOpenOptionsToValidate = papszOptionsToValidate;
4110
0
        }
4111
4112
0
        const int nIdentifyRes =
4113
0
            poDriver->pfnIdentifyEx
4114
0
                ? poDriver->pfnIdentifyEx(poDriver, &oOpenInfo)
4115
0
            : poDriver->pfnIdentify ? poDriver->pfnIdentify(&oOpenInfo)
4116
0
                                    : GDAL_IDENTIFY_UNKNOWN;
4117
0
        if (nIdentifyRes == FALSE)
4118
0
        {
4119
0
            CSLDestroy(papszTmpOpenOptions);
4120
0
            CSLDestroy(papszTmpOpenOptionsToValidate);
4121
0
            oOpenInfo.papszOpenOptions = papszOpenOptionsCleaned;
4122
0
            continue;
4123
0
        }
4124
0
        else if (iPass == 1 && nIdentifyRes < 0 &&
4125
0
                 poDriver->pfnOpen == nullptr &&
4126
0
                 poDriver->GetMetadataItem("IS_NON_LOADED_PLUGIN"))
4127
0
        {
4128
            // Not loaded plugin
4129
0
            apoSecondPassDrivers.push_back(poDriver);
4130
0
            CSLDestroy(papszTmpOpenOptions);
4131
0
            CSLDestroy(papszTmpOpenOptionsToValidate);
4132
0
            oOpenInfo.papszOpenOptions = papszOpenOptionsCleaned;
4133
0
            continue;
4134
0
        }
4135
4136
0
        const bool bIdentifyRes = nIdentifyRes == GDAL_IDENTIFY_TRUE;
4137
0
        if (bIdentifyRes)
4138
0
        {
4139
0
            GDALValidateOpenOptions(poDriver, papszOptionsToValidate);
4140
0
        }
4141
4142
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
4143
0
        const bool bFpAvailableBefore = oOpenInfo.fpL != nullptr;
4144
0
        CPLErrorReset();
4145
0
#endif
4146
4147
0
        sAntiRecursion.nRecLevel++;
4148
0
        sAntiRecursion.aosDatasetNamesWithFlags.insert(dsCtxt);
4149
4150
0
        GDALDataset *poDS = poDriver->Open(&oOpenInfo, false);
4151
4152
0
        sAntiRecursion.nRecLevel--;
4153
0
        sAntiRecursion.aosDatasetNamesWithFlags.erase(dsCtxt);
4154
4155
0
        if (poDriver->pfnOpen != nullptr)
4156
0
        {
4157
            // If we couldn't determine for sure with Identify() (it returned
4158
            // -1), but Open() managed to open the file, post validate options.
4159
0
            if (poDS != nullptr &&
4160
0
                (poDriver->pfnIdentify || poDriver->pfnIdentifyEx) &&
4161
0
                !bIdentifyRes)
4162
0
            {
4163
0
                GDALValidateOpenOptions(poDriver, papszOptionsToValidate);
4164
0
            }
4165
0
        }
4166
0
        else if (poDriver->pfnOpenWithDriverArg != nullptr)
4167
0
        {
4168
            // do nothing
4169
0
        }
4170
0
        else if (bIdentifyRes &&
4171
0
                 poDriver->GetMetadataItem("MISSING_PLUGIN_FILENAME"))
4172
0
        {
4173
0
            if (!poMissingPluginDriver)
4174
0
            {
4175
0
                poMissingPluginDriver = poDriver;
4176
0
            }
4177
0
        }
4178
0
        else
4179
0
        {
4180
            // should not happen given the GDAL_DCAP_OPEN check
4181
0
            CSLDestroy(papszTmpOpenOptions);
4182
0
            CSLDestroy(papszTmpOpenOptionsToValidate);
4183
0
            oOpenInfo.papszOpenOptions = papszOpenOptionsCleaned;
4184
0
            continue;
4185
0
        }
4186
4187
0
        CSLDestroy(papszTmpOpenOptions);
4188
0
        CSLDestroy(papszTmpOpenOptionsToValidate);
4189
0
        oOpenInfo.papszOpenOptions = papszOpenOptionsCleaned;
4190
4191
0
        if (poDS != nullptr)
4192
0
        {
4193
0
            if (poDS->papszOpenOptions == nullptr)
4194
0
            {
4195
0
                poDS->papszOpenOptions = papszOpenOptionsCleaned;
4196
0
                papszOpenOptionsCleaned = nullptr;
4197
0
            }
4198
4199
            // Deal with generic OVERVIEW_LEVEL open option, unless it is
4200
            // driver specific.
4201
0
            if (CSLFetchNameValue(papszOpenOptions, "OVERVIEW_LEVEL") !=
4202
0
                    nullptr &&
4203
0
                !poDriver->HasOpenOption("OVERVIEW_LEVEL"))
4204
0
            {
4205
0
                CPLString osVal(
4206
0
                    CSLFetchNameValue(papszOpenOptions, "OVERVIEW_LEVEL"));
4207
0
                const int nOvrLevel = EQUAL(osVal, "NONE") ? -1 : atoi(osVal);
4208
0
                const bool bThisLevelOnly =
4209
0
                    nOvrLevel == -1 || osVal.ifind("only") != std::string::npos;
4210
0
                GDALDataset *poOvrDS =
4211
0
                    GDALCreateOverviewDataset(poDS, nOvrLevel, bThisLevelOnly);
4212
0
                if (poOvrDS && (nOpenFlags & GDAL_OF_SHARED) != 0)
4213
0
                {
4214
0
                    if (strcmp(pszFilename, poDS->GetDescription()) != 0)
4215
0
                    {
4216
0
                        CPLError(
4217
0
                            CE_Warning, CPLE_NotSupported,
4218
0
                            "A dataset opened by GDALOpenShared should have "
4219
0
                            "the same filename (%s) "
4220
0
                            "and description (%s)",
4221
0
                            pszFilename, poDS->GetDescription());
4222
0
                    }
4223
0
                    else
4224
0
                    {
4225
0
                        CSLDestroy(poDS->papszOpenOptions);
4226
0
                        poDS->papszOpenOptions = CSLDuplicate(papszOpenOptions);
4227
0
                        poDS->papszOpenOptions = CSLSetNameValue(
4228
0
                            poDS->papszOpenOptions, "OVERVIEW_LEVEL", nullptr);
4229
0
                    }
4230
0
                }
4231
0
                poDS->ReleaseRef();
4232
0
                poDS = poOvrDS;
4233
0
                if (poDS == nullptr)
4234
0
                {
4235
0
                    if (nOpenFlags & GDAL_OF_VERBOSE_ERROR)
4236
0
                    {
4237
0
                        CPLError(CE_Failure, CPLE_OpenFailed,
4238
0
                                 "Cannot open overview level %d of %s",
4239
0
                                 nOvrLevel, pszFilename);
4240
0
                    }
4241
0
                }
4242
0
                else
4243
0
                {
4244
                    // For thread-safe opening, currently poDS is what will be
4245
                    // the "master" dataset owned by the thread-safe dataset
4246
                    // returned to the user, hence we do not register it as a
4247
                    // visible one in the open dataset list, or mark it as shared.
4248
0
                    if (!(nOpenFlags & GDAL_OF_INTERNAL) &&
4249
0
                        !(nOpenFlags & GDAL_OF_THREAD_SAFE))
4250
0
                    {
4251
0
                        poDS->AddToDatasetOpenList();
4252
0
                    }
4253
0
                    if (nOpenFlags & GDAL_OF_SHARED)
4254
0
                    {
4255
0
                        CSLDestroy(poDS->papszOpenOptions);
4256
0
                        poDS->papszOpenOptions = CSLDuplicate(papszOpenOptions);
4257
0
                        poDS->nOpenFlags = nOpenFlags;
4258
0
                        if (!(nOpenFlags & GDAL_OF_THREAD_SAFE))
4259
0
                            poDS->MarkAsShared();
4260
0
                    }
4261
0
                }
4262
0
            }
4263
0
            else if (nOpenFlags & GDAL_OF_SHARED)
4264
0
            {
4265
0
                if (strcmp(pszFilename, poDS->GetDescription()) != 0)
4266
0
                {
4267
0
                    CPLError(CE_Warning, CPLE_NotSupported,
4268
0
                             "A dataset opened by GDALOpenShared should have "
4269
0
                             "the same filename (%s) "
4270
0
                             "and description (%s)",
4271
0
                             pszFilename, poDS->GetDescription());
4272
0
                }
4273
0
                else if (!(nOpenFlags & GDAL_OF_THREAD_SAFE))
4274
0
                {
4275
                    // For thread-safe opening, currently poDS is what will be
4276
                    // the "master" dataset owned by the thread-safe dataset
4277
                    // returned to the user, hence we do not or mark it as shared.
4278
0
                    poDS->MarkAsShared();
4279
0
                }
4280
0
            }
4281
4282
0
            VSIErrorReset();
4283
4284
0
            CSLDestroy(papszOpenOptionsCleaned);
4285
4286
0
#ifdef OGRAPISPY_ENABLED
4287
0
            if (iSnapshot != INT_MIN)
4288
0
            {
4289
0
                GDALDatasetH hDS = GDALDataset::ToHandle(poDS);
4290
0
                OGRAPISpyOpen(pszFilename, bUpdate, iSnapshot, &hDS);
4291
0
                poDS = GDALDataset::FromHandle(hDS);
4292
0
            }
4293
0
#endif
4294
4295
0
            if (poDS)
4296
0
            {
4297
0
                poDS->m_bCanBeReopened = true;
4298
4299
0
                if ((nOpenFlags & GDAL_OF_THREAD_SAFE) != 0)
4300
0
                {
4301
0
                    poDS =
4302
0
                        GDALGetThreadSafeDataset(
4303
0
                            std::unique_ptr<GDALDataset>(poDS), GDAL_OF_RASTER)
4304
0
                            .release();
4305
0
                    if (poDS)
4306
0
                    {
4307
0
                        poDS->m_bCanBeReopened = true;
4308
0
                        poDS->poDriver = poDriver;
4309
0
                        poDS->nOpenFlags = nOpenFlags;
4310
0
                        if (!(nOpenFlags & GDAL_OF_INTERNAL))
4311
0
                            poDS->AddToDatasetOpenList();
4312
0
                        if (nOpenFlags & GDAL_OF_SHARED)
4313
0
                            poDS->MarkAsShared();
4314
0
                    }
4315
0
                }
4316
0
            }
4317
4318
0
            return poDS;
4319
0
        }
4320
4321
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
4322
0
        if (bFpAvailableBefore && oOpenInfo.fpL == nullptr)
4323
0
        {
4324
            // In case the file descriptor was "consumed" by a driver
4325
            // that ultimately failed, re-open it for next drivers.
4326
0
            oOpenInfo.fpL = VSIFOpenL(
4327
0
                pszFilename, (oOpenInfo.eAccess == GA_Update) ? "r+b" : "rb");
4328
0
        }
4329
#else
4330
        if (CPLGetLastErrorNo() != 0 && CPLGetLastErrorType() > CE_Warning)
4331
        {
4332
            CSLDestroy(papszOpenOptionsCleaned);
4333
4334
#ifdef OGRAPISPY_ENABLED
4335
            if (iSnapshot != INT_MIN)
4336
            {
4337
                GDALDatasetH hDS = nullptr;
4338
                OGRAPISpyOpen(pszFilename, bUpdate, iSnapshot, &hDS);
4339
            }
4340
#endif
4341
            return nullptr;
4342
        }
4343
#endif
4344
0
    }
4345
4346
    // cppcheck-suppress knownConditionTrueFalse
4347
0
    if (iPass == 1 && !apoSecondPassDrivers.empty())
4348
0
    {
4349
0
        CPLDebugOnly("GDAL", "GDALOpen(): Second pass");
4350
0
        iPass = 2;
4351
0
        goto retry;
4352
0
    }
4353
4354
0
    CSLDestroy(papszOpenOptionsCleaned);
4355
4356
0
#ifdef OGRAPISPY_ENABLED
4357
0
    if (iSnapshot != INT_MIN)
4358
0
    {
4359
0
        GDALDatasetH hDS = nullptr;
4360
0
        OGRAPISpyOpen(pszFilename, bUpdate, iSnapshot, &hDS);
4361
0
    }
4362
0
#endif
4363
4364
0
    if (nOpenFlags & GDAL_OF_VERBOSE_ERROR)
4365
0
    {
4366
0
        if (nDriverCount == 0)
4367
0
        {
4368
0
            CPLError(CE_Failure, CPLE_OpenFailed, "No driver registered.");
4369
0
        }
4370
0
        else if (poMissingPluginDriver)
4371
0
        {
4372
0
            std::string osMsg("`");
4373
0
            osMsg += pszFilename;
4374
0
            osMsg += "' not recognized as being in a supported file format. "
4375
0
                     "It could have been recognized by driver ";
4376
0
            osMsg += poMissingPluginDriver->GetDescription();
4377
0
            osMsg += ", but plugin ";
4378
0
            osMsg +=
4379
0
                GDALGetMessageAboutMissingPluginDriver(poMissingPluginDriver);
4380
4381
0
            CPLError(CE_Failure, CPLE_OpenFailed, "%s", osMsg.c_str());
4382
0
        }
4383
        // Check to see if there was a filesystem error, and report it if so.
4384
        // If not, return a more generic error.
4385
0
        else if (!VSIToCPLError(CE_Failure, CPLE_OpenFailed))
4386
0
        {
4387
0
            if (oOpenInfo.bStatOK)
4388
0
            {
4389
0
                CPLError(CE_Failure, CPLE_OpenFailed,
4390
0
                         "`%s' not recognized as being in a supported file "
4391
0
                         "format.",
4392
0
                         pszFilename);
4393
0
            }
4394
0
            else
4395
0
            {
4396
                // If Stat failed and no VSI error was set, assume it is because
4397
                // the file did not exist on the filesystem.
4398
0
                CPLError(CE_Failure, CPLE_OpenFailed,
4399
0
                         "`%s' does not exist in the file system, "
4400
0
                         "and is not recognized as a supported dataset name.",
4401
0
                         pszFilename);
4402
0
            }
4403
0
        }
4404
0
    }
4405
4406
0
    return nullptr;
4407
0
}
4408
4409
/************************************************************************/
4410
/*                           GDALOpenShared()                           */
4411
/************************************************************************/
4412
4413
/**
4414
 * \brief Open a raster file as a GDALDataset.
4415
 *
4416
 * This function works the same as GDALOpen(), but allows the sharing of
4417
 * GDALDataset handles for a dataset with other callers to GDALOpenShared().
4418
 *
4419
 * In particular, GDALOpenShared() will first consult its list of currently
4420
 * open and shared GDALDataset's, and if the GetDescription() name for one
4421
 * exactly matches the pszFilename passed to GDALOpenShared() it will be
4422
 * referenced and returned.
4423
 *
4424
 * If GDALOpenShared() is called on the same
4425
 * pszFilename from two different threads, a different GDALDataset object will
4426
 * be returned as it is not safe to use the same dataset from different threads,
4427
 * unless the user does explicitly use mutexes in its code.
4428
 *
4429
 * For drivers supporting the VSI virtual file API, it is possible to open a
4430
 * file in a .zip archive (see VSIInstallZipFileHandler()), in a
4431
 * .tar/.tar.gz/.tgz archive (see VSIInstallTarFileHandler()) or on a HTTP / FTP
4432
 * server (see VSIInstallCurlFileHandler())
4433
 *
4434
 * \sa GDALOpen()
4435
 * \sa GDALOpenEx()
4436
 *
4437
 * @param pszFilename the name of the file to access.  In the case of
4438
 * exotic drivers this may not refer to a physical file, but instead contain
4439
 * information for the driver on how to access a dataset.  It should be in
4440
 * UTF-8 encoding.
4441
 *
4442
 * @param eAccess the desired access, either GA_Update or GA_ReadOnly.  Many
4443
 * drivers support only read only access.
4444
 *
4445
 * @return A GDALDatasetH handle or NULL on failure.  For C++ applications
4446
 * this handle can be cast to a GDALDataset *.
4447
 */
4448
4449
GDALDatasetH CPL_STDCALL GDALOpenShared(const char *pszFilename,
4450
                                        GDALAccess eAccess)
4451
0
{
4452
0
    VALIDATE_POINTER1(pszFilename, "GDALOpenShared", nullptr);
4453
0
    return GDALOpenEx(pszFilename,
4454
0
                      GDAL_OF_RASTER |
4455
0
                          (eAccess == GA_Update ? GDAL_OF_UPDATE : 0) |
4456
0
                          GDAL_OF_SHARED | GDAL_OF_VERBOSE_ERROR,
4457
0
                      nullptr, nullptr, nullptr);
4458
0
}
4459
4460
/************************************************************************/
4461
/*                             GDALClose()                              */
4462
/************************************************************************/
4463
4464
/**
4465
 * \brief Close GDAL dataset.
4466
 *
4467
 * For non-shared datasets (opened with GDALOpen()) the dataset is closed
4468
 * using the C++ "delete" operator, recovering all dataset related resources.
4469
 * For shared datasets (opened with GDALOpenShared()) the dataset is
4470
 * dereferenced, and closed only if the referenced count has dropped below 1.
4471
 *
4472
 * @param hDS The dataset to close.  May be cast from a "GDALDataset *".
4473
 * @return CE_None in case of success (return value since GDAL 3.7). On a
4474
 * shared dataset whose reference count is not dropped below 1, CE_None will
4475
 * be returned.
4476
 */
4477
4478
CPLErr CPL_STDCALL GDALClose(GDALDatasetH hDS)
4479
4480
0
{
4481
0
    if (!hDS)
4482
0
        return CE_None;
4483
4484
0
#ifdef OGRAPISPY_ENABLED
4485
0
    if (bOGRAPISpyEnabled)
4486
0
        OGRAPISpyPreClose(hDS);
4487
0
#endif
4488
4489
0
    GDALDataset *poDS = GDALDataset::FromHandle(hDS);
4490
4491
0
    if (poDS->GetShared())
4492
0
    {
4493
        /* --------------------------------------------------------------------
4494
         */
4495
        /*      If this file is in the shared dataset list then dereference */
4496
        /*      it, and only delete/remote it if the reference count has */
4497
        /*      dropped to zero. */
4498
        /* --------------------------------------------------------------------
4499
         */
4500
0
        if (poDS->Dereference() > 0)
4501
0
            return CE_None;
4502
4503
0
        CPLErr eErr = poDS->Close();
4504
0
        delete poDS;
4505
4506
0
#ifdef OGRAPISPY_ENABLED
4507
0
        if (bOGRAPISpyEnabled)
4508
0
            OGRAPISpyPostClose();
4509
0
#endif
4510
4511
0
        return eErr;
4512
0
    }
4513
4514
    /* -------------------------------------------------------------------- */
4515
    /*      This is not shared dataset, so directly delete it.              */
4516
    /* -------------------------------------------------------------------- */
4517
0
    CPLErr eErr = poDS->Close();
4518
0
    delete poDS;
4519
4520
0
#ifdef OGRAPISPY_ENABLED
4521
0
    if (bOGRAPISpyEnabled)
4522
0
        OGRAPISpyPostClose();
4523
0
#endif
4524
0
    return eErr;
4525
0
}
4526
4527
/************************************************************************/
4528
/*                        GDALDumpOpenDataset()                         */
4529
/************************************************************************/
4530
4531
static int GDALDumpOpenSharedDatasetsForeach(void *elt, void *user_data)
4532
0
{
4533
0
    SharedDatasetCtxt *psStruct = static_cast<SharedDatasetCtxt *>(elt);
4534
0
    FILE *fp = static_cast<FILE *>(user_data);
4535
0
    GDALDataset *poDS = psStruct->poDS;
4536
4537
0
    const char *pszDriverName = poDS->GetDriver() == nullptr
4538
0
                                    ? "DriverIsNULL"
4539
0
                                    : poDS->GetDriver()->GetDescription();
4540
4541
0
    poDS->Reference();
4542
0
    CPL_IGNORE_RET_VAL(
4543
0
        VSIFPrintf(fp, "  %d %c %-6s %7d %dx%dx%d %s\n", poDS->Dereference(),
4544
0
                   poDS->GetShared() ? 'S' : 'N', pszDriverName,
4545
0
                   static_cast<int>(psStruct->nPID), poDS->GetRasterXSize(),
4546
0
                   poDS->GetRasterYSize(), poDS->GetRasterCount(),
4547
0
                   poDS->GetDescription()));
4548
4549
0
    return TRUE;
4550
0
}
4551
4552
static int GDALDumpOpenDatasetsForeach(GDALDataset *poDS, FILE *fp)
4553
0
{
4554
4555
    // Don't list shared datasets. They have already been listed by
4556
    // GDALDumpOpenSharedDatasetsForeach.
4557
0
    if (poDS->GetShared())
4558
0
        return TRUE;
4559
4560
0
    const char *pszDriverName = poDS->GetDriver() == nullptr
4561
0
                                    ? "DriverIsNULL"
4562
0
                                    : poDS->GetDriver()->GetDescription();
4563
4564
0
    poDS->Reference();
4565
0
    CPL_IGNORE_RET_VAL(
4566
0
        VSIFPrintf(fp, "  %d %c %-6s %7d %dx%dx%d %s\n", poDS->Dereference(),
4567
0
                   poDS->GetShared() ? 'S' : 'N', pszDriverName, -1,
4568
0
                   poDS->GetRasterXSize(), poDS->GetRasterYSize(),
4569
0
                   poDS->GetRasterCount(), poDS->GetDescription()));
4570
4571
0
    return TRUE;
4572
0
}
4573
4574
/**
4575
 * \brief List open datasets.
4576
 *
4577
 * Dumps a list of all open datasets (shared or not) to the indicated
4578
 * text file (may be stdout or stderr).   This function is primarily intended
4579
 * to assist in debugging "dataset leaks" and reference counting issues.
4580
 * The information reported includes the dataset name, referenced count,
4581
 * shared status, driver name, size, and band count.
4582
 */
4583
4584
int CPL_STDCALL GDALDumpOpenDatasets(FILE *fp)
4585
4586
0
{
4587
0
    VALIDATE_POINTER1(fp, "GDALDumpOpenDatasets", 0);
4588
4589
0
    CPLMutexHolderD(&hDLMutex);
4590
4591
0
    if (poAllDatasetMap == nullptr)
4592
0
        return 0;
4593
4594
0
    CPL_IGNORE_RET_VAL(VSIFPrintf(fp, "Open GDAL Datasets:\n"));
4595
4596
0
    for (const auto &oIter : *poAllDatasetMap)
4597
0
    {
4598
0
        GDALDumpOpenDatasetsForeach(oIter.first, fp);
4599
0
    }
4600
4601
0
    if (phSharedDatasetSet != nullptr)
4602
0
    {
4603
0
        CPLHashSetForeach(phSharedDatasetSet, GDALDumpOpenSharedDatasetsForeach,
4604
0
                          fp);
4605
0
    }
4606
0
    return static_cast<int>(poAllDatasetMap->size());
4607
0
}
4608
4609
/************************************************************************/
4610
/*                        BeginAsyncReader()                            */
4611
/************************************************************************/
4612
4613
/**
4614
 * \brief Sets up an asynchronous data request
4615
 *
4616
 * This method establish an asynchronous raster read request for the
4617
 * indicated window on the dataset into the indicated buffer.  The parameters
4618
 * for windowing, buffer size, buffer type and buffer organization are similar
4619
 * to those for GDALDataset::RasterIO(); however, this call only launches
4620
 * the request and filling the buffer is accomplished via calls to
4621
 * GetNextUpdatedRegion() on the return GDALAsyncReader session object.
4622
 *
4623
 * Once all processing for the created session is complete, or if no further
4624
 * refinement of the request is required, the GDALAsyncReader object should
4625
 * be destroyed with the GDALDataset::EndAsyncReader() method.
4626
 *
4627
 * Note that the data buffer (pData) will potentially continue to be
4628
 * updated as long as the session lives, but it is not deallocated when
4629
 * the session (GDALAsyncReader) is destroyed with EndAsyncReader().  It
4630
 * should be deallocated by the application at that point.
4631
 *
4632
 * Additional information on asynchronous IO in GDAL may be found at:
4633
 *   https://gdal.org/development/rfc/rfc24_progressive_data_support.html
4634
 *
4635
 * This method is the same as the C GDALBeginAsyncReader() function.
4636
 *
4637
 * @param nXOff The pixel offset to the top left corner of the region
4638
 * of the band to be accessed.  This would be zero to start from the left side.
4639
 *
4640
 * @param nYOff The line offset to the top left corner of the region
4641
 * of the band to be accessed.  This would be zero to start from the top.
4642
 *
4643
 * @param nXSize The width of the region of the band to be accessed in pixels.
4644
 *
4645
 * @param nYSize The height of the region of the band to be accessed in lines.
4646
 *
4647
 * @param pBuf The buffer into which the data should be read. This buffer must
4648
 * contain at least nBufXSize * nBufYSize * nBandCount words of type eBufType.
4649
 * It is organized in left to right,top to bottom pixel order.  Spacing is
4650
 * controlled by the nPixelSpace, and nLineSpace parameters.
4651
 *
4652
 * @param nBufXSize the width of the buffer image into which the desired region
4653
 * is to be read, or from which it is to be written.
4654
 *
4655
 * @param nBufYSize the height of the buffer image into which the desired
4656
 * region is to be read, or from which it is to be written.
4657
 *
4658
 * @param eBufType the type of the pixel values in the pData data buffer.  The
4659
 * pixel values will automatically be translated to/from the GDALRasterBand
4660
 * data type as needed.
4661
 *
4662
 * @param nBandCount the number of bands being read or written.
4663
 *
4664
 * @param panBandMap the list of nBandCount band numbers being read/written.
4665
 * Note band numbers are 1 based.   This may be NULL to select the first
4666
 * nBandCount bands.
4667
 *
4668
 * @param nPixelSpace The byte offset from the start of one pixel value in
4669
 * pData to the start of the next pixel value within a scanline.  If defaulted
4670
 * (0) the size of the datatype eBufType is used.
4671
 *
4672
 * @param nLineSpace The byte offset from the start of one scanline in
4673
 * pData to the start of the next.  If defaulted the size of the datatype
4674
 * eBufType * nBufXSize is used.
4675
 *
4676
 * @param nBandSpace the byte offset from the start of one bands data to the
4677
 * start of the next.  If defaulted (zero) the value will be
4678
 * nLineSpace * nBufYSize implying band sequential organization
4679
 * of the data buffer.
4680
 *
4681
 * @param papszOptions Driver specific control options in a string list or NULL.
4682
 * Consult driver documentation for options supported.
4683
 *
4684
 * @return The GDALAsyncReader object representing the request.
4685
 */
4686
4687
GDALAsyncReader *GDALDataset::BeginAsyncReader(
4688
    int nXOff, int nYOff, int nXSize, int nYSize, void *pBuf, int nBufXSize,
4689
    int nBufYSize, GDALDataType eBufType, int nBandCount, int *panBandMap,
4690
    int nPixelSpace, int nLineSpace, int nBandSpace, char **papszOptions)
4691
0
{
4692
    // See gdaldefaultasync.cpp
4693
4694
0
    return GDALGetDefaultAsyncReader(this, nXOff, nYOff, nXSize, nYSize, pBuf,
4695
0
                                     nBufXSize, nBufYSize, eBufType, nBandCount,
4696
0
                                     panBandMap, nPixelSpace, nLineSpace,
4697
0
                                     nBandSpace, papszOptions);
4698
0
}
4699
4700
/************************************************************************/
4701
/*                        GDALBeginAsyncReader()                      */
4702
/************************************************************************/
4703
4704
/**
4705
 * \brief Sets up an asynchronous data request
4706
 *
4707
 * This method establish an asynchronous raster read request for the
4708
 * indicated window on the dataset into the indicated buffer.  The parameters
4709
 * for windowing, buffer size, buffer type and buffer organization are similar
4710
 * to those for GDALDataset::RasterIO(); however, this call only launches
4711
 * the request and filling the buffer is accomplished via calls to
4712
 * GetNextUpdatedRegion() on the return GDALAsyncReader session object.
4713
 *
4714
 * Once all processing for the created session is complete, or if no further
4715
 * refinement of the request is required, the GDALAsyncReader object should
4716
 * be destroyed with the GDALDataset::EndAsyncReader() method.
4717
 *
4718
 * Note that the data buffer (pData) will potentially continue to be
4719
 * updated as long as the session lives, but it is not deallocated when
4720
 * the session (GDALAsyncReader) is destroyed with EndAsyncReader().  It
4721
 * should be deallocated by the application at that point.
4722
 *
4723
 * Additional information on asynchronous IO in GDAL may be found at:
4724
 *   https://gdal.org/development/rfc/rfc24_progressive_data_support.html
4725
 *
4726
 * This method is the same as the C++ GDALDataset::BeginAsyncReader() method.
4727
 *
4728
 * @param hDS handle to the dataset object.
4729
 *
4730
 * @param nXOff The pixel offset to the top left corner of the region
4731
 * of the band to be accessed.  This would be zero to start from the left side.
4732
 *
4733
 * @param nYOff The line offset to the top left corner of the region
4734
 * of the band to be accessed.  This would be zero to start from the top.
4735
 *
4736
 * @param nXSize The width of the region of the band to be accessed in pixels.
4737
 *
4738
 * @param nYSize The height of the region of the band to be accessed in lines.
4739
 *
4740
 * @param pBuf The buffer into which the data should be read. This buffer must
4741
 * contain at least nBufXSize * nBufYSize * nBandCount words of type eBufType.
4742
 * It is organized in left to right,top to bottom pixel order.  Spacing is
4743
 * controlled by the nPixelSpace, and nLineSpace parameters.
4744
 *
4745
 * @param nBufXSize the width of the buffer image into which the desired region
4746
 * is to be read, or from which it is to be written.
4747
 *
4748
 * @param nBufYSize the height of the buffer image into which the desired
4749
 * region is to be read, or from which it is to be written.
4750
 *
4751
 * @param eBufType the type of the pixel values in the pData data buffer.  The
4752
 * pixel values will automatically be translated to/from the GDALRasterBand
4753
 * data type as needed.
4754
 *
4755
 * @param nBandCount the number of bands being read or written.
4756
 *
4757
 * @param panBandMap the list of nBandCount band numbers being read/written.
4758
 * Note band numbers are 1 based.   This may be NULL to select the first
4759
 * nBandCount bands.
4760
 *
4761
 * @param nPixelSpace The byte offset from the start of one pixel value in
4762
 * pData to the start of the next pixel value within a scanline.  If defaulted
4763
 * (0) the size of the datatype eBufType is used.
4764
 *
4765
 * @param nLineSpace The byte offset from the start of one scanline in
4766
 * pData to the start of the next.  If defaulted the size of the datatype
4767
 * eBufType * nBufXSize is used.
4768
 *
4769
 * @param nBandSpace the byte offset from the start of one bands data to the
4770
 * start of the next.  If defaulted (zero) the value will be
4771
 * nLineSpace * nBufYSize implying band sequential organization
4772
 * of the data buffer.
4773
 *
4774
 * @param papszOptions Driver specific control options in a string list or NULL.
4775
 * Consult driver documentation for options supported.
4776
 *
4777
 * @return handle representing the request.
4778
 */
4779
4780
GDALAsyncReaderH CPL_STDCALL GDALBeginAsyncReader(
4781
    GDALDatasetH hDS, int nXOff, int nYOff, int nXSize, int nYSize, void *pBuf,
4782
    int nBufXSize, int nBufYSize, GDALDataType eBufType, int nBandCount,
4783
    int *panBandMap, int nPixelSpace, int nLineSpace, int nBandSpace,
4784
    CSLConstList papszOptions)
4785
4786
0
{
4787
0
    VALIDATE_POINTER1(hDS, "GDALDataset", nullptr);
4788
0
    return static_cast<GDALAsyncReaderH>(
4789
0
        GDALDataset::FromHandle(hDS)->BeginAsyncReader(
4790
0
            nXOff, nYOff, nXSize, nYSize, pBuf, nBufXSize, nBufYSize, eBufType,
4791
0
            nBandCount, panBandMap, nPixelSpace, nLineSpace, nBandSpace,
4792
0
            const_cast<char **>(papszOptions)));
4793
0
}
4794
4795
/************************************************************************/
4796
/*                        EndAsyncReader()                            */
4797
/************************************************************************/
4798
4799
/**
4800
 * End asynchronous request.
4801
 *
4802
 * This method destroys an asynchronous io request and recovers all
4803
 * resources associated with it.
4804
 *
4805
 * This method is the same as the C function GDALEndAsyncReader().
4806
 *
4807
 * @param poARIO pointer to a GDALAsyncReader
4808
 */
4809
4810
void GDALDataset::EndAsyncReader(GDALAsyncReader *poARIO)
4811
0
{
4812
0
    delete poARIO;
4813
0
}
4814
4815
/************************************************************************/
4816
/*                        GDALEndAsyncReader()                        */
4817
/************************************************************************/
4818
4819
/**
4820
 * End asynchronous request.
4821
 *
4822
 * This method destroys an asynchronous io request and recovers all
4823
 * resources associated with it.
4824
 *
4825
 * This method is the same as the C++ method GDALDataset::EndAsyncReader().
4826
 *
4827
 * @param hDS handle to the dataset object.
4828
 * @param hAsyncReaderH handle returned by GDALBeginAsyncReader()
4829
 */
4830
4831
void CPL_STDCALL GDALEndAsyncReader(GDALDatasetH hDS,
4832
                                    GDALAsyncReaderH hAsyncReaderH)
4833
0
{
4834
0
    VALIDATE_POINTER0(hDS, "GDALDataset");
4835
0
    VALIDATE_POINTER0(hAsyncReaderH, "GDALAsyncReader");
4836
0
    GDALDataset::FromHandle(hDS)->EndAsyncReader(
4837
0
        static_cast<GDALAsyncReader *>(hAsyncReaderH));
4838
0
}
4839
4840
/************************************************************************/
4841
/*                       CloseDependentDatasets()                       */
4842
/************************************************************************/
4843
4844
/**
4845
 * Drop references to any other datasets referenced by this dataset.
4846
 *
4847
 * This method should release any reference to other datasets (e.g. a VRT
4848
 * dataset to its sources), but not close the current dataset itself.
4849
 *
4850
 * If at least, one reference to a dependent dataset has been dropped,
4851
 * this method should return TRUE. Otherwise it *should* return FALSE.
4852
 * (Failure to return the proper value might result in infinite loop)
4853
 *
4854
 * This method can be called several times on a given dataset. After
4855
 * the first time, it should not do anything and return FALSE.
4856
 *
4857
 * The driver implementation may choose to destroy its raster bands,
4858
 * so be careful not to call any method on the raster bands afterwards.
4859
 *
4860
 * Basically the only safe action you can do after calling
4861
 * CloseDependentDatasets() is to call the destructor.
4862
 *
4863
 * Note: the only legitimate caller of CloseDependentDatasets() is
4864
 * GDALDriverManager::~GDALDriverManager()
4865
 *
4866
 * @return TRUE if at least one reference to another dataset has been dropped.
4867
 */
4868
int GDALDataset::CloseDependentDatasets()
4869
0
{
4870
0
    return oOvManager.CloseDependentDatasets();
4871
0
}
4872
4873
/************************************************************************/
4874
/*                            ReportError()                             */
4875
/************************************************************************/
4876
4877
#ifndef DOXYGEN_XML
4878
/**
4879
 * \brief Emits an error related to a dataset.
4880
 *
4881
 * This function is a wrapper for regular CPLError(). The only difference
4882
 * with CPLError() is that it prepends the error message with the dataset
4883
 * name.
4884
 *
4885
 * @param eErrClass one of CE_Warning, CE_Failure or CE_Fatal.
4886
 * @param err_no the error number (CPLE_*) from cpl_error.h.
4887
 * @param fmt a printf() style format string.  Any additional arguments
4888
 * will be treated as arguments to fill in this format in a manner
4889
 * similar to printf().
4890
 *
4891
 */
4892
4893
void GDALDataset::ReportError(CPLErr eErrClass, CPLErrorNum err_no,
4894
                              const char *fmt, ...) const
4895
0
{
4896
0
    va_list args;
4897
0
    va_start(args, fmt);
4898
0
    ReportErrorV(GetDescription(), eErrClass, err_no, fmt, args);
4899
0
    va_end(args);
4900
0
}
4901
4902
/**
4903
 * \brief Emits an error related to a dataset (static method)
4904
 *
4905
 * This function is a wrapper for regular CPLError(). The only difference
4906
 * with CPLError() is that it prepends the error message with the dataset
4907
 * name.
4908
 *
4909
 * @param pszDSName dataset name.
4910
 * @param eErrClass one of CE_Warning, CE_Failure or CE_Fatal.
4911
 * @param err_no the error number (CPLE_*) from cpl_error.h.
4912
 * @param fmt a printf() style format string.  Any additional arguments
4913
 * will be treated as arguments to fill in this format in a manner
4914
 * similar to printf().
4915
 *
4916
 * @since GDAL 3.2.0
4917
 */
4918
4919
void GDALDataset::ReportError(const char *pszDSName, CPLErr eErrClass,
4920
                              CPLErrorNum err_no, const char *fmt, ...)
4921
0
{
4922
0
    va_list args;
4923
0
    va_start(args, fmt);
4924
0
    ReportErrorV(pszDSName, eErrClass, err_no, fmt, args);
4925
0
    va_end(args);
4926
0
}
4927
4928
void GDALDataset::ReportErrorV(const char *pszDSName, CPLErr eErrClass,
4929
                               CPLErrorNum err_no, const char *fmt,
4930
                               va_list args)
4931
0
{
4932
0
    pszDSName = CPLGetFilename(pszDSName);
4933
0
    if (pszDSName[0] != '\0')
4934
0
    {
4935
0
        CPLError(eErrClass, err_no, "%s",
4936
0
                 std::string(pszDSName)
4937
0
                     .append(": ")
4938
0
                     .append(CPLString().vPrintf(fmt, args))
4939
0
                     .c_str());
4940
0
    }
4941
0
    else
4942
0
    {
4943
0
        CPLErrorV(eErrClass, err_no, fmt, args);
4944
0
    }
4945
0
}
4946
#endif
4947
4948
/************************************************************************/
4949
/*                            GetMetadata()                             */
4950
/************************************************************************/
4951
char **GDALDataset::GetMetadata(const char *pszDomain)
4952
0
{
4953
#ifndef WITHOUT_DERIVED
4954
    if (pszDomain != nullptr && EQUAL(pszDomain, "DERIVED_SUBDATASETS"))
4955
    {
4956
        oDerivedMetadataList.Clear();
4957
4958
        // First condition: at least one raster band.
4959
        if (GetRasterCount() > 0)
4960
        {
4961
            // Check if there is at least one complex band.
4962
            bool hasAComplexBand = false;
4963
4964
            for (int rasterId = 1; rasterId <= GetRasterCount(); ++rasterId)
4965
            {
4966
                if (GDALDataTypeIsComplex(
4967
                        GetRasterBand(rasterId)->GetRasterDataType()))
4968
                {
4969
                    hasAComplexBand = true;
4970
                    break;
4971
                }
4972
            }
4973
4974
            unsigned int nbSupportedDerivedDS = 0;
4975
            const DerivedDatasetDescription *poDDSDesc =
4976
                GDALGetDerivedDatasetDescriptions(&nbSupportedDerivedDS);
4977
4978
            int nNumDataset = 1;
4979
            for (unsigned int derivedId = 0; derivedId < nbSupportedDerivedDS;
4980
                 ++derivedId)
4981
            {
4982
                if (hasAComplexBand ||
4983
                    CPLString(poDDSDesc[derivedId].pszInputPixelType) !=
4984
                        "complex")
4985
                {
4986
                    oDerivedMetadataList.SetNameValue(
4987
                        CPLSPrintf("DERIVED_SUBDATASET_%d_NAME", nNumDataset),
4988
                        CPLSPrintf("DERIVED_SUBDATASET:%s:%s",
4989
                                   poDDSDesc[derivedId].pszDatasetName,
4990
                                   GetDescription()));
4991
4992
                    CPLString osDesc(
4993
                        CPLSPrintf("%s from %s",
4994
                                   poDDSDesc[derivedId].pszDatasetDescription,
4995
                                   GetDescription()));
4996
                    oDerivedMetadataList.SetNameValue(
4997
                        CPLSPrintf("DERIVED_SUBDATASET_%d_DESC", nNumDataset),
4998
                        osDesc.c_str());
4999
5000
                    nNumDataset++;
5001
                }
5002
            }
5003
        }
5004
        return oDerivedMetadataList.List();
5005
    }
5006
#endif
5007
5008
0
    return GDALMajorObject::GetMetadata(pszDomain);
5009
0
}
5010
5011
// clang-format off
5012
5013
/**
5014
 * \fn GDALDataset::SetMetadata( char ** papszMetadata, const char * pszDomain)
5015
 * \brief Set metadata.
5016
 *
5017
 * CAUTION: depending on the format, older values of the updated information
5018
 * might still be found in the file in a "ghost" state, even if no longer
5019
 * accessible through the GDAL API. This is for example the case of the GTiff
5020
 * format (this is not a exhaustive list)
5021
 *
5022
 * The C function GDALSetMetadata() does the same thing as this method.
5023
 *
5024
 * @param papszMetadata the metadata in name=value string list format to
5025
 * apply.
5026
 * @param pszDomain the domain of interest.  Use "" or NULL for the default
5027
 * domain.
5028
 * @return CE_None on success, CE_Failure on failure and CE_Warning if the
5029
 * metadata has been accepted, but is likely not maintained persistently
5030
 * by the underlying object between sessions.
5031
 */
5032
5033
/**
5034
 * \fn GDALDataset::SetMetadataItem( const char * pszName, const char * pszValue, const char * pszDomain)
5035
 * \brief Set single metadata item.
5036
 *
5037
 * CAUTION: depending on the format, older values of the updated information
5038
 * might still be found in the file in a "ghost" state, even if no longer
5039
 * accessible through the GDAL API. This is for example the case of the GTiff
5040
 * format (this is not a exhaustive list)
5041
 *
5042
 * The C function GDALSetMetadataItem() does the same thing as this method.
5043
 *
5044
 * @param pszName the key for the metadata item to fetch.
5045
 * @param pszValue the value to assign to the key.
5046
 * @param pszDomain the domain to set within, use NULL for the default domain.
5047
 *
5048
 * @return CE_None on success, or an error code on failure.
5049
 */
5050
5051
// clang-format on
5052
5053
/************************************************************************/
5054
/*                            GetMetadataDomainList()                   */
5055
/************************************************************************/
5056
5057
char **GDALDataset::GetMetadataDomainList()
5058
0
{
5059
0
    char **currentDomainList = CSLDuplicate(oMDMD.GetDomainList());
5060
5061
    // Ensure that we do not duplicate DERIVED domain.
5062
0
    if (GetRasterCount() > 0 &&
5063
0
        CSLFindString(currentDomainList, "DERIVED_SUBDATASETS") == -1)
5064
0
    {
5065
0
        currentDomainList =
5066
0
            CSLAddString(currentDomainList, "DERIVED_SUBDATASETS");
5067
0
    }
5068
0
    return currentDomainList;
5069
0
}
5070
5071
/************************************************************************/
5072
/*                            GetDriverName()                           */
5073
/************************************************************************/
5074
5075
/** Return driver name.
5076
 * @return driver name.
5077
 */
5078
const char *GDALDataset::GetDriverName() const
5079
0
{
5080
0
    if (poDriver)
5081
0
        return poDriver->GetDescription();
5082
0
    return "";
5083
0
}
5084
5085
/************************************************************************/
5086
/*                     GDALDatasetReleaseResultSet()                    */
5087
/************************************************************************/
5088
5089
/**
5090
 \brief Release results of ExecuteSQL().
5091
5092
 This function should only be used to deallocate OGRLayers resulting from
5093
 an ExecuteSQL() call on the same GDALDataset.  Failure to deallocate a
5094
 results set before destroying the GDALDataset may cause errors.
5095
5096
 This function is the same as the C++ method GDALDataset::ReleaseResultSet()
5097
5098
5099
 @param hDS the dataset handle.
5100
 @param hLayer the result of a previous ExecuteSQL() call.
5101
5102
*/
5103
void GDALDatasetReleaseResultSet(GDALDatasetH hDS, OGRLayerH hLayer)
5104
5105
0
{
5106
0
    VALIDATE_POINTER0(hDS, "GDALDatasetReleaseResultSet");
5107
5108
0
#ifdef OGRAPISPY_ENABLED
5109
0
    if (bOGRAPISpyEnabled)
5110
0
        OGRAPISpy_DS_ReleaseResultSet(hDS, hLayer);
5111
0
#endif
5112
5113
0
    GDALDataset::FromHandle(hDS)->ReleaseResultSet(
5114
0
        OGRLayer::FromHandle(hLayer));
5115
0
}
5116
5117
/************************************************************************/
5118
/*                       GDALDatasetGetLayerCount()                     */
5119
/************************************************************************/
5120
5121
/**
5122
 \brief Get the number of layers in this dataset.
5123
5124
 This function is the same as the C++ method GDALDataset::GetLayerCount()
5125
5126
5127
 @param hDS the dataset handle.
5128
 @return layer count.
5129
*/
5130
5131
int GDALDatasetGetLayerCount(GDALDatasetH hDS)
5132
5133
0
{
5134
0
    VALIDATE_POINTER1(hDS, "GDALDatasetH", 0);
5135
5136
0
#ifdef OGRAPISPY_ENABLED
5137
0
    if (bOGRAPISpyEnabled)
5138
0
        OGRAPISpy_DS_GetLayerCount(reinterpret_cast<GDALDatasetH>(hDS));
5139
0
#endif
5140
5141
0
    return GDALDataset::FromHandle(hDS)->GetLayerCount();
5142
0
}
5143
5144
/************************************************************************/
5145
/*                        GDALDatasetGetLayer()                         */
5146
/************************************************************************/
5147
5148
/**
5149
 \brief Fetch a layer by index.
5150
5151
 The returned layer remains owned by the
5152
 GDALDataset and should not be deleted by the application.
5153
5154
 This function is the same as the C++ method GDALDataset::GetLayer()
5155
5156
5157
 @param hDS the dataset handle.
5158
 @param iLayer a layer number between 0 and GetLayerCount()-1.
5159
5160
 @return the layer, or NULL if iLayer is out of range or an error occurs.
5161
*/
5162
5163
OGRLayerH GDALDatasetGetLayer(GDALDatasetH hDS, int iLayer)
5164
5165
0
{
5166
0
    VALIDATE_POINTER1(hDS, "GDALDatasetGetLayer", nullptr);
5167
5168
0
    OGRLayerH hLayer =
5169
0
        OGRLayer::ToHandle(GDALDataset::FromHandle(hDS)->GetLayer(iLayer));
5170
5171
0
#ifdef OGRAPISPY_ENABLED
5172
0
    if (bOGRAPISpyEnabled)
5173
0
        OGRAPISpy_DS_GetLayer(hDS, iLayer, hLayer);
5174
0
#endif
5175
5176
0
    return hLayer;
5177
0
}
5178
5179
/************************************************************************/
5180
/*                     GDALDatasetGetLayerByName()                      */
5181
/************************************************************************/
5182
5183
/**
5184
 \brief Fetch a layer by name.
5185
5186
 The returned layer remains owned by the
5187
 GDALDataset and should not be deleted by the application.
5188
5189
 This function is the same as the C++ method GDALDataset::GetLayerByName()
5190
5191
5192
 @param hDS the dataset handle.
5193
 @param pszName the layer name of the layer to fetch.
5194
5195
 @return the layer, or NULL if Layer is not found or an error occurs.
5196
*/
5197
5198
OGRLayerH GDALDatasetGetLayerByName(GDALDatasetH hDS, const char *pszName)
5199
5200
0
{
5201
0
    VALIDATE_POINTER1(hDS, "GDALDatasetGetLayerByName", nullptr);
5202
5203
0
    OGRLayerH hLayer = OGRLayer::ToHandle(
5204
0
        GDALDataset::FromHandle(hDS)->GetLayerByName(pszName));
5205
5206
0
#ifdef OGRAPISPY_ENABLED
5207
0
    if (bOGRAPISpyEnabled)
5208
0
        OGRAPISpy_DS_GetLayerByName(hDS, pszName, hLayer);
5209
0
#endif
5210
5211
0
    return hLayer;
5212
0
}
5213
5214
/************************************************************************/
5215
/*                        GDALDatasetIsLayerPrivate()                   */
5216
/************************************************************************/
5217
5218
/**
5219
 \brief Returns true if the layer at the specified index is deemed a private or
5220
 system table, or an internal detail only.
5221
5222
 This function is the same as the C++ method GDALDataset::IsLayerPrivate()
5223
5224
 @since GDAL 3.4
5225
5226
 @param hDS the dataset handle.
5227
 @param iLayer a layer number between 0 and GetLayerCount()-1.
5228
5229
 @return true if the layer is a private or system table.
5230
*/
5231
5232
int GDALDatasetIsLayerPrivate(GDALDatasetH hDS, int iLayer)
5233
5234
0
{
5235
0
    VALIDATE_POINTER1(hDS, "GDALDatasetIsLayerPrivate", false);
5236
5237
0
    const bool res = GDALDataset::FromHandle(hDS)->IsLayerPrivate(iLayer);
5238
5239
0
    return res ? 1 : 0;
5240
0
}
5241
5242
/************************************************************************/
5243
/*                            GetLayerIndex()                           */
5244
/************************************************************************/
5245
5246
/**
5247
 \brief Returns the index of the layer specified by name.
5248
5249
 @since GDAL 3.12
5250
5251
 @param pszName layer name (not NULL)
5252
5253
 @return an index >= 0, or -1 if not found.
5254
*/
5255
5256
int GDALDataset::GetLayerIndex(const char *pszName) const
5257
0
{
5258
0
    const int nLayerCount = GetLayerCount();
5259
0
    int iMatch = -1;
5260
0
    for (int i = 0; i < nLayerCount; ++i)
5261
0
    {
5262
0
        if (const auto poLayer = GetLayer(i))
5263
0
        {
5264
0
            const char *pszLayerName = poLayer->GetDescription();
5265
0
            if (strcmp(pszName, pszLayerName) == 0)
5266
0
            {
5267
0
                iMatch = i;
5268
0
                break;
5269
0
            }
5270
0
            else if (EQUAL(pszName, pszLayerName))
5271
0
            {
5272
0
                iMatch = i;
5273
0
            }
5274
0
        }
5275
0
    }
5276
0
    return iMatch;
5277
0
}
5278
5279
/************************************************************************/
5280
/*                        GDALDatasetDeleteLayer()                      */
5281
/************************************************************************/
5282
5283
/**
5284
 \brief Delete the indicated layer from the datasource.
5285
5286
 If this function is supported
5287
 the ODsCDeleteLayer capability will test TRUE on the GDALDataset.
5288
5289
 This method is the same as the C++ method GDALDataset::DeleteLayer().
5290
5291
5292
 @param hDS the dataset handle.
5293
 @param iLayer the index of the layer to delete.
5294
5295
 @return OGRERR_NONE on success, or OGRERR_UNSUPPORTED_OPERATION if deleting
5296
 layers is not supported for this datasource.
5297
5298
*/
5299
OGRErr GDALDatasetDeleteLayer(GDALDatasetH hDS, int iLayer)
5300
5301
0
{
5302
0
    VALIDATE_POINTER1(hDS, "GDALDatasetH", OGRERR_INVALID_HANDLE);
5303
5304
0
#ifdef OGRAPISPY_ENABLED
5305
0
    if (bOGRAPISpyEnabled)
5306
0
        OGRAPISpy_DS_DeleteLayer(hDS, iLayer);
5307
0
#endif
5308
5309
0
    return GDALDataset::FromHandle(hDS)->DeleteLayer(iLayer);
5310
0
}
5311
5312
/************************************************************************/
5313
/*                            CreateLayer()                             */
5314
/************************************************************************/
5315
5316
/**
5317
\brief This method attempts to create a new layer on the dataset with the
5318
indicated name, coordinate system, geometry type.
5319
5320
The papszOptions argument
5321
can be used to control driver specific creation options.  These options are
5322
normally documented in the format specific documentation.
5323
That function will try to validate the creation option list passed to the
5324
driver with the GDALValidateCreationOptions() method. This check can be
5325
disabled by defining the configuration option GDAL_VALIDATE_CREATION_OPTIONS set
5326
to NO.
5327
5328
Drivers should extend the ICreateLayer() method and not
5329
CreateLayer(). CreateLayer() adds validation of layer creation options, before
5330
delegating the actual work to ICreateLayer().
5331
5332
This method is the same as the C function GDALDatasetCreateLayer() and the
5333
deprecated OGR_DS_CreateLayer().
5334
5335
Example:
5336
5337
\code{.cpp}
5338
#include "gdal.h"
5339
#include "cpl_string.h"
5340
5341
...
5342
5343
        OGRLayer *poLayer;
5344
        char     **papszOptions;
5345
5346
        if( !poDS->TestCapability( ODsCCreateLayer ) )
5347
        {
5348
        ...
5349
        }
5350
5351
        papszOptions = CSLSetNameValue( papszOptions, "DIM", "2" );
5352
        poLayer = poDS->CreateLayer( "NewLayer", nullptr, wkbUnknown,
5353
                                     papszOptions );
5354
        CSLDestroy( papszOptions );
5355
5356
        if( poLayer == NULL )
5357
        {
5358
            ...
5359
        }
5360
\endcode
5361
5362
@param pszName the name for the new layer.  This should ideally not
5363
match any existing layer on the datasource.
5364
@param poSpatialRef the coordinate system to use for the new layer, or NULL if
5365
no coordinate system is available.
5366
@param eGType the geometry type for the layer.  Use wkbUnknown if there
5367
are no constraints on the types geometry to be written.
5368
@param papszOptions a StringList of name=value options.  Options are driver
5369
specific.
5370
5371
@return NULL is returned on failure, or a new OGRLayer handle on success.
5372
5373
*/
5374
5375
OGRLayer *GDALDataset::CreateLayer(const char *pszName,
5376
                                   const OGRSpatialReference *poSpatialRef,
5377
                                   OGRwkbGeometryType eGType,
5378
                                   CSLConstList papszOptions)
5379
5380
0
{
5381
0
    if (eGType == wkbNone)
5382
0
    {
5383
0
        return CreateLayer(pszName, nullptr, papszOptions);
5384
0
    }
5385
0
    else
5386
0
    {
5387
0
        OGRGeomFieldDefn oGeomFieldDefn("", eGType);
5388
0
        oGeomFieldDefn.SetSpatialRef(poSpatialRef);
5389
0
        return CreateLayer(pszName, &oGeomFieldDefn, papszOptions);
5390
0
    }
5391
0
}
5392
5393
/**
5394
\brief This method attempts to create a new layer on the dataset with the
5395
indicated name and geometry field definition.
5396
5397
When poGeomFieldDefn is not null, most drivers should honor
5398
poGeomFieldDefn->GetType() and poGeomFieldDefn->GetSpatialRef().
5399
Drivers that honor poGeomFieldDefn->GetCoordinatePrecision() will declare the
5400
GDAL_DCAP_HONOR_GEOM_COORDINATE_PRECISION capability. Drivers may honor
5401
poGeomFieldDefn->GetNameRef() and poGeomFieldDefn->IsNullable(), but there are
5402
very few currently.
5403
5404
Note that even if a geometry coordinate precision is set and a driver honors the
5405
GDAL_DCAP_HONOR_GEOM_COORDINATE_PRECISION capability, geometries passed to
5406
OGRLayer::CreateFeature() and OGRLayer::SetFeature() are assumed to be compatible
5407
with the coordinate precision. That is they are assumed to be valid once their
5408
coordinates are rounded to it. If it might not be the case, the user may set
5409
the OGR_APPLY_GEOM_SET_PRECISION configuration option before calling CreateFeature()
5410
or SetFeature() to force the OGRGeometry::SetPrecision() method to be called on
5411
the passed geometries.
5412
5413
The papszOptions argument
5414
can be used to control driver specific creation options. These options are
5415
normally documented in the format specific documentation.
5416
This function will try to validate the creation option list passed to the
5417
driver with the GDALValidateCreationOptions() method. This check can be
5418
disabled by defining the configuration option GDAL_VALIDATE_CREATION_OPTIONS set
5419
to NO.
5420
5421
Drivers should extend the ICreateLayer() method and not
5422
CreateLayer(). CreateLayer() adds validation of layer creation options, before
5423
delegating the actual work to ICreateLayer().
5424
5425
This method is the same as the C function GDALDatasetCreateLayerFromGeomFieldDefn().
5426
5427
@param pszName the name for the new layer.  This should ideally not
5428
match any existing layer on the datasource.
5429
@param poGeomFieldDefn the geometry field definition to use for the new layer,
5430
or NULL if there is no geometry field.
5431
@param papszOptions a StringList of name=value options.  Options are driver
5432
specific.
5433
5434
@return NULL is returned on failure, or a new OGRLayer handle on success.
5435
@since 3.9
5436
5437
*/
5438
5439
OGRLayer *GDALDataset::CreateLayer(const char *pszName,
5440
                                   const OGRGeomFieldDefn *poGeomFieldDefn,
5441
                                   CSLConstList papszOptions)
5442
5443
0
{
5444
0
    if (CPLTestBool(
5445
0
            CPLGetConfigOption("GDAL_VALIDATE_CREATION_OPTIONS", "YES")))
5446
0
    {
5447
0
        ValidateLayerCreationOptions(papszOptions);
5448
0
    }
5449
5450
0
    OGRLayer *poLayer;
5451
0
    if (poGeomFieldDefn)
5452
0
    {
5453
0
        OGRGeomFieldDefn oGeomFieldDefn(poGeomFieldDefn);
5454
0
        if (OGR_GT_IsNonLinear(poGeomFieldDefn->GetType()) &&
5455
0
            !TestCapability(ODsCCurveGeometries))
5456
0
        {
5457
0
            oGeomFieldDefn.SetType(
5458
0
                OGR_GT_GetLinear(poGeomFieldDefn->GetType()));
5459
0
        }
5460
5461
0
        poLayer = ICreateLayer(pszName, &oGeomFieldDefn, papszOptions);
5462
0
    }
5463
0
    else
5464
0
    {
5465
0
        poLayer = ICreateLayer(pszName, nullptr, papszOptions);
5466
0
    }
5467
0
#ifdef DEBUG
5468
0
    if (poLayer != nullptr && OGR_GT_IsNonLinear(poLayer->GetGeomType()) &&
5469
0
        !poLayer->TestCapability(OLCCurveGeometries))
5470
0
    {
5471
0
        CPLError(CE_Warning, CPLE_AppDefined,
5472
0
                 "Inconsistent driver: Layer geometry type is non-linear, but "
5473
0
                 "TestCapability(OLCCurveGeometries) returns FALSE.");
5474
0
    }
5475
0
#endif
5476
5477
0
    return poLayer;
5478
0
}
5479
5480
//! @cond Doxygen_Suppress
5481
5482
// Technical override to avoid ambiguous choice between the old and new
5483
// new CreateLayer() signatures.
5484
OGRLayer *GDALDataset::CreateLayer(const char *pszName)
5485
0
{
5486
0
    OGRGeomFieldDefn oGeomFieldDefn("", wkbUnknown);
5487
0
    return CreateLayer(pszName, &oGeomFieldDefn, nullptr);
5488
0
}
5489
5490
// Technical override to avoid ambiguous choice between the old and new
5491
// new CreateLayer() signatures.
5492
OGRLayer *GDALDataset::CreateLayer(const char *pszName, std::nullptr_t)
5493
0
{
5494
0
    OGRGeomFieldDefn oGeomFieldDefn("", wkbUnknown);
5495
0
    return CreateLayer(pszName, &oGeomFieldDefn, nullptr);
5496
0
}
5497
5498
//!@endcond
5499
5500
/************************************************************************/
5501
/*                         GDALDatasetCreateLayer()                     */
5502
/************************************************************************/
5503
5504
/**
5505
\brief This function attempts to create a new layer on the dataset with the
5506
indicated name, coordinate system, geometry type.
5507
5508
The papszOptions argument can be used to control driver specific creation
5509
options.  These options are normally documented in the format specific
5510
documentation.
5511
5512
This method is the same as the C++ method GDALDataset::CreateLayer().
5513
5514
Example:
5515
5516
\code{.c}
5517
#include "gdal.h"
5518
#include "cpl_string.h"
5519
5520
...
5521
5522
        OGRLayerH  hLayer;
5523
        char     **papszOptions;
5524
5525
        if( !GDALDatasetTestCapability( hDS, ODsCCreateLayer ) )
5526
        {
5527
        ...
5528
        }
5529
5530
        papszOptions = CSLSetNameValue( papszOptions, "DIM", "2" );
5531
        hLayer = GDALDatasetCreateLayer( hDS, "NewLayer", NULL, wkbUnknown,
5532
                                         papszOptions );
5533
        CSLDestroy( papszOptions );
5534
5535
        if( hLayer == NULL )
5536
        {
5537
            ...
5538
        }
5539
\endcode
5540
5541
5542
@param hDS the dataset handle
5543
@param pszName the name for the new layer.  This should ideally not
5544
match any existing layer on the datasource.
5545
@param hSpatialRef the coordinate system to use for the new layer, or NULL if
5546
no coordinate system is available.
5547
@param eGType the geometry type for the layer.  Use wkbUnknown if there
5548
are no constraints on the types geometry to be written.
5549
@param papszOptions a StringList of name=value options.  Options are driver
5550
specific.
5551
5552
@return NULL is returned on failure, or a new OGRLayer handle on success.
5553
5554
*/
5555
5556
OGRLayerH GDALDatasetCreateLayer(GDALDatasetH hDS, const char *pszName,
5557
                                 OGRSpatialReferenceH hSpatialRef,
5558
                                 OGRwkbGeometryType eGType,
5559
                                 CSLConstList papszOptions)
5560
5561
0
{
5562
0
    VALIDATE_POINTER1(hDS, "GDALDatasetCreateLayer", nullptr);
5563
5564
0
    if (pszName == nullptr)
5565
0
    {
5566
0
        CPLError(CE_Failure, CPLE_ObjectNull,
5567
0
                 "Name was NULL in GDALDatasetCreateLayer");
5568
0
        return nullptr;
5569
0
    }
5570
5571
0
    OGRLayerH hLayer =
5572
0
        OGRLayer::ToHandle(GDALDataset::FromHandle(hDS)->CreateLayer(
5573
0
            pszName, OGRSpatialReference::FromHandle(hSpatialRef), eGType,
5574
0
            const_cast<char **>(papszOptions)));
5575
5576
0
#ifdef OGRAPISPY_ENABLED
5577
0
    if (bOGRAPISpyEnabled)
5578
0
        OGRAPISpy_DS_CreateLayer(hDS, pszName, hSpatialRef, eGType,
5579
0
                                 const_cast<char **>(papszOptions), hLayer);
5580
0
#endif
5581
5582
0
    return hLayer;
5583
0
}
5584
5585
/************************************************************************/
5586
/*                 GDALDatasetCreateLayerFromGeomFieldDefn()            */
5587
/************************************************************************/
5588
5589
/**
5590
\brief This function attempts to create a new layer on the dataset with the
5591
indicated name and geometry field.
5592
5593
When poGeomFieldDefn is not null, most drivers should honor
5594
poGeomFieldDefn->GetType() and poGeomFieldDefn->GetSpatialRef().
5595
Drivers that honor poGeomFieldDefn->GetCoordinatePrecision() will declare the
5596
GDAL_DCAP_HONOR_GEOM_COORDINATE_PRECISION capability. Drivers may honor
5597
poGeomFieldDefn->GetNameRef() and poGeomFieldDefn->IsNullable(), but there are
5598
very few currently.
5599
5600
Note that even if a geometry coordinate precision is set and a driver honors the
5601
GDAL_DCAP_HONOR_GEOM_COORDINATE_PRECISION capability, geometries passed to
5602
OGRLayer::CreateFeature() and OGRLayer::SetFeature() are assumed to be compatible
5603
with the coordinate precision. That is they are assumed to be valid once their
5604
coordinates are rounded to it. If it might not be the case, the user may set
5605
the OGR_APPLY_GEOM_SET_PRECISION configuration option before calling CreateFeature()
5606
or SetFeature() to force the OGRGeometry::SetPrecision() method to be called on
5607
the passed geometries.
5608
5609
The papszOptions argument can be used to control driver specific creation
5610
options.  These options are normally documented in the format specific
5611
documentation.
5612
5613
This method is the same as the C++ method GDALDataset::CreateLayer().
5614
5615
@param hDS the dataset handle
5616
@param pszName the name for the new layer.  This should ideally not
5617
match any existing layer on the datasource.
5618
@param hGeomFieldDefn the geometry field definition. May be NULL to indicate
5619
a non-spatial file (or if adding geometry fields later with OGR_L_CreateGeomField()
5620
for drivers supporting that interface).
5621
@param papszOptions a StringList of name=value options.  Options are driver
5622
specific.
5623
5624
@return NULL is returned on failure, or a new OGRLayer handle on success.
5625
5626
@since GDAL 3.9
5627
5628
*/
5629
5630
OGRLayerH
5631
GDALDatasetCreateLayerFromGeomFieldDefn(GDALDatasetH hDS, const char *pszName,
5632
                                        OGRGeomFieldDefnH hGeomFieldDefn,
5633
                                        CSLConstList papszOptions)
5634
5635
0
{
5636
0
    VALIDATE_POINTER1(hDS, "GDALDatasetCreateLayerFromGeomFieldDefn", nullptr);
5637
5638
0
    if (!pszName)
5639
0
    {
5640
0
        CPLError(CE_Failure, CPLE_ObjectNull,
5641
0
                 "Name was NULL in GDALDatasetCreateLayerFromGeomFieldDefn");
5642
0
        return nullptr;
5643
0
    }
5644
5645
0
    OGRLayerH hLayer =
5646
0
        OGRLayer::ToHandle(GDALDataset::FromHandle(hDS)->CreateLayer(
5647
0
            pszName, OGRGeomFieldDefn::FromHandle(hGeomFieldDefn),
5648
0
            papszOptions));
5649
0
    return hLayer;
5650
0
}
5651
5652
/************************************************************************/
5653
/*                         GDALDatasetCopyLayer()                       */
5654
/************************************************************************/
5655
5656
/**
5657
 \brief Duplicate an existing layer.
5658
5659
 This function creates a new layer, duplicate the field definitions of the
5660
 source layer and then duplicate each features of the source layer.
5661
 The papszOptions argument
5662
 can be used to control driver specific creation options.  These options are
5663
 normally documented in the format specific documentation.
5664
 The source layer may come from another dataset.
5665
5666
 This method is the same as the C++ method GDALDataset::CopyLayer()
5667
5668
5669
 @param hDS the dataset handle.
5670
 @param hSrcLayer source layer.
5671
 @param pszNewName the name of the layer to create.
5672
 @param papszOptions a StringList of name=value options.  Options are driver
5673
                     specific.
5674
5675
 @return a handle to the layer, or NULL if an error occurs.
5676
*/
5677
OGRLayerH GDALDatasetCopyLayer(GDALDatasetH hDS, OGRLayerH hSrcLayer,
5678
                               const char *pszNewName,
5679
                               CSLConstList papszOptions)
5680
5681
0
{
5682
0
    VALIDATE_POINTER1(hDS, "OGR_DS_CopyGDALDatasetCopyLayerLayer", nullptr);
5683
0
    VALIDATE_POINTER1(hSrcLayer, "GDALDatasetCopyLayer", nullptr);
5684
0
    VALIDATE_POINTER1(pszNewName, "GDALDatasetCopyLayer", nullptr);
5685
5686
0
    return OGRLayer::ToHandle(GDALDataset::FromHandle(hDS)->CopyLayer(
5687
0
        OGRLayer::FromHandle(hSrcLayer), pszNewName,
5688
0
        const_cast<char **>(papszOptions)));
5689
0
}
5690
5691
/************************************************************************/
5692
/*                        GDALDatasetExecuteSQL()                       */
5693
/************************************************************************/
5694
5695
/**
5696
 \brief Execute an SQL statement against the data store.
5697
5698
 The result of an SQL query is either NULL for statements that are in error,
5699
 or that have no results set, or an OGRLayer pointer representing a results
5700
 set from the query.  Note that this OGRLayer is in addition to the layers
5701
 in the data store and must be destroyed with
5702
 ReleaseResultSet() before the dataset is closed
5703
 (destroyed).
5704
5705
 This method is the same as the C++ method GDALDataset::ExecuteSQL()
5706
5707
 For more information on the SQL dialect supported internally by OGR
5708
 review the <a href="https://gdal.org/user/ogr_sql_dialect.html">OGR SQL</a>
5709
 document.  Some drivers (i.e. Oracle and PostGIS) pass the SQL directly through
5710
 to the underlying RDBMS.
5711
5712
 Starting with OGR 1.10, the <a
5713
 href="https://gdal.org/user/sql_sqlite_dialect.html">SQLITE dialect</a> can
5714
 also be used.
5715
5716
5717
 @param hDS the dataset handle.
5718
 @param pszStatement the SQL statement to execute.
5719
 @param hSpatialFilter geometry which represents a spatial filter. Can be NULL.
5720
5721
 @param pszDialect allows control of the statement dialect. If set to NULL, the
5722
 OGR SQL engine will be used, except for RDBMS drivers that will use their
5723
 dedicated SQL engine, unless OGRSQL is explicitly passed as the
5724
 dialect. Starting with OGR 1.10, the SQLITE dialect can also be used.
5725
5726
 @return an OGRLayer containing the results of the query.  Deallocate with
5727
 GDALDatasetReleaseResultSet().
5728
5729
*/
5730
5731
OGRLayerH GDALDatasetExecuteSQL(GDALDatasetH hDS, const char *pszStatement,
5732
                                OGRGeometryH hSpatialFilter,
5733
                                const char *pszDialect)
5734
5735
0
{
5736
0
    VALIDATE_POINTER1(hDS, "GDALDatasetExecuteSQL", nullptr);
5737
5738
0
    OGRLayerH hLayer =
5739
0
        OGRLayer::ToHandle(GDALDataset::FromHandle(hDS)->ExecuteSQL(
5740
0
            pszStatement, OGRGeometry::FromHandle(hSpatialFilter), pszDialect));
5741
5742
0
#ifdef OGRAPISPY_ENABLED
5743
0
    if (bOGRAPISpyEnabled)
5744
0
        OGRAPISpy_DS_ExecuteSQL(hDS, pszStatement, hSpatialFilter, pszDialect,
5745
0
                                hLayer);
5746
0
#endif
5747
5748
0
    return hLayer;
5749
0
}
5750
5751
/************************************************************************/
5752
/*                        GDALDatasetAbortSQL()                         */
5753
/************************************************************************/
5754
5755
/**
5756
 \brief Abort any SQL statement running in the data store.
5757
5758
 This function can be safely called from any thread (pending that the dataset
5759
 object is still alive). Driver implementations will make sure that it can be
5760
 called in a thread-safe way.
5761
5762
 This might not be implemented by all drivers. At time of writing, only SQLite,
5763
 GPKG and PG drivers implement it
5764
5765
 This method is the same as the C++ method GDALDataset::AbortSQL()
5766
5767
 @since GDAL 3.2.0
5768
5769
 @param hDS the dataset handle.
5770
5771
 @return OGRERR_NONE on success, or OGRERR_UNSUPPORTED_OPERATION if AbortSQL
5772
 is not supported for this datasource. .
5773
5774
*/
5775
5776
OGRErr GDALDatasetAbortSQL(GDALDatasetH hDS)
5777
5778
0
{
5779
0
    VALIDATE_POINTER1(hDS, "GDALDatasetAbortSQL", OGRERR_FAILURE);
5780
0
    return GDALDataset::FromHandle(hDS)->AbortSQL();
5781
0
}
5782
5783
/************************************************************************/
5784
/*                      GDALDatasetGetStyleTable()                      */
5785
/************************************************************************/
5786
5787
/**
5788
 \brief Returns dataset style table.
5789
5790
 This function is the same as the C++ method GDALDataset::GetStyleTable()
5791
5792
5793
 @param hDS the dataset handle
5794
 @return handle to a style table which should not be modified or freed by the
5795
 caller.
5796
*/
5797
5798
OGRStyleTableH GDALDatasetGetStyleTable(GDALDatasetH hDS)
5799
5800
0
{
5801
0
    VALIDATE_POINTER1(hDS, "OGR_DS_GetStyleTable", nullptr);
5802
5803
0
    return reinterpret_cast<OGRStyleTableH>(
5804
0
        GDALDataset::FromHandle(hDS)->GetStyleTable());
5805
0
}
5806
5807
/************************************************************************/
5808
/*                    GDALDatasetSetStyleTableDirectly()                */
5809
/************************************************************************/
5810
5811
/**
5812
 \brief Set dataset style table.
5813
5814
 This function operate exactly as GDALDatasetSetStyleTable() except that it
5815
 assumes ownership of the passed table.
5816
5817
 This function is the same as the C++ method
5818
 GDALDataset::SetStyleTableDirectly()
5819
5820
5821
 @param hDS the dataset handle
5822
 @param hStyleTable style table handle to set
5823
5824
*/
5825
5826
void GDALDatasetSetStyleTableDirectly(GDALDatasetH hDS,
5827
                                      OGRStyleTableH hStyleTable)
5828
5829
0
{
5830
0
    VALIDATE_POINTER0(hDS, "OGR_DS_SetStyleTableDirectly");
5831
5832
0
    GDALDataset::FromHandle(hDS)->SetStyleTableDirectly(
5833
0
        reinterpret_cast<OGRStyleTable *>(hStyleTable));
5834
0
}
5835
5836
/************************************************************************/
5837
/*                     GDALDatasetSetStyleTable()                       */
5838
/************************************************************************/
5839
5840
/**
5841
 \brief Set dataset style table.
5842
5843
 This function operate exactly as GDALDatasetSetStyleTableDirectly() except that
5844
 it assumes ownership of the passed table.
5845
5846
 This function is the same as the C++ method GDALDataset::SetStyleTable()
5847
5848
5849
 @param hDS the dataset handle
5850
 @param hStyleTable style table handle to set
5851
5852
*/
5853
5854
void GDALDatasetSetStyleTable(GDALDatasetH hDS, OGRStyleTableH hStyleTable)
5855
5856
0
{
5857
0
    VALIDATE_POINTER0(hDS, "OGR_DS_SetStyleTable");
5858
0
    VALIDATE_POINTER0(hStyleTable, "OGR_DS_SetStyleTable");
5859
5860
0
    GDALDataset::FromHandle(hDS)->SetStyleTable(
5861
0
        reinterpret_cast<OGRStyleTable *>(hStyleTable));
5862
0
}
5863
5864
/************************************************************************/
5865
/*                    ValidateLayerCreationOptions()                    */
5866
/************************************************************************/
5867
5868
//! @cond Doxygen_Suppress
5869
int GDALDataset::ValidateLayerCreationOptions(const char *const *papszLCO)
5870
0
{
5871
0
    const char *pszOptionList =
5872
0
        GetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST);
5873
0
    if (pszOptionList == nullptr && poDriver != nullptr)
5874
0
    {
5875
0
        pszOptionList =
5876
0
            poDriver->GetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST);
5877
0
    }
5878
0
    CPLString osDataset;
5879
0
    osDataset.Printf("dataset %s", GetDescription());
5880
0
    return GDALValidateOptions(pszOptionList, papszLCO, "layer creation option",
5881
0
                               osDataset);
5882
0
}
5883
5884
//! @endcond
5885
5886
/************************************************************************/
5887
/*                              Release()                               */
5888
/************************************************************************/
5889
5890
/**
5891
\brief Drop a reference to this dataset, and if the reference count drops to one
5892
close (destroy) the dataset.
5893
5894
This method is the same as the C function OGRReleaseDataSource().
5895
5896
@deprecated. Use GDALClose() instead
5897
5898
@return OGRERR_NONE on success or an error code.
5899
*/
5900
5901
OGRErr GDALDataset::Release()
5902
5903
0
{
5904
0
    ReleaseRef();
5905
0
    return OGRERR_NONE;
5906
0
}
5907
5908
/************************************************************************/
5909
/*                            GetRefCount()                             */
5910
/************************************************************************/
5911
5912
/**
5913
\brief Fetch reference count.
5914
5915
This method is the same as the C function OGR_DS_GetRefCount().
5916
5917
@return the current reference count for the datasource object itself.
5918
*/
5919
5920
int GDALDataset::GetRefCount() const
5921
0
{
5922
0
    return nRefCount;
5923
0
}
5924
5925
/************************************************************************/
5926
/*                         GetSummaryRefCount()                         */
5927
/************************************************************************/
5928
5929
/**
5930
\brief Fetch reference count of datasource and all owned layers.
5931
5932
This method is the same as the C function  OGR_DS_GetSummaryRefCount().
5933
5934
@deprecated
5935
5936
@return the current summary reference count for the datasource and its layers.
5937
*/
5938
5939
int GDALDataset::GetSummaryRefCount() const
5940
5941
0
{
5942
0
    CPLMutexHolderD(m_poPrivate ? &(m_poPrivate->hMutex) : nullptr);
5943
0
    int nSummaryCount = nRefCount;
5944
0
    GDALDataset *poUseThis = const_cast<GDALDataset *>(this);
5945
5946
0
    for (int iLayer = 0; iLayer < poUseThis->GetLayerCount(); ++iLayer)
5947
0
        nSummaryCount += poUseThis->GetLayer(iLayer)->GetRefCount();
5948
5949
0
    return nSummaryCount;
5950
0
}
5951
5952
/************************************************************************/
5953
/*                           ICreateLayer()                             */
5954
/************************************************************************/
5955
5956
/**
5957
 \brief This method attempts to create a new layer on the dataset with the
5958
 indicated name, coordinate system, geometry type.
5959
5960
 This method is reserved to implementation by drivers.
5961
5962
 The papszOptions argument can be used to control driver specific creation
5963
 options.  These options are normally documented in the format specific
5964
 documentation.
5965
5966
 @param pszName the name for the new layer.  This should ideally not
5967
 match any existing layer on the datasource.
5968
 @param poGeomFieldDefn the geometry field definition to use for the new layer,
5969
 or NULL if there is no geometry field.
5970
 @param papszOptions a StringList of name=value options.  Options are driver
5971
 specific.
5972
5973
 @return NULL is returned on failure, or a new OGRLayer handle on success.
5974
5975
*/
5976
5977
OGRLayer *
5978
GDALDataset::ICreateLayer(CPL_UNUSED const char *pszName,
5979
                          CPL_UNUSED const OGRGeomFieldDefn *poGeomFieldDefn,
5980
                          CPL_UNUSED CSLConstList papszOptions)
5981
5982
0
{
5983
0
    CPLError(CE_Failure, CPLE_NotSupported,
5984
0
             "CreateLayer() not supported by this dataset.");
5985
5986
0
    return nullptr;
5987
0
}
5988
5989
/************************************************************************/
5990
/*                             CopyLayer()                              */
5991
/************************************************************************/
5992
5993
/**
5994
 \brief Duplicate an existing layer.
5995
5996
 This method creates a new layer, duplicate the field definitions of the
5997
 source layer and then duplicate each features of the source layer.
5998
 The papszOptions argument
5999
 can be used to control driver specific creation options.  These options are
6000
 normally documented in the format specific documentation.
6001
 The source layer may come from another dataset.
6002
6003
 This method is the same as the C function GDALDatasetCopyLayer() and the
6004
 deprecated OGR_DS_CopyLayer().
6005
6006
 @param poSrcLayer source layer.
6007
 @param pszNewName the name of the layer to create.
6008
 @param papszOptions a StringList of name=value options.  Options are driver
6009
                     specific. There is a common option to set output layer
6010
                     spatial reference: DST_SRSWKT. The option should be in
6011
                     WKT format. Starting with GDAL 3.7, the common option
6012
                     COPY_MD can be set to NO to prevent the default copying
6013
                     of the metadata from the source layer to the target layer.
6014
6015
 @return a handle to the layer, or NULL if an error occurs.
6016
*/
6017
6018
OGRLayer *GDALDataset::CopyLayer(OGRLayer *poSrcLayer, const char *pszNewName,
6019
                                 char **papszOptions)
6020
6021
0
{
6022
    /* -------------------------------------------------------------------- */
6023
    /*      Create the layer.                                               */
6024
    /* -------------------------------------------------------------------- */
6025
0
    if (!TestCapability(ODsCCreateLayer))
6026
0
    {
6027
0
        CPLError(CE_Failure, CPLE_NotSupported,
6028
0
                 "This datasource does not support creation of layers.");
6029
0
        return nullptr;
6030
0
    }
6031
6032
0
    const char *pszSRSWKT = CSLFetchNameValue(papszOptions, "DST_SRSWKT");
6033
0
    OGRSpatialReference oDstSpaRef(pszSRSWKT);
6034
0
    oDstSpaRef.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
6035
0
    OGRFeatureDefn *poSrcDefn = poSrcLayer->GetLayerDefn();
6036
0
    OGRLayer *poDstLayer = nullptr;
6037
6038
0
    CPLStringList aosCleanedUpOptions(CSLDuplicate(papszOptions));
6039
0
    aosCleanedUpOptions.SetNameValue("DST_SRSWKT", nullptr);
6040
0
    aosCleanedUpOptions.SetNameValue("COPY_MD", nullptr);
6041
6042
0
    CPLErrorReset();
6043
0
    const int nSrcGeomFieldCount = poSrcDefn->GetGeomFieldCount();
6044
0
    if (nSrcGeomFieldCount == 1)
6045
0
    {
6046
0
        OGRGeomFieldDefn oGeomFieldDefn(poSrcDefn->GetGeomFieldDefn(0));
6047
0
        if (pszSRSWKT)
6048
0
            oGeomFieldDefn.SetSpatialRef(&oDstSpaRef);
6049
0
        poDstLayer = ICreateLayer(pszNewName, &oGeomFieldDefn,
6050
0
                                  aosCleanedUpOptions.List());
6051
0
    }
6052
0
    else
6053
0
    {
6054
0
        poDstLayer =
6055
0
            ICreateLayer(pszNewName, nullptr, aosCleanedUpOptions.List());
6056
0
    }
6057
6058
0
    if (poDstLayer == nullptr)
6059
0
        return nullptr;
6060
6061
0
    if (CPLTestBool(CSLFetchNameValueDef(papszOptions, "COPY_MD", "YES")))
6062
0
    {
6063
0
        char **papszMD = poSrcLayer->GetMetadata();
6064
0
        if (papszMD)
6065
0
            poDstLayer->SetMetadata(papszMD);
6066
0
    }
6067
6068
    /* -------------------------------------------------------------------- */
6069
    /*      Add fields.  Default to copy all fields, and make sure to       */
6070
    /*      establish a mapping between indices, rather than names, in      */
6071
    /*      case the target datasource has altered it (e.g. Shapefile       */
6072
    /*      limited to 10 char field names).                                */
6073
    /* -------------------------------------------------------------------- */
6074
0
    const int nSrcFieldCount = poSrcDefn->GetFieldCount();
6075
6076
    // Initialize the index-to-index map to -1's.
6077
0
    std::vector<int> anMap(nSrcFieldCount, -1);
6078
6079
    // Caution: At the time of writing, the MapInfo driver
6080
    // returns NULL until a field has been added.
6081
0
    OGRFeatureDefn *poDstFDefn = poDstLayer->GetLayerDefn();
6082
0
    int nDstFieldCount = poDstFDefn ? poDstFDefn->GetFieldCount() : 0;
6083
0
    for (int iField = 0; iField < nSrcFieldCount; ++iField)
6084
0
    {
6085
0
        OGRFieldDefn *poSrcFieldDefn = poSrcDefn->GetFieldDefn(iField);
6086
0
        OGRFieldDefn oFieldDefn(poSrcFieldDefn);
6087
6088
        // The field may have been already created at layer creation.
6089
0
        int iDstField = -1;
6090
0
        if (poDstFDefn)
6091
0
            iDstField = poDstFDefn->GetFieldIndex(oFieldDefn.GetNameRef());
6092
0
        if (iDstField >= 0)
6093
0
        {
6094
0
            anMap[iField] = iDstField;
6095
0
        }
6096
0
        else if (poDstLayer->CreateField(&oFieldDefn) == OGRERR_NONE)
6097
0
        {
6098
            // Now that we've created a field, GetLayerDefn() won't return NULL.
6099
0
            if (poDstFDefn == nullptr)
6100
0
                poDstFDefn = poDstLayer->GetLayerDefn();
6101
6102
            // Sanity check: if it fails, the driver is buggy.
6103
0
            if (poDstFDefn != nullptr &&
6104
0
                poDstFDefn->GetFieldCount() != nDstFieldCount + 1)
6105
0
            {
6106
0
                CPLError(CE_Warning, CPLE_AppDefined,
6107
0
                         "The output driver has claimed to have added the %s "
6108
0
                         "field, but it did not!",
6109
0
                         oFieldDefn.GetNameRef());
6110
0
            }
6111
0
            else
6112
0
            {
6113
0
                anMap[iField] = nDstFieldCount;
6114
0
                ++nDstFieldCount;
6115
0
            }
6116
0
        }
6117
0
    }
6118
6119
    /* -------------------------------------------------------------------- */
6120
0
    std::unique_ptr<OGRCoordinateTransformation> poCT;
6121
0
    const OGRSpatialReference *sourceSRS = poSrcLayer->GetSpatialRef();
6122
0
    if (sourceSRS != nullptr && pszSRSWKT != nullptr && !oDstSpaRef.IsEmpty() &&
6123
0
        sourceSRS->IsSame(&oDstSpaRef) == FALSE)
6124
0
    {
6125
0
        poCT.reset(OGRCreateCoordinateTransformation(sourceSRS, &oDstSpaRef));
6126
0
        if (nullptr == poCT)
6127
0
        {
6128
0
            CPLError(CE_Failure, CPLE_NotSupported,
6129
0
                     "This input/output spatial reference is not supported.");
6130
0
            return nullptr;
6131
0
        }
6132
0
    }
6133
    /* -------------------------------------------------------------------- */
6134
    /*      Create geometry fields.                                         */
6135
    /* -------------------------------------------------------------------- */
6136
0
    if (nSrcGeomFieldCount > 1 &&
6137
0
        TestCapability(ODsCCreateGeomFieldAfterCreateLayer))
6138
0
    {
6139
6140
0
        for (int iField = 0; iField < nSrcGeomFieldCount; ++iField)
6141
0
        {
6142
0
            if (nullptr == pszSRSWKT)
6143
0
            {
6144
0
                poDstLayer->CreateGeomField(
6145
0
                    poSrcDefn->GetGeomFieldDefn(iField));
6146
0
            }
6147
0
            else
6148
0
            {
6149
0
                OGRGeomFieldDefn *pDstGeomFieldDefn =
6150
0
                    poSrcDefn->GetGeomFieldDefn(iField);
6151
0
                pDstGeomFieldDefn->SetSpatialRef(&oDstSpaRef);
6152
0
                poDstLayer->CreateGeomField(pDstGeomFieldDefn);
6153
0
            }
6154
0
        }
6155
0
    }
6156
6157
    /* -------------------------------------------------------------------- */
6158
    /*      Check if the destination layer supports transactions and set a  */
6159
    /*      default number of features in a single transaction.             */
6160
    /* -------------------------------------------------------------------- */
6161
0
    const int nGroupTransactions =
6162
0
        poDstLayer->TestCapability(OLCTransactions) ? 128 : 0;
6163
6164
    /* -------------------------------------------------------------------- */
6165
    /*      Transfer features.                                              */
6166
    /* -------------------------------------------------------------------- */
6167
0
    poSrcLayer->ResetReading();
6168
6169
0
    if (nGroupTransactions <= 0)
6170
0
    {
6171
0
        while (true)
6172
0
        {
6173
0
            auto poFeature =
6174
0
                std::unique_ptr<OGRFeature>(poSrcLayer->GetNextFeature());
6175
6176
0
            if (poFeature == nullptr)
6177
0
                break;
6178
6179
0
            CPLErrorReset();
6180
0
            auto poDstFeature =
6181
0
                std::make_unique<OGRFeature>(poDstLayer->GetLayerDefn());
6182
6183
0
            if (poDstFeature->SetFrom(poFeature.get(), anMap.data(), TRUE) !=
6184
0
                OGRERR_NONE)
6185
0
            {
6186
0
                CPLError(CE_Failure, CPLE_AppDefined,
6187
0
                         "Unable to translate feature " CPL_FRMT_GIB
6188
0
                         " from layer %s.",
6189
0
                         poFeature->GetFID(), poSrcDefn->GetName());
6190
0
                return poDstLayer;
6191
0
            }
6192
6193
0
            if (nullptr != poCT)
6194
0
            {
6195
0
                for (int iField = 0; iField < nSrcGeomFieldCount; ++iField)
6196
0
                {
6197
0
                    OGRGeometry *pGeom = poDstFeature->GetGeomFieldRef(iField);
6198
0
                    if (nullptr == pGeom)
6199
0
                        continue;
6200
6201
0
                    const OGRErr eErr = pGeom->transform(poCT.get());
6202
0
                    if (eErr == OGRERR_NONE)
6203
0
                        continue;
6204
6205
0
                    CPLError(CE_Failure, CPLE_AppDefined,
6206
0
                             "Unable to transform geometry " CPL_FRMT_GIB
6207
0
                             " from layer %s.",
6208
0
                             poFeature->GetFID(), poSrcDefn->GetName());
6209
0
                    return poDstLayer;
6210
0
                }
6211
0
            }
6212
6213
0
            poDstFeature->SetFID(poFeature->GetFID());
6214
6215
0
            CPLErrorReset();
6216
0
            if (poDstLayer->CreateFeature(poDstFeature.get()) != OGRERR_NONE)
6217
0
            {
6218
0
                return poDstLayer;
6219
0
            }
6220
0
        }
6221
0
    }
6222
0
    else
6223
0
    {
6224
0
        std::vector<std::unique_ptr<OGRFeature>> apoDstFeatures;
6225
0
        try
6226
0
        {
6227
0
            apoDstFeatures.resize(nGroupTransactions);
6228
0
        }
6229
0
        catch (const std::exception &e)
6230
0
        {
6231
0
            CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what());
6232
0
            return poDstLayer;
6233
0
        }
6234
0
        bool bStopTransfer = false;
6235
0
        while (!bStopTransfer)
6236
0
        {
6237
            /* --------------------------------------------------------------------
6238
             */
6239
            /*      Fill the array with features. */
6240
            /* --------------------------------------------------------------------
6241
             */
6242
            // Number of features in the temporary array.
6243
0
            int nFeatCount = 0;  // Used after for.
6244
0
            for (nFeatCount = 0; nFeatCount < nGroupTransactions; ++nFeatCount)
6245
0
            {
6246
0
                auto poFeature =
6247
0
                    std::unique_ptr<OGRFeature>(poSrcLayer->GetNextFeature());
6248
6249
0
                if (poFeature == nullptr)
6250
0
                {
6251
0
                    bStopTransfer = true;
6252
0
                    break;
6253
0
                }
6254
6255
0
                CPLErrorReset();
6256
0
                apoDstFeatures[nFeatCount] =
6257
0
                    std::make_unique<OGRFeature>(poDstLayer->GetLayerDefn());
6258
6259
0
                if (apoDstFeatures[nFeatCount]->SetFrom(
6260
0
                        poFeature.get(), anMap.data(), TRUE) != OGRERR_NONE)
6261
0
                {
6262
0
                    CPLError(CE_Failure, CPLE_AppDefined,
6263
0
                             "Unable to translate feature " CPL_FRMT_GIB
6264
0
                             " from layer %s.",
6265
0
                             poFeature->GetFID(), poSrcDefn->GetName());
6266
0
                    bStopTransfer = true;
6267
0
                    poFeature.reset();
6268
0
                    break;
6269
0
                }
6270
6271
0
                if (nullptr != poCT)
6272
0
                {
6273
0
                    for (int iField = 0; iField < nSrcGeomFieldCount; ++iField)
6274
0
                    {
6275
0
                        OGRGeometry *pGeom =
6276
0
                            apoDstFeatures[nFeatCount]->GetGeomFieldRef(iField);
6277
0
                        if (nullptr == pGeom)
6278
0
                            continue;
6279
6280
0
                        const OGRErr eErr = pGeom->transform(poCT.get());
6281
0
                        if (eErr == OGRERR_NONE)
6282
0
                            continue;
6283
6284
0
                        CPLError(CE_Failure, CPLE_AppDefined,
6285
0
                                 "Unable to transform geometry " CPL_FRMT_GIB
6286
0
                                 " from layer %s.",
6287
0
                                 poFeature->GetFID(), poSrcDefn->GetName());
6288
0
                        bStopTransfer = true;
6289
0
                        poFeature.reset();
6290
0
                        break;
6291
0
                    }
6292
0
                }
6293
6294
0
                if (poFeature)
6295
0
                {
6296
0
                    apoDstFeatures[nFeatCount]->SetFID(poFeature->GetFID());
6297
0
                }
6298
0
            }
6299
6300
0
            CPLErrorReset();
6301
0
            bool bStopTransaction = false;
6302
0
            while (!bStopTransaction)
6303
0
            {
6304
0
                bStopTransaction = true;
6305
0
                if (poDstLayer->StartTransaction() != OGRERR_NONE)
6306
0
                    break;
6307
0
                for (int i = 0; i < nFeatCount; ++i)
6308
0
                {
6309
0
                    if (poDstLayer->CreateFeature(apoDstFeatures[i].get()) !=
6310
0
                        OGRERR_NONE)
6311
0
                    {
6312
0
                        bStopTransfer = true;
6313
0
                        bStopTransaction = false;
6314
0
                        break;
6315
0
                    }
6316
0
                    apoDstFeatures[i].reset();
6317
0
                }
6318
0
                if (bStopTransaction)
6319
0
                {
6320
0
                    if (poDstLayer->CommitTransaction() != OGRERR_NONE)
6321
0
                        break;
6322
0
                }
6323
0
                else
6324
0
                {
6325
0
                    poDstLayer->RollbackTransaction();
6326
0
                }
6327
0
            }
6328
0
        }
6329
0
    }
6330
6331
0
    return poDstLayer;
6332
0
}
6333
6334
/************************************************************************/
6335
/*                            DeleteLayer()                             */
6336
/************************************************************************/
6337
6338
/**
6339
 \fn GDALDataset::DeleteLayer(int)
6340
 \brief Delete the indicated layer from the datasource.
6341
6342
 If this method is supported
6343
 the ODsCDeleteLayer capability will test TRUE on the GDALDataset.
6344
6345
 This method is the same as the C function GDALDatasetDeleteLayer() and the
6346
 deprecated OGR_DS_DeleteLayer().
6347
6348
 @param iLayer the index of the layer to delete.
6349
6350
 @return OGRERR_NONE on success, or OGRERR_UNSUPPORTED_OPERATION if deleting
6351
 layers is not supported for this datasource.
6352
6353
*/
6354
6355
OGRErr GDALDataset::DeleteLayer(CPL_UNUSED int iLayer)
6356
6357
0
{
6358
0
    CPLError(CE_Failure, CPLE_NotSupported,
6359
0
             "DeleteLayer() not supported by this dataset.");
6360
6361
0
    return OGRERR_UNSUPPORTED_OPERATION;
6362
0
}
6363
6364
/************************************************************************/
6365
/*                           GetLayerByName()                           */
6366
/************************************************************************/
6367
6368
/**
6369
 \brief Fetch a layer by name.
6370
6371
 The returned layer remains owned by the
6372
 GDALDataset and should not be deleted by the application.
6373
6374
 This method is the same as the C function GDALDatasetGetLayerByName() and the
6375
 deprecated OGR_DS_GetLayerByName().
6376
6377
 @param pszName the layer name of the layer to fetch.
6378
6379
 @return the layer, or NULL if Layer is not found or an error occurs.
6380
*/
6381
6382
OGRLayer *GDALDataset::GetLayerByName(const char *pszName)
6383
6384
0
{
6385
0
    CPLMutexHolderD(m_poPrivate ? &(m_poPrivate->hMutex) : nullptr);
6386
6387
0
    if (!pszName)
6388
0
        return nullptr;
6389
6390
    // First a case sensitive check.
6391
0
    for (int i = 0; i < GetLayerCount(); ++i)
6392
0
    {
6393
0
        OGRLayer *poLayer = GetLayer(i);
6394
6395
0
        if (strcmp(pszName, poLayer->GetName()) == 0)
6396
0
            return poLayer;
6397
0
    }
6398
6399
    // Then case insensitive.
6400
0
    for (int i = 0; i < GetLayerCount(); ++i)
6401
0
    {
6402
0
        OGRLayer *poLayer = GetLayer(i);
6403
6404
0
        if (EQUAL(pszName, poLayer->GetName()))
6405
0
            return poLayer;
6406
0
    }
6407
6408
0
    return nullptr;
6409
0
}
6410
6411
//! @cond Doxygen_Suppress
6412
/************************************************************************/
6413
/*                       ProcessSQLCreateIndex()                        */
6414
/*                                                                      */
6415
/*      The correct syntax for creating an index in our dialect of      */
6416
/*      SQL is:                                                         */
6417
/*                                                                      */
6418
/*        CREATE INDEX ON <layername> USING <columnname>                */
6419
/************************************************************************/
6420
6421
OGRErr GDALDataset::ProcessSQLCreateIndex(const char *pszSQLCommand)
6422
6423
0
{
6424
0
    char **papszTokens = CSLTokenizeString(pszSQLCommand);
6425
6426
    /* -------------------------------------------------------------------- */
6427
    /*      Do some general syntax checking.                                */
6428
    /* -------------------------------------------------------------------- */
6429
0
    if (CSLCount(papszTokens) != 6 || !EQUAL(papszTokens[0], "CREATE") ||
6430
0
        !EQUAL(papszTokens[1], "INDEX") || !EQUAL(papszTokens[2], "ON") ||
6431
0
        !EQUAL(papszTokens[4], "USING"))
6432
0
    {
6433
0
        CSLDestroy(papszTokens);
6434
0
        CPLError(CE_Failure, CPLE_AppDefined,
6435
0
                 "Syntax error in CREATE INDEX command.\n"
6436
0
                 "Was '%s'\n"
6437
0
                 "Should be of form 'CREATE INDEX ON <table> USING <field>'",
6438
0
                 pszSQLCommand);
6439
0
        return OGRERR_FAILURE;
6440
0
    }
6441
6442
    /* -------------------------------------------------------------------- */
6443
    /*      Find the named layer.                                           */
6444
    /* -------------------------------------------------------------------- */
6445
0
    OGRLayer *poLayer = GetLayerByName(papszTokens[3]);
6446
0
    if (poLayer == nullptr)
6447
0
    {
6448
0
        CPLError(CE_Failure, CPLE_AppDefined,
6449
0
                 "CREATE INDEX ON failed, no such layer as `%s'.",
6450
0
                 papszTokens[3]);
6451
0
        CSLDestroy(papszTokens);
6452
0
        return OGRERR_FAILURE;
6453
0
    }
6454
6455
    /* -------------------------------------------------------------------- */
6456
    /*      Does this layer even support attribute indexes?                 */
6457
    /* -------------------------------------------------------------------- */
6458
0
    if (poLayer->GetIndex() == nullptr)
6459
0
    {
6460
0
        CPLError(CE_Failure, CPLE_AppDefined,
6461
0
                 "CREATE INDEX ON not supported by this driver.");
6462
0
        CSLDestroy(papszTokens);
6463
0
        return OGRERR_FAILURE;
6464
0
    }
6465
6466
    /* -------------------------------------------------------------------- */
6467
    /*      Find the named field.                                           */
6468
    /* -------------------------------------------------------------------- */
6469
0
    int i = poLayer->GetLayerDefn()->GetFieldIndex(papszTokens[5]);
6470
6471
0
    CSLDestroy(papszTokens);
6472
6473
0
    if (i >= poLayer->GetLayerDefn()->GetFieldCount())
6474
0
    {
6475
0
        CPLError(CE_Failure, CPLE_AppDefined, "`%s' failed, field not found.",
6476
0
                 pszSQLCommand);
6477
0
        return OGRERR_FAILURE;
6478
0
    }
6479
6480
    /* -------------------------------------------------------------------- */
6481
    /*      Attempt to create the index.                                    */
6482
    /* -------------------------------------------------------------------- */
6483
0
    OGRErr eErr = poLayer->GetIndex()->CreateIndex(i);
6484
0
    if (eErr == OGRERR_NONE)
6485
0
    {
6486
0
        eErr = poLayer->GetIndex()->IndexAllFeatures(i);
6487
0
    }
6488
0
    else
6489
0
    {
6490
0
        if (strlen(CPLGetLastErrorMsg()) == 0)
6491
0
            CPLError(CE_Failure, CPLE_AppDefined, "Cannot '%s'", pszSQLCommand);
6492
0
    }
6493
6494
0
    return eErr;
6495
0
}
6496
6497
/************************************************************************/
6498
/*                        ProcessSQLDropIndex()                         */
6499
/*                                                                      */
6500
/*      The correct syntax for dropping one or more indexes in          */
6501
/*      the OGR SQL dialect is:                                         */
6502
/*                                                                      */
6503
/*          DROP INDEX ON <layername> [USING <columnname>]              */
6504
/************************************************************************/
6505
6506
OGRErr GDALDataset::ProcessSQLDropIndex(const char *pszSQLCommand)
6507
6508
0
{
6509
0
    char **papszTokens = CSLTokenizeString(pszSQLCommand);
6510
6511
    /* -------------------------------------------------------------------- */
6512
    /*      Do some general syntax checking.                                */
6513
    /* -------------------------------------------------------------------- */
6514
0
    if ((CSLCount(papszTokens) != 4 && CSLCount(papszTokens) != 6) ||
6515
0
        !EQUAL(papszTokens[0], "DROP") || !EQUAL(papszTokens[1], "INDEX") ||
6516
0
        !EQUAL(papszTokens[2], "ON") ||
6517
0
        (CSLCount(papszTokens) == 6 && !EQUAL(papszTokens[4], "USING")))
6518
0
    {
6519
0
        CSLDestroy(papszTokens);
6520
0
        CPLError(CE_Failure, CPLE_AppDefined,
6521
0
                 "Syntax error in DROP INDEX command.\n"
6522
0
                 "Was '%s'\n"
6523
0
                 "Should be of form 'DROP INDEX ON <table> [USING <field>]'",
6524
0
                 pszSQLCommand);
6525
0
        return OGRERR_FAILURE;
6526
0
    }
6527
6528
    /* -------------------------------------------------------------------- */
6529
    /*      Find the named layer.                                           */
6530
    /* -------------------------------------------------------------------- */
6531
0
    OGRLayer *poLayer = GetLayerByName(papszTokens[3]);
6532
0
    if (poLayer == nullptr)
6533
0
    {
6534
0
        CPLError(CE_Failure, CPLE_AppDefined,
6535
0
                 "DROP INDEX ON failed, no such layer as `%s'.",
6536
0
                 papszTokens[3]);
6537
0
        CSLDestroy(papszTokens);
6538
0
        return OGRERR_FAILURE;
6539
0
    }
6540
6541
    /* -------------------------------------------------------------------- */
6542
    /*      Does this layer even support attribute indexes?                 */
6543
    /* -------------------------------------------------------------------- */
6544
0
    if (poLayer->GetIndex() == nullptr)
6545
0
    {
6546
0
        CPLError(CE_Failure, CPLE_AppDefined,
6547
0
                 "Indexes not supported by this driver.");
6548
0
        CSLDestroy(papszTokens);
6549
0
        return OGRERR_FAILURE;
6550
0
    }
6551
6552
    /* -------------------------------------------------------------------- */
6553
    /*      If we were not given a field name, drop all indexes.            */
6554
    /* -------------------------------------------------------------------- */
6555
0
    if (CSLCount(papszTokens) == 4)
6556
0
    {
6557
0
        for (int i = 0; i < poLayer->GetLayerDefn()->GetFieldCount(); ++i)
6558
0
        {
6559
0
            OGRAttrIndex *poAttrIndex;
6560
6561
0
            poAttrIndex = poLayer->GetIndex()->GetFieldIndex(i);
6562
0
            if (poAttrIndex != nullptr)
6563
0
            {
6564
0
                const OGRErr eErr = poLayer->GetIndex()->DropIndex(i);
6565
0
                if (eErr != OGRERR_NONE)
6566
0
                {
6567
0
                    CSLDestroy(papszTokens);
6568
0
                    return eErr;
6569
0
                }
6570
0
            }
6571
0
        }
6572
6573
0
        CSLDestroy(papszTokens);
6574
0
        return OGRERR_NONE;
6575
0
    }
6576
6577
    /* -------------------------------------------------------------------- */
6578
    /*      Find the named field.                                           */
6579
    /* -------------------------------------------------------------------- */
6580
0
    int i = poLayer->GetLayerDefn()->GetFieldIndex(papszTokens[5]);
6581
0
    CSLDestroy(papszTokens);
6582
6583
0
    if (i >= poLayer->GetLayerDefn()->GetFieldCount())
6584
0
    {
6585
0
        CPLError(CE_Failure, CPLE_AppDefined, "`%s' failed, field not found.",
6586
0
                 pszSQLCommand);
6587
0
        return OGRERR_FAILURE;
6588
0
    }
6589
6590
    /* -------------------------------------------------------------------- */
6591
    /*      Attempt to drop the index.                                      */
6592
    /* -------------------------------------------------------------------- */
6593
0
    const OGRErr eErr = poLayer->GetIndex()->DropIndex(i);
6594
6595
0
    return eErr;
6596
0
}
6597
6598
/************************************************************************/
6599
/*                        ProcessSQLDropTable()                         */
6600
/*                                                                      */
6601
/*      The correct syntax for dropping a table (layer) in the OGR SQL  */
6602
/*      dialect is:                                                     */
6603
/*                                                                      */
6604
/*          DROP TABLE <layername>                                      */
6605
/************************************************************************/
6606
6607
OGRErr GDALDataset::ProcessSQLDropTable(const char *pszSQLCommand)
6608
6609
0
{
6610
0
    char **papszTokens = CSLTokenizeString(pszSQLCommand);
6611
6612
    /* -------------------------------------------------------------------- */
6613
    /*      Do some general syntax checking.                                */
6614
    /* -------------------------------------------------------------------- */
6615
0
    if (CSLCount(papszTokens) != 3 || !EQUAL(papszTokens[0], "DROP") ||
6616
0
        !EQUAL(papszTokens[1], "TABLE"))
6617
0
    {
6618
0
        CSLDestroy(papszTokens);
6619
0
        CPLError(CE_Failure, CPLE_AppDefined,
6620
0
                 "Syntax error in DROP TABLE command.\n"
6621
0
                 "Was '%s'\n"
6622
0
                 "Should be of form 'DROP TABLE <table>'",
6623
0
                 pszSQLCommand);
6624
0
        return OGRERR_FAILURE;
6625
0
    }
6626
6627
    /* -------------------------------------------------------------------- */
6628
    /*      Find the named layer.                                           */
6629
    /* -------------------------------------------------------------------- */
6630
0
    OGRLayer *poLayer = nullptr;
6631
6632
0
    int i = 0;  // Used after for.
6633
0
    for (; i < GetLayerCount(); ++i)
6634
0
    {
6635
0
        poLayer = GetLayer(i);
6636
6637
0
        if (poLayer != nullptr && EQUAL(poLayer->GetName(), papszTokens[2]))
6638
0
            break;
6639
0
        poLayer = nullptr;
6640
0
    }
6641
6642
0
    if (poLayer == nullptr)
6643
0
    {
6644
0
        CPLError(CE_Failure, CPLE_AppDefined,
6645
0
                 "DROP TABLE failed, no such layer as `%s'.", papszTokens[2]);
6646
0
        CSLDestroy(papszTokens);
6647
0
        return OGRERR_FAILURE;
6648
0
    }
6649
6650
0
    CSLDestroy(papszTokens);
6651
6652
    /* -------------------------------------------------------------------- */
6653
    /*      Delete it.                                                      */
6654
    /* -------------------------------------------------------------------- */
6655
6656
0
    return DeleteLayer(i);
6657
0
}
6658
6659
//! @endcond
6660
6661
/************************************************************************/
6662
/*                    GDALDatasetParseSQLType()                       */
6663
/************************************************************************/
6664
6665
/* All arguments will be altered */
6666
static OGRFieldType GDALDatasetParseSQLType(char *pszType, int &nWidth,
6667
                                            int &nPrecision)
6668
0
{
6669
0
    char *pszParenthesis = strchr(pszType, '(');
6670
0
    if (pszParenthesis)
6671
0
    {
6672
0
        nWidth = atoi(pszParenthesis + 1);
6673
0
        *pszParenthesis = '\0';
6674
0
        char *pszComma = strchr(pszParenthesis + 1, ',');
6675
0
        if (pszComma)
6676
0
            nPrecision = atoi(pszComma + 1);
6677
0
    }
6678
6679
0
    OGRFieldType eType = OFTString;
6680
0
    if (EQUAL(pszType, "INTEGER"))
6681
0
        eType = OFTInteger;
6682
0
    else if (EQUAL(pszType, "INTEGER[]"))
6683
0
        eType = OFTIntegerList;
6684
0
    else if (EQUAL(pszType, "FLOAT") || EQUAL(pszType, "NUMERIC") ||
6685
0
             EQUAL(pszType, "DOUBLE") /* unofficial alias */ ||
6686
0
             EQUAL(pszType, "REAL") /* unofficial alias */)
6687
0
        eType = OFTReal;
6688
0
    else if (EQUAL(pszType, "FLOAT[]") || EQUAL(pszType, "NUMERIC[]") ||
6689
0
             EQUAL(pszType, "DOUBLE[]") /* unofficial alias */ ||
6690
0
             EQUAL(pszType, "REAL[]") /* unofficial alias */)
6691
0
        eType = OFTRealList;
6692
0
    else if (EQUAL(pszType, "CHARACTER") ||
6693
0
             EQUAL(pszType, "TEXT") /* unofficial alias */ ||
6694
0
             EQUAL(pszType, "STRING") /* unofficial alias */ ||
6695
0
             EQUAL(pszType, "VARCHAR") /* unofficial alias */)
6696
0
        eType = OFTString;
6697
0
    else if (EQUAL(pszType, "TEXT[]") ||
6698
0
             EQUAL(pszType, "STRING[]") /* unofficial alias */ ||
6699
0
             EQUAL(pszType, "VARCHAR[]") /* unofficial alias */)
6700
0
        eType = OFTStringList;
6701
0
    else if (EQUAL(pszType, "DATE"))
6702
0
        eType = OFTDate;
6703
0
    else if (EQUAL(pszType, "TIME"))
6704
0
        eType = OFTTime;
6705
0
    else if (EQUAL(pszType, "TIMESTAMP") ||
6706
0
             EQUAL(pszType, "DATETIME") /* unofficial alias */)
6707
0
        eType = OFTDateTime;
6708
0
    else
6709
0
        CPLError(CE_Warning, CPLE_NotSupported,
6710
0
                 "Unsupported column type '%s'. Defaulting to VARCHAR",
6711
0
                 pszType);
6712
6713
0
    return eType;
6714
0
}
6715
6716
/************************************************************************/
6717
/*                    ProcessSQLAlterTableAddColumn()                   */
6718
/*                                                                      */
6719
/*      The correct syntax for adding a column in the OGR SQL           */
6720
/*      dialect is:                                                     */
6721
/*                                                                      */
6722
/*       ALTER TABLE <layername> ADD [COLUMN] <columnname> <columntype> */
6723
/************************************************************************/
6724
6725
//! @cond Doxygen_Suppress
6726
OGRErr GDALDataset::ProcessSQLAlterTableAddColumn(const char *pszSQLCommand)
6727
6728
0
{
6729
0
    char **papszTokens = CSLTokenizeString(pszSQLCommand);
6730
6731
    /* -------------------------------------------------------------------- */
6732
    /*      Do some general syntax checking.                                */
6733
    /* -------------------------------------------------------------------- */
6734
0
    const char *pszLayerName = nullptr;
6735
0
    const char *pszColumnName = nullptr;
6736
0
    int iTypeIndex = 0;
6737
0
    const int nTokens = CSLCount(papszTokens);
6738
6739
0
    if (nTokens >= 7 && EQUAL(papszTokens[0], "ALTER") &&
6740
0
        EQUAL(papszTokens[1], "TABLE") && EQUAL(papszTokens[3], "ADD") &&
6741
0
        EQUAL(papszTokens[4], "COLUMN"))
6742
0
    {
6743
0
        pszLayerName = papszTokens[2];
6744
0
        pszColumnName = papszTokens[5];
6745
0
        iTypeIndex = 6;
6746
0
    }
6747
0
    else if (nTokens >= 6 && EQUAL(papszTokens[0], "ALTER") &&
6748
0
             EQUAL(papszTokens[1], "TABLE") && EQUAL(papszTokens[3], "ADD"))
6749
0
    {
6750
0
        pszLayerName = papszTokens[2];
6751
0
        pszColumnName = papszTokens[4];
6752
0
        iTypeIndex = 5;
6753
0
    }
6754
0
    else
6755
0
    {
6756
0
        CSLDestroy(papszTokens);
6757
0
        CPLError(CE_Failure, CPLE_AppDefined,
6758
0
                 "Syntax error in ALTER TABLE ADD COLUMN command.\n"
6759
0
                 "Was '%s'\n"
6760
0
                 "Should be of form 'ALTER TABLE <layername> ADD [COLUMN] "
6761
0
                 "<columnname> <columntype>'",
6762
0
                 pszSQLCommand);
6763
0
        return OGRERR_FAILURE;
6764
0
    }
6765
6766
    /* -------------------------------------------------------------------- */
6767
    /*      Merge type components into a single string if there were split  */
6768
    /*      with spaces                                                     */
6769
    /* -------------------------------------------------------------------- */
6770
0
    CPLString osType;
6771
0
    for (int i = iTypeIndex; i < nTokens; ++i)
6772
0
    {
6773
0
        osType += papszTokens[i];
6774
0
        CPLFree(papszTokens[i]);
6775
0
    }
6776
0
    char *pszType = papszTokens[iTypeIndex] = CPLStrdup(osType);
6777
0
    papszTokens[iTypeIndex + 1] = nullptr;
6778
6779
    /* -------------------------------------------------------------------- */
6780
    /*      Find the named layer.                                           */
6781
    /* -------------------------------------------------------------------- */
6782
0
    OGRLayer *poLayer = GetLayerByName(pszLayerName);
6783
0
    if (poLayer == nullptr)
6784
0
    {
6785
0
        CPLError(CE_Failure, CPLE_AppDefined,
6786
0
                 "%s failed, no such layer as `%s'.", pszSQLCommand,
6787
0
                 pszLayerName);
6788
0
        CSLDestroy(papszTokens);
6789
0
        return OGRERR_FAILURE;
6790
0
    }
6791
6792
    /* -------------------------------------------------------------------- */
6793
    /*      Add column.                                                     */
6794
    /* -------------------------------------------------------------------- */
6795
6796
0
    int nWidth = 0;
6797
0
    int nPrecision = 0;
6798
0
    OGRFieldType eType = GDALDatasetParseSQLType(pszType, nWidth, nPrecision);
6799
0
    OGRFieldDefn oFieldDefn(pszColumnName, eType);
6800
0
    oFieldDefn.SetWidth(nWidth);
6801
0
    oFieldDefn.SetPrecision(nPrecision);
6802
6803
0
    CSLDestroy(papszTokens);
6804
6805
0
    return poLayer->CreateField(&oFieldDefn);
6806
0
}
6807
6808
/************************************************************************/
6809
/*                    ProcessSQLAlterTableDropColumn()                  */
6810
/*                                                                      */
6811
/*      The correct syntax for dropping a column in the OGR SQL         */
6812
/*      dialect is:                                                     */
6813
/*                                                                      */
6814
/*          ALTER TABLE <layername> DROP [COLUMN] <columnname>          */
6815
/************************************************************************/
6816
6817
OGRErr GDALDataset::ProcessSQLAlterTableDropColumn(const char *pszSQLCommand)
6818
6819
0
{
6820
0
    char **papszTokens = CSLTokenizeString(pszSQLCommand);
6821
6822
    /* -------------------------------------------------------------------- */
6823
    /*      Do some general syntax checking.                                */
6824
    /* -------------------------------------------------------------------- */
6825
0
    const char *pszLayerName = nullptr;
6826
0
    const char *pszColumnName = nullptr;
6827
0
    if (CSLCount(papszTokens) == 6 && EQUAL(papszTokens[0], "ALTER") &&
6828
0
        EQUAL(papszTokens[1], "TABLE") && EQUAL(papszTokens[3], "DROP") &&
6829
0
        EQUAL(papszTokens[4], "COLUMN"))
6830
0
    {
6831
0
        pszLayerName = papszTokens[2];
6832
0
        pszColumnName = papszTokens[5];
6833
0
    }
6834
0
    else if (CSLCount(papszTokens) == 5 && EQUAL(papszTokens[0], "ALTER") &&
6835
0
             EQUAL(papszTokens[1], "TABLE") && EQUAL(papszTokens[3], "DROP"))
6836
0
    {
6837
0
        pszLayerName = papszTokens[2];
6838
0
        pszColumnName = papszTokens[4];
6839
0
    }
6840
0
    else
6841
0
    {
6842
0
        CSLDestroy(papszTokens);
6843
0
        CPLError(CE_Failure, CPLE_AppDefined,
6844
0
                 "Syntax error in ALTER TABLE DROP COLUMN command.\n"
6845
0
                 "Was '%s'\n"
6846
0
                 "Should be of form 'ALTER TABLE <layername> DROP [COLUMN] "
6847
0
                 "<columnname>'",
6848
0
                 pszSQLCommand);
6849
0
        return OGRERR_FAILURE;
6850
0
    }
6851
6852
    /* -------------------------------------------------------------------- */
6853
    /*      Find the named layer.                                           */
6854
    /* -------------------------------------------------------------------- */
6855
0
    OGRLayer *poLayer = GetLayerByName(pszLayerName);
6856
0
    if (poLayer == nullptr)
6857
0
    {
6858
0
        CPLError(CE_Failure, CPLE_AppDefined,
6859
0
                 "%s failed, no such layer as `%s'.", pszSQLCommand,
6860
0
                 pszLayerName);
6861
0
        CSLDestroy(papszTokens);
6862
0
        return OGRERR_FAILURE;
6863
0
    }
6864
6865
    /* -------------------------------------------------------------------- */
6866
    /*      Find the field.                                                 */
6867
    /* -------------------------------------------------------------------- */
6868
6869
0
    int nFieldIndex = poLayer->GetLayerDefn()->GetFieldIndex(pszColumnName);
6870
0
    if (nFieldIndex < 0)
6871
0
    {
6872
0
        CPLError(CE_Failure, CPLE_AppDefined,
6873
0
                 "%s failed, no such field as `%s'.", pszSQLCommand,
6874
0
                 pszColumnName);
6875
0
        CSLDestroy(papszTokens);
6876
0
        return OGRERR_FAILURE;
6877
0
    }
6878
6879
    /* -------------------------------------------------------------------- */
6880
    /*      Remove it.                                                      */
6881
    /* -------------------------------------------------------------------- */
6882
6883
0
    CSLDestroy(papszTokens);
6884
6885
0
    return poLayer->DeleteField(nFieldIndex);
6886
0
}
6887
6888
/************************************************************************/
6889
/*                 ProcessSQLAlterTableRenameColumn()                   */
6890
/*                                                                      */
6891
/*      The correct syntax for renaming a column in the OGR SQL         */
6892
/*      dialect is:                                                     */
6893
/*                                                                      */
6894
/*       ALTER TABLE <layername> RENAME [COLUMN] <oldname> TO <newname> */
6895
/************************************************************************/
6896
6897
OGRErr GDALDataset::ProcessSQLAlterTableRenameColumn(const char *pszSQLCommand)
6898
6899
0
{
6900
0
    char **papszTokens = CSLTokenizeString(pszSQLCommand);
6901
6902
    /* -------------------------------------------------------------------- */
6903
    /*      Do some general syntax checking.                                */
6904
    /* -------------------------------------------------------------------- */
6905
0
    const char *pszLayerName = nullptr;
6906
0
    const char *pszOldColName = nullptr;
6907
0
    const char *pszNewColName = nullptr;
6908
0
    if (CSLCount(papszTokens) == 8 && EQUAL(papszTokens[0], "ALTER") &&
6909
0
        EQUAL(papszTokens[1], "TABLE") && EQUAL(papszTokens[3], "RENAME") &&
6910
0
        EQUAL(papszTokens[4], "COLUMN") && EQUAL(papszTokens[6], "TO"))
6911
0
    {
6912
0
        pszLayerName = papszTokens[2];
6913
0
        pszOldColName = papszTokens[5];
6914
0
        pszNewColName = papszTokens[7];
6915
0
    }
6916
0
    else if (CSLCount(papszTokens) == 7 && EQUAL(papszTokens[0], "ALTER") &&
6917
0
             EQUAL(papszTokens[1], "TABLE") &&
6918
0
             EQUAL(papszTokens[3], "RENAME") && EQUAL(papszTokens[5], "TO"))
6919
0
    {
6920
0
        pszLayerName = papszTokens[2];
6921
0
        pszOldColName = papszTokens[4];
6922
0
        pszNewColName = papszTokens[6];
6923
0
    }
6924
0
    else
6925
0
    {
6926
0
        CSLDestroy(papszTokens);
6927
0
        CPLError(CE_Failure, CPLE_AppDefined,
6928
0
                 "Syntax error in ALTER TABLE RENAME COLUMN command.\n"
6929
0
                 "Was '%s'\n"
6930
0
                 "Should be of form 'ALTER TABLE <layername> RENAME [COLUMN] "
6931
0
                 "<columnname> TO <newname>'",
6932
0
                 pszSQLCommand);
6933
0
        return OGRERR_FAILURE;
6934
0
    }
6935
6936
    /* -------------------------------------------------------------------- */
6937
    /*      Find the named layer.                                           */
6938
    /* -------------------------------------------------------------------- */
6939
0
    OGRLayer *poLayer = GetLayerByName(pszLayerName);
6940
0
    if (poLayer == nullptr)
6941
0
    {
6942
0
        CPLError(CE_Failure, CPLE_AppDefined,
6943
0
                 "%s failed, no such layer as `%s'.", pszSQLCommand,
6944
0
                 pszLayerName);
6945
0
        CSLDestroy(papszTokens);
6946
0
        return OGRERR_FAILURE;
6947
0
    }
6948
6949
    /* -------------------------------------------------------------------- */
6950
    /*      Find the field.                                                 */
6951
    /* -------------------------------------------------------------------- */
6952
6953
0
    const int nFieldIndex =
6954
0
        poLayer->GetLayerDefn()->GetFieldIndex(pszOldColName);
6955
0
    if (nFieldIndex < 0)
6956
0
    {
6957
0
        CPLError(CE_Failure, CPLE_AppDefined,
6958
0
                 "%s failed, no such field as `%s'.", pszSQLCommand,
6959
0
                 pszOldColName);
6960
0
        CSLDestroy(papszTokens);
6961
0
        return OGRERR_FAILURE;
6962
0
    }
6963
6964
    /* -------------------------------------------------------------------- */
6965
    /*      Rename column.                                                  */
6966
    /* -------------------------------------------------------------------- */
6967
0
    OGRFieldDefn *poOldFieldDefn =
6968
0
        poLayer->GetLayerDefn()->GetFieldDefn(nFieldIndex);
6969
0
    OGRFieldDefn oNewFieldDefn(poOldFieldDefn);
6970
0
    oNewFieldDefn.SetName(pszNewColName);
6971
6972
0
    CSLDestroy(papszTokens);
6973
6974
0
    return poLayer->AlterFieldDefn(nFieldIndex, &oNewFieldDefn,
6975
0
                                   ALTER_NAME_FLAG);
6976
0
}
6977
6978
/************************************************************************/
6979
/*                 ProcessSQLAlterTableAlterColumn()                    */
6980
/*                                                                      */
6981
/*      The correct syntax for altering the type of a column in the     */
6982
/*      OGR SQL dialect is:                                             */
6983
/*                                                                      */
6984
/*   ALTER TABLE <layername> ALTER [COLUMN] <columnname> TYPE <newtype> */
6985
/************************************************************************/
6986
6987
OGRErr GDALDataset::ProcessSQLAlterTableAlterColumn(const char *pszSQLCommand)
6988
6989
0
{
6990
0
    char **papszTokens = CSLTokenizeString(pszSQLCommand);
6991
6992
    /* -------------------------------------------------------------------- */
6993
    /*      Do some general syntax checking.                                */
6994
    /* -------------------------------------------------------------------- */
6995
0
    const char *pszLayerName = nullptr;
6996
0
    const char *pszColumnName = nullptr;
6997
0
    int iTypeIndex = 0;
6998
0
    const int nTokens = CSLCount(papszTokens);
6999
7000
0
    if (nTokens >= 8 && EQUAL(papszTokens[0], "ALTER") &&
7001
0
        EQUAL(papszTokens[1], "TABLE") && EQUAL(papszTokens[3], "ALTER") &&
7002
0
        EQUAL(papszTokens[4], "COLUMN") && EQUAL(papszTokens[6], "TYPE"))
7003
0
    {
7004
0
        pszLayerName = papszTokens[2];
7005
0
        pszColumnName = papszTokens[5];
7006
0
        iTypeIndex = 7;
7007
0
    }
7008
0
    else if (nTokens >= 7 && EQUAL(papszTokens[0], "ALTER") &&
7009
0
             EQUAL(papszTokens[1], "TABLE") && EQUAL(papszTokens[3], "ALTER") &&
7010
0
             EQUAL(papszTokens[5], "TYPE"))
7011
0
    {
7012
0
        pszLayerName = papszTokens[2];
7013
0
        pszColumnName = papszTokens[4];
7014
0
        iTypeIndex = 6;
7015
0
    }
7016
0
    else
7017
0
    {
7018
0
        CSLDestroy(papszTokens);
7019
0
        CPLError(CE_Failure, CPLE_AppDefined,
7020
0
                 "Syntax error in ALTER TABLE ALTER COLUMN command.\n"
7021
0
                 "Was '%s'\n"
7022
0
                 "Should be of form 'ALTER TABLE <layername> ALTER [COLUMN] "
7023
0
                 "<columnname> TYPE <columntype>'",
7024
0
                 pszSQLCommand);
7025
0
        return OGRERR_FAILURE;
7026
0
    }
7027
7028
    /* -------------------------------------------------------------------- */
7029
    /*      Merge type components into a single string if there were split  */
7030
    /*      with spaces                                                     */
7031
    /* -------------------------------------------------------------------- */
7032
0
    CPLString osType;
7033
0
    for (int i = iTypeIndex; i < nTokens; ++i)
7034
0
    {
7035
0
        osType += papszTokens[i];
7036
0
        CPLFree(papszTokens[i]);
7037
0
    }
7038
0
    char *pszType = papszTokens[iTypeIndex] = CPLStrdup(osType);
7039
0
    papszTokens[iTypeIndex + 1] = nullptr;
7040
7041
    /* -------------------------------------------------------------------- */
7042
    /*      Find the named layer.                                           */
7043
    /* -------------------------------------------------------------------- */
7044
0
    OGRLayer *poLayer = GetLayerByName(pszLayerName);
7045
0
    if (poLayer == nullptr)
7046
0
    {
7047
0
        CPLError(CE_Failure, CPLE_AppDefined,
7048
0
                 "%s failed, no such layer as `%s'.", pszSQLCommand,
7049
0
                 pszLayerName);
7050
0
        CSLDestroy(papszTokens);
7051
0
        return OGRERR_FAILURE;
7052
0
    }
7053
7054
    /* -------------------------------------------------------------------- */
7055
    /*      Find the field.                                                 */
7056
    /* -------------------------------------------------------------------- */
7057
7058
0
    const int nFieldIndex =
7059
0
        poLayer->GetLayerDefn()->GetFieldIndex(pszColumnName);
7060
0
    if (nFieldIndex < 0)
7061
0
    {
7062
0
        CPLError(CE_Failure, CPLE_AppDefined,
7063
0
                 "%s failed, no such field as `%s'.", pszSQLCommand,
7064
0
                 pszColumnName);
7065
0
        CSLDestroy(papszTokens);
7066
0
        return OGRERR_FAILURE;
7067
0
    }
7068
7069
    /* -------------------------------------------------------------------- */
7070
    /*      Alter column.                                                   */
7071
    /* -------------------------------------------------------------------- */
7072
7073
0
    OGRFieldDefn *poOldFieldDefn =
7074
0
        poLayer->GetLayerDefn()->GetFieldDefn(nFieldIndex);
7075
0
    OGRFieldDefn oNewFieldDefn(poOldFieldDefn);
7076
7077
0
    int nWidth = 0;
7078
0
    int nPrecision = 0;
7079
0
    OGRFieldType eType = GDALDatasetParseSQLType(pszType, nWidth, nPrecision);
7080
0
    oNewFieldDefn.SetType(eType);
7081
0
    oNewFieldDefn.SetWidth(nWidth);
7082
0
    oNewFieldDefn.SetPrecision(nPrecision);
7083
7084
0
    int l_nFlags = 0;
7085
0
    if (poOldFieldDefn->GetType() != oNewFieldDefn.GetType())
7086
0
        l_nFlags |= ALTER_TYPE_FLAG;
7087
0
    if (poOldFieldDefn->GetWidth() != oNewFieldDefn.GetWidth() ||
7088
0
        poOldFieldDefn->GetPrecision() != oNewFieldDefn.GetPrecision())
7089
0
        l_nFlags |= ALTER_WIDTH_PRECISION_FLAG;
7090
7091
0
    CSLDestroy(papszTokens);
7092
7093
0
    if (l_nFlags == 0)
7094
0
        return OGRERR_NONE;
7095
7096
0
    return poLayer->AlterFieldDefn(nFieldIndex, &oNewFieldDefn, l_nFlags);
7097
0
}
7098
7099
//! @endcond
7100
7101
/************************************************************************/
7102
/*                             ExecuteSQL()                             */
7103
/************************************************************************/
7104
7105
/**
7106
 \brief Execute an SQL statement against the data store.
7107
7108
 The result of an SQL query is either NULL for statements that are in error,
7109
 or that have no results set, or an OGRLayer pointer representing a results
7110
 set from the query.  Note that this OGRLayer is in addition to the layers
7111
 in the data store and must be destroyed with
7112
 ReleaseResultSet() before the dataset is closed
7113
 (destroyed).
7114
7115
 This method is the same as the C function GDALDatasetExecuteSQL() and the
7116
 deprecated OGR_DS_ExecuteSQL().
7117
7118
 For more information on the SQL dialect supported internally by OGR
7119
 review the <a href="https://gdal.org/user/ogr_sql_dialect.html">OGR SQL</a>
7120
 document.  Some drivers (i.e. Oracle and PostGIS) pass the SQL directly through
7121
 to the underlying RDBMS.
7122
7123
 Starting with OGR 1.10, the <a
7124
 href="https://gdal.org/user/sql_sqlite_dialect.html">SQLITE dialect</a> can
7125
 also be used.
7126
7127
 @param pszStatement the SQL statement to execute.
7128
 @param poSpatialFilter geometry which represents a spatial filter. Can be NULL.
7129
 @param pszDialect allows control of the statement dialect. If set to NULL, the
7130
 OGR SQL engine will be used, except for RDBMS drivers that will use their
7131
 dedicated SQL engine, unless OGRSQL is explicitly passed as the
7132
 dialect. Starting with OGR 1.10, the SQLITE dialect can also be used.
7133
7134
 @return an OGRLayer containing the results of the query.  Deallocate with
7135
 ReleaseResultSet().
7136
7137
*/
7138
7139
OGRLayer *GDALDataset::ExecuteSQL(const char *pszStatement,
7140
                                  OGRGeometry *poSpatialFilter,
7141
                                  const char *pszDialect)
7142
7143
0
{
7144
0
    return ExecuteSQL(pszStatement, poSpatialFilter, pszDialect, nullptr);
7145
0
}
7146
7147
//! @cond Doxygen_Suppress
7148
OGRLayer *
7149
GDALDataset::ExecuteSQL(const char *pszStatement, OGRGeometry *poSpatialFilter,
7150
                        const char *pszDialect,
7151
                        swq_select_parse_options *poSelectParseOptions)
7152
7153
0
{
7154
0
    if (pszDialect != nullptr && EQUAL(pszDialect, "SQLite"))
7155
0
    {
7156
#ifdef SQLITE_ENABLED
7157
        return OGRSQLiteExecuteSQL(this, pszStatement, poSpatialFilter,
7158
                                   pszDialect);
7159
#else
7160
0
        CPLError(CE_Failure, CPLE_NotSupported,
7161
0
                 "The SQLite driver needs to be compiled to support the "
7162
0
                 "SQLite SQL dialect");
7163
0
        return nullptr;
7164
0
#endif
7165
0
    }
7166
7167
0
    if (pszDialect != nullptr && !EQUAL(pszDialect, "") &&
7168
0
        !EQUAL(pszDialect, "OGRSQL"))
7169
0
    {
7170
0
        std::string osDialectList = "'OGRSQL'";
7171
#ifdef SQLITE_ENABLED
7172
        osDialectList += ", 'SQLITE'";
7173
#endif
7174
0
        const char *pszDialects =
7175
0
            GetMetadataItem(GDAL_DMD_SUPPORTED_SQL_DIALECTS);
7176
0
        if (pszDialects)
7177
0
        {
7178
0
            const CPLStringList aosTokens(
7179
0
                CSLTokenizeString2(pszDialects, " ", 0));
7180
0
            for (int i = 0; i < aosTokens.size(); ++i)
7181
0
            {
7182
0
                if (!EQUAL(aosTokens[i], "OGRSQL") &&
7183
0
                    !EQUAL(aosTokens[i], "SQLITE"))
7184
0
                {
7185
0
                    osDialectList += ", '";
7186
0
                    osDialectList += aosTokens[i];
7187
0
                    osDialectList += "'";
7188
0
                }
7189
0
            }
7190
0
        }
7191
0
        CPLError(CE_Warning, CPLE_NotSupported,
7192
0
                 "Dialect '%s' is unsupported. Only supported dialects are %s. "
7193
0
                 "Defaulting to OGRSQL",
7194
0
                 pszDialect, osDialectList.c_str());
7195
0
    }
7196
7197
    /* -------------------------------------------------------------------- */
7198
    /*      Handle CREATE INDEX statements specially.                       */
7199
    /* -------------------------------------------------------------------- */
7200
0
    if (STARTS_WITH_CI(pszStatement, "CREATE INDEX"))
7201
0
    {
7202
0
        ProcessSQLCreateIndex(pszStatement);
7203
0
        return nullptr;
7204
0
    }
7205
7206
    /* -------------------------------------------------------------------- */
7207
    /*      Handle DROP INDEX statements specially.                         */
7208
    /* -------------------------------------------------------------------- */
7209
0
    if (STARTS_WITH_CI(pszStatement, "DROP INDEX"))
7210
0
    {
7211
0
        ProcessSQLDropIndex(pszStatement);
7212
0
        return nullptr;
7213
0
    }
7214
7215
    /* -------------------------------------------------------------------- */
7216
    /*      Handle DROP TABLE statements specially.                         */
7217
    /* -------------------------------------------------------------------- */
7218
0
    if (STARTS_WITH_CI(pszStatement, "DROP TABLE"))
7219
0
    {
7220
0
        ProcessSQLDropTable(pszStatement);
7221
0
        return nullptr;
7222
0
    }
7223
7224
    /* -------------------------------------------------------------------- */
7225
    /*      Handle ALTER TABLE statements specially.                        */
7226
    /* -------------------------------------------------------------------- */
7227
0
    if (STARTS_WITH_CI(pszStatement, "ALTER TABLE"))
7228
0
    {
7229
0
        char **papszTokens = CSLTokenizeString(pszStatement);
7230
0
        const int nTokens = CSLCount(papszTokens);
7231
0
        if (nTokens >= 4 && EQUAL(papszTokens[3], "ADD"))
7232
0
        {
7233
0
            ProcessSQLAlterTableAddColumn(pszStatement);
7234
0
            CSLDestroy(papszTokens);
7235
0
            return nullptr;
7236
0
        }
7237
0
        else if (nTokens >= 4 && EQUAL(papszTokens[3], "DROP"))
7238
0
        {
7239
0
            ProcessSQLAlterTableDropColumn(pszStatement);
7240
0
            CSLDestroy(papszTokens);
7241
0
            return nullptr;
7242
0
        }
7243
0
        else if (nTokens == 6 && EQUAL(papszTokens[3], "RENAME") &&
7244
0
                 EQUAL(papszTokens[4], "TO"))
7245
0
        {
7246
0
            const char *pszSrcTableName = papszTokens[2];
7247
0
            const char *pszDstTableName = papszTokens[5];
7248
0
            auto poSrcLayer = GetLayerByName(pszSrcTableName);
7249
0
            if (poSrcLayer)
7250
0
            {
7251
0
                CPL_IGNORE_RET_VAL(poSrcLayer->Rename(pszDstTableName));
7252
0
            }
7253
0
            else
7254
0
            {
7255
0
                CPLError(CE_Failure, CPLE_AppDefined, "Invalid layer name");
7256
0
            }
7257
0
            CSLDestroy(papszTokens);
7258
0
            return nullptr;
7259
0
        }
7260
0
        else if (nTokens >= 4 && EQUAL(papszTokens[3], "RENAME"))
7261
0
        {
7262
0
            ProcessSQLAlterTableRenameColumn(pszStatement);
7263
0
            CSLDestroy(papszTokens);
7264
0
            return nullptr;
7265
0
        }
7266
0
        else if (nTokens >= 4 && EQUAL(papszTokens[3], "ALTER"))
7267
0
        {
7268
0
            ProcessSQLAlterTableAlterColumn(pszStatement);
7269
0
            CSLDestroy(papszTokens);
7270
0
            return nullptr;
7271
0
        }
7272
0
        else
7273
0
        {
7274
0
            CPLError(CE_Failure, CPLE_AppDefined,
7275
0
                     "Unsupported ALTER TABLE command : %s", pszStatement);
7276
0
            CSLDestroy(papszTokens);
7277
0
            return nullptr;
7278
0
        }
7279
0
    }
7280
7281
    /* -------------------------------------------------------------------- */
7282
    /*      Preparse the SQL statement.                                     */
7283
    /* -------------------------------------------------------------------- */
7284
0
    swq_select *psSelectInfo = new swq_select();
7285
0
    swq_custom_func_registrar *poCustomFuncRegistrar = nullptr;
7286
0
    if (poSelectParseOptions != nullptr)
7287
0
        poCustomFuncRegistrar = poSelectParseOptions->poCustomFuncRegistrar;
7288
0
    if (psSelectInfo->preparse(pszStatement,
7289
0
                               poCustomFuncRegistrar != nullptr) != CE_None)
7290
0
    {
7291
0
        delete psSelectInfo;
7292
0
        return nullptr;
7293
0
    }
7294
7295
    /* -------------------------------------------------------------------- */
7296
    /*      If there is no UNION ALL, build result layer.                   */
7297
    /* -------------------------------------------------------------------- */
7298
0
    if (psSelectInfo->poOtherSelect == nullptr)
7299
0
    {
7300
0
        return BuildLayerFromSelectInfo(psSelectInfo, poSpatialFilter,
7301
0
                                        pszDialect, poSelectParseOptions);
7302
0
    }
7303
7304
    /* -------------------------------------------------------------------- */
7305
    /*      Build result union layer.                                       */
7306
    /* -------------------------------------------------------------------- */
7307
0
    int nSrcLayers = 0;
7308
0
    OGRLayer **papoSrcLayers = nullptr;
7309
7310
0
    do
7311
0
    {
7312
0
        swq_select *psNextSelectInfo = psSelectInfo->poOtherSelect;
7313
0
        psSelectInfo->poOtherSelect = nullptr;
7314
7315
0
        OGRLayer *poLayer = BuildLayerFromSelectInfo(
7316
0
            psSelectInfo, poSpatialFilter, pszDialect, poSelectParseOptions);
7317
0
        if (poLayer == nullptr)
7318
0
        {
7319
            // Each source layer owns an independent select info.
7320
0
            for (int i = 0; i < nSrcLayers; ++i)
7321
0
                delete papoSrcLayers[i];
7322
0
            CPLFree(papoSrcLayers);
7323
7324
            // So we just have to destroy the remaining select info.
7325
0
            delete psNextSelectInfo;
7326
7327
0
            return nullptr;
7328
0
        }
7329
0
        else
7330
0
        {
7331
0
            papoSrcLayers = static_cast<OGRLayer **>(CPLRealloc(
7332
0
                papoSrcLayers, sizeof(OGRLayer *) * (nSrcLayers + 1)));
7333
0
            papoSrcLayers[nSrcLayers] = poLayer;
7334
0
            ++nSrcLayers;
7335
7336
0
            psSelectInfo = psNextSelectInfo;
7337
0
        }
7338
0
    } while (psSelectInfo != nullptr);
7339
7340
0
    return new OGRUnionLayer("SELECT", nSrcLayers, papoSrcLayers, TRUE);
7341
0
}
7342
7343
//! @endcond
7344
7345
/************************************************************************/
7346
/*                             AbortSQL()                             */
7347
/************************************************************************/
7348
7349
/**
7350
 \brief Abort any SQL statement running in the data store.
7351
7352
 This function can be safely called from any thread (pending that the dataset
7353
 object is still alive). Driver implementations will make sure that it can be
7354
 called in a thread-safe way.
7355
7356
 This might not be implemented by all drivers. At time of writing, only SQLite,
7357
 GPKG and PG drivers implement it
7358
7359
 This method is the same as the C method GDALDatasetAbortSQL()
7360
7361
 @since GDAL 3.2.0
7362
7363
7364
*/
7365
7366
OGRErr GDALDataset::AbortSQL()
7367
0
{
7368
0
    CPLError(CE_Failure, CPLE_NotSupported,
7369
0
             "AbortSQL is not supported for this driver.");
7370
0
    return OGRERR_UNSUPPORTED_OPERATION;
7371
0
}
7372
7373
/************************************************************************/
7374
/*                        BuildLayerFromSelectInfo()                    */
7375
/************************************************************************/
7376
7377
struct GDALSQLParseInfo
7378
{
7379
    swq_field_list sFieldList;
7380
    int nExtraDSCount;
7381
    GDALDataset **papoExtraDS;
7382
    char *pszWHERE;
7383
};
7384
7385
OGRLayer *GDALDataset::BuildLayerFromSelectInfo(
7386
    swq_select *psSelectInfo, OGRGeometry *poSpatialFilter,
7387
    const char *pszDialect, swq_select_parse_options *poSelectParseOptions)
7388
0
{
7389
0
    std::unique_ptr<swq_select> psSelectInfoUnique(psSelectInfo);
7390
7391
0
    std::unique_ptr<OGRGenSQLResultsLayer> poResults;
7392
0
    GDALSQLParseInfo *psParseInfo =
7393
0
        BuildParseInfo(psSelectInfoUnique.get(), poSelectParseOptions);
7394
7395
0
    if (psParseInfo)
7396
0
    {
7397
0
        const auto nErrorCounter = CPLGetErrorCounter();
7398
0
        poResults = std::make_unique<OGRGenSQLResultsLayer>(
7399
0
            this, std::move(psSelectInfoUnique), poSpatialFilter,
7400
0
            psParseInfo->pszWHERE, pszDialect);
7401
0
        if (CPLGetErrorCounter() > nErrorCounter &&
7402
0
            CPLGetLastErrorType() != CE_None)
7403
0
            poResults.reset();
7404
0
    }
7405
7406
0
    DestroyParseInfo(psParseInfo);
7407
7408
0
    return poResults.release();
7409
0
}
7410
7411
/************************************************************************/
7412
/*                             DestroyParseInfo()                       */
7413
/************************************************************************/
7414
7415
//! @cond Doxygen_Suppress
7416
void GDALDataset::DestroyParseInfo(GDALSQLParseInfo *psParseInfo)
7417
0
{
7418
0
    if (psParseInfo == nullptr)
7419
0
        return;
7420
7421
0
    CPLFree(psParseInfo->sFieldList.names);
7422
0
    CPLFree(psParseInfo->sFieldList.types);
7423
0
    CPLFree(psParseInfo->sFieldList.table_ids);
7424
0
    CPLFree(psParseInfo->sFieldList.ids);
7425
7426
    // Release the datasets we have opened with OGROpenShared()
7427
    // It is safe to do that as the 'new OGRGenSQLResultsLayer' itself
7428
    // has taken a reference on them, which it will release in its
7429
    // destructor.
7430
0
    for (int iEDS = 0; iEDS < psParseInfo->nExtraDSCount; ++iEDS)
7431
0
        GDALClose(psParseInfo->papoExtraDS[iEDS]);
7432
7433
0
    CPLFree(psParseInfo->papoExtraDS);
7434
0
    CPLFree(psParseInfo->pszWHERE);
7435
0
    CPLFree(psParseInfo);
7436
0
}
7437
7438
/************************************************************************/
7439
/*                            BuildParseInfo()                          */
7440
/************************************************************************/
7441
7442
GDALSQLParseInfo *
7443
GDALDataset::BuildParseInfo(swq_select *psSelectInfo,
7444
                            swq_select_parse_options *poSelectParseOptions)
7445
0
{
7446
0
    int nFirstLayerFirstSpecialFieldIndex = 0;
7447
7448
0
    GDALSQLParseInfo *psParseInfo =
7449
0
        static_cast<GDALSQLParseInfo *>(CPLCalloc(1, sizeof(GDALSQLParseInfo)));
7450
7451
    /* -------------------------------------------------------------------- */
7452
    /*      Validate that all the source tables are recognized, count       */
7453
    /*      fields.                                                         */
7454
    /* -------------------------------------------------------------------- */
7455
0
    int nFieldCount = 0;
7456
7457
0
    for (int iTable = 0; iTable < psSelectInfo->table_count; iTable++)
7458
0
    {
7459
0
        swq_table_def *psTableDef = psSelectInfo->table_defs + iTable;
7460
0
        GDALDataset *poTableDS = this;
7461
7462
0
        if (psTableDef->data_source != nullptr)
7463
0
        {
7464
0
            poTableDS = GDALDataset::FromHandle(
7465
0
                OGROpenShared(psTableDef->data_source, FALSE, nullptr));
7466
0
            if (poTableDS == nullptr)
7467
0
            {
7468
0
                if (strlen(CPLGetLastErrorMsg()) == 0)
7469
0
                    CPLError(CE_Failure, CPLE_AppDefined,
7470
0
                             "Unable to open secondary datasource "
7471
0
                             "`%s' required by JOIN.",
7472
0
                             psTableDef->data_source);
7473
7474
0
                DestroyParseInfo(psParseInfo);
7475
0
                return nullptr;
7476
0
            }
7477
7478
            // Keep in an array to release at the end of this function.
7479
0
            psParseInfo->papoExtraDS = static_cast<GDALDataset **>(CPLRealloc(
7480
0
                psParseInfo->papoExtraDS,
7481
0
                sizeof(GDALDataset *) * (psParseInfo->nExtraDSCount + 1)));
7482
0
            psParseInfo->papoExtraDS[psParseInfo->nExtraDSCount++] = poTableDS;
7483
0
        }
7484
7485
0
        OGRLayer *poSrcLayer =
7486
0
            poTableDS->GetLayerByName(psTableDef->table_name);
7487
7488
0
        if (poSrcLayer == nullptr)
7489
0
        {
7490
0
            CPLError(CE_Failure, CPLE_AppDefined,
7491
0
                     "SELECT from table %s failed, no such table/featureclass.",
7492
0
                     psTableDef->table_name);
7493
7494
0
            DestroyParseInfo(psParseInfo);
7495
0
            return nullptr;
7496
0
        }
7497
7498
0
        nFieldCount += poSrcLayer->GetLayerDefn()->GetFieldCount();
7499
0
        if (iTable == 0 ||
7500
0
            (poSelectParseOptions &&
7501
0
             poSelectParseOptions->bAddSecondaryTablesGeometryFields))
7502
0
            nFieldCount += poSrcLayer->GetLayerDefn()->GetGeomFieldCount();
7503
7504
0
        const char *pszFID = poSrcLayer->GetFIDColumn();
7505
0
        if (pszFID && !EQUAL(pszFID, "") && !EQUAL(pszFID, "FID") &&
7506
0
            poSrcLayer->GetLayerDefn()->GetFieldIndex(pszFID) < 0)
7507
0
            nFieldCount++;
7508
0
    }
7509
7510
    /* -------------------------------------------------------------------- */
7511
    /*      Build the field list for all indicated tables.                  */
7512
    /* -------------------------------------------------------------------- */
7513
7514
0
    psParseInfo->sFieldList.table_count = psSelectInfo->table_count;
7515
0
    psParseInfo->sFieldList.table_defs = psSelectInfo->table_defs;
7516
7517
0
    psParseInfo->sFieldList.count = 0;
7518
0
    psParseInfo->sFieldList.names = static_cast<char **>(
7519
0
        CPLMalloc(sizeof(char *) * (nFieldCount + SPECIAL_FIELD_COUNT)));
7520
0
    psParseInfo->sFieldList.types = static_cast<swq_field_type *>(CPLMalloc(
7521
0
        sizeof(swq_field_type) * (nFieldCount + SPECIAL_FIELD_COUNT)));
7522
0
    psParseInfo->sFieldList.table_ids = static_cast<int *>(
7523
0
        CPLMalloc(sizeof(int) * (nFieldCount + SPECIAL_FIELD_COUNT)));
7524
0
    psParseInfo->sFieldList.ids = static_cast<int *>(
7525
0
        CPLMalloc(sizeof(int) * (nFieldCount + SPECIAL_FIELD_COUNT)));
7526
7527
0
    bool bIsFID64 = false;
7528
0
    for (int iTable = 0; iTable < psSelectInfo->table_count; iTable++)
7529
0
    {
7530
0
        swq_table_def *psTableDef = psSelectInfo->table_defs + iTable;
7531
0
        GDALDataset *poTableDS = this;
7532
7533
0
        if (psTableDef->data_source != nullptr)
7534
0
        {
7535
0
            poTableDS = GDALDataset::FromHandle(
7536
0
                OGROpenShared(psTableDef->data_source, FALSE, nullptr));
7537
0
            CPLAssert(poTableDS != nullptr);
7538
0
            poTableDS->Dereference();
7539
0
        }
7540
7541
0
        OGRLayer *poSrcLayer =
7542
0
            poTableDS->GetLayerByName(psTableDef->table_name);
7543
7544
0
        for (int iField = 0;
7545
0
             iField < poSrcLayer->GetLayerDefn()->GetFieldCount(); iField++)
7546
0
        {
7547
0
            OGRFieldDefn *poFDefn =
7548
0
                poSrcLayer->GetLayerDefn()->GetFieldDefn(iField);
7549
0
            const int iOutField = psParseInfo->sFieldList.count++;
7550
0
            psParseInfo->sFieldList.names[iOutField] =
7551
0
                const_cast<char *>(poFDefn->GetNameRef());
7552
0
            if (poFDefn->GetType() == OFTInteger)
7553
0
            {
7554
0
                if (poFDefn->GetSubType() == OFSTBoolean)
7555
0
                    psParseInfo->sFieldList.types[iOutField] = SWQ_BOOLEAN;
7556
0
                else
7557
0
                    psParseInfo->sFieldList.types[iOutField] = SWQ_INTEGER;
7558
0
            }
7559
0
            else if (poFDefn->GetType() == OFTInteger64)
7560
0
            {
7561
0
                if (poFDefn->GetSubType() == OFSTBoolean)
7562
0
                    psParseInfo->sFieldList.types[iOutField] = SWQ_BOOLEAN;
7563
0
                else
7564
0
                    psParseInfo->sFieldList.types[iOutField] = SWQ_INTEGER64;
7565
0
            }
7566
0
            else if (poFDefn->GetType() == OFTReal)
7567
0
                psParseInfo->sFieldList.types[iOutField] = SWQ_FLOAT;
7568
0
            else if (poFDefn->GetType() == OFTString)
7569
0
                psParseInfo->sFieldList.types[iOutField] = SWQ_STRING;
7570
0
            else if (poFDefn->GetType() == OFTTime)
7571
0
                psParseInfo->sFieldList.types[iOutField] = SWQ_TIME;
7572
0
            else if (poFDefn->GetType() == OFTDate)
7573
0
                psParseInfo->sFieldList.types[iOutField] = SWQ_DATE;
7574
0
            else if (poFDefn->GetType() == OFTDateTime)
7575
0
                psParseInfo->sFieldList.types[iOutField] = SWQ_TIMESTAMP;
7576
0
            else
7577
0
                psParseInfo->sFieldList.types[iOutField] = SWQ_OTHER;
7578
7579
0
            psParseInfo->sFieldList.table_ids[iOutField] = iTable;
7580
0
            psParseInfo->sFieldList.ids[iOutField] = iField;
7581
0
        }
7582
7583
0
        if (iTable == 0)
7584
0
        {
7585
0
            nFirstLayerFirstSpecialFieldIndex = psParseInfo->sFieldList.count;
7586
0
        }
7587
7588
0
        if (iTable == 0 ||
7589
0
            (poSelectParseOptions &&
7590
0
             poSelectParseOptions->bAddSecondaryTablesGeometryFields))
7591
0
        {
7592
7593
0
            for (int iField = 0;
7594
0
                 iField < poSrcLayer->GetLayerDefn()->GetGeomFieldCount();
7595
0
                 iField++)
7596
0
            {
7597
0
                OGRGeomFieldDefn *poFDefn =
7598
0
                    poSrcLayer->GetLayerDefn()->GetGeomFieldDefn(iField);
7599
0
                const int iOutField = psParseInfo->sFieldList.count++;
7600
0
                psParseInfo->sFieldList.names[iOutField] =
7601
0
                    const_cast<char *>(poFDefn->GetNameRef());
7602
0
                if (*psParseInfo->sFieldList.names[iOutField] == '\0')
7603
0
                    psParseInfo->sFieldList.names[iOutField] =
7604
0
                        const_cast<char *>(OGR_GEOMETRY_DEFAULT_NON_EMPTY_NAME);
7605
0
                psParseInfo->sFieldList.types[iOutField] = SWQ_GEOMETRY;
7606
7607
0
                psParseInfo->sFieldList.table_ids[iOutField] = iTable;
7608
0
                psParseInfo->sFieldList.ids[iOutField] =
7609
0
                    GEOM_FIELD_INDEX_TO_ALL_FIELD_INDEX(
7610
0
                        poSrcLayer->GetLayerDefn(), iField);
7611
0
            }
7612
0
        }
7613
7614
0
        if (iTable == 0 && poSrcLayer->GetMetadataItem(OLMD_FID64) != nullptr &&
7615
0
            EQUAL(poSrcLayer->GetMetadataItem(OLMD_FID64), "YES"))
7616
0
        {
7617
0
            bIsFID64 = true;
7618
0
        }
7619
0
    }
7620
7621
    /* -------------------------------------------------------------------- */
7622
    /*      Expand '*' in 'SELECT *' now before we add the pseudo fields    */
7623
    /* -------------------------------------------------------------------- */
7624
0
    const bool bAlwaysPrefixWithTableName =
7625
0
        poSelectParseOptions &&
7626
0
        poSelectParseOptions->bAlwaysPrefixWithTableName;
7627
0
    if (psSelectInfo->expand_wildcard(&psParseInfo->sFieldList,
7628
0
                                      bAlwaysPrefixWithTableName) != CE_None)
7629
0
    {
7630
0
        DestroyParseInfo(psParseInfo);
7631
0
        return nullptr;
7632
0
    }
7633
7634
0
    for (int iField = 0; iField < SPECIAL_FIELD_COUNT; iField++)
7635
0
    {
7636
0
        psParseInfo->sFieldList.names[psParseInfo->sFieldList.count] =
7637
0
            const_cast<char *>(SpecialFieldNames[iField]);
7638
0
        psParseInfo->sFieldList.types[psParseInfo->sFieldList.count] =
7639
0
            (iField == SPF_FID && bIsFID64) ? SWQ_INTEGER64
7640
0
                                            : SpecialFieldTypes[iField];
7641
0
        psParseInfo->sFieldList.table_ids[psParseInfo->sFieldList.count] = 0;
7642
0
        psParseInfo->sFieldList.ids[psParseInfo->sFieldList.count] =
7643
0
            nFirstLayerFirstSpecialFieldIndex + iField;
7644
0
        psParseInfo->sFieldList.count++;
7645
0
    }
7646
7647
    /* In the case a layer has an explicit FID column name, then add it */
7648
    /* so it can be selected */
7649
0
    for (int iTable = 0; iTable < psSelectInfo->table_count; iTable++)
7650
0
    {
7651
0
        swq_table_def *psTableDef = psSelectInfo->table_defs + iTable;
7652
0
        GDALDataset *poTableDS = this;
7653
7654
0
        if (psTableDef->data_source != nullptr)
7655
0
        {
7656
0
            poTableDS = GDALDataset::FromHandle(
7657
0
                OGROpenShared(psTableDef->data_source, FALSE, nullptr));
7658
0
            CPLAssert(poTableDS != nullptr);
7659
0
            poTableDS->Dereference();
7660
0
        }
7661
7662
0
        OGRLayer *poSrcLayer =
7663
0
            poTableDS->GetLayerByName(psTableDef->table_name);
7664
7665
0
        const char *pszFID = poSrcLayer->GetFIDColumn();
7666
0
        if (pszFID && !EQUAL(pszFID, "") && !EQUAL(pszFID, "FID") &&
7667
0
            poSrcLayer->GetLayerDefn()->GetFieldIndex(pszFID) < 0)
7668
0
        {
7669
0
            const int iOutField = psParseInfo->sFieldList.count++;
7670
0
            psParseInfo->sFieldList.names[iOutField] =
7671
0
                const_cast<char *>(pszFID);
7672
0
            if (poSrcLayer->GetMetadataItem(OLMD_FID64) != nullptr &&
7673
0
                EQUAL(poSrcLayer->GetMetadataItem(OLMD_FID64), "YES"))
7674
0
            {
7675
0
                psParseInfo->sFieldList.types[iOutField] = SWQ_INTEGER64;
7676
0
            }
7677
0
            else
7678
0
            {
7679
0
                psParseInfo->sFieldList.types[iOutField] = SWQ_INTEGER;
7680
0
            }
7681
0
            psParseInfo->sFieldList.table_ids[iOutField] = iTable;
7682
0
            psParseInfo->sFieldList.ids[iOutField] =
7683
0
                poSrcLayer->GetLayerDefn()->GetFieldCount() + SPF_FID;
7684
0
        }
7685
0
    }
7686
7687
    /* -------------------------------------------------------------------- */
7688
    /*      Finish the parse operation.                                     */
7689
    /* -------------------------------------------------------------------- */
7690
0
    if (psSelectInfo->parse(&psParseInfo->sFieldList, poSelectParseOptions) !=
7691
0
        CE_None)
7692
0
    {
7693
0
        DestroyParseInfo(psParseInfo);
7694
0
        return nullptr;
7695
0
    }
7696
7697
    /* -------------------------------------------------------------------- */
7698
    /*      Extract the WHERE expression to use separately.                 */
7699
    /* -------------------------------------------------------------------- */
7700
0
    if (psSelectInfo->where_expr != nullptr)
7701
0
    {
7702
0
        psParseInfo->pszWHERE =
7703
0
            psSelectInfo->where_expr->Unparse(&psParseInfo->sFieldList, '"');
7704
        // CPLDebug( "OGR", "Unparse() -> %s", pszWHERE );
7705
0
    }
7706
7707
0
    return psParseInfo;
7708
0
}
7709
7710
//! @endcond
7711
7712
/************************************************************************/
7713
/*                          ReleaseResultSet()                          */
7714
/************************************************************************/
7715
7716
/**
7717
 \brief Release results of ExecuteSQL().
7718
7719
 This method should only be used to deallocate OGRLayers resulting from
7720
 an ExecuteSQL() call on the same GDALDataset.  Failure to deallocate a
7721
 results set before destroying the GDALDataset may cause errors.
7722
7723
 This method is the same as the C function GDALDatasetReleaseResultSet() and the
7724
 deprecated OGR_DS_ReleaseResultSet().
7725
7726
 @param poResultsSet the result of a previous ExecuteSQL() call.
7727
*/
7728
7729
void GDALDataset::ReleaseResultSet(OGRLayer *poResultsSet)
7730
7731
0
{
7732
0
    delete poResultsSet;
7733
0
}
7734
7735
/************************************************************************/
7736
/*                            GetStyleTable()                           */
7737
/************************************************************************/
7738
7739
/**
7740
 \brief Returns dataset style table.
7741
7742
 This method is the same as the C function GDALDatasetGetStyleTable() and the
7743
 deprecated OGR_DS_GetStyleTable().
7744
7745
 @return pointer to a style table which should not be modified or freed by the
7746
 caller.
7747
*/
7748
7749
OGRStyleTable *GDALDataset::GetStyleTable()
7750
0
{
7751
0
    return m_poStyleTable;
7752
0
}
7753
7754
/************************************************************************/
7755
/*                         SetStyleTableDirectly()                      */
7756
/************************************************************************/
7757
7758
/**
7759
 \brief Set dataset style table.
7760
7761
 This method operate exactly as SetStyleTable() except that it
7762
 assumes ownership of the passed table.
7763
7764
 This method is the same as the C function GDALDatasetSetStyleTableDirectly()
7765
 and the deprecated OGR_DS_SetStyleTableDirectly().
7766
7767
 @param poStyleTable pointer to style table to set
7768
7769
*/
7770
void GDALDataset::SetStyleTableDirectly(OGRStyleTable *poStyleTable)
7771
0
{
7772
0
    if (m_poStyleTable)
7773
0
        delete m_poStyleTable;
7774
0
    m_poStyleTable = poStyleTable;
7775
0
}
7776
7777
/************************************************************************/
7778
/*                            SetStyleTable()                           */
7779
/************************************************************************/
7780
7781
/**
7782
 \brief Set dataset style table.
7783
7784
 This method operate exactly as SetStyleTableDirectly() except
7785
 that it does not assume ownership of the passed table.
7786
7787
 This method is the same as the C function GDALDatasetSetStyleTable() and the
7788
 deprecated OGR_DS_SetStyleTable().
7789
7790
 @param poStyleTable pointer to style table to set
7791
7792
*/
7793
7794
void GDALDataset::SetStyleTable(OGRStyleTable *poStyleTable)
7795
0
{
7796
0
    if (m_poStyleTable)
7797
0
        delete m_poStyleTable;
7798
0
    if (poStyleTable)
7799
0
        m_poStyleTable = poStyleTable->Clone();
7800
0
}
7801
7802
/************************************************************************/
7803
/*                         IsGenericSQLDialect()                        */
7804
/************************************************************************/
7805
7806
//! @cond Doxygen_Suppress
7807
int GDALDataset::IsGenericSQLDialect(const char *pszDialect)
7808
0
{
7809
0
    return pszDialect != nullptr &&
7810
0
           (EQUAL(pszDialect, "OGRSQL") || EQUAL(pszDialect, "SQLITE"));
7811
0
}
7812
7813
//! @endcond
7814
7815
/************************************************************************/
7816
/*                            GetLayerCount()                           */
7817
/************************************************************************/
7818
7819
/**
7820
 \brief Get the number of layers in this dataset.
7821
7822
 This method is the same as the C function GDALDatasetGetLayerCount(),
7823
 and the deprecated OGR_DS_GetLayerCount().
7824
7825
 Note that even if this method is const, there is no guarantee it can be
7826
 safely called by concurrent threads on the same GDALDataset object.
7827
7828
 @return layer count.
7829
*/
7830
7831
int GDALDataset::GetLayerCount() const
7832
0
{
7833
0
    return 0;
7834
0
}
7835
7836
/************************************************************************/
7837
/*                                GetLayer()                            */
7838
/************************************************************************/
7839
7840
/**
7841
 \fn const GDALDataset::GetLayer(int) const
7842
 \brief Fetch a layer by index.
7843
7844
 The returned layer remains owned by the
7845
 GDALDataset and should not be deleted by the application.
7846
7847
 Note that even if this method is const, there is no guarantee it can be
7848
 safely called by concurrent threads on the same GDALDataset object.
7849
7850
 See GetLayers() for a C++ iterator version of this method.
7851
7852
 This method is the same as the C function GDALDatasetGetLayer() and the
7853
 deprecated OGR_DS_GetLayer().
7854
7855
 @param iLayer a layer number between 0 and GetLayerCount()-1.
7856
7857
 @return the layer, or NULL if iLayer is out of range or an error occurs.
7858
7859
 @see GetLayers()
7860
7861
 @since GDAL 3.12
7862
*/
7863
7864
const OGRLayer *GDALDataset::GetLayer(CPL_UNUSED int iLayer) const
7865
0
{
7866
0
    return nullptr;
7867
0
}
7868
7869
/**
7870
 \fn GDALDataset::GetLayer(int)
7871
 \brief Fetch a layer by index.
7872
7873
 The returned layer remains owned by the
7874
 GDALDataset and should not be deleted by the application.
7875
7876
 See GetLayers() for a C++ iterator version of this method.
7877
7878
 This method is the same as the C function GDALDatasetGetLayer() and the
7879
 deprecated OGR_DS_GetLayer().
7880
7881
 @param iLayer a layer number between 0 and GetLayerCount()-1.
7882
7883
 @return the layer, or NULL if iLayer is out of range or an error occurs.
7884
7885
 @see GetLayers()
7886
*/
7887
7888
/************************************************************************/
7889
/*                                IsLayerPrivate()                      */
7890
/************************************************************************/
7891
7892
/**
7893
 \fn GDALDataset::IsLayerPrivate(int)
7894
 \brief Returns true if the layer at the specified index is deemed a private or
7895
 system table, or an internal detail only.
7896
7897
 This method is the same as the C function GDALDatasetIsLayerPrivate().
7898
7899
 @param iLayer a layer number between 0 and GetLayerCount()-1.
7900
7901
 @return true if the layer is a private or system table.
7902
7903
 @since GDAL 3.4
7904
*/
7905
7906
bool GDALDataset::IsLayerPrivate(CPL_UNUSED int iLayer) const
7907
0
{
7908
0
    return false;
7909
0
}
7910
7911
/************************************************************************/
7912
/*                           ResetReading()                             */
7913
/************************************************************************/
7914
7915
/**
7916
 \brief Reset feature reading to start on the first feature.
7917
7918
 This affects GetNextFeature().
7919
7920
 Depending on drivers, this may also have the side effect of calling
7921
 OGRLayer::ResetReading() on the layers of this dataset.
7922
7923
 This method is the same as the C function GDALDatasetResetReading().
7924
7925
*/
7926
void GDALDataset::ResetReading()
7927
0
{
7928
0
    if (!m_poPrivate)
7929
0
        return;
7930
0
    m_poPrivate->nCurrentLayerIdx = 0;
7931
0
    m_poPrivate->nLayerCount = -1;
7932
0
    m_poPrivate->poCurrentLayer = nullptr;
7933
0
    m_poPrivate->nFeatureReadInLayer = 0;
7934
0
    m_poPrivate->nFeatureReadInDataset = 0;
7935
0
    m_poPrivate->nTotalFeaturesInLayer = TOTAL_FEATURES_NOT_INIT;
7936
0
    m_poPrivate->nTotalFeatures = TOTAL_FEATURES_NOT_INIT;
7937
0
}
7938
7939
/************************************************************************/
7940
/*                         GDALDatasetResetReading()                    */
7941
/************************************************************************/
7942
7943
/**
7944
 \brief Reset feature reading to start on the first feature.
7945
7946
 This affects GDALDatasetGetNextFeature().
7947
7948
 Depending on drivers, this may also have the side effect of calling
7949
 OGR_L_ResetReading() on the layers of this dataset.
7950
7951
 This method is the same as the C++ method GDALDataset::ResetReading()
7952
7953
 @param hDS dataset handle
7954
*/
7955
void CPL_DLL GDALDatasetResetReading(GDALDatasetH hDS)
7956
0
{
7957
0
    VALIDATE_POINTER0(hDS, "GDALDatasetResetReading");
7958
7959
0
    return GDALDataset::FromHandle(hDS)->ResetReading();
7960
0
}
7961
7962
/************************************************************************/
7963
/*                          GetNextFeature()                            */
7964
/************************************************************************/
7965
7966
/**
7967
 \brief Fetch the next available feature from this dataset.
7968
7969
 This method is intended for the few drivers where OGRLayer::GetNextFeature()
7970
 is not efficient, but in general OGRLayer::GetNextFeature() is a more
7971
 natural API.
7972
7973
 See GetFeatures() for a C++ iterator version of this method.
7974
7975
 The returned feature becomes the responsibility of the caller to
7976
 delete with OGRFeature::DestroyFeature().
7977
7978
 Depending on the driver, this method may return features from layers in a
7979
 non sequential way. This is what may happen when the
7980
 ODsCRandomLayerRead capability is declared (for example for the
7981
 OSM and GMLAS drivers). When datasets declare this capability, it is strongly
7982
 advised to use GDALDataset::GetNextFeature() instead of
7983
 OGRLayer::GetNextFeature(), as the later might have a slow, incomplete or stub
7984
 implementation.
7985
7986
 The default implementation, used by most drivers, will
7987
 however iterate over each layer, and then over each feature within this
7988
 layer.
7989
7990
 This method takes into account spatial and attribute filters set on layers that
7991
 will be iterated upon.
7992
7993
 The ResetReading() method can be used to start at the beginning again.
7994
7995
 Depending on drivers, this may also have the side effect of calling
7996
 OGRLayer::GetNextFeature() on the layers of this dataset.
7997
7998
 This method is the same as the C function GDALDatasetGetNextFeature().
7999
8000
 @param ppoBelongingLayer a pointer to a OGRLayer* variable to receive the
8001
                          layer to which the object belongs to, or NULL.
8002
                          It is possible that the output of *ppoBelongingLayer
8003
                          to be NULL despite the feature not being NULL.
8004
 @param pdfProgressPct    a pointer to a double variable to receive the
8005
                          percentage progress (in [0,1] range), or NULL.
8006
                          On return, the pointed value might be negative if
8007
                          determining the progress is not possible.
8008
 @param pfnProgress       a progress callback to report progress (for
8009
                          GetNextFeature() calls that might have a long
8010
                          duration) and offer cancellation possibility, or NULL.
8011
 @param pProgressData     user data provided to pfnProgress, or NULL
8012
 @return a feature, or NULL if no more features are available.
8013
 @see GetFeatures()
8014
*/
8015
8016
OGRFeature *GDALDataset::GetNextFeature(OGRLayer **ppoBelongingLayer,
8017
                                        double *pdfProgressPct,
8018
                                        GDALProgressFunc pfnProgress,
8019
                                        void *pProgressData)
8020
0
{
8021
0
    if (!m_poPrivate || m_poPrivate->nCurrentLayerIdx < 0)
8022
0
    {
8023
0
        if (ppoBelongingLayer != nullptr)
8024
0
            *ppoBelongingLayer = nullptr;
8025
0
        if (pdfProgressPct != nullptr)
8026
0
            *pdfProgressPct = 1.0;
8027
0
        if (pfnProgress != nullptr)
8028
0
            pfnProgress(1.0, "", pProgressData);
8029
0
        return nullptr;
8030
0
    }
8031
8032
0
    if (m_poPrivate->poCurrentLayer == nullptr &&
8033
0
        (pdfProgressPct != nullptr || pfnProgress != nullptr))
8034
0
    {
8035
0
        if (m_poPrivate->nLayerCount < 0)
8036
0
        {
8037
0
            m_poPrivate->nLayerCount = GetLayerCount();
8038
0
        }
8039
8040
0
        if (m_poPrivate->nTotalFeatures == TOTAL_FEATURES_NOT_INIT)
8041
0
        {
8042
0
            m_poPrivate->nTotalFeatures = 0;
8043
0
            for (int i = 0; i < m_poPrivate->nLayerCount; i++)
8044
0
            {
8045
0
                OGRLayer *poLayer = GetLayer(i);
8046
0
                if (poLayer == nullptr ||
8047
0
                    !poLayer->TestCapability(OLCFastFeatureCount))
8048
0
                {
8049
0
                    m_poPrivate->nTotalFeatures = TOTAL_FEATURES_UNKNOWN;
8050
0
                    break;
8051
0
                }
8052
0
                GIntBig nCount = poLayer->GetFeatureCount(FALSE);
8053
0
                if (nCount < 0)
8054
0
                {
8055
0
                    m_poPrivate->nTotalFeatures = TOTAL_FEATURES_UNKNOWN;
8056
0
                    break;
8057
0
                }
8058
0
                m_poPrivate->nTotalFeatures += nCount;
8059
0
            }
8060
0
        }
8061
0
    }
8062
8063
0
    while (true)
8064
0
    {
8065
0
        if (m_poPrivate->poCurrentLayer == nullptr)
8066
0
        {
8067
0
            m_poPrivate->poCurrentLayer =
8068
0
                GetLayer(m_poPrivate->nCurrentLayerIdx);
8069
0
            if (m_poPrivate->poCurrentLayer == nullptr)
8070
0
            {
8071
0
                m_poPrivate->nCurrentLayerIdx = -1;
8072
0
                if (ppoBelongingLayer != nullptr)
8073
0
                    *ppoBelongingLayer = nullptr;
8074
0
                if (pdfProgressPct != nullptr)
8075
0
                    *pdfProgressPct = 1.0;
8076
0
                return nullptr;
8077
0
            }
8078
0
            m_poPrivate->poCurrentLayer->ResetReading();
8079
0
            m_poPrivate->nFeatureReadInLayer = 0;
8080
0
            if (m_poPrivate->nTotalFeatures < 0 && pdfProgressPct != nullptr)
8081
0
            {
8082
0
                if (m_poPrivate->poCurrentLayer->TestCapability(
8083
0
                        OLCFastFeatureCount))
8084
0
                    m_poPrivate->nTotalFeaturesInLayer =
8085
0
                        m_poPrivate->poCurrentLayer->GetFeatureCount(FALSE);
8086
0
                else
8087
0
                    m_poPrivate->nTotalFeaturesInLayer = 0;
8088
0
            }
8089
0
        }
8090
0
        OGRFeature *poFeature = m_poPrivate->poCurrentLayer->GetNextFeature();
8091
0
        if (poFeature == nullptr)
8092
0
        {
8093
0
            m_poPrivate->nCurrentLayerIdx++;
8094
0
            m_poPrivate->poCurrentLayer = nullptr;
8095
0
            continue;
8096
0
        }
8097
8098
0
        m_poPrivate->nFeatureReadInLayer++;
8099
0
        m_poPrivate->nFeatureReadInDataset++;
8100
0
        if (pdfProgressPct != nullptr || pfnProgress != nullptr)
8101
0
        {
8102
0
            double dfPct = 0.0;
8103
0
            if (m_poPrivate->nTotalFeatures > 0)
8104
0
            {
8105
0
                dfPct = 1.0 * m_poPrivate->nFeatureReadInDataset /
8106
0
                        m_poPrivate->nTotalFeatures;
8107
0
            }
8108
0
            else
8109
0
            {
8110
0
                dfPct = 1.0 * m_poPrivate->nCurrentLayerIdx /
8111
0
                        m_poPrivate->nLayerCount;
8112
0
                if (m_poPrivate->nTotalFeaturesInLayer > 0)
8113
0
                {
8114
0
                    dfPct += 1.0 * m_poPrivate->nFeatureReadInLayer /
8115
0
                             m_poPrivate->nTotalFeaturesInLayer /
8116
0
                             m_poPrivate->nLayerCount;
8117
0
                }
8118
0
            }
8119
0
            if (pdfProgressPct)
8120
0
                *pdfProgressPct = dfPct;
8121
0
            if (pfnProgress)
8122
0
                pfnProgress(dfPct, "", nullptr);
8123
0
        }
8124
8125
0
        if (ppoBelongingLayer != nullptr)
8126
0
            *ppoBelongingLayer = m_poPrivate->poCurrentLayer;
8127
0
        return poFeature;
8128
0
    }
8129
0
}
8130
8131
/************************************************************************/
8132
/*                     GDALDatasetGetNextFeature()                      */
8133
/************************************************************************/
8134
/**
8135
 \brief Fetch the next available feature from this dataset.
8136
8137
 This method is intended for the few drivers where OGR_L_GetNextFeature()
8138
 is not efficient, but in general OGR_L_GetNextFeature() is a more
8139
 natural API.
8140
8141
 The returned feature becomes the responsibility of the caller to
8142
 delete with OGRFeature::DestroyFeature().
8143
8144
 Depending on the driver, this method may return features from layers in a
8145
 non sequential way. This is what may happen when the
8146
 ODsCRandomLayerRead capability is declared (for example for the
8147
 OSM and GMLAS drivers). When datasets declare this capability, it is strongly
8148
 advised to use GDALDataset::GetNextFeature() instead of
8149
 OGRLayer::GetNextFeature(), as the later might have a slow, incomplete or stub
8150
 implementation.
8151
8152
 The default implementation, used by most drivers, will
8153
 however iterate over each layer, and then over each feature within this
8154
 layer.
8155
8156
 This method takes into account spatial and attribute filters set on layers that
8157
 will be iterated upon.
8158
8159
 The ResetReading() method can be used to start at the beginning again.
8160
8161
 Depending on drivers, this may also have the side effect of calling
8162
 OGRLayer::GetNextFeature() on the layers of this dataset.
8163
8164
 This method is the same as the C++ method GDALDataset::GetNextFeature()
8165
8166
 @param hDS               dataset handle.
8167
 @param phBelongingLayer  a pointer to a OGRLayer* variable to receive the
8168
                          layer to which the object belongs to, or NULL.
8169
                          It is possible that the output of *ppoBelongingLayer
8170
                          to be NULL despite the feature not being NULL.
8171
 @param pdfProgressPct    a pointer to a double variable to receive the
8172
                          percentage progress (in [0,1] range), or NULL.
8173
                          On return, the pointed value might be negative if
8174
                          determining the progress is not possible.
8175
 @param pfnProgress       a progress callback to report progress (for
8176
                          GetNextFeature() calls that might have a long
8177
                          duration) and offer cancellation possibility, or NULL
8178
 @param pProgressData     user data provided to pfnProgress, or NULL
8179
 @return a feature, or NULL if no more features are available.
8180
*/
8181
OGRFeatureH CPL_DLL GDALDatasetGetNextFeature(GDALDatasetH hDS,
8182
                                              OGRLayerH *phBelongingLayer,
8183
                                              double *pdfProgressPct,
8184
                                              GDALProgressFunc pfnProgress,
8185
                                              void *pProgressData)
8186
0
{
8187
0
    VALIDATE_POINTER1(hDS, "GDALDatasetGetNextFeature", nullptr);
8188
8189
0
    return OGRFeature::ToHandle(GDALDataset::FromHandle(hDS)->GetNextFeature(
8190
0
        reinterpret_cast<OGRLayer **>(phBelongingLayer), pdfProgressPct,
8191
0
        pfnProgress, pProgressData));
8192
0
}
8193
8194
/************************************************************************/
8195
/*                            TestCapability()                          */
8196
/************************************************************************/
8197
8198
/**
8199
 \fn GDALDataset::TestCapability( const char * pszCap )
8200
 \brief Test if capability is available.
8201
8202
 One of the following dataset capability names can be passed into this
8203
 method, and a TRUE or FALSE value will be returned indicating whether or not
8204
 the capability is available for this object.
8205
8206
 <ul>
8207
  <li> <b>ODsCCreateLayer</b>: True if this datasource can create new layers.<p>
8208
  <li> <b>ODsCDeleteLayer</b>: True if this datasource can delete existing
8209
          layers.<p>
8210
  <li> <b>ODsCCreateGeomFieldAfterCreateLayer</b>: True if the layers of this
8211
          datasource support CreateGeomField() just after layer creation.<p>
8212
  <li> <b>ODsCCurveGeometries</b>: True if this datasource supports curve
8213
          geometries.<p>
8214
  <li> <b>ODsCTransactions</b>: True if this datasource supports (efficient)
8215
          transactions.<p>
8216
  <li> <b>ODsCEmulatedTransactions</b>: True if this datasource supports
8217
          transactions through emulation.<p>
8218
  <li> <b>ODsCRandomLayerRead</b>: True if this datasource has a dedicated
8219
          GetNextFeature() implementation, potentially returning features from
8220
          layers in a non sequential way.<p>
8221
  <li> <b>ODsCRandomLayerWrite</b>: True if this datasource supports calling
8222
         CreateFeature() on layers in a non sequential way.<p>
8223
  <li> <b>GDsCAddRelationship</b>: True if AddRelationship() is supported</li>
8224
  <li> <b>GDsCDeleteRelationship</b>: True if DeleteRelationship() is supported</li>
8225
  <li> <b>GDsCUpdateRelationship</b>: True if UpdateRelationship() is supported</li>
8226
  <li> <b>GDsCFastGetExtent</b>: True if GetExtent() is fast</li>
8227
  <li> <b>GDsCFastGetExtentWGS84LongLat</b>: True if GetExtentWGS84LongLat() is fast</li>
8228
 </ul>
8229
8230
 The \#define macro forms of the capability names should be used in preference
8231
 to the strings themselves to avoid misspelling.
8232
8233
 This method is the same as the C function GDALDatasetTestCapability() and the
8234
 deprecated OGR_DS_TestCapability().
8235
8236
 @param pszCap the capability to test.
8237
8238
 @return TRUE if capability available otherwise FALSE.
8239
*/
8240
8241
int GDALDataset::TestCapability(const char *pszCap) const
8242
0
{
8243
0
    if (EQUAL(pszCap, GDsCFastGetExtent) ||
8244
0
        EQUAL(pszCap, GDsCFastGetExtentWGS84LongLat))
8245
0
    {
8246
0
        for (auto &&poLayer : GetLayers())
8247
0
        {
8248
0
            if (!poLayer->TestCapability(OLCFastGetExtent))
8249
0
                return FALSE;
8250
0
        }
8251
0
        return TRUE;
8252
0
    }
8253
0
    return FALSE;
8254
0
}
8255
8256
/************************************************************************/
8257
/*                     GDALDatasetTestCapability()                      */
8258
/************************************************************************/
8259
8260
/**
8261
 \brief Test if capability is available.
8262
8263
 One of the following dataset capability names can be passed into this
8264
 function, and a TRUE or FALSE value will be returned indicating whether or not
8265
 the capability is available for this object.
8266
8267
 <ul>
8268
  <li> <b>ODsCCreateLayer</b>: True if this datasource can create new layers.<p>
8269
  <li> <b>ODsCDeleteLayer</b>: True if this datasource can delete existing
8270
          layers.<p>
8271
  <li> <b>ODsCCreateGeomFieldAfterCreateLayer</b>: True if the layers of this
8272
          datasource support CreateGeomField() just after layer creation.<p>
8273
  <li> <b>ODsCCurveGeometries</b>: True if this datasource supports curve
8274
          geometries.<p>
8275
  <li> <b>ODsCTransactions</b>: True if this datasource supports (efficient)
8276
          transactions.<p>
8277
  <li> <b>ODsCEmulatedTransactions</b>: True if this datasource supports
8278
          transactions through emulation.<p>
8279
  <li> <b>ODsCRandomLayerRead</b>: True if this datasource has a dedicated
8280
          GetNextFeature() implementation, potentially returning features from
8281
          layers in a non sequential way.<p>
8282
  <li> <b>ODsCRandomLayerWrite</b>: True if this datasource supports calling
8283
          CreateFeature() on layers in a non sequential way.<p>
8284
  <li> <b>GDsCAddRelationship</b>: True if AddRelationship() is supported</li>
8285
  <li> <b>GDsCDeleteRelationship</b>: True if DeleteRelationship() is supported</li>
8286
  <li> <b>GDsCUpdateRelationship</b>: True if UpdateRelationship() is supported</li>
8287
  <li> <b>GDsCFastGetExtent</b>: True if GetExtent() is fast</li>
8288
  <li> <b>GDsCFastGetExtentWGS84LongLat</b>: True if GetExtentWGS84LongLat() is fast</li>
8289
 </ul>
8290
8291
 The \#define macro forms of the capability names should be used in preference
8292
 to the strings themselves to avoid misspelling.
8293
8294
 This function is the same as the C++ method GDALDataset::TestCapability()
8295
8296
8297
 @param hDS the dataset handle.
8298
 @param pszCap the capability to test.
8299
8300
 @return TRUE if capability available otherwise FALSE.
8301
*/
8302
int GDALDatasetTestCapability(GDALDatasetH hDS, const char *pszCap)
8303
8304
0
{
8305
0
    VALIDATE_POINTER1(hDS, "GDALDatasetTestCapability", 0);
8306
0
    VALIDATE_POINTER1(pszCap, "GDALDatasetTestCapability", 0);
8307
8308
0
    return GDALDataset::FromHandle(hDS)->TestCapability(pszCap);
8309
0
}
8310
8311
/************************************************************************/
8312
/*                           StartTransaction()                         */
8313
/************************************************************************/
8314
8315
/**
8316
 \fn GDALDataset::StartTransaction(int)
8317
 \brief For datasources which support transactions, StartTransaction creates a
8318
`transaction.
8319
8320
 If starting the transaction fails, will return
8321
 OGRERR_FAILURE. Datasources which do not support transactions will
8322
 always return OGRERR_UNSUPPORTED_OPERATION.
8323
8324
 Nested transactions are not supported.
8325
8326
 All changes done after the start of the transaction are definitely applied in
8327
 the datasource if CommitTransaction() is called. They may be canceled by
8328
 calling RollbackTransaction() instead.
8329
8330
 At the time of writing, transactions only apply on vector layers.
8331
8332
 Datasets that support transactions will advertise the ODsCTransactions
8333
 capability.  Use of transactions at dataset level is generally preferred to
8334
 transactions at layer level, whose scope is rarely limited to the layer from
8335
 which it was started.
8336
8337
 In case StartTransaction() fails, neither CommitTransaction() or
8338
 RollbackTransaction() should be called.
8339
8340
 If an error occurs after a successful StartTransaction(), the whole transaction
8341
 may or may not be implicitly canceled, depending on drivers. (e.g.  the PG
8342
 driver will cancel it, SQLite/GPKG not). In any case, in the event of an error,
8343
 an explicit call to RollbackTransaction() should be done to keep things
8344
 balanced.
8345
8346
 By default, when bForce is set to FALSE, only "efficient" transactions will be
8347
 attempted. Some drivers may offer an emulation of transactions, but sometimes
8348
 with significant overhead, in which case the user must explicitly allow for
8349
 such an emulation by setting bForce to TRUE. Drivers that offer emulated
8350
 transactions should advertise the ODsCEmulatedTransactions capability (and not
8351
 ODsCTransactions).
8352
8353
 This function is the same as the C function GDALDatasetStartTransaction().
8354
8355
 @param bForce can be set to TRUE if an emulation, possibly slow, of a
8356
 transaction
8357
               mechanism is acceptable.
8358
8359
 @return OGRERR_NONE on success.
8360
*/
8361
8362
OGRErr GDALDataset::StartTransaction(CPL_UNUSED int bForce)
8363
0
{
8364
0
    return OGRERR_UNSUPPORTED_OPERATION;
8365
0
}
8366
8367
/************************************************************************/
8368
/*                      GDALDatasetStartTransaction()                   */
8369
/************************************************************************/
8370
8371
/**
8372
 \brief For datasources which support transactions, StartTransaction creates a
8373
 transaction.
8374
8375
 If starting the transaction fails, will return
8376
 OGRERR_FAILURE. Datasources which do not support transactions will
8377
 always return OGRERR_UNSUPPORTED_OPERATION.
8378
8379
 Nested transactions are not supported.
8380
8381
 All changes done after the start of the transaction are definitely applied in
8382
 the datasource if CommitTransaction() is called. They may be canceled by
8383
 calling RollbackTransaction() instead.
8384
8385
 At the time of writing, transactions only apply on vector layers.
8386
8387
 Datasets that support transactions will advertise the ODsCTransactions
8388
 capability.
8389
 Use of transactions at dataset level is generally preferred to transactions at
8390
 layer level, whose scope is rarely limited to the layer from which it was
8391
 started.
8392
8393
 In case StartTransaction() fails, neither CommitTransaction() or
8394
 RollbackTransaction() should be called.
8395
8396
 If an error occurs after a successful StartTransaction(), the whole
8397
 transaction may or may not be implicitly canceled, depending on drivers. (e.g.
8398
 the PG driver will cancel it, SQLite/GPKG not). In any case, in the event of an
8399
 error, an explicit call to RollbackTransaction() should be done to keep things
8400
 balanced.
8401
8402
 By default, when bForce is set to FALSE, only "efficient" transactions will be
8403
 attempted. Some drivers may offer an emulation of transactions, but sometimes
8404
 with significant overhead, in which case the user must explicitly allow for
8405
 such an emulation by setting bForce to TRUE. Drivers that offer emulated
8406
 transactions should advertise the ODsCEmulatedTransactions capability (and not
8407
 ODsCTransactions).
8408
8409
 This function is the same as the C++ method GDALDataset::StartTransaction()
8410
8411
 @param hDS the dataset handle.
8412
 @param bForce can be set to TRUE if an emulation, possibly slow, of a
8413
 transaction
8414
               mechanism is acceptable.
8415
8416
 @return OGRERR_NONE on success.
8417
*/
8418
OGRErr GDALDatasetStartTransaction(GDALDatasetH hDS, int bForce)
8419
0
{
8420
0
    VALIDATE_POINTER1(hDS, "GDALDatasetStartTransaction",
8421
0
                      OGRERR_INVALID_HANDLE);
8422
8423
0
#ifdef OGRAPISPY_ENABLED
8424
0
    if (bOGRAPISpyEnabled)
8425
0
        OGRAPISpy_Dataset_StartTransaction(hDS, bForce);
8426
0
#endif
8427
8428
0
    return GDALDataset::FromHandle(hDS)->StartTransaction(bForce);
8429
0
}
8430
8431
/************************************************************************/
8432
/*                           CommitTransaction()                        */
8433
/************************************************************************/
8434
8435
/**
8436
 \brief For datasources which support transactions, CommitTransaction commits a
8437
 transaction.
8438
8439
 If no transaction is active, or the commit fails, will return
8440
 OGRERR_FAILURE. Datasources which do not support transactions will
8441
 always return OGRERR_UNSUPPORTED_OPERATION.
8442
8443
 Depending on drivers, this may or may not abort layer sequential readings that
8444
 are active.
8445
8446
 This function is the same as the C function GDALDatasetCommitTransaction().
8447
8448
 @return OGRERR_NONE on success.
8449
*/
8450
OGRErr GDALDataset::CommitTransaction()
8451
0
{
8452
0
    return OGRERR_UNSUPPORTED_OPERATION;
8453
0
}
8454
8455
/************************************************************************/
8456
/*                        GDALDatasetCommitTransaction()                */
8457
/************************************************************************/
8458
8459
/**
8460
 \brief For datasources which support transactions, CommitTransaction commits a
8461
 transaction.
8462
8463
 If no transaction is active, or the commit fails, will return
8464
 OGRERR_FAILURE. Datasources which do not support transactions will
8465
 always return OGRERR_UNSUPPORTED_OPERATION.
8466
8467
 Depending on drivers, this may or may not abort layer sequential readings that
8468
 are active.
8469
8470
 This function is the same as the C++ method GDALDataset::CommitTransaction()
8471
8472
 @return OGRERR_NONE on success.
8473
*/
8474
OGRErr GDALDatasetCommitTransaction(GDALDatasetH hDS)
8475
0
{
8476
0
    VALIDATE_POINTER1(hDS, "GDALDatasetCommitTransaction",
8477
0
                      OGRERR_INVALID_HANDLE);
8478
8479
0
#ifdef OGRAPISPY_ENABLED
8480
0
    if (bOGRAPISpyEnabled)
8481
0
        OGRAPISpy_Dataset_CommitTransaction(hDS);
8482
0
#endif
8483
8484
0
    return GDALDataset::FromHandle(hDS)->CommitTransaction();
8485
0
}
8486
8487
/************************************************************************/
8488
/*                           RollbackTransaction()                      */
8489
/************************************************************************/
8490
8491
/**
8492
 \brief For datasources which support transactions, RollbackTransaction will
8493
 roll back a datasource to its state before the start of the current
8494
 transaction.
8495
 If no transaction is active, or the rollback fails, will return
8496
 OGRERR_FAILURE. Datasources which do not support transactions will
8497
 always return OGRERR_UNSUPPORTED_OPERATION.
8498
8499
 This function is the same as the C function GDALDatasetRollbackTransaction().
8500
8501
 @return OGRERR_NONE on success.
8502
*/
8503
OGRErr GDALDataset::RollbackTransaction()
8504
0
{
8505
0
    return OGRERR_UNSUPPORTED_OPERATION;
8506
0
}
8507
8508
/************************************************************************/
8509
/*                     GDALDatasetRollbackTransaction()                 */
8510
/************************************************************************/
8511
8512
/**
8513
 \brief For datasources which support transactions, RollbackTransaction will
8514
 roll back a datasource to its state before the start of the current
8515
 transaction.
8516
 If no transaction is active, or the rollback fails, will return
8517
 OGRERR_FAILURE. Datasources which do not support transactions will
8518
 always return OGRERR_UNSUPPORTED_OPERATION.
8519
8520
 This function is the same as the C++ method GDALDataset::RollbackTransaction().
8521
8522
 @return OGRERR_NONE on success.
8523
*/
8524
OGRErr GDALDatasetRollbackTransaction(GDALDatasetH hDS)
8525
0
{
8526
0
    VALIDATE_POINTER1(hDS, "GDALDatasetRollbackTransaction",
8527
0
                      OGRERR_INVALID_HANDLE);
8528
8529
0
#ifdef OGRAPISPY_ENABLED
8530
0
    if (bOGRAPISpyEnabled)
8531
0
        OGRAPISpy_Dataset_RollbackTransaction(hDS);
8532
0
#endif
8533
8534
0
    return GDALDataset::FromHandle(hDS)->RollbackTransaction();
8535
0
}
8536
8537
//! @cond Doxygen_Suppress
8538
8539
/************************************************************************/
8540
/*                   ShareLockWithParentDataset()                       */
8541
/************************************************************************/
8542
8543
/* To be used typically by the GTiff driver to link overview datasets */
8544
/* with their main dataset, so that they share the same lock */
8545
/* Cf https://github.com/OSGeo/gdal/issues/1488 */
8546
/* The parent dataset should remain alive while the this dataset is alive */
8547
8548
void GDALDataset::ShareLockWithParentDataset(GDALDataset *poParentDataset)
8549
0
{
8550
0
    if (m_poPrivate != nullptr)
8551
0
    {
8552
0
        m_poPrivate->poParentDataset = poParentDataset;
8553
0
    }
8554
0
}
8555
8556
/************************************************************************/
8557
/*                   SetQueryLoggerFunc()                               */
8558
/************************************************************************/
8559
8560
bool GDALDataset::SetQueryLoggerFunc(CPL_UNUSED GDALQueryLoggerFunc callback,
8561
                                     CPL_UNUSED void *context)
8562
0
{
8563
0
    return false;
8564
0
}
8565
8566
/************************************************************************/
8567
/*                          EnterReadWrite()                            */
8568
/************************************************************************/
8569
8570
int GDALDataset::EnterReadWrite(GDALRWFlag eRWFlag)
8571
0
{
8572
0
    if (m_poPrivate == nullptr ||
8573
0
        IsThreadSafe(GDAL_OF_RASTER | (nOpenFlags & GDAL_OF_UPDATE)))
8574
0
        return FALSE;
8575
8576
0
    if (m_poPrivate->poParentDataset)
8577
0
        return m_poPrivate->poParentDataset->EnterReadWrite(eRWFlag);
8578
8579
0
    if (eAccess == GA_Update)
8580
0
    {
8581
0
        if (m_poPrivate->eStateReadWriteMutex ==
8582
0
            GDALAllowReadWriteMutexState::RW_MUTEX_STATE_UNKNOWN)
8583
0
        {
8584
            // In case dead-lock would occur, which is not impossible,
8585
            // this can be used to prevent it, but at the risk of other
8586
            // issues.
8587
0
            if (CPLTestBool(
8588
0
                    CPLGetConfigOption("GDAL_ENABLE_READ_WRITE_MUTEX", "YES")))
8589
0
            {
8590
0
                m_poPrivate->eStateReadWriteMutex =
8591
0
                    GDALAllowReadWriteMutexState::RW_MUTEX_STATE_ALLOWED;
8592
0
            }
8593
0
            else
8594
0
            {
8595
0
                m_poPrivate->eStateReadWriteMutex =
8596
0
                    GDALAllowReadWriteMutexState::RW_MUTEX_STATE_DISABLED;
8597
0
            }
8598
0
        }
8599
0
        if (m_poPrivate->eStateReadWriteMutex ==
8600
0
            GDALAllowReadWriteMutexState::RW_MUTEX_STATE_ALLOWED)
8601
0
        {
8602
            // There should be no race related to creating this mutex since
8603
            // it should be first created through IWriteBlock() / IRasterIO()
8604
            // and then GDALRasterBlock might call it from another thread.
8605
#ifdef DEBUG_VERBOSE
8606
            CPLDebug("GDAL",
8607
                     "[Thread " CPL_FRMT_GIB "] Acquiring RW mutex for %s",
8608
                     CPLGetPID(), GetDescription());
8609
#endif
8610
0
            CPLCreateOrAcquireMutex(&(m_poPrivate->hMutex), 1000.0);
8611
8612
0
            const int nCountMutex =
8613
0
                m_poPrivate->oMapThreadToMutexTakenCount[CPLGetPID()]++;
8614
0
            if (nCountMutex == 0 && eRWFlag == GF_Read)
8615
0
            {
8616
0
                CPLReleaseMutex(m_poPrivate->hMutex);
8617
0
                for (int i = 0; i < nBands; i++)
8618
0
                {
8619
0
                    auto blockCache = papoBands[i]->poBandBlockCache;
8620
0
                    if (blockCache)
8621
0
                        blockCache->WaitCompletionPendingTasks();
8622
0
                }
8623
0
                CPLCreateOrAcquireMutex(&(m_poPrivate->hMutex), 1000.0);
8624
0
            }
8625
8626
0
            return TRUE;
8627
0
        }
8628
0
    }
8629
0
    return FALSE;
8630
0
}
8631
8632
/************************************************************************/
8633
/*                         LeaveReadWrite()                             */
8634
/************************************************************************/
8635
8636
void GDALDataset::LeaveReadWrite()
8637
0
{
8638
0
    if (m_poPrivate)
8639
0
    {
8640
0
        if (m_poPrivate->poParentDataset)
8641
0
        {
8642
0
            m_poPrivate->poParentDataset->LeaveReadWrite();
8643
0
            return;
8644
0
        }
8645
8646
0
        m_poPrivate->oMapThreadToMutexTakenCount[CPLGetPID()]--;
8647
0
        CPLReleaseMutex(m_poPrivate->hMutex);
8648
#ifdef DEBUG_VERBOSE
8649
        CPLDebug("GDAL", "[Thread " CPL_FRMT_GIB "] Releasing RW mutex for %s",
8650
                 CPLGetPID(), GetDescription());
8651
#endif
8652
0
    }
8653
0
}
8654
8655
/************************************************************************/
8656
/*                           InitRWLock()                               */
8657
/************************************************************************/
8658
8659
void GDALDataset::InitRWLock()
8660
0
{
8661
0
    if (m_poPrivate)
8662
0
    {
8663
0
        if (m_poPrivate->poParentDataset)
8664
0
        {
8665
0
            m_poPrivate->poParentDataset->InitRWLock();
8666
0
            return;
8667
0
        }
8668
8669
0
        if (m_poPrivate->eStateReadWriteMutex ==
8670
0
            GDALAllowReadWriteMutexState::RW_MUTEX_STATE_UNKNOWN)
8671
0
        {
8672
0
            if (EnterReadWrite(GF_Write))
8673
0
                LeaveReadWrite();
8674
0
        }
8675
0
    }
8676
0
}
8677
8678
/************************************************************************/
8679
/*                       DisableReadWriteMutex()                        */
8680
/************************************************************************/
8681
8682
// The mutex logic is broken in multi-threaded situations, for example
8683
// with 2 WarpedVRT datasets being read at the same time. In that
8684
// particular case, the mutex is not needed, so allow the VRTWarpedDataset code
8685
// to disable it.
8686
void GDALDataset::DisableReadWriteMutex()
8687
0
{
8688
0
    if (m_poPrivate)
8689
0
    {
8690
0
        if (m_poPrivate->poParentDataset)
8691
0
        {
8692
0
            m_poPrivate->poParentDataset->DisableReadWriteMutex();
8693
0
            return;
8694
0
        }
8695
8696
0
        m_poPrivate->eStateReadWriteMutex =
8697
0
            GDALAllowReadWriteMutexState::RW_MUTEX_STATE_DISABLED;
8698
0
    }
8699
0
}
8700
8701
/************************************************************************/
8702
/*                      TemporarilyDropReadWriteLock()                  */
8703
/************************************************************************/
8704
8705
void GDALDataset::TemporarilyDropReadWriteLock()
8706
0
{
8707
0
    if (m_poPrivate == nullptr)
8708
0
        return;
8709
8710
0
    if (m_poPrivate->poParentDataset)
8711
0
    {
8712
0
        m_poPrivate->poParentDataset->TemporarilyDropReadWriteLock();
8713
0
        return;
8714
0
    }
8715
8716
0
#ifndef __COVERITY__
8717
0
    if (m_poPrivate->hMutex)
8718
0
    {
8719
#ifdef DEBUG_VERBOSE
8720
        CPLDebug("GDAL",
8721
                 "[Thread " CPL_FRMT_GIB "] "
8722
                 "Temporarily drop RW mutex for %s",
8723
                 CPLGetPID(), GetDescription());
8724
#endif
8725
0
        CPLAcquireMutex(m_poPrivate->hMutex, 1000.0);
8726
0
        const int nCount =
8727
0
            m_poPrivate->oMapThreadToMutexTakenCount[CPLGetPID()];
8728
#ifdef DEBUG_EXTRA
8729
        m_poPrivate->oMapThreadToMutexTakenCountSaved[CPLGetPID()] = nCount;
8730
#endif
8731
0
        for (int i = 0; i < nCount + 1; i++)
8732
0
        {
8733
            // The mutex is recursive
8734
0
            CPLReleaseMutex(m_poPrivate->hMutex);
8735
0
        }
8736
0
    }
8737
0
#endif
8738
0
}
8739
8740
/************************************************************************/
8741
/*                       ReacquireReadWriteLock()                       */
8742
/************************************************************************/
8743
8744
void GDALDataset::ReacquireReadWriteLock()
8745
0
{
8746
0
    if (m_poPrivate == nullptr)
8747
0
        return;
8748
8749
0
    if (m_poPrivate->poParentDataset)
8750
0
    {
8751
0
        m_poPrivate->poParentDataset->ReacquireReadWriteLock();
8752
0
        return;
8753
0
    }
8754
8755
0
#ifndef __COVERITY__
8756
0
    if (m_poPrivate->hMutex)
8757
0
    {
8758
#ifdef DEBUG_VERBOSE
8759
        CPLDebug("GDAL",
8760
                 "[Thread " CPL_FRMT_GIB "] "
8761
                 "Reacquire temporarily dropped RW mutex for %s",
8762
                 CPLGetPID(), GetDescription());
8763
#endif
8764
0
        CPLAcquireMutex(m_poPrivate->hMutex, 1000.0);
8765
0
        const int nCount =
8766
0
            m_poPrivate->oMapThreadToMutexTakenCount[CPLGetPID()];
8767
#ifdef DEBUG_EXTRA
8768
        CPLAssert(nCount ==
8769
                  m_poPrivate->oMapThreadToMutexTakenCountSaved[CPLGetPID()]);
8770
#endif
8771
0
        if (nCount == 0)
8772
0
            CPLReleaseMutex(m_poPrivate->hMutex);
8773
0
        for (int i = 0; i < nCount - 1; i++)
8774
0
        {
8775
            // The mutex is recursive
8776
0
            CPLAcquireMutex(m_poPrivate->hMutex, 1000.0);
8777
0
        }
8778
0
    }
8779
0
#endif
8780
0
}
8781
8782
/************************************************************************/
8783
/*                           AcquireMutex()                             */
8784
/************************************************************************/
8785
8786
int GDALDataset::AcquireMutex()
8787
0
{
8788
0
    if (m_poPrivate == nullptr)
8789
0
        return 0;
8790
0
    if (m_poPrivate->poParentDataset)
8791
0
    {
8792
0
        return m_poPrivate->poParentDataset->AcquireMutex();
8793
0
    }
8794
8795
0
    return CPLCreateOrAcquireMutex(&(m_poPrivate->hMutex), 1000.0);
8796
0
}
8797
8798
/************************************************************************/
8799
/*                          ReleaseMutex()                              */
8800
/************************************************************************/
8801
8802
void GDALDataset::ReleaseMutex()
8803
0
{
8804
0
    if (m_poPrivate)
8805
0
    {
8806
0
        if (m_poPrivate->poParentDataset)
8807
0
        {
8808
0
            m_poPrivate->poParentDataset->ReleaseMutex();
8809
0
            return;
8810
0
        }
8811
8812
0
        CPLReleaseMutex(m_poPrivate->hMutex);
8813
0
    }
8814
0
}
8815
8816
//! @endcond
8817
8818
/************************************************************************/
8819
/*              GDALDataset::Features::Iterator::Private                */
8820
/************************************************************************/
8821
8822
struct GDALDataset::Features::Iterator::Private
8823
{
8824
    GDALDataset::FeatureLayerPair m_oPair{};
8825
    GDALDataset *m_poDS = nullptr;
8826
    bool m_bEOF = true;
8827
};
8828
8829
GDALDataset::Features::Iterator::Iterator(GDALDataset *poDS, bool bStart)
8830
0
    : m_poPrivate(new GDALDataset::Features::Iterator::Private())
8831
0
{
8832
0
    m_poPrivate->m_poDS = poDS;
8833
0
    if (bStart)
8834
0
    {
8835
0
        poDS->ResetReading();
8836
0
        m_poPrivate->m_oPair.feature.reset(poDS->GetNextFeature(
8837
0
            &m_poPrivate->m_oPair.layer, nullptr, nullptr, nullptr));
8838
0
        m_poPrivate->m_bEOF = m_poPrivate->m_oPair.feature == nullptr;
8839
0
    }
8840
0
}
8841
8842
0
GDALDataset::Features::Iterator::~Iterator() = default;
8843
8844
const GDALDataset::FeatureLayerPair &
8845
GDALDataset::Features::Iterator::operator*() const
8846
0
{
8847
0
    return m_poPrivate->m_oPair;
8848
0
}
8849
8850
GDALDataset::Features::Iterator &GDALDataset::Features::Iterator::operator++()
8851
0
{
8852
0
    m_poPrivate->m_oPair.feature.reset(m_poPrivate->m_poDS->GetNextFeature(
8853
0
        &m_poPrivate->m_oPair.layer, nullptr, nullptr, nullptr));
8854
0
    m_poPrivate->m_bEOF = m_poPrivate->m_oPair.feature == nullptr;
8855
0
    return *this;
8856
0
}
8857
8858
bool GDALDataset::Features::Iterator::operator!=(const Iterator &it) const
8859
0
{
8860
0
    return m_poPrivate->m_bEOF != it.m_poPrivate->m_bEOF;
8861
0
}
8862
8863
/************************************************************************/
8864
/*                            GetFeatures()                             */
8865
/************************************************************************/
8866
8867
/** Function that return an iterable object over features in the dataset
8868
 * layer.
8869
 *
8870
 * This is a C++ iterator friendly version of GetNextFeature().
8871
 *
8872
 * Using this iterator for standard range-based loops is safe, but
8873
 * due to implementation limitations, you shouldn't try to access
8874
 * (dereference) more than one iterator step at a time, since the
8875
 * FeatureLayerPair reference which is returned is reused.
8876
 *
8877
 * Typical use is:
8878
 * \code{.cpp}
8879
 * for( auto&& oFeatureLayerPair: poDS->GetFeatures() )
8880
 * {
8881
 *       std::cout << "Feature of layer " <<
8882
 *               oFeatureLayerPair.layer->GetName() << std::endl;
8883
 *       oFeatureLayerPair.feature->DumpReadable();
8884
 * }
8885
 * \endcode
8886
 *
8887
 * @see GetNextFeature()
8888
 *
8889
 */
8890
GDALDataset::Features GDALDataset::GetFeatures()
8891
0
{
8892
0
    return Features(this);
8893
0
}
8894
8895
/************************************************************************/
8896
/*                                 begin()                              */
8897
/************************************************************************/
8898
8899
/**
8900
 \brief Return beginning of feature iterator.
8901
8902
*/
8903
8904
const GDALDataset::Features::Iterator GDALDataset::Features::begin() const
8905
0
{
8906
0
    return {m_poSelf, true};
8907
0
}
8908
8909
/************************************************************************/
8910
/*                                  end()                               */
8911
/************************************************************************/
8912
8913
/**
8914
 \brief Return end of feature iterator.
8915
8916
*/
8917
8918
const GDALDataset::Features::Iterator GDALDataset::Features::end() const
8919
0
{
8920
0
    return {m_poSelf, false};
8921
0
}
8922
8923
/************************************************************************/
8924
/*               GDALDataset::Layers::Iterator::Private                 */
8925
/************************************************************************/
8926
8927
struct GDALDataset::Layers::Iterator::Private
8928
{
8929
    OGRLayer *m_poLayer = nullptr;
8930
    int m_iCurLayer = 0;
8931
    int m_nLayerCount = 0;
8932
    GDALDataset *m_poDS = nullptr;
8933
};
8934
8935
0
GDALDataset::Layers::Iterator::Iterator() : m_poPrivate(new Private())
8936
0
{
8937
0
}
8938
8939
// False positive of cppcheck 1.72
8940
// cppcheck-suppress uninitMemberVar
8941
GDALDataset::Layers::Iterator::Iterator(const Iterator &oOther)
8942
0
    : m_poPrivate(new Private(*(oOther.m_poPrivate)))
8943
0
{
8944
0
}
8945
8946
GDALDataset::Layers::Iterator::Iterator(Iterator &&oOther) noexcept
8947
0
    : m_poPrivate(std::move(oOther.m_poPrivate))
8948
0
{
8949
0
}
8950
8951
GDALDataset::Layers::Iterator::Iterator(GDALDataset *poDS, bool bStart)
8952
0
    : m_poPrivate(new Private())
8953
0
{
8954
0
    m_poPrivate->m_poDS = poDS;
8955
0
    m_poPrivate->m_nLayerCount = poDS->GetLayerCount();
8956
0
    if (bStart)
8957
0
    {
8958
0
        if (m_poPrivate->m_nLayerCount)
8959
0
            m_poPrivate->m_poLayer = poDS->GetLayer(0);
8960
0
    }
8961
0
    else
8962
0
    {
8963
0
        m_poPrivate->m_iCurLayer = m_poPrivate->m_nLayerCount;
8964
0
    }
8965
0
}
8966
8967
0
GDALDataset::Layers::Iterator::~Iterator() = default;
8968
8969
// False positive of cppcheck 1.72
8970
// cppcheck-suppress operatorEqVarError
8971
GDALDataset::Layers::Iterator &
8972
GDALDataset::Layers::Iterator::operator=(const Iterator &oOther)
8973
0
{
8974
0
    *m_poPrivate = *oOther.m_poPrivate;
8975
0
    return *this;
8976
0
}
8977
8978
GDALDataset::Layers::Iterator &GDALDataset::Layers::Iterator::operator=(
8979
    GDALDataset::Layers::Iterator &&oOther) noexcept
8980
0
{
8981
0
    m_poPrivate = std::move(oOther.m_poPrivate);
8982
0
    return *this;
8983
0
}
8984
8985
OGRLayer *GDALDataset::Layers::Iterator::operator*() const
8986
0
{
8987
0
    return m_poPrivate->m_poLayer;
8988
0
}
8989
8990
GDALDataset::Layers::Iterator &GDALDataset::Layers::Iterator::operator++()
8991
0
{
8992
0
    m_poPrivate->m_iCurLayer++;
8993
0
    if (m_poPrivate->m_iCurLayer < m_poPrivate->m_nLayerCount)
8994
0
    {
8995
0
        m_poPrivate->m_poLayer =
8996
0
            m_poPrivate->m_poDS->GetLayer(m_poPrivate->m_iCurLayer);
8997
0
    }
8998
0
    else
8999
0
    {
9000
0
        m_poPrivate->m_poLayer = nullptr;
9001
0
    }
9002
0
    return *this;
9003
0
}
9004
9005
GDALDataset::Layers::Iterator GDALDataset::Layers::Iterator::operator++(int)
9006
0
{
9007
0
    GDALDataset::Layers::Iterator temp = *this;
9008
0
    ++(*this);
9009
0
    return temp;
9010
0
}
9011
9012
bool GDALDataset::Layers::Iterator::operator!=(const Iterator &it) const
9013
0
{
9014
0
    return m_poPrivate->m_iCurLayer != it.m_poPrivate->m_iCurLayer;
9015
0
}
9016
9017
/************************************************************************/
9018
/*                             GetLayers()                              */
9019
/************************************************************************/
9020
9021
/** Function that returns an iterable object over layers in the dataset.
9022
 *
9023
 * This is a C++ iterator friendly version of GetLayer().
9024
 *
9025
 * Typical use is:
9026
 * \code{.cpp}
9027
 * for( auto&& poLayer: poDS->GetLayers() )
9028
 * {
9029
 *       std::cout << "Layer  << poLayer->GetName() << std::endl;
9030
 * }
9031
 * \endcode
9032
 *
9033
 * @see GetLayer()
9034
 *
9035
 */
9036
GDALDataset::Layers GDALDataset::GetLayers()
9037
0
{
9038
0
    return Layers(this);
9039
0
}
9040
9041
/************************************************************************/
9042
/*                                 begin()                              */
9043
/************************************************************************/
9044
9045
/**
9046
 \brief Return beginning of layer iterator.
9047
9048
*/
9049
9050
GDALDataset::Layers::Iterator GDALDataset::Layers::begin() const
9051
0
{
9052
0
    return {m_poSelf, true};
9053
0
}
9054
9055
/************************************************************************/
9056
/*                                  end()                               */
9057
/************************************************************************/
9058
9059
/**
9060
 \brief Return end of layer iterator.
9061
9062
*/
9063
9064
GDALDataset::Layers::Iterator GDALDataset::Layers::end() const
9065
0
{
9066
0
    return {m_poSelf, false};
9067
0
}
9068
9069
/************************************************************************/
9070
/*                                  size()                             */
9071
/************************************************************************/
9072
9073
/**
9074
 \brief Get the number of layers in this dataset.
9075
9076
 @return layer count.
9077
9078
*/
9079
9080
size_t GDALDataset::Layers::size() const
9081
0
{
9082
0
    return static_cast<size_t>(m_poSelf->GetLayerCount());
9083
0
}
9084
9085
/************************************************************************/
9086
/*                                operator[]()                          */
9087
/************************************************************************/
9088
/**
9089
 \brief Fetch a layer by index.
9090
9091
 The returned layer remains owned by the
9092
 GDALDataset and should not be deleted by the application.
9093
9094
 @param iLayer a layer number between 0 and size()-1.
9095
9096
 @return the layer, or nullptr if iLayer is out of range or an error occurs.
9097
9098
*/
9099
9100
OGRLayer *GDALDataset::Layers::operator[](int iLayer)
9101
0
{
9102
0
    return m_poSelf->GetLayer(iLayer);
9103
0
}
9104
9105
/************************************************************************/
9106
/*                                operator[]()                          */
9107
/************************************************************************/
9108
/**
9109
 \brief Fetch a layer by index.
9110
9111
 The returned layer remains owned by the
9112
 GDALDataset and should not be deleted by the application.
9113
9114
 @param iLayer a layer number between 0 and size()-1.
9115
9116
 @return the layer, or nullptr if iLayer is out of range or an error occurs.
9117
9118
*/
9119
9120
OGRLayer *GDALDataset::Layers::operator[](size_t iLayer)
9121
0
{
9122
0
    return m_poSelf->GetLayer(static_cast<int>(iLayer));
9123
0
}
9124
9125
/************************************************************************/
9126
/*                                operator[]()                          */
9127
/************************************************************************/
9128
/**
9129
 \brief Fetch a layer by name.
9130
9131
 The returned layer remains owned by the
9132
 GDALDataset and should not be deleted by the application.
9133
9134
 @param pszLayerName layer name
9135
9136
 @return the layer, or nullptr if pszLayerName does not match with a layer
9137
9138
*/
9139
9140
OGRLayer *GDALDataset::Layers::operator[](const char *pszLayerName)
9141
0
{
9142
0
    return m_poSelf->GetLayerByName(pszLayerName);
9143
0
}
9144
9145
/************************************************************************/
9146
/*               GDALDataset::ConstLayers::Iterator::Private            */
9147
/************************************************************************/
9148
9149
struct GDALDataset::ConstLayers::Iterator::Private
9150
{
9151
    const OGRLayer *m_poLayer = nullptr;
9152
    int m_iCurLayer = 0;
9153
    int m_nLayerCount = 0;
9154
    const GDALDataset *m_poDS = nullptr;
9155
};
9156
9157
0
GDALDataset::ConstLayers::Iterator::Iterator() : m_poPrivate(new Private())
9158
0
{
9159
0
}
9160
9161
// False positive of cppcheck 1.72
9162
// cppcheck-suppress uninitMemberVar
9163
GDALDataset::ConstLayers::Iterator::Iterator(const Iterator &oOther)
9164
0
    : m_poPrivate(new Private(*(oOther.m_poPrivate)))
9165
0
{
9166
0
}
9167
9168
GDALDataset::ConstLayers::Iterator::Iterator(Iterator &&oOther) noexcept
9169
0
    : m_poPrivate(std::move(oOther.m_poPrivate))
9170
0
{
9171
0
}
9172
9173
GDALDataset::ConstLayers::Iterator::Iterator(const GDALDataset *poDS,
9174
                                             bool bStart)
9175
0
    : m_poPrivate(new Private())
9176
0
{
9177
0
    m_poPrivate->m_poDS = poDS;
9178
0
    m_poPrivate->m_nLayerCount = poDS->GetLayerCount();
9179
0
    if (bStart)
9180
0
    {
9181
0
        if (m_poPrivate->m_nLayerCount)
9182
0
            m_poPrivate->m_poLayer = poDS->GetLayer(0);
9183
0
    }
9184
0
    else
9185
0
    {
9186
0
        m_poPrivate->m_iCurLayer = m_poPrivate->m_nLayerCount;
9187
0
    }
9188
0
}
9189
9190
0
GDALDataset::ConstLayers::Iterator::~Iterator() = default;
9191
9192
// False positive of cppcheck 1.72
9193
// cppcheck-suppress operatorEqVarError
9194
GDALDataset::ConstLayers::Iterator &
9195
GDALDataset::ConstLayers::Iterator::operator=(const Iterator &oOther)
9196
0
{
9197
0
    *m_poPrivate = *oOther.m_poPrivate;
9198
0
    return *this;
9199
0
}
9200
9201
GDALDataset::ConstLayers::Iterator &
9202
GDALDataset::ConstLayers::Iterator::operator=(
9203
    GDALDataset::ConstLayers::Iterator &&oOther) noexcept
9204
0
{
9205
0
    m_poPrivate = std::move(oOther.m_poPrivate);
9206
0
    return *this;
9207
0
}
9208
9209
const OGRLayer *GDALDataset::ConstLayers::Iterator::operator*() const
9210
0
{
9211
0
    return m_poPrivate->m_poLayer;
9212
0
}
9213
9214
GDALDataset::ConstLayers::Iterator &
9215
GDALDataset::ConstLayers::Iterator::operator++()
9216
0
{
9217
0
    m_poPrivate->m_iCurLayer++;
9218
0
    if (m_poPrivate->m_iCurLayer < m_poPrivate->m_nLayerCount)
9219
0
    {
9220
0
        m_poPrivate->m_poLayer =
9221
0
            m_poPrivate->m_poDS->GetLayer(m_poPrivate->m_iCurLayer);
9222
0
    }
9223
0
    else
9224
0
    {
9225
0
        m_poPrivate->m_poLayer = nullptr;
9226
0
    }
9227
0
    return *this;
9228
0
}
9229
9230
GDALDataset::ConstLayers::Iterator
9231
GDALDataset::ConstLayers::Iterator::operator++(int)
9232
0
{
9233
0
    GDALDataset::ConstLayers::Iterator temp = *this;
9234
0
    ++(*this);
9235
0
    return temp;
9236
0
}
9237
9238
bool GDALDataset::ConstLayers::Iterator::operator!=(const Iterator &it) const
9239
0
{
9240
0
    return m_poPrivate->m_iCurLayer != it.m_poPrivate->m_iCurLayer;
9241
0
}
9242
9243
/************************************************************************/
9244
/*                             GetLayers()                              */
9245
/************************************************************************/
9246
9247
/** Function that returns an iterable object over layers in the dataset.
9248
 *
9249
 * This is a C++ iterator friendly version of GetLayer().
9250
 *
9251
 * Typical use is:
9252
 * \code{.cpp}
9253
 * for( auto&& poLayer: poDS->GetLayers() )
9254
 * {
9255
 *       std::cout << "Layer  << poLayer->GetName() << std::endl;
9256
 * }
9257
 * \endcode
9258
 *
9259
 * @see GetLayer()
9260
 *
9261
 * @since GDAL 3.12
9262
 */
9263
GDALDataset::ConstLayers GDALDataset::GetLayers() const
9264
0
{
9265
0
    return ConstLayers(this);
9266
0
}
9267
9268
/************************************************************************/
9269
/*                                 begin()                              */
9270
/************************************************************************/
9271
9272
/**
9273
 \brief Return beginning of layer iterator.
9274
9275
 @since GDAL 3.12
9276
*/
9277
9278
GDALDataset::ConstLayers::Iterator GDALDataset::ConstLayers::begin() const
9279
0
{
9280
0
    return {m_poSelf, true};
9281
0
}
9282
9283
/************************************************************************/
9284
/*                                  end()                               */
9285
/************************************************************************/
9286
9287
/**
9288
 \brief Return end of layer iterator.
9289
9290
 @since GDAL 3.12
9291
*/
9292
9293
GDALDataset::ConstLayers::Iterator GDALDataset::ConstLayers::end() const
9294
0
{
9295
0
    return {m_poSelf, false};
9296
0
}
9297
9298
/************************************************************************/
9299
/*                                  size()                             */
9300
/************************************************************************/
9301
9302
/**
9303
 \brief Get the number of layers in this dataset.
9304
9305
 @return layer count.
9306
9307
 @since GDAL 3.12
9308
*/
9309
9310
size_t GDALDataset::ConstLayers::size() const
9311
0
{
9312
0
    return static_cast<size_t>(m_poSelf->GetLayerCount());
9313
0
}
9314
9315
/************************************************************************/
9316
/*                                operator[]()                          */
9317
/************************************************************************/
9318
/**
9319
 \brief Fetch a layer by index.
9320
9321
 The returned layer remains owned by the
9322
 GDALDataset and should not be deleted by the application.
9323
9324
 @param iLayer a layer number between 0 and size()-1.
9325
9326
 @return the layer, or nullptr if iLayer is out of range or an error occurs.
9327
9328
 @since GDAL 3.12
9329
*/
9330
9331
const OGRLayer *GDALDataset::ConstLayers::operator[](int iLayer)
9332
0
{
9333
0
    return m_poSelf->GetLayer(iLayer);
9334
0
}
9335
9336
/************************************************************************/
9337
/*                                operator[]()                          */
9338
/************************************************************************/
9339
/**
9340
 \brief Fetch a layer by index.
9341
9342
 The returned layer remains owned by the
9343
 GDALDataset and should not be deleted by the application.
9344
9345
 @param iLayer a layer number between 0 and size()-1.
9346
9347
 @return the layer, or nullptr if iLayer is out of range or an error occurs.
9348
9349
 @since GDAL 3.12
9350
*/
9351
9352
const OGRLayer *GDALDataset::ConstLayers::operator[](size_t iLayer)
9353
0
{
9354
0
    return m_poSelf->GetLayer(static_cast<int>(iLayer));
9355
0
}
9356
9357
/************************************************************************/
9358
/*                                operator[]()                          */
9359
/************************************************************************/
9360
/**
9361
 \brief Fetch a layer by name.
9362
9363
 The returned layer remains owned by the
9364
 GDALDataset and should not be deleted by the application.
9365
9366
 @param pszLayerName layer name
9367
9368
 @return the layer, or nullptr if pszLayerName does not match with a layer
9369
9370
 @since GDAL 3.12
9371
*/
9372
9373
const OGRLayer *GDALDataset::ConstLayers::operator[](const char *pszLayerName)
9374
0
{
9375
0
    return const_cast<GDALDataset *>(m_poSelf)->GetLayerByName(pszLayerName);
9376
0
}
9377
9378
/************************************************************************/
9379
/*               GDALDataset::Bands::Iterator::Private                 */
9380
/************************************************************************/
9381
9382
struct GDALDataset::Bands::Iterator::Private
9383
{
9384
    GDALRasterBand *m_poBand = nullptr;
9385
    int m_iCurBand = 0;
9386
    int m_nBandCount = 0;
9387
    GDALDataset *m_poDS = nullptr;
9388
};
9389
9390
GDALDataset::Bands::Iterator::Iterator(GDALDataset *poDS, bool bStart)
9391
0
    : m_poPrivate(new GDALDataset::Bands::Iterator::Private())
9392
0
{
9393
0
    m_poPrivate->m_poDS = poDS;
9394
0
    m_poPrivate->m_nBandCount = poDS->GetRasterCount();
9395
0
    if (bStart)
9396
0
    {
9397
0
        if (m_poPrivate->m_nBandCount)
9398
0
            m_poPrivate->m_poBand = poDS->GetRasterBand(1);
9399
0
    }
9400
0
    else
9401
0
    {
9402
0
        m_poPrivate->m_iCurBand = m_poPrivate->m_nBandCount;
9403
0
    }
9404
0
}
9405
9406
0
GDALDataset::Bands::Iterator::~Iterator() = default;
9407
9408
GDALRasterBand *GDALDataset::Bands::Iterator::operator*()
9409
0
{
9410
0
    return m_poPrivate->m_poBand;
9411
0
}
9412
9413
GDALDataset::Bands::Iterator &GDALDataset::Bands::Iterator::operator++()
9414
0
{
9415
0
    m_poPrivate->m_iCurBand++;
9416
0
    if (m_poPrivate->m_iCurBand < m_poPrivate->m_nBandCount)
9417
0
    {
9418
0
        m_poPrivate->m_poBand =
9419
0
            m_poPrivate->m_poDS->GetRasterBand(1 + m_poPrivate->m_iCurBand);
9420
0
    }
9421
0
    else
9422
0
    {
9423
0
        m_poPrivate->m_poBand = nullptr;
9424
0
    }
9425
0
    return *this;
9426
0
}
9427
9428
bool GDALDataset::Bands::Iterator::operator!=(const Iterator &it) const
9429
0
{
9430
0
    return m_poPrivate->m_iCurBand != it.m_poPrivate->m_iCurBand;
9431
0
}
9432
9433
/************************************************************************/
9434
/*                              GetBands()                              */
9435
/************************************************************************/
9436
9437
/** Function that returns an iterable object over GDALRasterBand in the dataset.
9438
 *
9439
 * This is a C++ iterator friendly version of GetRasterBand().
9440
 *
9441
 * Typical use is:
9442
 * \code{.cpp}
9443
 * for( auto&& poBand: poDS->GetBands() )
9444
 * {
9445
 *       std::cout << "Band  << poBand->GetDescription() << std::endl;
9446
 * }
9447
 * \endcode
9448
 *
9449
 * @see GetRasterBand()
9450
 *
9451
 */
9452
GDALDataset::Bands GDALDataset::GetBands()
9453
0
{
9454
0
    return Bands(this);
9455
0
}
9456
9457
/************************************************************************/
9458
/*                                 begin()                              */
9459
/************************************************************************/
9460
9461
/**
9462
 \brief Return beginning of band iterator.
9463
9464
*/
9465
9466
const GDALDataset::Bands::Iterator GDALDataset::Bands::begin() const
9467
0
{
9468
0
    return {m_poSelf, true};
9469
0
}
9470
9471
/************************************************************************/
9472
/*                                  end()                               */
9473
/************************************************************************/
9474
9475
/**
9476
 \brief Return end of band iterator.
9477
9478
*/
9479
9480
const GDALDataset::Bands::Iterator GDALDataset::Bands::end() const
9481
0
{
9482
0
    return {m_poSelf, false};
9483
0
}
9484
9485
/************************************************************************/
9486
/*                                  size()                             */
9487
/************************************************************************/
9488
9489
/**
9490
 \brief Get the number of raster bands in this dataset.
9491
9492
 @return raster band count.
9493
9494
*/
9495
9496
size_t GDALDataset::Bands::size() const
9497
0
{
9498
0
    return static_cast<size_t>(m_poSelf->GetRasterCount());
9499
0
}
9500
9501
/************************************************************************/
9502
/*                                operator[]()                          */
9503
/************************************************************************/
9504
/**
9505
 \brief Fetch a raster band by index.
9506
9507
 The returned band remains owned by the
9508
 GDALDataset and should not be deleted by the application.
9509
9510
 @warning Contrary to GDALDataset::GetRasterBand(), the indexing here is
9511
 consistent with the conventions of C/C++, i.e. starting at 0.
9512
9513
 @param iBand a band index between 0 and size()-1.
9514
9515
 @return the band, or nullptr if iBand is out of range or an error occurs.
9516
9517
*/
9518
9519
GDALRasterBand *GDALDataset::Bands::operator[](int iBand)
9520
0
{
9521
0
    return m_poSelf->GetRasterBand(1 + iBand);
9522
0
}
9523
9524
/************************************************************************/
9525
/*                                operator[]()                          */
9526
/************************************************************************/
9527
9528
/**
9529
 \brief Fetch a raster band by index.
9530
9531
 The returned band remains owned by the
9532
 GDALDataset and should not be deleted by the application.
9533
9534
 @warning Contrary to GDALDataset::GetRasterBand(), the indexing here is
9535
 consistent with the conventions of C/C++, i.e. starting at 0.
9536
9537
 @param iBand a band index between 0 and size()-1.
9538
9539
 @return the band, or nullptr if iBand is out of range or an error occurs.
9540
9541
*/
9542
9543
GDALRasterBand *GDALDataset::Bands::operator[](size_t iBand)
9544
0
{
9545
0
    return m_poSelf->GetRasterBand(1 + static_cast<int>(iBand));
9546
0
}
9547
9548
/************************************************************************/
9549
/*               GDALDataset::ConstBands::Iterator::Private             */
9550
/************************************************************************/
9551
9552
struct GDALDataset::ConstBands::Iterator::Private
9553
{
9554
    const GDALRasterBand *m_poBand = nullptr;
9555
    int m_iCurBand = 0;
9556
    int m_nBandCount = 0;
9557
    const GDALDataset *m_poDS = nullptr;
9558
};
9559
9560
GDALDataset::ConstBands::Iterator::Iterator(const GDALDataset *poDS,
9561
                                            bool bStart)
9562
0
    : m_poPrivate(new GDALDataset::ConstBands::Iterator::Private())
9563
0
{
9564
0
    m_poPrivate->m_poDS = poDS;
9565
0
    m_poPrivate->m_nBandCount = poDS->GetRasterCount();
9566
0
    if (bStart)
9567
0
    {
9568
0
        if (m_poPrivate->m_nBandCount)
9569
0
            m_poPrivate->m_poBand = poDS->GetRasterBand(1);
9570
0
    }
9571
0
    else
9572
0
    {
9573
0
        m_poPrivate->m_iCurBand = m_poPrivate->m_nBandCount;
9574
0
    }
9575
0
}
9576
9577
0
GDALDataset::ConstBands::Iterator::~Iterator() = default;
9578
9579
const GDALRasterBand *GDALDataset::ConstBands::Iterator::operator*() const
9580
0
{
9581
0
    return m_poPrivate->m_poBand;
9582
0
}
9583
9584
GDALDataset::ConstBands::Iterator &
9585
GDALDataset::ConstBands::Iterator::operator++()
9586
0
{
9587
0
    m_poPrivate->m_iCurBand++;
9588
0
    if (m_poPrivate->m_iCurBand < m_poPrivate->m_nBandCount)
9589
0
    {
9590
0
        m_poPrivate->m_poBand =
9591
0
            m_poPrivate->m_poDS->GetRasterBand(1 + m_poPrivate->m_iCurBand);
9592
0
    }
9593
0
    else
9594
0
    {
9595
0
        m_poPrivate->m_poBand = nullptr;
9596
0
    }
9597
0
    return *this;
9598
0
}
9599
9600
bool GDALDataset::ConstBands::Iterator::operator!=(const Iterator &it) const
9601
0
{
9602
0
    return m_poPrivate->m_iCurBand != it.m_poPrivate->m_iCurBand;
9603
0
}
9604
9605
/************************************************************************/
9606
/*                             GetBands()                               */
9607
/************************************************************************/
9608
9609
/** Function that returns an iterable object over GDALRasterBand in the dataset.
9610
 *
9611
 * This is a C++ iterator friendly version of GetRasterBand().
9612
 *
9613
 * Typical use is:
9614
 * \code{.cpp}
9615
 * for( const auto* poBand: poDS->GetConstBands() )
9616
 * {
9617
 *       std::cout << "Band  << poBand->GetDescription() << std::endl;
9618
 * }
9619
 * \endcode
9620
 *
9621
 * @see GetRasterBand()
9622
 *
9623
 * @since GDAL 3.12
9624
 */
9625
GDALDataset::ConstBands GDALDataset::GetBands() const
9626
0
{
9627
0
    return ConstBands(this);
9628
0
}
9629
9630
/************************************************************************/
9631
/*                                 begin()                              */
9632
/************************************************************************/
9633
9634
/**
9635
 \brief Return beginning of band iterator.
9636
9637
 @since GDAL 3.12
9638
*/
9639
9640
const GDALDataset::ConstBands::Iterator GDALDataset::ConstBands::begin() const
9641
0
{
9642
0
    return {m_poSelf, true};
9643
0
}
9644
9645
/************************************************************************/
9646
/*                                  end()                               */
9647
/************************************************************************/
9648
9649
/**
9650
 \brief Return end of band iterator.
9651
9652
 @since GDAL 3.12
9653
*/
9654
9655
const GDALDataset::ConstBands::Iterator GDALDataset::ConstBands::end() const
9656
0
{
9657
0
    return {m_poSelf, false};
9658
0
}
9659
9660
/************************************************************************/
9661
/*                                  size()                             */
9662
/************************************************************************/
9663
9664
/**
9665
 \brief Get the number of raster bands in this dataset.
9666
9667
 @return raster band count.
9668
9669
 @since GDAL 3.12
9670
*/
9671
9672
size_t GDALDataset::ConstBands::size() const
9673
0
{
9674
0
    return static_cast<size_t>(m_poSelf->GetRasterCount());
9675
0
}
9676
9677
/************************************************************************/
9678
/*                                operator[]()                          */
9679
/************************************************************************/
9680
/**
9681
 \brief Fetch a raster band by index.
9682
9683
 The returned band remains owned by the
9684
 GDALDataset and should not be deleted by the application.
9685
9686
 @warning Contrary to GDALDataset::GetRasterBand(), the indexing here is
9687
 consistent with the conventions of C/C++, i.e. starting at 0.
9688
9689
 @param iBand a band index between 0 and size()-1.
9690
9691
 @return the band, or nullptr if iBand is out of range or an error occurs.
9692
9693
 @since GDAL 3.12
9694
*/
9695
9696
const GDALRasterBand *GDALDataset::ConstBands::operator[](int iBand) const
9697
0
{
9698
0
    return m_poSelf->GetRasterBand(1 + iBand);
9699
0
}
9700
9701
/************************************************************************/
9702
/*                                operator[]()                          */
9703
/************************************************************************/
9704
9705
/**
9706
 \brief Fetch a raster band by index.
9707
9708
 The returned band remains owned by the
9709
 GDALDataset and should not be deleted by the application.
9710
9711
 @warning Contrary to GDALDataset::GetRasterBand(), the indexing here is
9712
 consistent with the conventions of C/C++, i.e. starting at 0.
9713
9714
 @param iBand a band index between 0 and size()-1.
9715
9716
 @return the band, or nullptr if iBand is out of range or an error occurs.
9717
9718
 @since GDAL 3.12
9719
*/
9720
9721
const GDALRasterBand *GDALDataset::ConstBands::operator[](size_t iBand) const
9722
0
{
9723
0
    return m_poSelf->GetRasterBand(1 + static_cast<int>(iBand));
9724
0
}
9725
9726
/************************************************************************/
9727
/*                           GetRootGroup()                             */
9728
/************************************************************************/
9729
9730
/**
9731
 \brief Return the root GDALGroup of this dataset.
9732
9733
 Only valid for multidimensional datasets.
9734
9735
 This is the same as the C function GDALDatasetGetRootGroup().
9736
9737
 @since GDAL 3.1
9738
*/
9739
9740
std::shared_ptr<GDALGroup> GDALDataset::GetRootGroup() const
9741
0
{
9742
0
    return nullptr;
9743
0
}
9744
9745
/************************************************************************/
9746
/*                        GetRawBinaryLayout()                          */
9747
/************************************************************************/
9748
9749
//! @cond Doxygen_Suppress
9750
/**
9751
 \brief Return the layout of a dataset that can be considered as a raw binary
9752
 format.
9753
9754
 @param sLayout Structure that will be set if the dataset is a raw binary one.
9755
 @return true if the dataset is a raw binary one.
9756
 @since GDAL 3.1
9757
*/
9758
9759
bool GDALDataset::GetRawBinaryLayout(RawBinaryLayout &sLayout)
9760
0
{
9761
0
    CPL_IGNORE_RET_VAL(sLayout);
9762
0
    return false;
9763
0
}
9764
9765
//! @endcond
9766
9767
/************************************************************************/
9768
/*                          ClearStatistics()                           */
9769
/************************************************************************/
9770
9771
/**
9772
 \brief Clear statistics
9773
9774
 Only implemented for now in PAM supported datasets
9775
9776
 This is the same as the C function GDALDatasetClearStatistics().
9777
9778
 @since GDAL 3.2
9779
*/
9780
9781
void GDALDataset::ClearStatistics()
9782
0
{
9783
0
    auto poRootGroup = GetRootGroup();
9784
0
    if (poRootGroup)
9785
0
        poRootGroup->ClearStatistics();
9786
0
}
9787
9788
/************************************************************************/
9789
/*                        GDALDatasetClearStatistics()                  */
9790
/************************************************************************/
9791
9792
/**
9793
 \brief Clear statistics
9794
9795
 This is the same as the C++ method GDALDataset::ClearStatistics().
9796
9797
 @since GDAL 3.2
9798
*/
9799
9800
void GDALDatasetClearStatistics(GDALDatasetH hDS)
9801
0
{
9802
0
    VALIDATE_POINTER0(hDS, __func__);
9803
0
    GDALDataset::FromHandle(hDS)->ClearStatistics();
9804
0
}
9805
9806
/************************************************************************/
9807
/*                        GetFieldDomainNames()                         */
9808
/************************************************************************/
9809
9810
/** Returns a list of the names of all field domains stored in the dataset.
9811
 *
9812
 * @note The default implementation assumes that drivers fully populate
9813
 * m_oMapFieldDomains when opening a dataset. If this assumption is incorrect
9814
 * then a specialized implementation of GetFieldDomainNames() must be
9815
 * implemented.
9816
 *
9817
 * @param papszOptions Driver specific options determining how attributes
9818
 * should be retrieved. Pass nullptr for default behavior.
9819
 *
9820
 * @return list of field domain names
9821
 * @since GDAL 3.5
9822
 */
9823
std::vector<std::string>
9824
GDALDataset::GetFieldDomainNames(CPL_UNUSED CSLConstList papszOptions) const
9825
0
{
9826
9827
0
    std::vector<std::string> names;
9828
0
    names.reserve(m_oMapFieldDomains.size());
9829
0
    for (const auto &it : m_oMapFieldDomains)
9830
0
    {
9831
0
        names.emplace_back(it.first);
9832
0
    }
9833
0
    return names;
9834
0
}
9835
9836
/************************************************************************/
9837
/*                      GDALDatasetGetFieldDomainNames()                */
9838
/************************************************************************/
9839
9840
/** Returns a list of the names of all field domains stored in the dataset.
9841
 *
9842
 * This is the same as the C++ method GDALDataset::GetFieldDomainNames().
9843
 *
9844
 * @param hDS Dataset handle.
9845
 * @param papszOptions Driver specific options determining how attributes
9846
 * should be retrieved. Pass nullptr for default behavior.
9847
 *
9848
 * @return list of field domain names, to be freed with CSLDestroy()
9849
 * @since GDAL 3.5
9850
 */
9851
char **GDALDatasetGetFieldDomainNames(GDALDatasetH hDS,
9852
                                      CSLConstList papszOptions)
9853
0
{
9854
0
    VALIDATE_POINTER1(hDS, __func__, nullptr);
9855
0
    auto names =
9856
0
        GDALDataset::FromHandle(hDS)->GetFieldDomainNames(papszOptions);
9857
0
    CPLStringList res;
9858
0
    for (const auto &name : names)
9859
0
    {
9860
0
        res.AddString(name.c_str());
9861
0
    }
9862
0
    return res.StealList();
9863
0
}
9864
9865
/************************************************************************/
9866
/*                        GetFieldDomain()                              */
9867
/************************************************************************/
9868
9869
/** Get a field domain from its name.
9870
 *
9871
 * @return the field domain, or nullptr if not found.
9872
 * @since GDAL 3.3
9873
 */
9874
const OGRFieldDomain *GDALDataset::GetFieldDomain(const std::string &name) const
9875
0
{
9876
0
    const auto iter = m_oMapFieldDomains.find(name);
9877
0
    if (iter == m_oMapFieldDomains.end())
9878
0
        return nullptr;
9879
0
    return iter->second.get();
9880
0
}
9881
9882
/************************************************************************/
9883
/*                      GDALDatasetGetFieldDomain()                     */
9884
/************************************************************************/
9885
9886
/** Get a field domain from its name.
9887
 *
9888
 * This is the same as the C++ method GDALDataset::GetFieldDomain().
9889
 *
9890
 * @param hDS Dataset handle.
9891
 * @param pszName Name of field domain.
9892
 * @return the field domain (ownership remains to the dataset), or nullptr if
9893
 * not found.
9894
 * @since GDAL 3.3
9895
 */
9896
OGRFieldDomainH GDALDatasetGetFieldDomain(GDALDatasetH hDS, const char *pszName)
9897
0
{
9898
0
    VALIDATE_POINTER1(hDS, __func__, nullptr);
9899
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
9900
0
    return OGRFieldDomain::ToHandle(const_cast<OGRFieldDomain *>(
9901
0
        GDALDataset::FromHandle(hDS)->GetFieldDomain(pszName)));
9902
0
}
9903
9904
/************************************************************************/
9905
/*                         AddFieldDomain()                             */
9906
/************************************************************************/
9907
9908
/** Add a field domain to the dataset.
9909
 *
9910
 * Only a few drivers will support this operation, and some of them might only
9911
 * support it only for some types of field domains.
9912
 * At the time of writing (GDAL 3.3), only the Memory and GeoPackage drivers
9913
 * support this operation. A dataset having at least some support for this
9914
 * operation should report the ODsCAddFieldDomain dataset capability.
9915
 *
9916
 * Anticipated failures will not be emitted through the CPLError()
9917
 * infrastructure, but will be reported in the failureReason output parameter.
9918
 *
9919
 * @note Drivers should make sure to update m_oMapFieldDomains in order for the
9920
 * default implementation of GetFieldDomainNames() to work correctly, or
9921
 * alternatively a specialized implementation of GetFieldDomainNames() should be
9922
 * implemented.
9923
 *
9924
 * @param domain The domain definition.
9925
 * @param failureReason      Output parameter. Will contain an error message if
9926
 *                           an error occurs.
9927
 * @return true in case of success.
9928
 * @since GDAL 3.3
9929
 */
9930
bool GDALDataset::AddFieldDomain(
9931
    CPL_UNUSED std::unique_ptr<OGRFieldDomain> &&domain,
9932
    std::string &failureReason)
9933
0
{
9934
0
    failureReason = "AddFieldDomain not supported by this driver";
9935
0
    return false;
9936
0
}
9937
9938
/************************************************************************/
9939
/*                     GDALDatasetAddFieldDomain()                      */
9940
/************************************************************************/
9941
9942
/** Add a field domain to the dataset.
9943
 *
9944
 * Only a few drivers will support this operation, and some of them might only
9945
 * support it only for some types of field domains.
9946
 * At the time of writing (GDAL 3.3), only the Memory and GeoPackage drivers
9947
 * support this operation. A dataset having at least some support for this
9948
 * operation should report the ODsCAddFieldDomain dataset capability.
9949
 *
9950
 * Anticipated failures will not be emitted through the CPLError()
9951
 * infrastructure, but will be reported in the ppszFailureReason output
9952
 * parameter.
9953
 *
9954
 * @param hDS                Dataset handle.
9955
 * @param hFieldDomain       The domain definition. Contrary to the C++ version,
9956
 *                           the passed object is copied.
9957
 * @param ppszFailureReason  Output parameter. Will contain an error message if
9958
 *                           an error occurs (*ppszFailureReason to be freed
9959
 *                           with CPLFree). May be NULL.
9960
 * @return true in case of success.
9961
 * @since GDAL 3.3
9962
 */
9963
bool GDALDatasetAddFieldDomain(GDALDatasetH hDS, OGRFieldDomainH hFieldDomain,
9964
                               char **ppszFailureReason)
9965
0
{
9966
0
    VALIDATE_POINTER1(hDS, __func__, false);
9967
0
    VALIDATE_POINTER1(hFieldDomain, __func__, false);
9968
0
    auto poDomain = std::unique_ptr<OGRFieldDomain>(
9969
0
        OGRFieldDomain::FromHandle(hFieldDomain)->Clone());
9970
0
    if (poDomain == nullptr)
9971
0
        return false;
9972
0
    std::string failureReason;
9973
0
    const bool bRet = GDALDataset::FromHandle(hDS)->AddFieldDomain(
9974
0
        std::move(poDomain), failureReason);
9975
0
    if (ppszFailureReason)
9976
0
    {
9977
0
        *ppszFailureReason =
9978
0
            failureReason.empty() ? nullptr : CPLStrdup(failureReason.c_str());
9979
0
    }
9980
0
    return bRet;
9981
0
}
9982
9983
/************************************************************************/
9984
/*                        DeleteFieldDomain()                           */
9985
/************************************************************************/
9986
9987
/** Removes a field domain from the dataset.
9988
 *
9989
 * Only a few drivers will support this operation.
9990
 *
9991
 * At the time of writing (GDAL 3.5), only the Memory and GeoPackage drivers
9992
 * support this operation. A dataset having at least some support for this
9993
 * operation should report the ODsCDeleteFieldDomain dataset capability.
9994
 *
9995
 * Anticipated failures will not be emitted through the CPLError()
9996
 * infrastructure, but will be reported in the failureReason output parameter.
9997
 *
9998
 * @note Drivers should make sure to update m_oMapFieldDomains in order for the
9999
 * default implementation of GetFieldDomainNames() to work correctly, or
10000
 * alternatively a specialized implementation of GetFieldDomainNames() should be
10001
 * implemented.
10002
 *
10003
 * @param name The domain name.
10004
 * @param failureReason      Output parameter. Will contain an error message if
10005
 *                           an error occurs.
10006
 * @return true in case of success.
10007
 * @since GDAL 3.5
10008
 */
10009
bool GDALDataset::DeleteFieldDomain(CPL_UNUSED const std::string &name,
10010
                                    std::string &failureReason)
10011
0
{
10012
0
    failureReason = "DeleteFieldDomain not supported by this driver";
10013
0
    return false;
10014
0
}
10015
10016
/************************************************************************/
10017
/*                  GDALDatasetDeleteFieldDomain()                      */
10018
/************************************************************************/
10019
10020
/** Removes a field domain from the dataset.
10021
 *
10022
 * Only a few drivers will support this operation.
10023
 *
10024
 * At the time of writing (GDAL 3.5), only the Memory and GeoPackage drivers
10025
 * support this operation. A dataset having at least some support for this
10026
 * operation should report the ODsCDeleteFieldDomain dataset capability.
10027
 *
10028
 * Anticipated failures will not be emitted through the CPLError()
10029
 * infrastructure, but will be reported in the ppszFailureReason output
10030
 * parameter.
10031
 *
10032
 * @param hDS                Dataset handle.
10033
 * @param pszName            The domain name.
10034
 * @param ppszFailureReason  Output parameter. Will contain an error message if
10035
 *                           an error occurs (*ppszFailureReason to be freed
10036
 *                           with CPLFree). May be NULL.
10037
 * @return true in case of success.
10038
 * @since GDAL 3.3
10039
 */
10040
bool GDALDatasetDeleteFieldDomain(GDALDatasetH hDS, const char *pszName,
10041
                                  char **ppszFailureReason)
10042
0
{
10043
0
    VALIDATE_POINTER1(hDS, __func__, false);
10044
0
    VALIDATE_POINTER1(pszName, __func__, false);
10045
0
    std::string failureReason;
10046
0
    const bool bRet =
10047
0
        GDALDataset::FromHandle(hDS)->DeleteFieldDomain(pszName, failureReason);
10048
0
    if (ppszFailureReason)
10049
0
    {
10050
0
        *ppszFailureReason =
10051
0
            failureReason.empty() ? nullptr : CPLStrdup(failureReason.c_str());
10052
0
    }
10053
0
    return bRet;
10054
0
}
10055
10056
/************************************************************************/
10057
/*                       UpdateFieldDomain()                            */
10058
/************************************************************************/
10059
10060
/** Updates an existing field domain by replacing its definition.
10061
 *
10062
 * The existing field domain with matching name will be replaced.
10063
 *
10064
 * Only a few drivers will support this operation, and some of them might only
10065
 * support it only for some types of field domains.
10066
 * At the time of writing (GDAL 3.5), only the Memory driver
10067
 * supports this operation. A dataset having at least some support for this
10068
 * operation should report the ODsCUpdateFieldDomain dataset capability.
10069
 *
10070
 * Anticipated failures will not be emitted through the CPLError()
10071
 * infrastructure, but will be reported in the failureReason output parameter.
10072
 *
10073
 * @param domain The domain definition.
10074
 * @param failureReason      Output parameter. Will contain an error message if
10075
 *                           an error occurs.
10076
 * @return true in case of success.
10077
 * @since GDAL 3.5
10078
 */
10079
bool GDALDataset::UpdateFieldDomain(
10080
    CPL_UNUSED std::unique_ptr<OGRFieldDomain> &&domain,
10081
    std::string &failureReason)
10082
0
{
10083
0
    failureReason = "UpdateFieldDomain not supported by this driver";
10084
0
    return false;
10085
0
}
10086
10087
/************************************************************************/
10088
/*                  GDALDatasetUpdateFieldDomain()                      */
10089
/************************************************************************/
10090
10091
/** Updates an existing field domain by replacing its definition.
10092
 *
10093
 * The existing field domain with matching name will be replaced.
10094
 *
10095
 * Only a few drivers will support this operation, and some of them might only
10096
 * support it only for some types of field domains.
10097
 * At the time of writing (GDAL 3.5), only the Memory driver
10098
 * supports this operation. A dataset having at least some support for this
10099
 * operation should report the ODsCUpdateFieldDomain dataset capability.
10100
 *
10101
 * Anticipated failures will not be emitted through the CPLError()
10102
 * infrastructure, but will be reported in the failureReason output parameter.
10103
 *
10104
 * @param hDS                Dataset handle.
10105
 * @param hFieldDomain       The domain definition. Contrary to the C++ version,
10106
 *                           the passed object is copied.
10107
 * @param ppszFailureReason  Output parameter. Will contain an error message if
10108
 *                           an error occurs (*ppszFailureReason to be freed
10109
 *                           with CPLFree). May be NULL.
10110
 * @return true in case of success.
10111
 * @since GDAL 3.5
10112
 */
10113
bool GDALDatasetUpdateFieldDomain(GDALDatasetH hDS,
10114
                                  OGRFieldDomainH hFieldDomain,
10115
                                  char **ppszFailureReason)
10116
0
{
10117
0
    VALIDATE_POINTER1(hDS, __func__, false);
10118
0
    VALIDATE_POINTER1(hFieldDomain, __func__, false);
10119
0
    auto poDomain = std::unique_ptr<OGRFieldDomain>(
10120
0
        OGRFieldDomain::FromHandle(hFieldDomain)->Clone());
10121
0
    if (poDomain == nullptr)
10122
0
        return false;
10123
0
    std::string failureReason;
10124
0
    const bool bRet = GDALDataset::FromHandle(hDS)->UpdateFieldDomain(
10125
0
        std::move(poDomain), failureReason);
10126
0
    if (ppszFailureReason)
10127
0
    {
10128
0
        *ppszFailureReason =
10129
0
            failureReason.empty() ? nullptr : CPLStrdup(failureReason.c_str());
10130
0
    }
10131
0
    return bRet;
10132
0
}
10133
10134
/************************************************************************/
10135
/*                        GetRelationshipNames()                        */
10136
/************************************************************************/
10137
10138
/** Returns a list of the names of all relationships stored in the dataset.
10139
 *
10140
 * @param papszOptions Driver specific options determining how relationships
10141
 * should be retrieved. Pass nullptr for default behavior.
10142
 *
10143
 * @return list of relationship names
10144
 * @since GDAL 3.6
10145
 */
10146
std::vector<std::string>
10147
GDALDataset::GetRelationshipNames(CPL_UNUSED CSLConstList papszOptions) const
10148
0
{
10149
0
    return {};
10150
0
}
10151
10152
/************************************************************************/
10153
/*                     GDALDatasetGetRelationshipNames()                */
10154
/************************************************************************/
10155
10156
/** Returns a list of the names of all relationships stored in the dataset.
10157
 *
10158
 * This is the same as the C++ method GDALDataset::GetRelationshipNames().
10159
 *
10160
 * @param hDS Dataset handle.
10161
 * @param papszOptions Driver specific options determining how relationships
10162
 * should be retrieved. Pass nullptr for default behavior.
10163
 *
10164
 * @return list of relationship names, to be freed with CSLDestroy()
10165
 * @since GDAL 3.6
10166
 */
10167
char **GDALDatasetGetRelationshipNames(GDALDatasetH hDS,
10168
                                       CSLConstList papszOptions)
10169
0
{
10170
0
    VALIDATE_POINTER1(hDS, __func__, nullptr);
10171
0
    auto names =
10172
0
        GDALDataset::FromHandle(hDS)->GetRelationshipNames(papszOptions);
10173
0
    CPLStringList res;
10174
0
    for (const auto &name : names)
10175
0
    {
10176
0
        res.AddString(name.c_str());
10177
0
    }
10178
0
    return res.StealList();
10179
0
}
10180
10181
/************************************************************************/
10182
/*                        GetRelationship()                             */
10183
/************************************************************************/
10184
10185
/** Get a relationship from its name.
10186
 *
10187
 * @return the relationship, or nullptr if not found.
10188
 * @since GDAL 3.6
10189
 */
10190
const GDALRelationship *
10191
GDALDataset::GetRelationship(CPL_UNUSED const std::string &name) const
10192
0
{
10193
0
    return nullptr;
10194
0
}
10195
10196
/************************************************************************/
10197
/*                      GDALDatasetGetRelationship()                    */
10198
/************************************************************************/
10199
10200
/** Get a relationship from its name.
10201
 *
10202
 * This is the same as the C++ method GDALDataset::GetRelationship().
10203
 *
10204
 * @param hDS Dataset handle.
10205
 * @param pszName Name of relationship.
10206
 * @return the relationship (ownership remains to the dataset), or nullptr if
10207
 * not found.
10208
 * @since GDAL 3.6
10209
 */
10210
GDALRelationshipH GDALDatasetGetRelationship(GDALDatasetH hDS,
10211
                                             const char *pszName)
10212
0
{
10213
0
    VALIDATE_POINTER1(hDS, __func__, nullptr);
10214
0
    VALIDATE_POINTER1(pszName, __func__, nullptr);
10215
0
    return GDALRelationship::ToHandle(const_cast<GDALRelationship *>(
10216
0
        GDALDataset::FromHandle(hDS)->GetRelationship(pszName)));
10217
0
}
10218
10219
/************************************************************************/
10220
/*                         AddRelationship()                            */
10221
/************************************************************************/
10222
10223
/** Add a relationship to the dataset.
10224
 *
10225
 * Only a few drivers will support this operation, and some of them might only
10226
 * support it only for some types of relationships.
10227
 *
10228
 * A dataset having at least some support for this
10229
 * operation should report the GDsCAddRelationship dataset capability.
10230
 *
10231
 * Anticipated failures will not be emitted through the CPLError()
10232
 * infrastructure, but will be reported in the failureReason output parameter.
10233
 *
10234
 * When adding a many-to-many relationship
10235
 * (GDALRelationshipCardinality::GRC_MANY_TO_MANY), it is possible to omit the
10236
 * mapping table name (see GDALRelationship::GetMappingTableName) to instruct
10237
 * the driver to create an appropriately named and structured mapping table.
10238
 * Some dataset formats require particular naming conventions and field
10239
 * structures for the mapping table, and delegating the construction of the
10240
 * mapping table to the driver will avoid these pitfalls.
10241
 *
10242
 * @param relationship The relationship definition.
10243
 * @param failureReason      Output parameter. Will contain an error message if
10244
 *                           an error occurs.
10245
 * @return true in case of success.
10246
 * @since GDAL 3.6
10247
 */
10248
bool GDALDataset::AddRelationship(
10249
    CPL_UNUSED std::unique_ptr<GDALRelationship> &&relationship,
10250
    std::string &failureReason)
10251
0
{
10252
0
    failureReason = "AddRelationship not supported by this driver";
10253
0
    return false;
10254
0
}
10255
10256
/************************************************************************/
10257
/*                     GDALDatasetAddRelationship()                     */
10258
/************************************************************************/
10259
10260
/** Add a relationship to the dataset.
10261
 *
10262
 * Only a few drivers will support this operation, and some of them might only
10263
 * support it only for some types of relationships.
10264
 *
10265
 * A dataset having at least some support for this
10266
 * operation should report the GDsCAddRelationship dataset capability.
10267
 *
10268
 * Anticipated failures will not be emitted through the CPLError()
10269
 * infrastructure, but will be reported in the failureReason output parameter.
10270
 *
10271
 * When adding a many-to-many relationship
10272
 * (GDALRelationshipCardinality::GRC_MANY_TO_MANY), it is possible to omit the
10273
 * mapping table name (see GDALRelationshipGetMappingTableName) to instruct the
10274
 * driver to create an appropriately named and structured mapping table. Some
10275
 * dataset formats require particular naming conventions and field structures
10276
 * for the mapping table, and delegating the construction of the mapping table
10277
 * to the driver will avoid these pitfalls.
10278
 *
10279
 * @param hDS                Dataset handle.
10280
 * @param hRelationship      The relationship definition. Contrary to the C++
10281
 * version, the passed object is copied.
10282
 * @param ppszFailureReason  Output parameter. Will contain an error message if
10283
 *                           an error occurs (*ppszFailureReason to be freed
10284
 *                           with CPLFree). May be NULL.
10285
 * @return true in case of success.
10286
 * @since GDAL 3.6
10287
 */
10288
bool GDALDatasetAddRelationship(GDALDatasetH hDS,
10289
                                GDALRelationshipH hRelationship,
10290
                                char **ppszFailureReason)
10291
0
{
10292
0
    VALIDATE_POINTER1(hDS, __func__, false);
10293
0
    VALIDATE_POINTER1(hRelationship, __func__, false);
10294
0
    std::unique_ptr<GDALRelationship> poRelationship(
10295
0
        new GDALRelationship(*GDALRelationship::FromHandle(hRelationship)));
10296
0
    std::string failureReason;
10297
0
    const bool bRet = GDALDataset::FromHandle(hDS)->AddRelationship(
10298
0
        std::move(poRelationship), failureReason);
10299
0
    if (ppszFailureReason)
10300
0
    {
10301
0
        *ppszFailureReason =
10302
0
            failureReason.empty() ? nullptr : CPLStrdup(failureReason.c_str());
10303
0
    }
10304
0
    return bRet;
10305
0
}
10306
10307
/************************************************************************/
10308
/*                        DeleteRelationship()                          */
10309
/************************************************************************/
10310
10311
/** Removes a relationship from the dataset.
10312
 *
10313
 * Only a few drivers will support this operation.
10314
 *
10315
 * A dataset having at least some support for this
10316
 * operation should report the GDsCDeleteRelationship dataset capability.
10317
 *
10318
 * Anticipated failures will not be emitted through the CPLError()
10319
 * infrastructure, but will be reported in the failureReason output parameter.
10320
 *
10321
 * @param name The relationship name.
10322
 * @param failureReason      Output parameter. Will contain an error message if
10323
 *                           an error occurs.
10324
 * @return true in case of success.
10325
 * @since GDAL 3.6
10326
 */
10327
bool GDALDataset::DeleteRelationship(CPL_UNUSED const std::string &name,
10328
                                     std::string &failureReason)
10329
0
{
10330
0
    failureReason = "DeleteRelationship not supported by this driver";
10331
0
    return false;
10332
0
}
10333
10334
/************************************************************************/
10335
/*                  GDALDatasetDeleteRelationship()                     */
10336
/************************************************************************/
10337
10338
/** Removes a relationship from the dataset.
10339
 *
10340
 * Only a few drivers will support this operation.
10341
 *
10342
 * A dataset having at least some support for this
10343
 * operation should report the GDsCDeleteRelationship dataset capability.
10344
 *
10345
 * Anticipated failures will not be emitted through the CPLError()
10346
 * infrastructure, but will be reported in the ppszFailureReason output
10347
 * parameter.
10348
 *
10349
 * @param hDS                Dataset handle.
10350
 * @param pszName            The relationship name.
10351
 * @param ppszFailureReason  Output parameter. Will contain an error message if
10352
 *                           an error occurs (*ppszFailureReason to be freed
10353
 *                           with CPLFree). May be NULL.
10354
 * @return true in case of success.
10355
 * @since GDAL 3.6
10356
 */
10357
bool GDALDatasetDeleteRelationship(GDALDatasetH hDS, const char *pszName,
10358
                                   char **ppszFailureReason)
10359
0
{
10360
0
    VALIDATE_POINTER1(hDS, __func__, false);
10361
0
    VALIDATE_POINTER1(pszName, __func__, false);
10362
0
    std::string failureReason;
10363
0
    const bool bRet = GDALDataset::FromHandle(hDS)->DeleteRelationship(
10364
0
        pszName, failureReason);
10365
0
    if (ppszFailureReason)
10366
0
    {
10367
0
        *ppszFailureReason =
10368
0
            failureReason.empty() ? nullptr : CPLStrdup(failureReason.c_str());
10369
0
    }
10370
0
    return bRet;
10371
0
}
10372
10373
/************************************************************************/
10374
/*                       UpdateRelationship()                           */
10375
/************************************************************************/
10376
10377
/** Updates an existing relationship by replacing its definition.
10378
 *
10379
 * The existing relationship with matching name will be replaced.
10380
 *
10381
 * Only a few drivers will support this operation, and some of them might only
10382
 * support it only for some types of relationships.
10383
 * A dataset having at least some support for this
10384
 * operation should report the GDsCUpdateRelationship dataset capability.
10385
 *
10386
 * Anticipated failures will not be emitted through the CPLError()
10387
 * infrastructure, but will be reported in the failureReason output parameter.
10388
 *
10389
 * @param relationship   The relationship definition.
10390
 * @param failureReason  Output parameter. Will contain an error message if
10391
 *                       an error occurs.
10392
 * @return true in case of success.
10393
 * @since GDAL 3.6
10394
 */
10395
bool GDALDataset::UpdateRelationship(
10396
    CPL_UNUSED std::unique_ptr<GDALRelationship> &&relationship,
10397
    std::string &failureReason)
10398
0
{
10399
0
    failureReason = "UpdateRelationship not supported by this driver";
10400
0
    return false;
10401
0
}
10402
10403
/************************************************************************/
10404
/*                  GDALDatasetUpdateRelationship()                     */
10405
/************************************************************************/
10406
10407
/** Updates an existing relationship by replacing its definition.
10408
 *
10409
 * The existing relationship with matching name will be replaced.
10410
 *
10411
 * Only a few drivers will support this operation, and some of them might only
10412
 * support it only for some types of relationships.
10413
 * A dataset having at least some support for this
10414
 * operation should report the GDsCUpdateRelationship dataset capability.
10415
 *
10416
 * Anticipated failures will not be emitted through the CPLError()
10417
 * infrastructure, but will be reported in the failureReason output parameter.
10418
 *
10419
 * @param hDS                Dataset handle.
10420
 * @param hRelationship      The relationship definition. Contrary to the C++
10421
 * version, the passed object is copied.
10422
 * @param ppszFailureReason  Output parameter. Will contain an error message if
10423
 *                           an error occurs (*ppszFailureReason to be freed
10424
 *                           with CPLFree). May be NULL.
10425
 * @return true in case of success.
10426
 * @since GDAL 3.5
10427
 */
10428
bool GDALDatasetUpdateRelationship(GDALDatasetH hDS,
10429
                                   GDALRelationshipH hRelationship,
10430
                                   char **ppszFailureReason)
10431
0
{
10432
0
    VALIDATE_POINTER1(hDS, __func__, false);
10433
0
    VALIDATE_POINTER1(hRelationship, __func__, false);
10434
0
    std::unique_ptr<GDALRelationship> poRelationship(
10435
0
        new GDALRelationship(*GDALRelationship::FromHandle(hRelationship)));
10436
0
    std::string failureReason;
10437
0
    const bool bRet = GDALDataset::FromHandle(hDS)->UpdateRelationship(
10438
0
        std::move(poRelationship), failureReason);
10439
0
    if (ppszFailureReason)
10440
0
    {
10441
0
        *ppszFailureReason =
10442
0
            failureReason.empty() ? nullptr : CPLStrdup(failureReason.c_str());
10443
0
    }
10444
0
    return bRet;
10445
0
}
10446
10447
/************************************************************************/
10448
/*                  GDALDatasetSetQueryLoggerFunc()                     */
10449
/************************************************************************/
10450
10451
/**
10452
 * Sets the SQL query logger callback.
10453
 *
10454
 * When supported by the driver, the callback will be called with
10455
 * the executed SQL text, the error message, the execution time in milliseconds,
10456
 * the number of records fetched/affected and the client status data.
10457
 *
10458
 * A value of -1 in the execution time or in the number of records indicates
10459
 * that the values are unknown.
10460
 *
10461
 * @param hDS                   Dataset handle.
10462
 * @param pfnQueryLoggerFunc    Callback function
10463
 * @param poQueryLoggerArg      Opaque client status data
10464
 * @return                      true in case of success.
10465
 * @since                       GDAL 3.7
10466
 */
10467
bool GDALDatasetSetQueryLoggerFunc(GDALDatasetH hDS,
10468
                                   GDALQueryLoggerFunc pfnQueryLoggerFunc,
10469
                                   void *poQueryLoggerArg)
10470
0
{
10471
0
    VALIDATE_POINTER1(hDS, __func__, false);
10472
0
    return GDALDataset::FromHandle(hDS)->SetQueryLoggerFunc(pfnQueryLoggerFunc,
10473
0
                                                            poQueryLoggerArg);
10474
0
}
10475
10476
//! @cond Doxygen_Suppress
10477
10478
/************************************************************************/
10479
/*                       SetEnableOverviews()                           */
10480
/************************************************************************/
10481
10482
void GDALDataset::SetEnableOverviews(bool bEnable)
10483
0
{
10484
0
    if (m_poPrivate)
10485
0
    {
10486
0
        m_poPrivate->m_bOverviewsEnabled = bEnable;
10487
0
    }
10488
0
}
10489
10490
/************************************************************************/
10491
/*                      AreOverviewsEnabled()                           */
10492
/************************************************************************/
10493
10494
bool GDALDataset::AreOverviewsEnabled() const
10495
0
{
10496
0
    return m_poPrivate ? m_poPrivate->m_bOverviewsEnabled : true;
10497
0
}
10498
10499
/************************************************************************/
10500
/*                             IsAllBands()                             */
10501
/************************************************************************/
10502
10503
bool GDALDataset::IsAllBands(int nBandCount, const int *panBandList) const
10504
0
{
10505
0
    if (nBands != nBandCount)
10506
0
        return false;
10507
0
    if (panBandList)
10508
0
    {
10509
0
        for (int i = 0; i < nBandCount; ++i)
10510
0
        {
10511
0
            if (panBandList[i] != i + 1)
10512
0
                return false;
10513
0
        }
10514
0
    }
10515
0
    return true;
10516
0
}
10517
10518
//! @endcond
10519
10520
/************************************************************************/
10521
/*                       GetCompressionFormats()                        */
10522
/************************************************************************/
10523
10524
/** Return the compression formats that can be natively obtained for the
10525
 * window of interest and requested bands.
10526
 *
10527
 * For example, a tiled dataset may be able to return data in a compressed
10528
 * format if the window of interest matches exactly a tile. For some formats,
10529
 * drivers may also be able to merge several tiles together (not currently
10530
 * implemented though).
10531
 *
10532
 * Each format string is a pseudo MIME type, whose first part can be passed
10533
 * as the pszFormat argument of ReadCompressedData(), with additional
10534
 * parameters specified as key=value with a semi-colon separator.
10535
 *
10536
 * The amount and types of optional parameters passed after the MIME type is
10537
 * format dependent, and driver dependent (some drivers might not be able to
10538
 * return those extra information without doing a rather costly processing).
10539
 *
10540
 * For example, a driver might return "JPEG;frame_type=SOF0_baseline;"
10541
 * "bit_depth=8;num_components=3;subsampling=4:2:0;colorspace=YCbCr", and
10542
 * consequently "JPEG" can be passed as the pszFormat argument of
10543
 * ReadCompressedData(). For JPEG, implementations can use the
10544
 * GDALGetCompressionFormatForJPEG() helper method to generate a string like
10545
 * above from a JPEG codestream.
10546
 *
10547
 * Several values might be returned. For example,
10548
 * the JPEGXL driver will return "JXL", but also potentially "JPEG"
10549
 * if the JPEGXL codestream includes a JPEG reconstruction box.
10550
 *
10551
 * In the general case this method will return an empty list.
10552
 *
10553
 * This is the same as C function GDALDatasetGetCompressionFormats().
10554
 *
10555
 * @param nXOff The pixel offset to the top left corner of the region
10556
 * of the band to be accessed.  This would be zero to start from the left side.
10557
 *
10558
 * @param nYOff The line offset to the top left corner of the region
10559
 * of the band to be accessed.  This would be zero to start from the top.
10560
 *
10561
 * @param nXSize The width of the region of the band to be accessed in pixels.
10562
 *
10563
 * @param nYSize The height of the region of the band to be accessed in lines.
10564
 *
10565
 * @param nBandCount the number of bands being requested.
10566
 *
10567
 * @param panBandList the list of nBandCount band numbers.
10568
 * Note band numbers are 1 based. This may be NULL to select the first
10569
 * nBandCount bands.
10570
 *
10571
 * @return a list of compatible formats (which may be empty)
10572
 *
10573
 * For example, to check if native compression format(s) are available on the
10574
 * whole image:
10575
 * \code{.cpp}
10576
 *   const CPLStringList aosFormats =
10577
 *      poDataset->GetCompressionFormats(0, 0,
10578
 *                                       poDataset->GetRasterXSize(),
10579
 *                                       poDataset->GetRasterYSize(),
10580
 *                                       poDataset->GetRasterCount(),
10581
 *                                       nullptr);
10582
 *   for( const char* pszFormat: aosFormats )
10583
 *   {
10584
 *      // Remove optional parameters and just print out the MIME type.
10585
 *      const CPLStringList aosTokens(CSLTokenizeString2(pszFormat, ";", 0));
10586
 *      printf("Found format %s\n, aosTokens[0]);
10587
 *   }
10588
 * \endcode
10589
 *
10590
 * @since GDAL 3.7
10591
 */
10592
CPLStringList
10593
GDALDataset::GetCompressionFormats(CPL_UNUSED int nXOff, CPL_UNUSED int nYOff,
10594
                                   CPL_UNUSED int nXSize, CPL_UNUSED int nYSize,
10595
                                   CPL_UNUSED int nBandCount,
10596
                                   CPL_UNUSED const int *panBandList)
10597
0
{
10598
0
    return CPLStringList();
10599
0
}
10600
10601
/************************************************************************/
10602
/*                 GDALDatasetGetCompressionFormats()                   */
10603
/************************************************************************/
10604
10605
/** Return the compression formats that can be natively obtained for the
10606
 * window of interest and requested bands.
10607
 *
10608
 * For example, a tiled dataset may be able to return data in a compressed
10609
 * format if the window of interest matches exactly a tile. For some formats,
10610
 * drivers may also be able to merge several tiles together (not currently
10611
 * implemented though).
10612
 *
10613
 * Each format string is a pseudo MIME type, whose first part can be passed
10614
 * as the pszFormat argument of ReadCompressedData(), with additional
10615
 * parameters specified as key=value with a semi-colon separator.
10616
 *
10617
 * The amount and types of optional parameters passed after the MIME type is
10618
 * format dependent, and driver dependent (some drivers might not be able to
10619
 * return those extra information without doing a rather costly processing).
10620
 *
10621
 * For example, a driver might return "JPEG;frame_type=SOF0_baseline;"
10622
 * "bit_depth=8;num_components=3;subsampling=4:2:0;colorspace=YCbCr", and
10623
 * consequently "JPEG" can be passed as the pszFormat argument of
10624
 * ReadCompressedData(). For JPEG, implementations can use the
10625
 * GDALGetCompressionFormatForJPEG() helper method to generate a string like
10626
 * above from a JPEG codestream.
10627
 *
10628
 * Several values might be returned. For example,
10629
 * the JPEGXL driver will return "JXL", but also potentially "JPEG"
10630
 * if the JPEGXL codestream includes a JPEG reconstruction box.
10631
 *
10632
 * In the general case this method will return an empty list.
10633
 *
10634
 * This is the same as C++ method GDALDataset::GetCompressionFormats().
10635
 *
10636
 * @param hDS Dataset handle.
10637
 *
10638
 * @param nXOff The pixel offset to the top left corner of the region
10639
 * of the band to be accessed.  This would be zero to start from the left side.
10640
 *
10641
 * @param nYOff The line offset to the top left corner of the region
10642
 * of the band to be accessed.  This would be zero to start from the top.
10643
 *
10644
 * @param nXSize The width of the region of the band to be accessed in pixels.
10645
 *
10646
 * @param nYSize The height of the region of the band to be accessed in lines.
10647
 *
10648
 * @param nBandCount the number of bands being requested.
10649
 *
10650
 * @param panBandList the list of nBandCount band numbers.
10651
 * Note band numbers are 1 based. This may be NULL to select the first
10652
 * nBandCount bands.
10653
 *
10654
 * @return a list of compatible formats (which may be empty) that should be
10655
 * freed with CSLDestroy(), or nullptr.
10656
 *
10657
 * @since GDAL 3.7
10658
 */
10659
char **GDALDatasetGetCompressionFormats(GDALDatasetH hDS, int nXOff, int nYOff,
10660
                                        int nXSize, int nYSize, int nBandCount,
10661
                                        const int *panBandList)
10662
0
{
10663
0
    VALIDATE_POINTER1(hDS, __func__, nullptr);
10664
0
    return GDALDataset::FromHandle(hDS)
10665
0
        ->GetCompressionFormats(nXOff, nYOff, nXSize, nYSize, nBandCount,
10666
0
                                panBandList)
10667
0
        .StealList();
10668
0
}
10669
10670
/************************************************************************/
10671
/*                         ReadCompressedData()                         */
10672
/************************************************************************/
10673
10674
/** Return the compressed content that can be natively obtained for the
10675
 * window of interest and requested bands.
10676
 *
10677
 * For example, a tiled dataset may be able to return data in compressed format
10678
 * if the window of interest matches exactly a tile. For some formats, drivers
10679
 * may also be example to merge several tiles together (not currently
10680
 * implemented though).
10681
 *
10682
 * The implementation should make sure that the content returned forms a valid
10683
 * standalone file. For example, for the GeoTIFF implementation of this method,
10684
 * when extracting a JPEG tile, the method will automatically add the content
10685
 * of the JPEG Huffman and/or quantization tables that might be stored in the
10686
 * TIFF JpegTables tag, and not in tile data itself.
10687
 *
10688
 * In the general case this method will return CE_Failure.
10689
 *
10690
 * This is the same as C function GDALDatasetReadCompressedData().
10691
 *
10692
 * @param pszFormat Requested compression format (e.g. "JPEG",
10693
 * "WEBP", "JXL"). This is the MIME type of one of the values
10694
 * returned by GetCompressionFormats(). The format string is designed to
10695
 * potentially include at a later point key=value optional parameters separated
10696
 * by a semi-colon character. At time of writing, none are implemented.
10697
 * ReadCompressedData() implementations should verify optional parameters and
10698
 * return CE_Failure if they cannot support one of them.
10699
 *
10700
 * @param nXOff The pixel offset to the top left corner of the region
10701
 * of the band to be accessed.  This would be zero to start from the left side.
10702
 *
10703
 * @param nYOff The line offset to the top left corner of the region
10704
 * of the band to be accessed.  This would be zero to start from the top.
10705
 *
10706
 * @param nXSize The width of the region of the band to be accessed in pixels.
10707
 *
10708
 * @param nYSize The height of the region of the band to be accessed in lines.
10709
 *
10710
 * @param nBandCount the number of bands being requested.
10711
 *
10712
 * @param panBandList the list of nBandCount band numbers.
10713
 * Note band numbers are 1 based. This may be NULL to select the first
10714
 * nBandCount bands.
10715
 *
10716
 * @param ppBuffer Pointer to a buffer to store the compressed data or nullptr.
10717
 * If ppBuffer is not nullptr, then pnBufferSize should also not be nullptr.
10718
 * If ppBuffer is not nullptr, and *ppBuffer is not nullptr, then the provided
10719
 * buffer will be filled with the compressed data, provided that pnBufferSize
10720
 * and *pnBufferSize are not nullptr, and *pnBufferSize, indicating the size
10721
 * of *ppBuffer, is sufficiently large to hold the data.
10722
 * If ppBuffer is not nullptr, but *ppBuffer is nullptr, then the method will
10723
 * allocate *ppBuffer using VSIMalloc(), and thus the caller is responsible to
10724
 * free it with VSIFree().
10725
 * If ppBuffer is nullptr, then the compressed data itself will not be returned,
10726
 * but *pnBufferSize will be updated with an upper bound of the size that would
10727
 * be necessary to hold it (if pnBufferSize != nullptr).
10728
 *
10729
 * @param pnBufferSize Output buffer size, or nullptr.
10730
 * If ppBuffer != nullptr && *ppBuffer != nullptr, then pnBufferSize should
10731
 * be != nullptr and *pnBufferSize contain the size of *ppBuffer. If the
10732
 * method is successful, *pnBufferSize will be updated with the actual size
10733
 * used.
10734
 *
10735
 * @param ppszDetailedFormat Pointer to an output string, or nullptr.
10736
 * If ppszDetailedFormat is not nullptr, then, on success, the method will
10737
 * allocate a new string in *ppszDetailedFormat (to be freed with VSIFree())
10738
 * *ppszDetailedFormat might contain strings like
10739
 * "JPEG;frame_type=SOF0_baseline;bit_depth=8;num_components=3;"
10740
 * "subsampling=4:2:0;colorspace=YCbCr" or simply the MIME type.
10741
 * The string will contain at least as much information as what
10742
 * GetCompressionFormats() returns, and potentially more when
10743
 * ppBuffer != nullptr.
10744
 *
10745
 * @return CE_None in case of success, CE_Failure otherwise.
10746
 *
10747
 * For example, to request JPEG content on the whole image and let GDAL deal
10748
 * with the buffer allocation.
10749
 * \code{.cpp}
10750
 *   void* pBuffer = nullptr;
10751
 *   size_t nBufferSize = 0;
10752
 *   CPLErr eErr =
10753
 *      poDataset->ReadCompressedData("JPEG",
10754
 *                                    0, 0,
10755
 *                                    poDataset->GetRasterXSize(),
10756
 *                                    poDataset->GetRasterYSize(),
10757
 *                                    poDataset->GetRasterCount(),
10758
 *                                    nullptr, // panBandList
10759
 *                                    &pBuffer,
10760
 *                                    &nBufferSize,
10761
 *                                    nullptr // ppszDetailedFormat
10762
 *                                   );
10763
 *   if (eErr == CE_None)
10764
 *   {
10765
 *       CPLAssert(pBuffer != nullptr);
10766
 *       CPLAssert(nBufferSize > 0);
10767
 *       VSILFILE* fp = VSIFOpenL("my.jpeg", "wb");
10768
 *       if (fp)
10769
 *       {
10770
 *           VSIFWriteL(pBuffer, nBufferSize, 1, fp);
10771
 *           VSIFCloseL(fp);
10772
 *       }
10773
 *       VSIFree(pBuffer);
10774
 *   }
10775
 * \endcode
10776
 *
10777
 * Or to manage the buffer allocation on your side:
10778
 * \code{.cpp}
10779
 *   size_t nUpperBoundBufferSize = 0;
10780
 *   CPLErr eErr =
10781
 *      poDataset->ReadCompressedData("JPEG",
10782
 *                                    0, 0,
10783
 *                                    poDataset->GetRasterXSize(),
10784
 *                                    poDataset->GetRasterYSize(),
10785
 *                                    poDataset->GetRasterCount(),
10786
 *                                    nullptr, // panBandList
10787
 *                                    nullptr, // ppBuffer,
10788
 *                                    &nUpperBoundBufferSize,
10789
 *                                    nullptr // ppszDetailedFormat
10790
 *                                   );
10791
 *   if (eErr == CE_None)
10792
 *   {
10793
 *       std::vector<uint8_t> myBuffer;
10794
 *       myBuffer.resize(nUpperBoundBufferSize);
10795
 *       void* pBuffer = myBuffer.data();
10796
 *       size_t nActualSize = nUpperBoundBufferSize;
10797
 *       char* pszDetailedFormat = nullptr;
10798
 *       // We also request detailed format, but we could have passed it to
10799
 *       // nullptr as well.
10800
 *       eErr =
10801
 *         poDataset->ReadCompressedData("JPEG",
10802
 *                                       0, 0,
10803
 *                                       poDataset->GetRasterXSize(),
10804
 *                                       poDataset->GetRasterYSize(),
10805
 *                                       poDataset->GetRasterCount(),
10806
 *                                       nullptr, // panBandList
10807
 *                                       &pBuffer,
10808
 *                                       &nActualSize,
10809
 *                                       &pszDetailedFormat);
10810
 *       if (eErr == CE_None)
10811
 *       {
10812
 *          CPLAssert(pBuffer == myBuffer.data()); // pointed value not modified
10813
 *          CPLAssert(nActualSize <= nUpperBoundBufferSize);
10814
 *          myBuffer.resize(nActualSize);
10815
 *          // do something useful
10816
 *          VSIFree(pszDetailedFormat);
10817
 *       }
10818
 *   }
10819
 * \endcode
10820
 *
10821
 * @since GDAL 3.7
10822
 */
10823
CPLErr GDALDataset::ReadCompressedData(
10824
    CPL_UNUSED const char *pszFormat, CPL_UNUSED int nXOff,
10825
    CPL_UNUSED int nYOff, CPL_UNUSED int nXSize, CPL_UNUSED int nYSize,
10826
    CPL_UNUSED int nBandCount, CPL_UNUSED const int *panBandList,
10827
    CPL_UNUSED void **ppBuffer, CPL_UNUSED size_t *pnBufferSize,
10828
    CPL_UNUSED char **ppszDetailedFormat)
10829
0
{
10830
0
    return CE_Failure;
10831
0
}
10832
10833
/************************************************************************/
10834
/*                  GDALDatasetReadCompressedData()                     */
10835
/************************************************************************/
10836
10837
/** Return the compressed content that can be natively obtained for the
10838
 * window of interest and requested bands.
10839
 *
10840
 * For example, a tiled dataset may be able to return data in compressed format
10841
 * if the window of interest matches exactly a tile. For some formats, drivers
10842
 * may also be example to merge several tiles together (not currently
10843
 * implemented though).
10844
 *
10845
 * The implementation should make sure that the content returned forms a valid
10846
 * standalone file. For example, for the GeoTIFF implementation of this method,
10847
 * when extracting a JPEG tile, the method will automatically adds the content
10848
 * of the JPEG Huffman and/or quantization tables that might be stored in the
10849
 * TIFF JpegTables tag, and not in tile data itself.
10850
 *
10851
 * In the general case this method will return CE_Failure.
10852
 *
10853
 * This is the same as C++ method GDALDataset:ReadCompressedData().
10854
 *
10855
 * @param hDS Dataset handle.
10856
 *
10857
 * @param pszFormat Requested compression format (e.g. "JPEG",
10858
 * "WEBP", "JXL"). This is the MIME type of one of the values
10859
 * returned by GetCompressionFormats(). The format string is designed to
10860
 * potentially include at a later point key=value optional parameters separated
10861
 * by a semi-colon character. At time of writing, none are implemented.
10862
 * ReadCompressedData() implementations should verify optional parameters and
10863
 * return CE_Failure if they cannot support one of them.
10864
 *
10865
 * @param nXOff The pixel offset to the top left corner of the region
10866
 * of the band to be accessed.  This would be zero to start from the left side.
10867
 *
10868
 * @param nYOff The line offset to the top left corner of the region
10869
 * of the band to be accessed.  This would be zero to start from the top.
10870
 *
10871
 * @param nXSize The width of the region of the band to be accessed in pixels.
10872
 *
10873
 * @param nYSize The height of the region of the band to be accessed in lines.
10874
 *
10875
 * @param nBandCount the number of bands being requested.
10876
 *
10877
 * @param panBandList the list of nBandCount band numbers.
10878
 * Note band numbers are 1 based. This may be NULL to select the first
10879
 * nBandCount bands.
10880
 *
10881
 * @param ppBuffer Pointer to a buffer to store the compressed data or nullptr.
10882
 * If ppBuffer is not nullptr, then pnBufferSize should also not be nullptr.
10883
 * If ppBuffer is not nullptr, and *ppBuffer is not nullptr, then the provided
10884
 * buffer will be filled with the compressed data, provided that pnBufferSize
10885
 * and *pnBufferSize are not nullptr, and *pnBufferSize, indicating the size
10886
 * of *ppBuffer, is sufficiently large to hold the data.
10887
 * If ppBuffer is not nullptr, but *ppBuffer is nullptr, then the method will
10888
 * allocate *ppBuffer using VSIMalloc(), and thus the caller is responsible to
10889
 * free it with VSIFree().
10890
 * If ppBuffer is nullptr, then the compressed data itself will not be returned,
10891
 * but *pnBufferSize will be updated with an upper bound of the size that would
10892
 * be necessary to hold it (if pnBufferSize != nullptr).
10893
 *
10894
 * @param pnBufferSize Output buffer size, or nullptr.
10895
 * If ppBuffer != nullptr && *ppBuffer != nullptr, then pnBufferSize should
10896
 * be != nullptr and *pnBufferSize contain the size of *ppBuffer. If the
10897
 * method is successful, *pnBufferSize will be updated with the actual size
10898
 * used.
10899
 *
10900
 * @param ppszDetailedFormat Pointer to an output string, or nullptr.
10901
 * If ppszDetailedFormat is not nullptr, then, on success, the method will
10902
 * allocate a new string in *ppszDetailedFormat (to be freed with VSIFree())
10903
 * *ppszDetailedFormat might contain strings like
10904
 * "JPEG;frame_type=SOF0_baseline;bit_depth=8;num_components=3;"
10905
 * "subsampling=4:2:0;colorspace=YCbCr" or simply the MIME type.
10906
 * The string will contain at least as much information as what
10907
 * GetCompressionFormats() returns, and potentially more when
10908
 * ppBuffer != nullptr.
10909
 *
10910
 * @return CE_None in case of success, CE_Failure otherwise.
10911
 *
10912
 * @since GDAL 3.7
10913
 */
10914
CPLErr GDALDatasetReadCompressedData(GDALDatasetH hDS, const char *pszFormat,
10915
                                     int nXOff, int nYOff, int nXSize,
10916
                                     int nYSize, int nBandCount,
10917
                                     const int *panBandList, void **ppBuffer,
10918
                                     size_t *pnBufferSize,
10919
                                     char **ppszDetailedFormat)
10920
0
{
10921
0
    VALIDATE_POINTER1(hDS, __func__, CE_Failure);
10922
0
    return GDALDataset::FromHandle(hDS)->ReadCompressedData(
10923
0
        pszFormat, nXOff, nYOff, nXSize, nYSize, nBandCount, panBandList,
10924
0
        ppBuffer, pnBufferSize, ppszDetailedFormat);
10925
0
}
10926
10927
/************************************************************************/
10928
/*                           CanBeCloned()                              */
10929
/************************************************************************/
10930
10931
//! @cond Doxygen_Suppress
10932
10933
/** This method is called by GDALThreadSafeDataset::Create() to determine if
10934
 * it is possible to create a thread-safe wrapper for a dataset, which involves
10935
 * the ability to Clone() it.
10936
 *
10937
 * Implementations of this method must be thread-safe.
10938
 *
10939
 * @param nScopeFlags Combination of GDAL_OF_RASTER, GDAL_OF_VECTOR, etc. flags,
10940
 *                    expressing the intended use for thread-safety.
10941
 *                    Currently, the only valid scope is in the base
10942
 *                    implementation is GDAL_OF_RASTER.
10943
 * @param bCanShareState Determines if cloned datasets are allowed to share
10944
 *                       state with the dataset they have been cloned from.
10945
 *                       If set to true, the dataset from which they have been
10946
 *                       cloned from must remain opened during the lifetime of
10947
 *                       its clones.
10948
 * @return true if the Clone() method is expected to succeed with the same values
10949
 *         of nScopeFlags and bCanShareState.
10950
 */
10951
bool GDALDataset::CanBeCloned(int nScopeFlags,
10952
                              [[maybe_unused]] bool bCanShareState) const
10953
0
{
10954
0
    return m_bCanBeReopened && nScopeFlags == GDAL_OF_RASTER;
10955
0
}
10956
10957
//! @endcond
10958
10959
/************************************************************************/
10960
/*                               Clone()                                */
10961
/************************************************************************/
10962
10963
//! @cond Doxygen_Suppress
10964
10965
/** This method "clones" the current dataset, that is it returns a new instance
10966
 * that is opened on the same underlying "file".
10967
 *
10968
 * The base implementation uses GDALDataset::Open() to re-open the dataset.
10969
 * The MEM driver has a specialized implementation that returns a new instance,
10970
 * but which shares the same memory buffer as this.
10971
 *
10972
 * Implementations of this method must be thread-safe.
10973
 *
10974
 * @param nScopeFlags Combination of GDAL_OF_RASTER, GDAL_OF_VECTOR, etc. flags,
10975
 *                    expressing the intended use for thread-safety.
10976
 *                    Currently, the only valid scope is in the base
10977
 *                    implementation is GDAL_OF_RASTER.
10978
 * @param bCanShareState Determines if cloned datasets are allowed to share
10979
 *                       state with the dataset they have been cloned from.
10980
 *                       If set to true, the dataset from which they have been
10981
 *                       cloned from must remain opened during the lifetime of
10982
 *                       its clones.
10983
 * @return a new instance, or nullptr in case of error.
10984
 */
10985
std::unique_ptr<GDALDataset>
10986
GDALDataset::Clone(int nScopeFlags, [[maybe_unused]] bool bCanShareState) const
10987
0
{
10988
0
    CPLStringList aosAllowedDrivers;
10989
0
    if (poDriver)
10990
0
        aosAllowedDrivers.AddString(poDriver->GetDescription());
10991
0
    return std::unique_ptr<GDALDataset>(GDALDataset::Open(
10992
0
        GetDescription(),
10993
0
        nScopeFlags | GDAL_OF_INTERNAL | GDAL_OF_VERBOSE_ERROR,
10994
0
        aosAllowedDrivers.List(), papszOpenOptions));
10995
0
}
10996
10997
//! @endcond
10998
10999
/************************************************************************/
11000
/*                    GeolocationToPixelLine()                          */
11001
/************************************************************************/
11002
11003
/** Transform georeferenced coordinates to pixel/line coordinates.
11004
 *
11005
 * When poSRS is null, those georeferenced coordinates (dfGeolocX, dfGeolocY)
11006
 * must be in the "natural" SRS of the dataset, that is the one returned by
11007
 * GetSpatialRef() if there is a geotransform, GetGCPSpatialRef() if there are
11008
 * GCPs, WGS 84 if there are RPC coefficients, or the SRS of the geolocation
11009
 * array (generally WGS 84) if there is a geolocation array.
11010
 * If that natural SRS is a geographic one, dfGeolocX must be a longitude, and
11011
 * dfGeolocY a latitude. If that natural SRS is a projected one, dfGeolocX must
11012
 * be a easting, and dfGeolocY a northing.
11013
 *
11014
 * When poSRS is set to a non-null value, (dfGeolocX, dfGeolocY) must be
11015
 * expressed in that CRS, and that tuple must be conformant with the
11016
 * data-axis-to-crs-axis setting of poSRS, that is the one returned by
11017
 * the OGRSpatialReference::GetDataAxisToSRSAxisMapping(). If you want to be sure
11018
 * of the axis order, then make sure to call poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
11019
 * before calling this method, and in that case, dfGeolocX must be a longitude
11020
 * or an easting value, and dfGeolocX a latitude or a northing value.
11021
 *
11022
 * This method uses GDALCreateGenImgProjTransformer2() underneath.
11023
 *
11024
 * @param dfGeolocX X coordinate of the position (longitude or easting if poSRS
11025
 * is null, otherwise consistent with poSRS data-axis-to-crs-axis mapping),
11026
 * where interpolation should be done.
11027
 * @param dfGeolocY Y coordinate of the position (latitude or northing if poSRS
11028
 * is null, otherwise consistent with poSRS data-axis-to-crs-axis mapping),
11029
 * where interpolation should be done.
11030
 * @param poSRS If set, override the natural CRS in which dfGeolocX, dfGeolocY are expressed
11031
 * @param[out] pdfPixel Pointer to the variable where to the store the pixel/column coordinate.
11032
 * @param[out] pdfLine Pointer to the variable where to the store the line coordinate.
11033
 * @param papszTransformerOptions Options accepted by GDALCreateGenImgProjTransformer2(), or nullptr.
11034
 *
11035
 * @return CE_None on success, or an error code on failure.
11036
 * @since GDAL 3.11
11037
 */
11038
11039
CPLErr
11040
GDALDataset::GeolocationToPixelLine(double dfGeolocX, double dfGeolocY,
11041
                                    const OGRSpatialReference *poSRS,
11042
                                    double *pdfPixel, double *pdfLine,
11043
                                    CSLConstList papszTransformerOptions) const
11044
0
{
11045
0
    CPLStringList aosTO(papszTransformerOptions);
11046
11047
0
    if (poSRS)
11048
0
    {
11049
0
        const char *const apszOptions[] = {"FORMAT=WKT2", nullptr};
11050
0
        const std::string osWKT = poSRS->exportToWkt(apszOptions);
11051
0
        aosTO.SetNameValue("DST_SRS", osWKT.c_str());
11052
0
        const auto eAxisMappingStrategy = poSRS->GetAxisMappingStrategy();
11053
0
        if (eAxisMappingStrategy == OAMS_TRADITIONAL_GIS_ORDER)
11054
0
            aosTO.SetNameValue("DST_SRS_AXIS_MAPPING_STRATEGY",
11055
0
                               "TRADITIONAL_GIS_ORDER");
11056
0
        else if (eAxisMappingStrategy == OAMS_AUTHORITY_COMPLIANT)
11057
0
            aosTO.SetNameValue("DST_SRS_AXIS_MAPPING_STRATEGY",
11058
0
                               "AUTHORITY_COMPLIANT");
11059
0
        else
11060
0
        {
11061
0
            const auto &anValues = poSRS->GetDataAxisToSRSAxisMapping();
11062
0
            std::string osVal;
11063
0
            for (int v : anValues)
11064
0
            {
11065
0
                if (!osVal.empty())
11066
0
                    osVal += ',';
11067
0
                osVal += std::to_string(v);
11068
0
            }
11069
0
            aosTO.SetNameValue("DST_SRS_DATA_AXIS_TO_SRS_AXIS_MAPPING",
11070
0
                               osVal.c_str());
11071
0
        }
11072
0
    }
11073
11074
0
    auto hTransformer = GDALCreateGenImgProjTransformer2(
11075
0
        GDALDataset::ToHandle(const_cast<GDALDataset *>(this)), nullptr,
11076
0
        aosTO.List());
11077
0
    if (hTransformer == nullptr)
11078
0
    {
11079
0
        return CE_Failure;
11080
0
    }
11081
11082
0
    double z = 0;
11083
0
    int bSuccess = 0;
11084
0
    GDALGenImgProjTransform(hTransformer, TRUE, 1, &dfGeolocX, &dfGeolocY, &z,
11085
0
                            &bSuccess);
11086
0
    GDALDestroyTransformer(hTransformer);
11087
0
    if (bSuccess)
11088
0
    {
11089
0
        if (pdfPixel)
11090
0
            *pdfPixel = dfGeolocX;
11091
0
        if (pdfLine)
11092
0
            *pdfLine = dfGeolocY;
11093
0
        return CE_None;
11094
0
    }
11095
0
    else
11096
0
    {
11097
0
        return CE_Failure;
11098
0
    }
11099
0
}
11100
11101
/************************************************************************/
11102
/*                  GDALDatasetGeolocationToPixelLine()                 */
11103
/************************************************************************/
11104
11105
/** Transform georeferenced coordinates to pixel/line coordinates.
11106
 *
11107
 * @see GDALDataset::GeolocationToPixelLine()
11108
 * @since GDAL 3.11
11109
 */
11110
11111
CPLErr GDALDatasetGeolocationToPixelLine(GDALDatasetH hDS, double dfGeolocX,
11112
                                         double dfGeolocY,
11113
                                         OGRSpatialReferenceH hSRS,
11114
                                         double *pdfPixel, double *pdfLine,
11115
                                         CSLConstList papszTransformerOptions)
11116
0
{
11117
0
    VALIDATE_POINTER1(hDS, "GDALDatasetGeolocationToPixelLine", CE_Failure);
11118
11119
0
    GDALDataset *poDS = GDALDataset::FromHandle(hDS);
11120
0
    return poDS->GeolocationToPixelLine(
11121
0
        dfGeolocX, dfGeolocY, OGRSpatialReference::FromHandle(hSRS), pdfPixel,
11122
0
        pdfLine, papszTransformerOptions);
11123
0
}
11124
11125
/************************************************************************/
11126
/*                               GetExtent()                            */
11127
/************************************************************************/
11128
11129
/** Return extent of dataset in specified CRS.
11130
 *
11131
 * OGREnvelope.MinX/MaxX represents longitudes, and MinY/MaxY latitudes.
11132
 *
11133
 * For rasters, the base implementation of this method only succeeds if
11134
 * GetGeoTransform() and GetSpatialRef() succeed.
11135
 * For vectors, the base implementation of this method iterates over layers
11136
 * and call their OGRLayer::GetExtent() method.
11137
 *
11138
 * TestCapability(GDsCFastGetExtent) can be used to test if the execution
11139
 * time of this method is fast.
11140
 *
11141
 * This is the same as C function GDALGetExtent()
11142
 *
11143
 * @param[out] psExtent Pointer to output extent. Must NOT be null.
11144
 * @param poCRS CRS in which to express the extent. If not specified, this will
11145
 * be the raster CRS or the CRS of the first layer for a vector dataset.
11146
 * @return CE_None in case of success, CE_Failure otherwise
11147
 * @since GDAL 3.12
11148
 */
11149
11150
CPLErr GDALDataset::GetExtent(OGREnvelope *psExtent,
11151
                              const OGRSpatialReference *poCRS) const
11152
0
{
11153
0
    const OGRSpatialReference *poThisCRS = GetSpatialRefRasterOnly();
11154
0
    int nLayerCount = 0;
11155
0
    if (!poThisCRS)
11156
0
    {
11157
0
        nLayerCount = GetLayerCount();
11158
0
        if (nLayerCount >= 1)
11159
0
        {
11160
0
            if (auto poLayer = GetLayer(0))
11161
0
                poThisCRS = poLayer->GetSpatialRef();
11162
0
        }
11163
0
    }
11164
0
    if (!poCRS)
11165
0
        poCRS = poThisCRS;
11166
0
    else if (!poThisCRS)
11167
0
        return CE_Failure;
11168
11169
0
    *psExtent = OGREnvelope();
11170
11171
0
    GDALGeoTransform gt;
11172
0
    auto poThisDS = const_cast<GDALDataset *>(this);
11173
0
    const bool bHasGT = poThisDS->GetGeoTransform(gt) == CE_None;
11174
0
    if (bHasGT)
11175
0
    {
11176
0
        std::unique_ptr<OGRCoordinateTransformation> poCT;
11177
0
        if (poCRS)
11178
0
        {
11179
0
            poCT.reset(OGRCreateCoordinateTransformation(poThisCRS, poCRS));
11180
0
        }
11181
11182
0
        constexpr int DENSIFY_POINT_COUNT = 21;
11183
0
        double dfULX = gt[0];
11184
0
        double dfULY = gt[3];
11185
0
        double dfURX = 0, dfURY = 0;
11186
0
        gt.Apply(nRasterXSize, 0, &dfURX, &dfURY);
11187
0
        double dfLLX = 0, dfLLY = 0;
11188
0
        gt.Apply(0, nRasterYSize, &dfLLX, &dfLLY);
11189
0
        double dfLRX = 0, dfLRY = 0;
11190
0
        gt.Apply(nRasterXSize, nRasterYSize, &dfLRX, &dfLRY);
11191
0
        const double xmin = std::min({dfULX, dfURX, dfLLX, dfLRX});
11192
0
        const double ymin = std::min({dfULY, dfURY, dfLLY, dfLRY});
11193
0
        const double xmax = std::max({dfULX, dfURX, dfLLX, dfLRX});
11194
0
        const double ymax = std::max({dfULY, dfURY, dfLLY, dfLRY});
11195
0
        if (poCT)
11196
0
        {
11197
0
            OGREnvelope sEnvTmp;
11198
0
            if (!poCT->TransformBounds(xmin, ymin, xmax, ymax, &(sEnvTmp.MinX),
11199
0
                                       &(sEnvTmp.MinY), &(sEnvTmp.MaxX),
11200
0
                                       &(sEnvTmp.MaxY), DENSIFY_POINT_COUNT))
11201
0
            {
11202
0
                return CE_Failure;
11203
0
            }
11204
0
            *psExtent = sEnvTmp;
11205
0
        }
11206
0
        else
11207
0
        {
11208
0
            psExtent->MinX = xmin;
11209
0
            psExtent->MinY = ymin;
11210
0
            psExtent->MaxX = xmax;
11211
0
            psExtent->MaxY = ymax;
11212
0
        }
11213
0
    }
11214
11215
0
    if (nLayerCount > 0)
11216
0
    {
11217
0
        for (auto &&poLayer : poThisDS->GetLayers())
11218
0
        {
11219
0
            auto poLayerCRS = poLayer->GetSpatialRef();
11220
0
            if (poLayerCRS)
11221
0
            {
11222
0
                OGREnvelope sLayerExtent;
11223
0
                if (poLayer->GetExtent(&sLayerExtent) == OGRERR_NONE)
11224
0
                {
11225
0
                    auto poCT = std::unique_ptr<OGRCoordinateTransformation>(
11226
0
                        OGRCreateCoordinateTransformation(poLayerCRS, poCRS));
11227
0
                    if (poCT)
11228
0
                    {
11229
0
                        constexpr int DENSIFY_POINT_COUNT = 21;
11230
0
                        OGREnvelope sEnvTmp;
11231
0
                        if (poCT->TransformBounds(
11232
0
                                sLayerExtent.MinX, sLayerExtent.MinY,
11233
0
                                sLayerExtent.MaxX, sLayerExtent.MaxY,
11234
0
                                &(sEnvTmp.MinX), &(sEnvTmp.MinY),
11235
0
                                &(sEnvTmp.MaxX), &(sEnvTmp.MaxY),
11236
0
                                DENSIFY_POINT_COUNT))
11237
0
                        {
11238
0
                            psExtent->Merge(sEnvTmp);
11239
0
                        }
11240
0
                    }
11241
0
                }
11242
0
            }
11243
0
        }
11244
0
    }
11245
11246
0
    return psExtent->IsInit() ? CE_None : CE_Failure;
11247
0
}
11248
11249
/************************************************************************/
11250
/*                           GDALGetExtent()                            */
11251
/************************************************************************/
11252
11253
/** Return extent of dataset in specified CRS.
11254
 *
11255
 * OGREnvelope.MinX/MaxX represents longitudes, and MinY/MaxY latitudes.
11256
 *
11257
 * For rasters, the base implementation of this method only succeeds if
11258
 * GetGeoTransform() and GetSpatialRef() succeed.
11259
 * For vectors, the base implementation of this method iterates over layers
11260
 * and call their OGRLayer::GetExtent() method.
11261
 *
11262
 * TestCapability(GDsCFastGetExtent) can be used to test if the execution
11263
 * time of this method is fast.
11264
 *
11265
 * This is the same as C++ method GDALDataset::GetExtent()
11266
 *
11267
 * @param hDS Dataset handle. Must NOT be null.
11268
 * @param[out] psExtent Pointer to output extent. Must NOT be null.
11269
 * @param hCRS CRS in which to express the extent. If not specified, this will
11270
 * be the raster CRS or the CRS of the first layer for a vector dataset.
11271
 * @return extent in poCRS (valid only if IsInit() method returns true)
11272
 * @since GDAL 3.12
11273
 */
11274
11275
CPLErr GDALGetExtent(GDALDatasetH hDS, OGREnvelope *psExtent,
11276
                     OGRSpatialReferenceH hCRS)
11277
0
{
11278
0
    VALIDATE_POINTER1(hDS, __func__, CE_Failure);
11279
0
    VALIDATE_POINTER1(psExtent, __func__, CE_Failure);
11280
0
    return GDALDataset::FromHandle(hDS)->GetExtent(
11281
0
        psExtent, OGRSpatialReference::FromHandle(hCRS));
11282
0
}
11283
11284
/************************************************************************/
11285
/*                         GetExtentWGS84LongLat()                      */
11286
/************************************************************************/
11287
11288
/** Return extent of dataset in WGS84 longitude/latitude
11289
 *
11290
 * OGREnvelope.MinX/MaxX represents longitudes, and MinY/MaxY latitudes.
11291
 *
11292
 * TestCapability(GDsCFastGetExtentWGS84LongLat) can be used to test if the execution
11293
 * time of this method is fast.
11294
 *
11295
 * This is the same as C function GDALGetExtentWGS84LongLat()
11296
 *
11297
 * @return extent (valid only if IsInit() method returns true)
11298
 * @since GDAL 3.12
11299
 */
11300
11301
CPLErr GDALDataset::GetExtentWGS84LongLat(OGREnvelope *psExtent) const
11302
0
{
11303
0
    OGRSpatialReference oSRS_WGS84;
11304
0
    oSRS_WGS84.SetFromUserInput("WGS84");
11305
0
    oSRS_WGS84.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
11306
0
    return GetExtent(psExtent, &oSRS_WGS84);
11307
0
}
11308
11309
/************************************************************************/
11310
/*                    GDALGetExtentWGS84LongLat()                       */
11311
/************************************************************************/
11312
11313
/** Return extent of dataset in WGS84 longitude/latitude
11314
 *
11315
 * OGREnvelope.MinX/MaxX represents longitudes, and MinY/MaxY latitudes.
11316
 *
11317
 * TestCapability(GDsCFastGetExtentWGS84LongLat) can be used to test if the execution
11318
 * time of this method is fast.
11319
 *
11320
 * This is the same as C++ method GDALDataset::GetExtentWGS84LongLat()
11321
 *
11322
 * @param hDS Dataset handle. Must NOT be null.
11323
 * @param[out] psExtent Pointer to output extent. Must NOT be null.
11324
 * @return extent (valid only if IsInit() method returns true)
11325
 * @since GDAL 3.12
11326
 */
11327
11328
CPLErr GDALGetExtentWGS84LongLat(GDALDatasetH hDS, OGREnvelope *psExtent)
11329
0
{
11330
0
    VALIDATE_POINTER1(hDS, __func__, CE_Failure);
11331
0
    VALIDATE_POINTER1(psExtent, __func__, CE_Failure);
11332
0
    return GDALDataset::FromHandle(hDS)->GetExtentWGS84LongLat(psExtent);
11333
0
}
11334
11335
/************************************************************************/
11336
/*                  ReportUpdateNotSupportedByDriver()                  */
11337
/************************************************************************/
11338
11339
//! @cond Doxygen_Suppress
11340
11341
/* static */
11342
void GDALDataset::ReportUpdateNotSupportedByDriver(const char *pszDriverName)
11343
0
{
11344
0
    CPLError(CE_Failure, CPLE_NotSupported,
11345
0
             "The %s driver does not support update access to existing "
11346
0
             "datasets.",
11347
0
             pszDriverName);
11348
0
}
11349
11350
//! @endcond
11351
11352
/************************************************************************/
11353
/*                         BuildFilename()                              */
11354
/************************************************************************/
11355
11356
/** Generates a filename, potentially relative to another one.
11357
 *
11358
 * Given the path to a reference directory, and a path to a file
11359
 * referenced from it, build a path to the file that the current application
11360
 * can use. If the file path is already absolute, rather than relative, or if
11361
 * bRelativeToReferencePath is false, then the filename of interest will be
11362
 * returned unaltered.
11363
 *
11364
 * This is enhanced version of CPLProjectRelativeFilenameSafe() that takes
11365
 * into account the subdataset syntax.
11366
 *
11367
 * Examples:
11368
 * \code{.cpp}
11369
 * BuildFilename("tmp/abc.gif", "abc/def", true) == "abc/def/tmp/abc.gif"
11370
 * BuildFilename("../abc.gif", "/abc/def") == "/abc/abc.gif"
11371
 * BuildFilename("abc.gif", "C:\WIN", true) == "C:\WIN\abc.gif"
11372
 * BuildFilename("abc.gif", "C:\WIN", false) == "abc.gif"
11373
 * BuildFilename("/home/even/foo.tif", "/home/even/workdir", true) == "/home/even/foo.tif"
11374
 * \endcode
11375
 *
11376
 * @param pszFilename Filename of interest.
11377
 * @param pszReferencePath Path to a reference directory.
11378
 * @param bRelativeToReferencePath Whether pszFilename, if a relative path, is
11379
 *                                 relative to pszReferencePath
11380
 * @since 3.11
11381
 */
11382
11383
/* static */
11384
std::string GDALDataset::BuildFilename(const char *pszFilename,
11385
                                       const char *pszReferencePath,
11386
                                       bool bRelativeToReferencePath)
11387
0
{
11388
0
    std::string osSrcDSName;
11389
0
    if (pszReferencePath != nullptr && bRelativeToReferencePath)
11390
0
    {
11391
        // Try subdatasetinfo API first
11392
        // Note: this will become the only branch when subdatasetinfo will become
11393
        //       available for NITF_IM, RASTERLITE and TILEDB
11394
0
        const auto oSubDSInfo{GDALGetSubdatasetInfo(pszFilename)};
11395
0
        if (oSubDSInfo && !oSubDSInfo->GetPathComponent().empty())
11396
0
        {
11397
0
            auto path{oSubDSInfo->GetPathComponent()};
11398
0
            osSrcDSName = oSubDSInfo->ModifyPathComponent(
11399
0
                CPLProjectRelativeFilenameSafe(pszReferencePath, path.c_str())
11400
0
                    .c_str());
11401
0
            GDALDestroySubdatasetInfo(oSubDSInfo);
11402
0
        }
11403
0
        else
11404
0
        {
11405
0
            bool bDone = false;
11406
0
            for (const char *pszSyntax : apszSpecialSubDatasetSyntax)
11407
0
            {
11408
0
                CPLString osPrefix(pszSyntax);
11409
0
                osPrefix.resize(strchr(pszSyntax, ':') - pszSyntax + 1);
11410
0
                if (pszSyntax[osPrefix.size()] == '"')
11411
0
                    osPrefix += '"';
11412
0
                if (EQUALN(pszFilename, osPrefix, osPrefix.size()))
11413
0
                {
11414
0
                    if (STARTS_WITH_CI(pszSyntax + osPrefix.size(), "{ANY}"))
11415
0
                    {
11416
0
                        const char *pszLastPart = strrchr(pszFilename, ':') + 1;
11417
                        // CSV:z:/foo.xyz
11418
0
                        if ((pszLastPart[0] == '/' || pszLastPart[0] == '\\') &&
11419
0
                            pszLastPart - pszFilename >= 3 &&
11420
0
                            pszLastPart[-3] == ':')
11421
0
                        {
11422
0
                            pszLastPart -= 2;
11423
0
                        }
11424
0
                        CPLString osPrefixFilename = pszFilename;
11425
0
                        osPrefixFilename.resize(pszLastPart - pszFilename);
11426
0
                        osSrcDSName = osPrefixFilename +
11427
0
                                      CPLProjectRelativeFilenameSafe(
11428
0
                                          pszReferencePath, pszLastPart);
11429
0
                        bDone = true;
11430
0
                    }
11431
0
                    else if (STARTS_WITH_CI(pszSyntax + osPrefix.size(),
11432
0
                                            "{FILENAME}"))
11433
0
                    {
11434
0
                        CPLString osFilename(pszFilename + osPrefix.size());
11435
0
                        size_t nPos = 0;
11436
0
                        if (osFilename.size() >= 3 && osFilename[1] == ':' &&
11437
0
                            (osFilename[2] == '\\' || osFilename[2] == '/'))
11438
0
                            nPos = 2;
11439
0
                        nPos = osFilename.find(
11440
0
                            pszSyntax[osPrefix.size() + strlen("{FILENAME}")],
11441
0
                            nPos);
11442
0
                        if (nPos != std::string::npos)
11443
0
                        {
11444
0
                            const CPLString osSuffix = osFilename.substr(nPos);
11445
0
                            osFilename.resize(nPos);
11446
0
                            osSrcDSName = osPrefix +
11447
0
                                          CPLProjectRelativeFilenameSafe(
11448
0
                                              pszReferencePath, osFilename) +
11449
0
                                          osSuffix;
11450
0
                            bDone = true;
11451
0
                        }
11452
0
                    }
11453
0
                    break;
11454
0
                }
11455
0
            }
11456
0
            if (!bDone)
11457
0
            {
11458
0
                std::string osReferencePath = pszReferencePath;
11459
0
                if (!CPLIsFilenameRelative(pszReferencePath))
11460
0
                {
11461
                    // Simplify path by replacing "foo/a/../b" with "foo/b"
11462
0
                    while (STARTS_WITH(pszFilename, "../"))
11463
0
                    {
11464
0
                        osReferencePath =
11465
0
                            CPLGetPathSafe(osReferencePath.c_str());
11466
0
                        pszFilename += strlen("../");
11467
0
                    }
11468
0
                }
11469
11470
0
                osSrcDSName = CPLProjectRelativeFilenameSafe(
11471
0
                    osReferencePath.c_str(), pszFilename);
11472
0
            }
11473
0
        }
11474
0
    }
11475
0
    else
11476
0
    {
11477
0
        osSrcDSName = pszFilename;
11478
0
    }
11479
0
    return osSrcDSName;
11480
0
}
11481
11482
/************************************************************************/
11483
/*                       GDALMDArrayFromDataset                         */
11484
/************************************************************************/
11485
11486
class GDALMDArrayFromDataset final : public GDALMDArray
11487
{
11488
    CPL_DISALLOW_COPY_ASSIGN(GDALMDArrayFromDataset)
11489
11490
    GDALDataset *const m_poDS;
11491
    const GDALExtendedDataType m_dt;
11492
    std::vector<std::shared_ptr<GDALDimension>> m_dims{};
11493
    std::string m_osUnit{};
11494
    std::vector<GByte> m_abyNoData{};
11495
    std::shared_ptr<GDALMDArray> m_varX{};
11496
    std::shared_ptr<GDALMDArray> m_varY{};
11497
    std::shared_ptr<GDALMDArray> m_varBand{};
11498
    const std::string m_osFilename;
11499
    int m_iBandDim = 0;
11500
    int m_iYDim = 1;
11501
    int m_iXDim = 2;
11502
11503
    bool ReadWrite(GDALRWFlag eRWFlag, const GUInt64 *arrayStartIdx,
11504
                   const size_t *count, const GInt64 *arrayStep,
11505
                   const GPtrDiff_t *bufferStride,
11506
                   const GDALExtendedDataType &bufferDataType,
11507
                   void *pBuffer) const;
11508
11509
  protected:
11510
    GDALMDArrayFromDataset(GDALDataset *poDS, CSLConstList papszOptions)
11511
0
        : GDALAbstractMDArray(std::string(),
11512
0
                              std::string(poDS->GetDescription())),
11513
0
          GDALMDArray(std::string(), std::string(poDS->GetDescription())),
11514
0
          m_poDS(poDS), m_dt(GDALExtendedDataType::Create(
11515
0
                            poDS->GetRasterBand(1)->GetRasterDataType())),
11516
0
          m_osFilename(poDS->GetDescription())
11517
0
    {
11518
0
        m_poDS->Reference();
11519
11520
0
        const int nBandCount = poDS->GetRasterCount();
11521
0
        for (int i = 1; i <= nBandCount; ++i)
11522
0
        {
11523
0
            const auto poBand = poDS->GetRasterBand(i);
11524
0
            if (i == 1)
11525
0
                m_osUnit = poBand->GetUnitType();
11526
0
            else if (m_osUnit != poBand->GetUnitType())
11527
0
                m_osUnit.clear();
11528
11529
0
            std::vector<GByte> abyNoData;
11530
0
            int bHasNoData = false;
11531
0
            switch (poBand->GetRasterDataType())
11532
0
            {
11533
0
                case GDT_Int64:
11534
0
                {
11535
0
                    const auto nNoData =
11536
0
                        poBand->GetNoDataValueAsInt64(&bHasNoData);
11537
0
                    if (bHasNoData)
11538
0
                    {
11539
0
                        abyNoData.resize(m_dt.GetSize());
11540
0
                        GDALCopyWords64(&nNoData, GDT_Int64, 0, &abyNoData[0],
11541
0
                                        m_dt.GetNumericDataType(), 0, 1);
11542
0
                    }
11543
0
                    break;
11544
0
                }
11545
11546
0
                case GDT_UInt64:
11547
0
                {
11548
0
                    const auto nNoData =
11549
0
                        poBand->GetNoDataValueAsUInt64(&bHasNoData);
11550
0
                    if (bHasNoData)
11551
0
                    {
11552
0
                        abyNoData.resize(m_dt.GetSize());
11553
0
                        GDALCopyWords64(&nNoData, GDT_UInt64, 0, &abyNoData[0],
11554
0
                                        m_dt.GetNumericDataType(), 0, 1);
11555
0
                    }
11556
0
                    break;
11557
0
                }
11558
11559
0
                default:
11560
0
                {
11561
0
                    const auto dfNoData = poBand->GetNoDataValue(&bHasNoData);
11562
0
                    if (bHasNoData)
11563
0
                    {
11564
0
                        abyNoData.resize(m_dt.GetSize());
11565
0
                        GDALCopyWords64(&dfNoData, GDT_Float64, 0,
11566
0
                                        &abyNoData[0],
11567
0
                                        m_dt.GetNumericDataType(), 0, 1);
11568
0
                    }
11569
0
                    break;
11570
0
                }
11571
0
            }
11572
11573
0
            if (i == 1)
11574
0
                m_abyNoData = std::move(abyNoData);
11575
0
            else if (m_abyNoData != abyNoData)
11576
0
                m_abyNoData.clear();
11577
0
        }
11578
11579
0
        const int nXSize = poDS->GetRasterXSize();
11580
0
        const int nYSize = poDS->GetRasterYSize();
11581
11582
0
        auto poSRS = poDS->GetSpatialRef();
11583
0
        std::string osTypeY;
11584
0
        std::string osTypeX;
11585
0
        std::string osDirectionY;
11586
0
        std::string osDirectionX;
11587
0
        if (poSRS && poSRS->GetAxesCount() == 2)
11588
0
        {
11589
0
            const auto &mapping = poSRS->GetDataAxisToSRSAxisMapping();
11590
0
            OGRAxisOrientation eOrientation1 = OAO_Other;
11591
0
            poSRS->GetAxis(nullptr, 0, &eOrientation1);
11592
0
            OGRAxisOrientation eOrientation2 = OAO_Other;
11593
0
            poSRS->GetAxis(nullptr, 1, &eOrientation2);
11594
0
            if (eOrientation1 == OAO_East && eOrientation2 == OAO_North)
11595
0
            {
11596
0
                if (mapping == std::vector<int>{1, 2})
11597
0
                {
11598
0
                    osTypeY = GDAL_DIM_TYPE_HORIZONTAL_Y;
11599
0
                    osDirectionY = "NORTH";
11600
0
                    osTypeX = GDAL_DIM_TYPE_HORIZONTAL_X;
11601
0
                    osDirectionX = "EAST";
11602
0
                }
11603
0
            }
11604
0
            else if (eOrientation1 == OAO_North && eOrientation2 == OAO_East)
11605
0
            {
11606
0
                if (mapping == std::vector<int>{2, 1})
11607
0
                {
11608
0
                    osTypeY = GDAL_DIM_TYPE_HORIZONTAL_Y;
11609
0
                    osDirectionY = "NORTH";
11610
0
                    osTypeX = GDAL_DIM_TYPE_HORIZONTAL_X;
11611
0
                    osDirectionX = "EAST";
11612
0
                }
11613
0
            }
11614
0
        }
11615
11616
0
        const bool bBandYX = [papszOptions, poDS, nBandCount]()
11617
0
        {
11618
0
            const char *pszDimOrder =
11619
0
                CSLFetchNameValueDef(papszOptions, "DIM_ORDER", "AUTO");
11620
0
            if (EQUAL(pszDimOrder, "AUTO"))
11621
0
            {
11622
0
                const char *pszInterleave =
11623
0
                    poDS->GetMetadataItem("INTERLEAVE", "IMAGE_STRUCTURE");
11624
0
                return nBandCount == 1 || !pszInterleave ||
11625
0
                       !EQUAL(pszInterleave, "PIXEL");
11626
0
            }
11627
0
            else
11628
0
            {
11629
0
                return EQUAL(pszDimOrder, "BAND,Y,X");
11630
0
            }
11631
0
        }();
11632
0
        const char *const pszBandDimName =
11633
0
            CSLFetchNameValueDef(papszOptions, "BAND_DIM_NAME", "Band");
11634
0
        auto poBandDim = std::make_shared<GDALDimensionWeakIndexingVar>(
11635
0
            "/", pszBandDimName, std::string(), std::string(), nBandCount);
11636
0
        const char *const pszYDimName =
11637
0
            CSLFetchNameValueDef(papszOptions, "Y_DIM_NAME", "Y");
11638
0
        auto poYDim = std::make_shared<GDALDimensionWeakIndexingVar>(
11639
0
            "/", pszYDimName, osTypeY, osDirectionY, nYSize);
11640
0
        const char *const pszXDimName =
11641
0
            CSLFetchNameValueDef(papszOptions, "X_DIM_NAME", "X");
11642
0
        auto poXDim = std::make_shared<GDALDimensionWeakIndexingVar>(
11643
0
            "/", pszXDimName, osTypeX, osDirectionX, nXSize);
11644
11645
0
        const char *const pszBandIndexingVarItem = CSLFetchNameValueDef(
11646
0
            papszOptions, "BAND_INDEXING_VAR_ITEM", "{Description}");
11647
0
        if (EQUAL(pszBandIndexingVarItem, "{Description}"))
11648
0
        {
11649
0
            const auto oIndexingVarType =
11650
0
                GDALExtendedDataType::CreateString(strlen("Band 65535"));
11651
0
            auto poBandVar = MEMMDArray::Create("/", poBandDim->GetName(),
11652
0
                                                {poBandDim}, oIndexingVarType);
11653
0
            CPL_IGNORE_RET_VAL(poBandVar->Init());
11654
0
            for (int i = 0; i < nBandCount; ++i)
11655
0
            {
11656
0
                const char *pszDesc =
11657
0
                    poDS->GetRasterBand(i + 1)->GetDescription();
11658
0
                const std::string osBandName =
11659
0
                    pszDesc[0] ? pszDesc : CPLSPrintf("Band %d", i + 1);
11660
0
                const char *pszBandName = osBandName.c_str();
11661
0
                const char *const apszBandVal[] = {pszBandName};
11662
0
                const GUInt64 anStartIdx[] = {static_cast<GUInt64>(i)};
11663
0
                const size_t anCount[] = {1};
11664
0
                const GInt64 arrayStep[] = {1};
11665
0
                const GPtrDiff_t anBufferStride[] = {1};
11666
0
                poBandVar->Write(anStartIdx, anCount, arrayStep, anBufferStride,
11667
0
                                 oIndexingVarType, apszBandVal);
11668
0
            }
11669
0
            m_varBand = std::move(poBandVar);
11670
0
            poBandDim->SetIndexingVariable(m_varBand);
11671
0
        }
11672
0
        else if (EQUAL(pszBandIndexingVarItem, "{Index}"))
11673
0
        {
11674
0
            const auto oIndexingVarType =
11675
0
                GDALExtendedDataType::Create(GDT_Int32);
11676
0
            auto poBandVar = MEMMDArray::Create("/", poBandDim->GetName(),
11677
0
                                                {poBandDim}, oIndexingVarType);
11678
0
            CPL_IGNORE_RET_VAL(poBandVar->Init());
11679
0
            for (int i = 0; i < nBandCount; ++i)
11680
0
            {
11681
0
                const int anBandIdx[] = {i + 1};
11682
0
                const GUInt64 anStartIdx[] = {static_cast<GUInt64>(i)};
11683
0
                const size_t anCount[] = {1};
11684
0
                const GInt64 arrayStep[] = {1};
11685
0
                const GPtrDiff_t anBufferStride[] = {1};
11686
0
                poBandVar->Write(anStartIdx, anCount, arrayStep, anBufferStride,
11687
0
                                 oIndexingVarType, anBandIdx);
11688
0
            }
11689
0
            m_varBand = std::move(poBandVar);
11690
0
            poBandDim->SetIndexingVariable(m_varBand);
11691
0
        }
11692
0
        else if (EQUAL(pszBandIndexingVarItem, "{ColorInterpretation}"))
11693
0
        {
11694
0
            size_t nMaxLen = 0;
11695
0
            for (int i = 0; i < nBandCount; ++i)
11696
0
            {
11697
0
                const char *pszDesc = GDALGetColorInterpretationName(
11698
0
                    poDS->GetRasterBand(i + 1)->GetColorInterpretation());
11699
0
                nMaxLen = std::max(nMaxLen, strlen(pszDesc));
11700
0
            }
11701
0
            const auto oIndexingVarType =
11702
0
                GDALExtendedDataType::CreateString(nMaxLen);
11703
0
            auto poBandVar = MEMMDArray::Create("/", poBandDim->GetName(),
11704
0
                                                {poBandDim}, oIndexingVarType);
11705
0
            CPL_IGNORE_RET_VAL(poBandVar->Init());
11706
0
            for (int i = 0; i < nBandCount; ++i)
11707
0
            {
11708
0
                const char *pszDesc = GDALGetColorInterpretationName(
11709
0
                    poDS->GetRasterBand(i + 1)->GetColorInterpretation());
11710
0
                const char *const apszBandVal[] = {pszDesc};
11711
0
                const GUInt64 anStartIdx[] = {static_cast<GUInt64>(i)};
11712
0
                const size_t anCount[] = {1};
11713
0
                const GInt64 arrayStep[] = {1};
11714
0
                const GPtrDiff_t anBufferStride[] = {1};
11715
0
                poBandVar->Write(anStartIdx, anCount, arrayStep, anBufferStride,
11716
0
                                 oIndexingVarType, apszBandVal);
11717
0
            }
11718
0
            m_varBand = std::move(poBandVar);
11719
0
            poBandDim->SetIndexingVariable(m_varBand);
11720
0
        }
11721
0
        else if (!EQUAL(pszBandIndexingVarItem, "{None}"))
11722
0
        {
11723
0
            const char *const pszBandIndexingVarType = CSLFetchNameValueDef(
11724
0
                papszOptions, "BAND_INDEXING_VAR_TYPE", "String");
11725
0
            size_t nMaxLen = 0;
11726
0
            if (EQUAL(pszBandIndexingVarType, "String"))
11727
0
            {
11728
0
                for (int i = 0; i < nBandCount; ++i)
11729
0
                {
11730
0
                    const char *pszVal =
11731
0
                        poDS->GetRasterBand(i + 1)->GetMetadataItem(
11732
0
                            pszBandIndexingVarItem);
11733
0
                    if (pszVal)
11734
0
                        nMaxLen = std::max(nMaxLen, strlen(pszVal));
11735
0
                }
11736
0
            }
11737
0
            const auto oIndexingVarType =
11738
0
                EQUAL(pszBandIndexingVarType, "String")
11739
0
                    ? GDALExtendedDataType::CreateString(nMaxLen)
11740
0
                : EQUAL(pszBandIndexingVarType, "Integer")
11741
0
                    ? GDALExtendedDataType::Create(GDT_Int32)
11742
0
                    : GDALExtendedDataType::Create(GDT_Float64);
11743
0
            auto poBandVar = MEMMDArray::Create("/", poBandDim->GetName(),
11744
0
                                                {poBandDim}, oIndexingVarType);
11745
0
            CPL_IGNORE_RET_VAL(poBandVar->Init());
11746
0
            for (int i = 0; i < nBandCount; ++i)
11747
0
            {
11748
0
                const GUInt64 anStartIdx[] = {static_cast<GUInt64>(i)};
11749
0
                const size_t anCount[] = {1};
11750
0
                const GInt64 arrayStep[] = {1};
11751
0
                const GPtrDiff_t anBufferStride[] = {1};
11752
0
                const char *pszVal =
11753
0
                    poDS->GetRasterBand(i + 1)->GetMetadataItem(
11754
0
                        pszBandIndexingVarItem);
11755
0
                if (oIndexingVarType.GetClass() == GEDTC_STRING)
11756
0
                {
11757
0
                    const char *const apszBandVal[] = {pszVal ? pszVal : ""};
11758
0
                    poBandVar->Write(anStartIdx, anCount, arrayStep,
11759
0
                                     anBufferStride, oIndexingVarType,
11760
0
                                     apszBandVal);
11761
0
                }
11762
0
                else if (oIndexingVarType.GetNumericDataType() == GDT_Int32)
11763
0
                {
11764
0
                    const int anVal[] = {pszVal ? atoi(pszVal) : 0};
11765
0
                    poBandVar->Write(anStartIdx, anCount, arrayStep,
11766
0
                                     anBufferStride, oIndexingVarType, anVal);
11767
0
                }
11768
0
                else
11769
0
                {
11770
0
                    const double adfVal[] = {pszVal ? CPLAtof(pszVal) : 0.0};
11771
0
                    poBandVar->Write(anStartIdx, anCount, arrayStep,
11772
0
                                     anBufferStride, oIndexingVarType, adfVal);
11773
0
                }
11774
0
            }
11775
0
            m_varBand = std::move(poBandVar);
11776
0
            poBandDim->SetIndexingVariable(m_varBand);
11777
0
        }
11778
11779
0
        GDALGeoTransform gt;
11780
0
        if (m_poDS->GetGeoTransform(gt) == CE_None && gt[2] == 0 && gt[4] == 0)
11781
0
        {
11782
0
            m_varX = GDALMDArrayRegularlySpaced::Create(
11783
0
                "/", poBandDim->GetName(), poXDim, gt[0], gt[1], 0.5);
11784
0
            poXDim->SetIndexingVariable(m_varX);
11785
11786
0
            m_varY = GDALMDArrayRegularlySpaced::Create(
11787
0
                "/", poYDim->GetName(), poYDim, gt[3], gt[5], 0.5);
11788
0
            poYDim->SetIndexingVariable(m_varY);
11789
0
        }
11790
0
        if (bBandYX)
11791
0
        {
11792
0
            m_dims = {std::move(poBandDim), std::move(poYDim),
11793
0
                      std::move(poXDim)};
11794
0
        }
11795
0
        else
11796
0
        {
11797
0
            m_iYDim = 0;
11798
0
            m_iXDim = 1;
11799
0
            m_iBandDim = 2;
11800
0
            m_dims = {std::move(poYDim), std::move(poXDim),
11801
0
                      std::move(poBandDim)};
11802
0
        }
11803
0
    }
11804
11805
    bool IRead(const GUInt64 *arrayStartIdx, const size_t *count,
11806
               const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
11807
               const GDALExtendedDataType &bufferDataType,
11808
               void *pDstBuffer) const override;
11809
11810
    bool IWrite(const GUInt64 *arrayStartIdx, const size_t *count,
11811
                const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
11812
                const GDALExtendedDataType &bufferDataType,
11813
                const void *pSrcBuffer) override
11814
0
    {
11815
0
        return ReadWrite(GF_Write, arrayStartIdx, count, arrayStep,
11816
0
                         bufferStride, bufferDataType,
11817
0
                         const_cast<void *>(pSrcBuffer));
11818
0
    }
11819
11820
  public:
11821
    ~GDALMDArrayFromDataset() override
11822
0
    {
11823
0
        m_poDS->ReleaseRef();
11824
0
    }
11825
11826
    static std::shared_ptr<GDALMDArray> Create(GDALDataset *poDS,
11827
                                               CSLConstList papszOptions)
11828
0
    {
11829
0
        auto array(std::shared_ptr<GDALMDArrayFromDataset>(
11830
0
            new GDALMDArrayFromDataset(poDS, papszOptions)));
11831
0
        array->SetSelf(array);
11832
0
        return array;
11833
0
    }
11834
11835
    bool IsWritable() const override
11836
0
    {
11837
0
        return m_poDS->GetAccess() == GA_Update;
11838
0
    }
11839
11840
    const std::string &GetFilename() const override
11841
0
    {
11842
0
        return m_osFilename;
11843
0
    }
11844
11845
    const std::vector<std::shared_ptr<GDALDimension>> &
11846
    GetDimensions() const override
11847
0
    {
11848
0
        return m_dims;
11849
0
    }
11850
11851
    const GDALExtendedDataType &GetDataType() const override
11852
0
    {
11853
0
        return m_dt;
11854
0
    }
11855
11856
    const std::string &GetUnit() const override
11857
0
    {
11858
0
        return m_osUnit;
11859
0
    }
11860
11861
    const void *GetRawNoDataValue() const override
11862
0
    {
11863
0
        return m_abyNoData.empty() ? nullptr : m_abyNoData.data();
11864
0
    }
11865
11866
    double GetOffset(bool *pbHasOffset,
11867
                     GDALDataType *peStorageType) const override
11868
0
    {
11869
0
        double dfRes = 0;
11870
0
        int bHasOffset = false;
11871
0
        auto poFirstBand = m_poDS->GetRasterBand(1);
11872
0
        if (poFirstBand)  // to avoid -Wnull-dereference
11873
0
        {
11874
0
            dfRes = poFirstBand->GetOffset(&bHasOffset);
11875
0
            for (int i = 2; bHasOffset && i <= m_poDS->GetRasterCount(); ++i)
11876
0
            {
11877
0
                const double dfOtherRes =
11878
0
                    m_poDS->GetRasterBand(i)->GetOffset(&bHasOffset);
11879
0
                bHasOffset = bHasOffset && (dfOtherRes == dfRes);
11880
0
            }
11881
0
        }
11882
0
        if (pbHasOffset)
11883
0
            *pbHasOffset = CPL_TO_BOOL(bHasOffset);
11884
0
        if (peStorageType)
11885
0
            *peStorageType = GDT_Unknown;
11886
0
        return dfRes;
11887
0
    }
11888
11889
    double GetScale(bool *pbHasScale,
11890
                    GDALDataType *peStorageType) const override
11891
0
    {
11892
0
        double dfRes = 0;
11893
0
        int bHasScale = false;
11894
0
        auto poFirstBand = m_poDS->GetRasterBand(1);
11895
0
        if (poFirstBand)  // to avoid -Wnull-dereference
11896
0
        {
11897
0
            dfRes = poFirstBand->GetScale(&bHasScale);
11898
0
            for (int i = 2; bHasScale && i <= m_poDS->GetRasterCount(); ++i)
11899
0
            {
11900
0
                const double dfOtherRes =
11901
0
                    m_poDS->GetRasterBand(i)->GetScale(&bHasScale);
11902
0
                bHasScale = bHasScale && (dfOtherRes == dfRes);
11903
0
            }
11904
0
        }
11905
0
        if (pbHasScale)
11906
0
            *pbHasScale = CPL_TO_BOOL(bHasScale);
11907
0
        if (peStorageType)
11908
0
            *peStorageType = GDT_Unknown;
11909
0
        return dfRes;
11910
0
    }
11911
11912
    std::shared_ptr<OGRSpatialReference> GetSpatialRef() const override
11913
0
    {
11914
0
        auto poSrcSRS = m_poDS->GetSpatialRef();
11915
0
        if (!poSrcSRS)
11916
0
            return nullptr;
11917
0
        auto poSRS = std::shared_ptr<OGRSpatialReference>(poSrcSRS->Clone());
11918
11919
0
        auto axisMapping = poSRS->GetDataAxisToSRSAxisMapping();
11920
0
        for (auto &m : axisMapping)
11921
0
        {
11922
0
            if (m == 1)
11923
0
                m = m_iXDim + 1;
11924
0
            else if (m == 2)
11925
0
                m = m_iYDim + 1;
11926
0
            else
11927
0
                m = 0;
11928
0
        }
11929
0
        poSRS->SetDataAxisToSRSAxisMapping(axisMapping);
11930
0
        return poSRS;
11931
0
    }
11932
11933
    std::vector<GUInt64> GetBlockSize() const override
11934
0
    {
11935
0
        int nBlockXSize = 0;
11936
0
        int nBlockYSize = 0;
11937
0
        m_poDS->GetRasterBand(1)->GetBlockSize(&nBlockXSize, &nBlockYSize);
11938
0
        if (m_iBandDim == 0)
11939
0
        {
11940
0
            return std::vector<GUInt64>{1, static_cast<GUInt64>(nBlockYSize),
11941
0
                                        static_cast<GUInt64>(nBlockXSize)};
11942
0
        }
11943
0
        else
11944
0
        {
11945
0
            return std::vector<GUInt64>{static_cast<GUInt64>(nBlockYSize),
11946
0
                                        static_cast<GUInt64>(nBlockXSize), 1};
11947
0
        }
11948
0
    }
11949
11950
    std::vector<std::shared_ptr<GDALAttribute>>
11951
    GetAttributes(CSLConstList) const override
11952
0
    {
11953
0
        std::vector<std::shared_ptr<GDALAttribute>> res;
11954
0
        auto papszMD = m_poDS->GetMetadata();
11955
0
        for (auto iter = papszMD; iter && iter[0]; ++iter)
11956
0
        {
11957
0
            char *pszKey = nullptr;
11958
0
            const char *pszValue = CPLParseNameValue(*iter, &pszKey);
11959
0
            if (pszKey && pszValue)
11960
0
            {
11961
0
                res.emplace_back(
11962
0
                    std::make_shared<GDALMDIAsAttribute>(pszKey, pszValue));
11963
0
            }
11964
0
            CPLFree(pszKey);
11965
0
        }
11966
0
        return res;
11967
0
    }
11968
};
11969
11970
bool GDALMDArrayFromDataset::IRead(const GUInt64 *arrayStartIdx,
11971
                                   const size_t *count, const GInt64 *arrayStep,
11972
                                   const GPtrDiff_t *bufferStride,
11973
                                   const GDALExtendedDataType &bufferDataType,
11974
                                   void *pDstBuffer) const
11975
0
{
11976
0
    return ReadWrite(GF_Read, arrayStartIdx, count, arrayStep, bufferStride,
11977
0
                     bufferDataType, pDstBuffer);
11978
0
}
11979
11980
/************************************************************************/
11981
/*                            ReadWrite()                               */
11982
/************************************************************************/
11983
11984
bool GDALMDArrayFromDataset::ReadWrite(
11985
    GDALRWFlag eRWFlag, const GUInt64 *arrayStartIdx, const size_t *count,
11986
    const GInt64 *arrayStep, const GPtrDiff_t *bufferStride,
11987
    const GDALExtendedDataType &bufferDataType, void *pBuffer) const
11988
0
{
11989
0
    const auto eDT(bufferDataType.GetNumericDataType());
11990
0
    const auto nDTSize(GDALGetDataTypeSizeBytes(eDT));
11991
0
    const int nX =
11992
0
        arrayStep[m_iXDim] > 0
11993
0
            ? static_cast<int>(arrayStartIdx[m_iXDim])
11994
0
            : static_cast<int>(arrayStartIdx[m_iXDim] -
11995
0
                               (count[m_iXDim] - 1) * -arrayStep[m_iXDim]);
11996
0
    const int nY =
11997
0
        arrayStep[m_iYDim] > 0
11998
0
            ? static_cast<int>(arrayStartIdx[m_iYDim])
11999
0
            : static_cast<int>(arrayStartIdx[m_iYDim] -
12000
0
                               (count[m_iYDim] - 1) * -arrayStep[m_iYDim]);
12001
0
    const int nSizeX =
12002
0
        static_cast<int>(count[m_iXDim] * ABS(arrayStep[m_iXDim]));
12003
0
    const int nSizeY =
12004
0
        static_cast<int>(count[m_iYDim] * ABS(arrayStep[m_iYDim]));
12005
0
    GByte *pabyBuffer = static_cast<GByte *>(pBuffer);
12006
0
    int nStrideXSign = 1;
12007
0
    if (arrayStep[m_iXDim] < 0)
12008
0
    {
12009
0
        pabyBuffer += (count[m_iXDim] - 1) * bufferStride[m_iXDim] * nDTSize;
12010
0
        nStrideXSign = -1;
12011
0
    }
12012
0
    int nStrideYSign = 1;
12013
0
    if (arrayStep[m_iYDim] < 0)
12014
0
    {
12015
0
        pabyBuffer += (count[m_iYDim] - 1) * bufferStride[m_iYDim] * nDTSize;
12016
0
        nStrideYSign = -1;
12017
0
    }
12018
0
    const GSpacing nPixelSpace =
12019
0
        static_cast<GSpacing>(nStrideXSign * bufferStride[m_iXDim] * nDTSize);
12020
0
    const GSpacing nLineSpace =
12021
0
        static_cast<GSpacing>(nStrideYSign * bufferStride[m_iYDim] * nDTSize);
12022
0
    const GSpacing nBandSpace =
12023
0
        static_cast<GSpacing>(bufferStride[m_iBandDim] * nDTSize);
12024
0
    std::vector<int> anBandList;
12025
0
    for (int i = 0; i < static_cast<int>(count[m_iBandDim]); ++i)
12026
0
        anBandList.push_back(1 + static_cast<int>(arrayStartIdx[m_iBandDim]) +
12027
0
                             i * static_cast<int>(arrayStep[m_iBandDim]));
12028
12029
0
    return m_poDS->RasterIO(eRWFlag, nX, nY, nSizeX, nSizeY, pabyBuffer,
12030
0
                            static_cast<int>(count[m_iXDim]),
12031
0
                            static_cast<int>(count[m_iYDim]), eDT,
12032
0
                            static_cast<int>(count[m_iBandDim]),
12033
0
                            anBandList.data(), nPixelSpace, nLineSpace,
12034
0
                            nBandSpace, nullptr) == CE_None;
12035
0
}
12036
12037
/************************************************************************/
12038
/*                            AsMDArray()                               */
12039
/************************************************************************/
12040
12041
/** Return a view of this dataset as a 3D multidimensional GDALMDArray.
12042
 *
12043
 * If this dataset is not already marked as shared, it will be, so that the
12044
 * returned array holds a reference to it.
12045
 *
12046
 * If the dataset has a geotransform attached, the X and Y dimensions of the
12047
 * returned array will have an associated indexing variable.
12048
 *
12049
 * The currently supported list of options is:
12050
 * <ul>
12051
 * <li>DIM_ORDER=&lt;order&gt; where order can be "AUTO", "Band,Y,X" or "Y,X,Band".
12052
 * "Band,Y,X" means that the first (slowest changing) dimension is Band
12053
 * and the last (fastest changing direction) is X
12054
 * "Y,X,Band" means that the first (slowest changing) dimension is Y
12055
 * and the last (fastest changing direction) is Band.
12056
 * "AUTO" (the default) selects "Band,Y,X" for single band datasets, or takes
12057
 * into account the INTERLEAVE metadata item in the IMAGE_STRUCTURE domain.
12058
 * If it equals BAND, then "Band,Y,X" is used. Otherwise (if it equals PIXEL),
12059
 * "Y,X,Band" is use.
12060
 * </li>
12061
 * <li>BAND_INDEXING_VAR_ITEM={Description}|{None}|{Index}|{ColorInterpretation}|&lt;BandMetadataItem&gt;:
12062
 * item from which to build the band indexing variable.
12063
 * <ul>
12064
 * <li>"{Description}", the default, means to use the band description (or "Band index" if empty).</li>
12065
 * <li>"{None}" means that no band indexing variable must be created.</li>
12066
 * <li>"{Index}" means that the band index (starting at one) is used.</li>
12067
 * <li>"{ColorInterpretation}" means that the band color interpretation is used (i.e. "Red", "Green", "Blue").</li>
12068
 * <li>&lt;BandMetadataItem&gt; is the name of a band metadata item to use.</li>
12069
 * </ul>
12070
 * </li>
12071
 * <li>BAND_INDEXING_VAR_TYPE=String|Real|Integer: the data type of the band
12072
 * indexing variable, when BAND_INDEXING_VAR_ITEM corresponds to a band metadata item.
12073
 * Defaults to String.
12074
 * </li>
12075
 * <li>BAND_DIM_NAME=&lt;string&gt;: Name of the band dimension.
12076
 * Defaults to "Band".
12077
 * </li>
12078
 * <li>X_DIM_NAME=&lt;string&gt;: Name of the X dimension. Defaults to "X".
12079
 * </li>
12080
 * <li>Y_DIM_NAME=&lt;string&gt;: Name of the Y dimension. Defaults to "Y".
12081
 * </li>
12082
 * </ul>
12083
 *
12084
 * This is the same as the C function GDALDatasetAsMDArray().
12085
 *
12086
 * The "reverse" method is GDALMDArray::AsClassicDataset().
12087
 *
12088
 * @param papszOptions Null-terminated list of strings, or nullptr.
12089
 * @return a new array, or nullptr.
12090
 *
12091
 * @since GDAL 3.12
12092
 */
12093
std::shared_ptr<GDALMDArray> GDALDataset::AsMDArray(CSLConstList papszOptions)
12094
0
{
12095
0
    if (!GetShared())
12096
0
    {
12097
0
        MarkAsShared();
12098
0
    }
12099
0
    if (nBands == 0 || nRasterXSize == 0 || nRasterYSize == 0)
12100
0
    {
12101
0
        ReportError(
12102
0
            CE_Failure, CPLE_AppDefined,
12103
0
            "Degenerated array (band, Y and/or X dimension of size zero)");
12104
0
        return nullptr;
12105
0
    }
12106
0
    const GDALDataType eDT = papoBands[0]->GetRasterDataType();
12107
0
    for (int i = 1; i < nBands; ++i)
12108
0
    {
12109
0
        if (eDT != papoBands[i]->GetRasterDataType())
12110
0
        {
12111
0
            ReportError(CE_Failure, CPLE_AppDefined,
12112
0
                        "Non-uniform data type amongst bands");
12113
0
            return nullptr;
12114
0
        }
12115
0
    }
12116
0
    const char *pszDimOrder =
12117
0
        CSLFetchNameValueDef(papszOptions, "DIM_ORDER", "AUTO");
12118
0
    if (!EQUAL(pszDimOrder, "AUTO") && !EQUAL(pszDimOrder, "Band,Y,X") &&
12119
0
        !EQUAL(pszDimOrder, "Y,X,Band"))
12120
0
    {
12121
0
        ReportError(CE_Failure, CPLE_IllegalArg,
12122
0
                    "Illegal value for DIM_ORDER option");
12123
0
        return nullptr;
12124
0
    }
12125
0
    return GDALMDArrayFromDataset::Create(this, papszOptions);
12126
0
}