Coverage Report

Created: 2025-11-16 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/vrt/vrtdataset.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  Virtual GDAL Datasets
4
 * Purpose:  Implementation of VRTDataset
5
 * Author:   Frank Warmerdam <warmerdam@pobox.com>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2001, Frank Warmerdam <warmerdam@pobox.com>
9
 * Copyright (c) 2007-2014, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "vrtdataset.h"
15
16
#include "cpl_error_internal.h"
17
#include "cpl_minixml.h"
18
#include "cpl_string.h"
19
#include "gdal_frmts.h"
20
#include "ogr_spatialref.h"
21
#include "gdal_thread_pool.h"
22
#include "gdal_utils.h"
23
24
#include <algorithm>
25
#include <cassert>
26
#include <cmath>
27
#include <set>
28
#include <typeinfo>
29
#include "gdal_proxy.h"
30
31
/*! @cond Doxygen_Suppress */
32
33
0
#define VRT_PROTOCOL_PREFIX "vrt://"
34
35
constexpr int DEFAULT_BLOCK_SIZE = 128;
36
37
/************************************************************************/
38
/*                            VRTDataset()                              */
39
/************************************************************************/
40
41
VRTDataset::VRTDataset(int nXSize, int nYSize, int nBlockXSize, int nBlockYSize)
42
0
{
43
0
    nRasterXSize = nXSize;
44
0
    nRasterYSize = nYSize;
45
46
0
    m_bBlockSizeSpecified = nBlockXSize > 0 && nBlockYSize > 0;
47
0
    m_nBlockXSize =
48
0
        nBlockXSize > 0 ? nBlockXSize : std::min(DEFAULT_BLOCK_SIZE, nXSize);
49
0
    m_nBlockYSize =
50
0
        nBlockYSize > 0 ? nBlockYSize : std::min(DEFAULT_BLOCK_SIZE, nYSize);
51
52
0
    GDALRegister_VRT();
53
54
0
    poDriver = static_cast<GDALDriver *>(GDALGetDriverByName("VRT"));
55
0
}
56
57
/************************************************************************/
58
/*                          IsDefaultBlockSize()                        */
59
/************************************************************************/
60
61
/* static */ bool VRTDataset::IsDefaultBlockSize(int nBlockSize, int nDimension)
62
0
{
63
0
    return nBlockSize == DEFAULT_BLOCK_SIZE ||
64
0
           (nBlockSize < DEFAULT_BLOCK_SIZE && nBlockSize == nDimension);
65
0
}
66
67
/*! @endcond */
68
69
/************************************************************************/
70
/*                              VRTCreate()                             */
71
/************************************************************************/
72
73
/**
74
 * @see VRTDataset::VRTDataset()
75
 */
76
77
VRTDatasetH CPL_STDCALL VRTCreate(int nXSize, int nYSize)
78
79
0
{
80
0
    auto poDS = new VRTDataset(nXSize, nYSize);
81
0
    poDS->eAccess = GA_Update;
82
0
    return poDS;
83
0
}
84
85
/*! @cond Doxygen_Suppress */
86
87
/************************************************************************/
88
/*                            ~VRTDataset()                            */
89
/************************************************************************/
90
91
VRTDataset::~VRTDataset()
92
93
0
{
94
0
    VRTDataset::FlushCache(true);
95
0
    CPLFree(m_pszVRTPath);
96
97
0
    for (size_t i = 0; i < m_apoOverviews.size(); i++)
98
0
        delete m_apoOverviews[i];
99
0
    for (size_t i = 0; i < m_apoOverviewsBak.size(); i++)
100
0
        delete m_apoOverviewsBak[i];
101
0
    CSLDestroy(m_papszXMLVRTMetadata);
102
0
}
103
104
/************************************************************************/
105
/*                             FlushCache()                             */
106
/************************************************************************/
107
108
CPLErr VRTDataset::FlushCache(bool bAtClosing)
109
110
0
{
111
0
    if (m_poRootGroup)
112
0
    {
113
0
        m_poRootGroup->SetVRTPath(CPLGetPathSafe(GetDescription()));
114
0
        return m_poRootGroup->Serialize() ? CE_None : CE_Failure;
115
0
    }
116
0
    else
117
0
        return VRTFlushCacheStruct<VRTDataset>::FlushCache(*this, bAtClosing);
118
0
}
119
120
/************************************************************************/
121
/*                             FlushCache()                             */
122
/************************************************************************/
123
124
CPLErr VRTWarpedDataset::FlushCache(bool bAtClosing)
125
126
0
{
127
0
    return VRTFlushCacheStruct<VRTWarpedDataset>::FlushCache(*this, bAtClosing);
128
0
}
129
130
/************************************************************************/
131
/*                             FlushCache()                             */
132
/************************************************************************/
133
134
CPLErr VRTPansharpenedDataset::FlushCache(bool bAtClosing)
135
136
0
{
137
0
    return VRTFlushCacheStruct<VRTPansharpenedDataset>::FlushCache(*this,
138
0
                                                                   bAtClosing);
139
0
}
140
141
/************************************************************************/
142
/*                             FlushCache()                             */
143
/************************************************************************/
144
145
CPLErr VRTProcessedDataset::FlushCache(bool bAtClosing)
146
147
0
{
148
0
    return VRTFlushCacheStruct<VRTProcessedDataset>::FlushCache(*this,
149
0
                                                                bAtClosing);
150
0
}
151
152
/************************************************************************/
153
/*                             FlushCache()                             */
154
/************************************************************************/
155
156
template <class T>
157
CPLErr VRTFlushCacheStruct<T>::FlushCache(T &obj, bool bAtClosing)
158
0
{
159
0
    CPLErr eErr = obj.GDALDataset::FlushCache(bAtClosing);
160
161
0
    if (!obj.m_bNeedsFlush || !obj.m_bWritable)
162
0
        return eErr;
163
164
    // We don't write to disk if there is no filename.  This is a
165
    // memory only dataset.
166
0
    if (strlen(obj.GetDescription()) == 0 ||
167
0
        STARTS_WITH_CI(obj.GetDescription(), "<VRTDataset"))
168
0
        return eErr;
169
170
0
    obj.m_bNeedsFlush = false;
171
172
    // Serialize XML representation to disk
173
0
    const std::string osVRTPath(CPLGetPathSafe(obj.GetDescription()));
174
0
    CPLXMLNode *psDSTree = obj.T::SerializeToXML(osVRTPath.c_str());
175
0
    if (!CPLSerializeXMLTreeToFile(psDSTree, obj.GetDescription()))
176
0
        eErr = CE_Failure;
177
0
    CPLDestroyXMLNode(psDSTree);
178
0
    return eErr;
179
0
}
Unexecuted instantiation: VRTFlushCacheStruct<VRTDataset>::FlushCache(VRTDataset&, bool)
Unexecuted instantiation: VRTFlushCacheStruct<VRTWarpedDataset>::FlushCache(VRTWarpedDataset&, bool)
Unexecuted instantiation: VRTFlushCacheStruct<VRTPansharpenedDataset>::FlushCache(VRTPansharpenedDataset&, bool)
Unexecuted instantiation: VRTFlushCacheStruct<VRTProcessedDataset>::FlushCache(VRTProcessedDataset&, bool)
180
181
/************************************************************************/
182
/*                            GetMetadata()                             */
183
/************************************************************************/
184
185
char **VRTDataset::GetMetadata(const char *pszDomain)
186
0
{
187
0
    if (pszDomain != nullptr && EQUAL(pszDomain, "xml:VRT"))
188
0
    {
189
        /* ------------------------------------------------------------------ */
190
        /*      Convert tree to a single block of XML text.                   */
191
        /* ------------------------------------------------------------------ */
192
0
        const char *pszDescription = GetDescription();
193
0
        char *l_pszVRTPath = CPLStrdup(
194
0
            pszDescription[0] && !STARTS_WITH(pszDescription, "<VRTDataset")
195
0
                ? CPLGetPathSafe(pszDescription).c_str()
196
0
                : "");
197
0
        CPLXMLNode *psDSTree = SerializeToXML(l_pszVRTPath);
198
0
        char *pszXML = CPLSerializeXMLTree(psDSTree);
199
200
0
        CPLDestroyXMLNode(psDSTree);
201
202
0
        CPLFree(l_pszVRTPath);
203
204
0
        CSLDestroy(m_papszXMLVRTMetadata);
205
0
        m_papszXMLVRTMetadata =
206
0
            static_cast<char **>(CPLMalloc(2 * sizeof(char *)));
207
0
        m_papszXMLVRTMetadata[0] = pszXML;
208
0
        m_papszXMLVRTMetadata[1] = nullptr;
209
0
        return m_papszXMLVRTMetadata;
210
0
    }
211
212
0
    return GDALDataset::GetMetadata(pszDomain);
213
0
}
214
215
/************************************************************************/
216
/*                          GetMetadataItem()                           */
217
/************************************************************************/
218
219
const char *VRTDataset::GetMetadataItem(const char *pszName,
220
                                        const char *pszDomain)
221
222
0
{
223
0
    if (pszName && pszDomain && EQUAL(pszDomain, "__DEBUG__"))
224
0
    {
225
0
        if (EQUAL(pszName, "MULTI_THREADED_RASTERIO_LAST_USED"))
226
0
            return m_bMultiThreadedRasterIOLastUsed ? "1" : "0";
227
0
        else if (EQUAL(pszName, "CheckCompatibleForDatasetIO()"))
228
0
            return CheckCompatibleForDatasetIO() ? "1" : "0";
229
0
    }
230
0
    return GDALDataset::GetMetadataItem(pszName, pszDomain);
231
0
}
232
233
/*! @endcond */
234
235
/************************************************************************/
236
/*                            VRTFlushCache(bool bAtClosing) */
237
/************************************************************************/
238
239
/**
240
 * @see VRTDataset::FlushCache(bool bAtClosing)
241
 */
242
243
void CPL_STDCALL VRTFlushCache(VRTDatasetH hDataset)
244
0
{
245
0
    VALIDATE_POINTER0(hDataset, "VRTFlushCache");
246
247
0
    static_cast<VRTDataset *>(GDALDataset::FromHandle(hDataset))
248
0
        ->FlushCache(false);
249
0
}
250
251
/*! @cond Doxygen_Suppress */
252
253
/************************************************************************/
254
/*                           SerializeToXML()                           */
255
/************************************************************************/
256
257
CPLXMLNode *VRTDataset::SerializeToXML(const char *pszVRTPathIn)
258
259
0
{
260
0
    if (m_poRootGroup)
261
0
        return m_poRootGroup->SerializeToXML(pszVRTPathIn);
262
263
    /* -------------------------------------------------------------------- */
264
    /*      Setup root node and attributes.                                 */
265
    /* -------------------------------------------------------------------- */
266
0
    CPLXMLNode *psDSTree = CPLCreateXMLNode(nullptr, CXT_Element, "VRTDataset");
267
268
0
    char szNumber[128] = {'\0'};
269
0
    snprintf(szNumber, sizeof(szNumber), "%d", GetRasterXSize());
270
0
    CPLSetXMLValue(psDSTree, "#rasterXSize", szNumber);
271
272
0
    snprintf(szNumber, sizeof(szNumber), "%d", GetRasterYSize());
273
0
    CPLSetXMLValue(psDSTree, "#rasterYSize", szNumber);
274
275
    /* -------------------------------------------------------------------- */
276
    /*      SRS                                                             */
277
    /* -------------------------------------------------------------------- */
278
0
    if (m_poSRS && !m_poSRS->IsEmpty())
279
0
    {
280
0
        char *pszWKT = nullptr;
281
0
        m_poSRS->exportToWkt(&pszWKT);
282
0
        CPLXMLNode *psSRSNode =
283
0
            CPLCreateXMLElementAndValue(psDSTree, "SRS", pszWKT);
284
0
        CPLFree(pszWKT);
285
0
        const auto &mapping = m_poSRS->GetDataAxisToSRSAxisMapping();
286
0
        CPLString osMapping;
287
0
        for (size_t i = 0; i < mapping.size(); ++i)
288
0
        {
289
0
            if (!osMapping.empty())
290
0
                osMapping += ",";
291
0
            osMapping += CPLSPrintf("%d", mapping[i]);
292
0
        }
293
0
        CPLAddXMLAttributeAndValue(psSRSNode, "dataAxisToSRSAxisMapping",
294
0
                                   osMapping.c_str());
295
0
        const double dfCoordinateEpoch = m_poSRS->GetCoordinateEpoch();
296
0
        if (dfCoordinateEpoch > 0)
297
0
        {
298
0
            std::string osCoordinateEpoch = CPLSPrintf("%f", dfCoordinateEpoch);
299
0
            if (osCoordinateEpoch.find('.') != std::string::npos)
300
0
            {
301
0
                while (osCoordinateEpoch.back() == '0')
302
0
                    osCoordinateEpoch.pop_back();
303
0
            }
304
0
            CPLAddXMLAttributeAndValue(psSRSNode, "coordinateEpoch",
305
0
                                       osCoordinateEpoch.c_str());
306
0
        }
307
0
    }
308
309
    /* -------------------------------------------------------------------- */
310
    /*      Geotransform.                                                   */
311
    /* -------------------------------------------------------------------- */
312
0
    if (m_bGeoTransformSet)
313
0
    {
314
0
        CPLSetXMLValue(
315
0
            psDSTree, "GeoTransform",
316
0
            CPLSPrintf("%24.16e,%24.16e,%24.16e,%24.16e,%24.16e,%24.16e",
317
0
                       m_gt[0], m_gt[1], m_gt[2], m_gt[3], m_gt[4], m_gt[5]));
318
0
    }
319
320
    /* -------------------------------------------------------------------- */
321
    /*      Metadata                                                        */
322
    /* -------------------------------------------------------------------- */
323
0
    CPLXMLNode *psMD = oMDMD.Serialize();
324
0
    if (psMD != nullptr)
325
0
    {
326
0
        CPLAddXMLChild(psDSTree, psMD);
327
0
    }
328
329
    /* -------------------------------------------------------------------- */
330
    /*      GCPs                                                            */
331
    /* -------------------------------------------------------------------- */
332
0
    if (!m_asGCPs.empty())
333
0
    {
334
0
        GDALSerializeGCPListToXML(psDSTree, m_asGCPs, m_poGCP_SRS.get());
335
0
    }
336
337
    /* -------------------------------------------------------------------- */
338
    /*      Serialize bands.                                                */
339
    /* -------------------------------------------------------------------- */
340
0
    CPLXMLNode *psLastChild = psDSTree->psChild;
341
0
    for (; psLastChild != nullptr && psLastChild->psNext;
342
0
         psLastChild = psLastChild->psNext)
343
0
    {
344
0
    }
345
0
    CPLAssert(psLastChild);  // we have at least rasterXSize
346
0
    bool bHasWarnedAboutRAMUsage = false;
347
0
    size_t nAccRAMUsage = 0;
348
0
    for (int iBand = 0; iBand < nBands; iBand++)
349
0
    {
350
0
        CPLXMLNode *psBandTree =
351
0
            static_cast<VRTRasterBand *>(papoBands[iBand])
352
0
                ->SerializeToXML(pszVRTPathIn, bHasWarnedAboutRAMUsage,
353
0
                                 nAccRAMUsage);
354
355
0
        if (psBandTree != nullptr)
356
0
        {
357
0
            psLastChild->psNext = psBandTree;
358
0
            psLastChild = psBandTree;
359
0
        }
360
0
    }
361
362
    /* -------------------------------------------------------------------- */
363
    /*      Serialize dataset mask band.                                    */
364
    /* -------------------------------------------------------------------- */
365
0
    if (m_poMaskBand)
366
0
    {
367
0
        CPLXMLNode *psBandTree = m_poMaskBand->SerializeToXML(
368
0
            pszVRTPathIn, bHasWarnedAboutRAMUsage, nAccRAMUsage);
369
370
0
        if (psBandTree != nullptr)
371
0
        {
372
0
            CPLXMLNode *psMaskBandElement =
373
0
                CPLCreateXMLNode(psDSTree, CXT_Element, "MaskBand");
374
0
            CPLAddXMLChild(psMaskBandElement, psBandTree);
375
0
        }
376
0
    }
377
378
    /* -------------------------------------------------------------------- */
379
    /*      Overview factors.                                               */
380
    /* -------------------------------------------------------------------- */
381
0
    if (!m_anOverviewFactors.empty())
382
0
    {
383
0
        CPLString osOverviewList;
384
0
        for (int nOvFactor : m_anOverviewFactors)
385
0
        {
386
0
            if (!osOverviewList.empty())
387
0
                osOverviewList += " ";
388
0
            osOverviewList += CPLSPrintf("%d", nOvFactor);
389
0
        }
390
0
        CPLXMLNode *psOverviewList = CPLCreateXMLElementAndValue(
391
0
            psDSTree, "OverviewList", osOverviewList);
392
0
        if (!m_osOverviewResampling.empty())
393
0
        {
394
0
            CPLAddXMLAttributeAndValue(psOverviewList, "resampling",
395
0
                                       m_osOverviewResampling);
396
0
        }
397
0
    }
398
399
0
    return psDSTree;
400
0
}
401
402
/*! @endcond */
403
/************************************************************************/
404
/*                          VRTSerializeToXML()                         */
405
/************************************************************************/
406
407
/**
408
 * @see VRTDataset::SerializeToXML()
409
 */
410
411
CPLXMLNode *CPL_STDCALL VRTSerializeToXML(VRTDatasetH hDataset,
412
                                          const char *pszVRTPath)
413
0
{
414
0
    VALIDATE_POINTER1(hDataset, "VRTSerializeToXML", nullptr);
415
416
0
    return static_cast<VRTDataset *>(GDALDataset::FromHandle(hDataset))
417
0
        ->SerializeToXML(pszVRTPath);
418
0
}
419
420
/*! @cond Doxygen_Suppress */
421
422
/************************************************************************/
423
/*                        IsRawRasterBandEnabled()                      */
424
/************************************************************************/
425
426
/** Return whether VRTRawRasterBand support is enabled */
427
428
/* static */
429
bool VRTDataset::IsRawRasterBandEnabled()
430
0
{
431
0
#ifdef GDAL_VRT_ENABLE_RAWRASTERBAND
432
0
    if (CPLTestBool(CPLGetConfigOption("GDAL_VRT_ENABLE_RAWRASTERBAND", "YES")))
433
0
    {
434
0
        return true;
435
0
    }
436
0
    else
437
0
    {
438
0
        CPLError(CE_Failure, CPLE_NotSupported,
439
0
                 "VRTRawRasterBand support has been disabled at run-time.");
440
0
    }
441
0
    return false;
442
#else
443
    CPLError(CE_Failure, CPLE_NotSupported,
444
             "VRTRawRasterBand is disabled in this GDAL build");
445
    return false;
446
#endif
447
0
}
448
449
/************************************************************************/
450
/*                             InitBand()                               */
451
/************************************************************************/
452
453
std::unique_ptr<VRTRasterBand>
454
VRTDataset::InitBand(const char *pszSubclass, int nBand,
455
                     bool bAllowPansharpenedOrProcessed)
456
0
{
457
0
    if (auto poProcessedDS = dynamic_cast<VRTProcessedDataset *>(this))
458
0
    {
459
0
        if (bAllowPansharpenedOrProcessed &&
460
0
            EQUAL(pszSubclass, "VRTProcessedRasterBand"))
461
0
        {
462
0
            return std::make_unique<VRTProcessedRasterBand>(poProcessedDS,
463
0
                                                            nBand);
464
0
        }
465
0
    }
466
0
    else if (EQUAL(pszSubclass, "VRTSourcedRasterBand"))
467
0
        return std::make_unique<VRTSourcedRasterBand>(this, nBand);
468
0
    else if (EQUAL(pszSubclass, "VRTDerivedRasterBand"))
469
0
        return std::make_unique<VRTDerivedRasterBand>(this, nBand);
470
0
    else if (EQUAL(pszSubclass, "VRTRawRasterBand"))
471
0
    {
472
0
#ifdef GDAL_VRT_ENABLE_RAWRASTERBAND
473
0
        if (!VRTDataset::IsRawRasterBandEnabled())
474
0
        {
475
0
            return nullptr;
476
0
        }
477
0
        return std::make_unique<VRTRawRasterBand>(this, nBand);
478
#else
479
        CPLError(CE_Failure, CPLE_NotSupported,
480
                 "VRTDataset::InitBand(): cannot instantiate VRTRawRasterBand, "
481
                 "because disabled in this GDAL build");
482
        return nullptr;
483
#endif
484
0
    }
485
0
    else if (EQUAL(pszSubclass, "VRTWarpedRasterBand") &&
486
0
             dynamic_cast<VRTWarpedDataset *>(this) != nullptr)
487
0
    {
488
0
        return std::make_unique<VRTWarpedRasterBand>(this, nBand);
489
0
    }
490
0
    else if (bAllowPansharpenedOrProcessed &&
491
0
             EQUAL(pszSubclass, "VRTPansharpenedRasterBand") &&
492
0
             dynamic_cast<VRTPansharpenedDataset *>(this) != nullptr)
493
0
    {
494
0
        return std::make_unique<VRTPansharpenedRasterBand>(this, nBand);
495
0
    }
496
497
0
    CPLError(CE_Failure, CPLE_AppDefined,
498
0
             "VRTRasterBand of unrecognized subclass '%s'.", pszSubclass);
499
0
    return nullptr;
500
0
}
501
502
/************************************************************************/
503
/*                              XMLInit()                               */
504
/************************************************************************/
505
506
CPLErr VRTDataset::XMLInit(const CPLXMLNode *psTree, const char *pszVRTPathIn)
507
508
0
{
509
0
    if (pszVRTPathIn != nullptr)
510
0
        m_pszVRTPath = CPLStrdup(pszVRTPathIn);
511
512
    /* -------------------------------------------------------------------- */
513
    /*      Check for an SRS node.                                          */
514
    /* -------------------------------------------------------------------- */
515
0
    const CPLXMLNode *psSRSNode = CPLGetXMLNode(psTree, "SRS");
516
0
    if (psSRSNode)
517
0
    {
518
0
        m_poSRS.reset(new OGRSpatialReference());
519
0
        m_poSRS->SetFromUserInput(
520
0
            CPLGetXMLValue(psSRSNode, nullptr, ""),
521
0
            OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get());
522
0
        const char *pszMapping =
523
0
            CPLGetXMLValue(psSRSNode, "dataAxisToSRSAxisMapping", nullptr);
524
0
        if (pszMapping)
525
0
        {
526
0
            char **papszTokens =
527
0
                CSLTokenizeStringComplex(pszMapping, ",", FALSE, FALSE);
528
0
            std::vector<int> anMapping;
529
0
            for (int i = 0; papszTokens && papszTokens[i]; i++)
530
0
            {
531
0
                anMapping.push_back(atoi(papszTokens[i]));
532
0
            }
533
0
            CSLDestroy(papszTokens);
534
0
            m_poSRS->SetDataAxisToSRSAxisMapping(anMapping);
535
0
        }
536
0
        else
537
0
        {
538
0
            m_poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
539
0
        }
540
541
0
        const char *pszCoordinateEpoch =
542
0
            CPLGetXMLValue(psSRSNode, "coordinateEpoch", nullptr);
543
0
        if (pszCoordinateEpoch)
544
0
            m_poSRS->SetCoordinateEpoch(CPLAtof(pszCoordinateEpoch));
545
0
    }
546
547
    /* -------------------------------------------------------------------- */
548
    /*      Check for a GeoTransform node.                                  */
549
    /* -------------------------------------------------------------------- */
550
0
    const char *pszGT = CPLGetXMLValue(psTree, "GeoTransform", "");
551
0
    if (strlen(pszGT) > 0)
552
0
    {
553
0
        const CPLStringList aosTokens(
554
0
            CSLTokenizeStringComplex(pszGT, ",", FALSE, FALSE));
555
0
        if (aosTokens.size() != 6)
556
0
        {
557
0
            CPLError(CE_Warning, CPLE_AppDefined,
558
0
                     "GeoTransform node does not have expected six values.");
559
0
        }
560
0
        else
561
0
        {
562
0
            for (int iTA = 0; iTA < 6; iTA++)
563
0
                m_gt[iTA] = CPLAtof(aosTokens[iTA]);
564
0
            m_bGeoTransformSet = TRUE;
565
0
        }
566
0
    }
567
568
    /* -------------------------------------------------------------------- */
569
    /*      Check for GCPs.                                                 */
570
    /* -------------------------------------------------------------------- */
571
0
    if (const CPLXMLNode *psGCPList = CPLGetXMLNode(psTree, "GCPList"))
572
0
    {
573
0
        OGRSpatialReference *poSRS = nullptr;
574
0
        GDALDeserializeGCPListFromXML(psGCPList, m_asGCPs, &poSRS);
575
0
        m_poGCP_SRS.reset(poSRS);
576
0
    }
577
578
    /* -------------------------------------------------------------------- */
579
    /*      Apply any dataset level metadata.                               */
580
    /* -------------------------------------------------------------------- */
581
0
    oMDMD.XMLInit(psTree, TRUE);
582
583
    /* -------------------------------------------------------------------- */
584
    /*      Create dataset mask band.                                       */
585
    /* -------------------------------------------------------------------- */
586
587
    /* Parse dataset mask band first */
588
0
    const CPLXMLNode *psMaskBandNode = CPLGetXMLNode(psTree, "MaskBand");
589
590
0
    const CPLXMLNode *psChild = nullptr;
591
0
    if (psMaskBandNode)
592
0
        psChild = psMaskBandNode->psChild;
593
0
    else
594
0
        psChild = nullptr;
595
596
0
    for (; psChild != nullptr; psChild = psChild->psNext)
597
0
    {
598
0
        if (psChild->eType == CXT_Element &&
599
0
            EQUAL(psChild->pszValue, "VRTRasterBand"))
600
0
        {
601
0
            const char *pszSubclass =
602
0
                CPLGetXMLValue(psChild, "subclass", "VRTSourcedRasterBand");
603
604
0
            auto poBand = InitBand(pszSubclass, 0, false);
605
0
            if (poBand != nullptr &&
606
0
                poBand->XMLInit(psChild, pszVRTPathIn, m_oMapSharedSources) ==
607
0
                    CE_None)
608
0
            {
609
0
                SetMaskBand(std::move(poBand));
610
0
                break;
611
0
            }
612
0
            else
613
0
            {
614
0
                return CE_Failure;
615
0
            }
616
0
        }
617
0
    }
618
619
    /* -------------------------------------------------------------------- */
620
    /*      Create band information objects.                                */
621
    /* -------------------------------------------------------------------- */
622
0
    int l_nBands = 0;
623
0
    for (psChild = psTree->psChild; psChild != nullptr;
624
0
         psChild = psChild->psNext)
625
0
    {
626
0
        if (psChild->eType == CXT_Element &&
627
0
            EQUAL(psChild->pszValue, "VRTRasterBand"))
628
0
        {
629
0
            const char *pszSubclass =
630
0
                CPLGetXMLValue(psChild, "subclass", "VRTSourcedRasterBand");
631
0
            if (dynamic_cast<VRTProcessedDataset *>(this) &&
632
0
                !EQUAL(pszSubclass, "VRTProcessedRasterBand"))
633
0
            {
634
0
                CPLError(CE_Failure, CPLE_NotSupported,
635
0
                         "Only subClass=VRTProcessedRasterBand supported");
636
0
                return CE_Failure;
637
0
            }
638
639
0
            if (CPLGetXMLNode(psChild, "PixelFunctionType") != nullptr &&
640
0
                !EQUAL(pszSubclass, "VRTDerivedRasterBand"))
641
0
            {
642
0
                CPLError(CE_Failure, CPLE_NotSupported,
643
0
                         "Pixel functions may only be used with "
644
0
                         "subClass=VRTDerivedRasterBand");
645
0
                return CE_Failure;
646
0
            }
647
648
0
            auto poBand = InitBand(pszSubclass, l_nBands + 1, true);
649
0
            if (poBand != nullptr &&
650
0
                poBand->XMLInit(psChild, pszVRTPathIn, m_oMapSharedSources) ==
651
0
                    CE_None)
652
0
            {
653
0
                l_nBands++;
654
0
                SetBand(l_nBands, std::move(poBand));
655
0
            }
656
0
            else
657
0
            {
658
0
                return CE_Failure;
659
0
            }
660
0
        }
661
0
    }
662
663
0
    if (const CPLXMLNode *psGroup = CPLGetXMLNode(psTree, "Group"))
664
0
    {
665
0
        const char *pszName = CPLGetXMLValue(psGroup, "name", nullptr);
666
0
        if (pszName == nullptr || !EQUAL(pszName, "/"))
667
0
        {
668
0
            CPLError(CE_Failure, CPLE_AppDefined,
669
0
                     "Missing name or not equal to '/'");
670
0
            return CE_Failure;
671
0
        }
672
673
0
        m_poRootGroup = VRTGroup::Create(std::string(), "/");
674
0
        m_poRootGroup->SetIsRootGroup();
675
0
        if (!m_poRootGroup->XMLInit(m_poRootGroup, m_poRootGroup, psGroup,
676
0
                                    pszVRTPathIn))
677
0
        {
678
0
            return CE_Failure;
679
0
        }
680
0
    }
681
682
    /* -------------------------------------------------------------------- */
683
    /*      Create virtual overviews.                                       */
684
    /* -------------------------------------------------------------------- */
685
0
    const char *pszSubClass = CPLGetXMLValue(psTree, "subClass", "");
686
0
    if (EQUAL(pszSubClass, ""))
687
0
    {
688
0
        m_aosOverviewList =
689
0
            CSLTokenizeString(CPLGetXMLValue(psTree, "OverviewList", ""));
690
0
        m_osOverviewResampling =
691
0
            CPLGetXMLValue(psTree, "OverviewList.resampling", "");
692
0
    }
693
694
0
    return CE_None;
695
0
}
696
697
/************************************************************************/
698
/*                            GetGCPCount()                             */
699
/************************************************************************/
700
701
int VRTDataset::GetGCPCount()
702
703
0
{
704
0
    return static_cast<int>(m_asGCPs.size());
705
0
}
706
707
/************************************************************************/
708
/*                               GetGCPs()                              */
709
/************************************************************************/
710
711
const GDAL_GCP *VRTDataset::GetGCPs()
712
713
0
{
714
0
    return gdal::GCP::c_ptr(m_asGCPs);
715
0
}
716
717
/************************************************************************/
718
/*                              SetGCPs()                               */
719
/************************************************************************/
720
721
CPLErr VRTDataset::SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
722
                           const OGRSpatialReference *poGCP_SRS)
723
724
0
{
725
0
    m_poGCP_SRS.reset(poGCP_SRS ? poGCP_SRS->Clone() : nullptr);
726
0
    m_asGCPs = gdal::GCP::fromC(pasGCPListIn, nGCPCountIn);
727
728
0
    SetNeedsFlush();
729
730
0
    return CE_None;
731
0
}
732
733
/************************************************************************/
734
/*                           SetSpatialRef()                            */
735
/************************************************************************/
736
737
CPLErr VRTDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
738
739
0
{
740
0
    m_poSRS.reset(poSRS ? poSRS->Clone() : nullptr);
741
742
0
    SetNeedsFlush();
743
744
0
    return CE_None;
745
0
}
746
747
/************************************************************************/
748
/*                          SetGeoTransform()                           */
749
/************************************************************************/
750
751
CPLErr VRTDataset::SetGeoTransform(const GDALGeoTransform &gt)
752
753
0
{
754
0
    m_gt = gt;
755
0
    m_bGeoTransformSet = TRUE;
756
757
0
    SetNeedsFlush();
758
759
0
    return CE_None;
760
0
}
761
762
/************************************************************************/
763
/*                          GetGeoTransform()                           */
764
/************************************************************************/
765
766
CPLErr VRTDataset::GetGeoTransform(GDALGeoTransform &gt) const
767
768
0
{
769
0
    gt = m_gt;
770
771
0
    return m_bGeoTransformSet ? CE_None : CE_Failure;
772
0
}
773
774
/************************************************************************/
775
/*                            SetMetadata()                             */
776
/************************************************************************/
777
778
CPLErr VRTDataset::SetMetadata(char **papszMetadata, const char *pszDomain)
779
780
0
{
781
0
    SetNeedsFlush();
782
783
0
    return GDALDataset::SetMetadata(papszMetadata, pszDomain);
784
0
}
785
786
/************************************************************************/
787
/*                          SetMetadataItem()                           */
788
/************************************************************************/
789
790
CPLErr VRTDataset::SetMetadataItem(const char *pszName, const char *pszValue,
791
                                   const char *pszDomain)
792
793
0
{
794
0
    SetNeedsFlush();
795
796
0
    return GDALDataset::SetMetadataItem(pszName, pszValue, pszDomain);
797
0
}
798
799
/************************************************************************/
800
/*                              Identify()                              */
801
/************************************************************************/
802
803
int VRTDataset::Identify(GDALOpenInfo *poOpenInfo)
804
805
0
{
806
0
    if (poOpenInfo->nHeaderBytes > 20 &&
807
0
        strstr(reinterpret_cast<const char *>(poOpenInfo->pabyHeader),
808
0
               "<VRTDataset") != nullptr)
809
0
        return TRUE;
810
811
0
    if (strstr(poOpenInfo->pszFilename, "<VRTDataset") != nullptr)
812
0
        return TRUE;
813
814
0
    if (STARTS_WITH_CI(poOpenInfo->pszFilename, VRT_PROTOCOL_PREFIX))
815
0
        return TRUE;
816
817
0
    return FALSE;
818
0
}
819
820
/************************************************************************/
821
/*                                Open()                                */
822
/************************************************************************/
823
824
GDALDataset *VRTDataset::Open(GDALOpenInfo *poOpenInfo)
825
826
0
{
827
    /* -------------------------------------------------------------------- */
828
    /*      Does this appear to be a virtual dataset definition XML         */
829
    /*      file?                                                           */
830
    /* -------------------------------------------------------------------- */
831
0
    if (!Identify(poOpenInfo))
832
0
        return nullptr;
833
834
0
    if (STARTS_WITH_CI(poOpenInfo->pszFilename, VRT_PROTOCOL_PREFIX))
835
0
        return OpenVRTProtocol(poOpenInfo->pszFilename);
836
837
    /* -------------------------------------------------------------------- */
838
    /*      Try to read the whole file into memory.                         */
839
    /* -------------------------------------------------------------------- */
840
0
    char *pszXML = nullptr;
841
0
    VSILFILE *fp = poOpenInfo->fpL;
842
843
0
    char *pszVRTPath = nullptr;
844
0
    if (fp != nullptr)
845
0
    {
846
0
        poOpenInfo->fpL = nullptr;
847
848
0
        GByte *pabyOut = nullptr;
849
0
        if (!VSIIngestFile(fp, poOpenInfo->pszFilename, &pabyOut, nullptr,
850
0
                           INT_MAX - 1))
851
0
        {
852
0
            CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
853
0
            return nullptr;
854
0
        }
855
0
        pszXML = reinterpret_cast<char *>(pabyOut);
856
857
0
        char *pszCurDir = CPLGetCurrentDir();
858
0
        std::string currentVrtFilename =
859
0
            CPLProjectRelativeFilenameSafe(pszCurDir, poOpenInfo->pszFilename);
860
0
        CPLString osInitialCurrentVrtFilename(currentVrtFilename);
861
0
        CPLFree(pszCurDir);
862
863
0
#if !defined(_WIN32)
864
0
        char filenameBuffer[2048];
865
866
0
        while (true)
867
0
        {
868
0
            VSIStatBuf statBuffer;
869
0
            int lstatCode = lstat(currentVrtFilename.c_str(), &statBuffer);
870
0
            if (lstatCode == -1)
871
0
            {
872
0
                if (errno == ENOENT)
873
0
                {
874
                    // File could be a virtual file, let later checks handle it.
875
0
                    break;
876
0
                }
877
0
                else
878
0
                {
879
0
                    CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
880
0
                    CPLFree(pszXML);
881
0
                    CPLError(CE_Failure, CPLE_FileIO, "Failed to lstat %s: %s",
882
0
                             currentVrtFilename.c_str(), VSIStrerror(errno));
883
0
                    return nullptr;
884
0
                }
885
0
            }
886
887
0
            if (!VSI_ISLNK(statBuffer.st_mode))
888
0
            {
889
0
                break;
890
0
            }
891
892
0
            const int bufferSize = static_cast<int>(
893
0
                readlink(currentVrtFilename.c_str(), filenameBuffer,
894
0
                         sizeof(filenameBuffer)));
895
0
            if (bufferSize != -1)
896
0
            {
897
0
                filenameBuffer[std::min(
898
0
                    bufferSize, static_cast<int>(sizeof(filenameBuffer)) - 1)] =
899
0
                    0;
900
                // The filename in filenameBuffer might be a relative path
901
                // from the linkfile resolve it before looping
902
0
                currentVrtFilename = CPLProjectRelativeFilenameSafe(
903
0
                    CPLGetDirnameSafe(currentVrtFilename.c_str()).c_str(),
904
0
                    filenameBuffer);
905
0
            }
906
0
            else
907
0
            {
908
0
                CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
909
0
                CPLFree(pszXML);
910
0
                CPLError(CE_Failure, CPLE_FileIO,
911
0
                         "Failed to read filename from symlink %s: %s",
912
0
                         currentVrtFilename.c_str(), VSIStrerror(errno));
913
0
                return nullptr;
914
0
            }
915
0
        }
916
0
#endif  // !defined(__WIN32)
917
918
0
        if (osInitialCurrentVrtFilename == currentVrtFilename)
919
0
            pszVRTPath =
920
0
                CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str());
921
0
        else
922
0
            pszVRTPath =
923
0
                CPLStrdup(CPLGetPathSafe(currentVrtFilename.c_str()).c_str());
924
925
0
        CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
926
0
    }
927
    /* -------------------------------------------------------------------- */
928
    /*      Or use the filename as the XML input.                           */
929
    /* -------------------------------------------------------------------- */
930
0
    else
931
0
    {
932
0
        pszXML = CPLStrdup(poOpenInfo->pszFilename);
933
0
    }
934
935
0
    if (CSLFetchNameValue(poOpenInfo->papszOpenOptions, "ROOT_PATH") != nullptr)
936
0
    {
937
0
        CPLFree(pszVRTPath);
938
0
        pszVRTPath = CPLStrdup(
939
0
            CSLFetchNameValue(poOpenInfo->papszOpenOptions, "ROOT_PATH"));
940
0
    }
941
942
    /* -------------------------------------------------------------------- */
943
    /*      Turn the XML representation into a VRTDataset.                  */
944
    /* -------------------------------------------------------------------- */
945
0
    auto poDS = OpenXML(pszXML, pszVRTPath, poOpenInfo->eAccess);
946
947
0
    if (poDS != nullptr)
948
0
        poDS->m_bNeedsFlush = false;
949
950
0
    if (poDS != nullptr)
951
0
    {
952
0
        if (poDS->GetRasterCount() == 0 &&
953
0
            (poOpenInfo->nOpenFlags & GDAL_OF_MULTIDIM_RASTER) == 0 &&
954
0
            strstr(pszXML, "VRTPansharpenedDataset") == nullptr)
955
0
        {
956
0
            poDS.reset();
957
0
        }
958
0
        else if (poDS->GetRootGroup() == nullptr &&
959
0
                 (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) == 0 &&
960
0
                 (poOpenInfo->nOpenFlags & GDAL_OF_MULTIDIM_RASTER) != 0)
961
0
        {
962
0
            poDS.reset();
963
0
        }
964
0
    }
965
966
0
    CPLFree(pszXML);
967
0
    CPLFree(pszVRTPath);
968
969
    /* -------------------------------------------------------------------- */
970
    /*      Initialize info for later overview discovery.                   */
971
    /* -------------------------------------------------------------------- */
972
973
0
    if (poDS != nullptr)
974
0
    {
975
0
        if (fp != nullptr)
976
0
        {
977
0
            poDS->oOvManager.Initialize(poDS.get(), poOpenInfo->pszFilename);
978
0
            if (poOpenInfo->AreSiblingFilesLoaded())
979
0
                poDS->oOvManager.TransferSiblingFiles(
980
0
                    poOpenInfo->StealSiblingFiles());
981
0
        }
982
983
        // Creating virtual overviews, but only if there is no higher priority
984
        // overview source, ie. a Overview element at VRT band level,
985
        // or external .vrt.ovr
986
0
        if (!poDS->m_aosOverviewList.empty())
987
0
        {
988
0
            if (poDS->nBands > 0)
989
0
            {
990
0
                auto poBand = dynamic_cast<VRTRasterBand *>(poDS->papoBands[0]);
991
0
                if (poBand && !poBand->m_aoOverviewInfos.empty())
992
0
                {
993
0
                    poDS->m_aosOverviewList.Clear();
994
0
                    CPLDebug("VRT",
995
0
                             "Ignoring virtual overviews of OverviewList "
996
0
                             "because Overview element is present on VRT band");
997
0
                }
998
0
                else if (poBand &&
999
0
                         poBand->GDALRasterBand::GetOverviewCount() > 0)
1000
0
                {
1001
0
                    poDS->m_aosOverviewList.Clear();
1002
0
                    CPLDebug("VRT",
1003
0
                             "Ignoring virtual overviews of OverviewList "
1004
0
                             "because external .vrt.ovr is available");
1005
0
                }
1006
0
            }
1007
0
            for (int iOverview = 0; iOverview < poDS->m_aosOverviewList.size();
1008
0
                 iOverview++)
1009
0
            {
1010
0
                const int nOvFactor = atoi(poDS->m_aosOverviewList[iOverview]);
1011
0
                if (nOvFactor <= 1)
1012
0
                {
1013
0
                    CPLError(CE_Failure, CPLE_AppDefined,
1014
0
                             "Invalid overview factor");
1015
0
                    return nullptr;
1016
0
                }
1017
1018
0
                poDS->AddVirtualOverview(
1019
0
                    nOvFactor, poDS->m_osOverviewResampling.empty()
1020
0
                                   ? "nearest"
1021
0
                                   : poDS->m_osOverviewResampling.c_str());
1022
0
            }
1023
0
            poDS->m_aosOverviewList.Clear();
1024
0
        }
1025
1026
0
        if (poDS->eAccess == GA_Update && poDS->m_poRootGroup &&
1027
0
            !STARTS_WITH_CI(poOpenInfo->pszFilename, "<VRT"))
1028
0
        {
1029
0
            poDS->m_poRootGroup->SetFilename(poOpenInfo->pszFilename);
1030
0
        }
1031
0
    }
1032
1033
0
    return poDS.release();
1034
0
}
1035
1036
/************************************************************************/
1037
/*                         OpenVRTProtocol()                            */
1038
/*                                                                      */
1039
/*      Create an open VRTDataset from a vrt:// string.                 */
1040
/************************************************************************/
1041
1042
GDALDataset *VRTDataset::OpenVRTProtocol(const char *pszSpec)
1043
1044
0
{
1045
0
    CPLAssert(STARTS_WITH_CI(pszSpec, VRT_PROTOCOL_PREFIX));
1046
0
    CPLString osFilename(pszSpec + strlen(VRT_PROTOCOL_PREFIX));
1047
0
    const auto nPosQuotationMark = osFilename.find('?');
1048
0
    CPLString osQueryString;
1049
0
    if (nPosQuotationMark != std::string::npos)
1050
0
    {
1051
0
        osQueryString = osFilename.substr(nPosQuotationMark + 1);
1052
0
        osFilename.resize(nPosQuotationMark);
1053
0
    }
1054
1055
    // Parse query string, get args required for initial Open()
1056
0
    const CPLStringList aosTokens(CSLTokenizeString2(osQueryString, "&", 0));
1057
0
    CPLStringList aosAllowedDrivers;
1058
0
    CPLStringList aosOpenOptions;
1059
1060
0
    for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(
1061
0
             aosTokens, /* bReturnNullKeyIfNotNameValue = */ true))
1062
0
    {
1063
0
        if (!pszKey)
1064
0
        {
1065
0
            CPLError(CE_Failure, CPLE_NotSupported,
1066
0
                     "Invalid option specification: %s\n"
1067
0
                     "must be in the form 'key=value'",
1068
0
                     pszValue);
1069
0
            return nullptr;
1070
0
        }
1071
0
        else if (EQUAL(pszKey, "if"))
1072
0
        {
1073
0
            if (!aosAllowedDrivers.empty())
1074
0
            {
1075
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1076
0
                         "'if' option should be specified once, use commas "
1077
0
                         "to input multiple values.");
1078
0
                return nullptr;
1079
0
            }
1080
0
            aosAllowedDrivers = CSLTokenizeString2(pszValue, ",", 0);
1081
0
        }
1082
0
        else if (EQUAL(pszKey, "oo"))
1083
0
        {
1084
0
            if (!aosOpenOptions.empty())
1085
0
            {
1086
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1087
0
                         "'oo' option should be specified once, use commas "
1088
0
                         "to input multiple values.");
1089
0
                return nullptr;
1090
0
            }
1091
0
            aosOpenOptions = CSLTokenizeString2(pszValue, ",", 0);
1092
0
        }
1093
0
    }
1094
1095
    // We don't open in GDAL_OF_SHARED mode to avoid issues when we open a
1096
    // http://.jp2 file with the JP2OpenJPEG driver through the HTTP driver,
1097
    // which returns a /vsimem/ file
1098
0
    auto poSrcDS = std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser>(
1099
0
        GDALDataset::Open(osFilename, GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
1100
0
                          aosAllowedDrivers.List(), aosOpenOptions.List(),
1101
0
                          nullptr));
1102
0
    if (poSrcDS == nullptr)
1103
0
    {
1104
0
        return nullptr;
1105
0
    }
1106
1107
0
    bool bFound_transpose = false;
1108
0
    for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(aosTokens))
1109
0
    {
1110
0
        if (EQUAL(pszKey, "transpose"))
1111
0
        {
1112
0
            bFound_transpose = true;
1113
0
            const CPLStringList aosTransposeTokens(
1114
0
                CSLTokenizeString2(pszValue, ":", 0));
1115
0
            if (aosTransposeTokens.size() != 2)
1116
0
            {
1117
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1118
0
                         "Invalid transpose option: %s", pszValue);
1119
0
                return nullptr;
1120
0
            }
1121
0
            const CPLStringList aosTransposeIndex(
1122
0
                CSLTokenizeString2(aosTransposeTokens[1], ",", 0));
1123
            // fail if not two values
1124
0
            if (aosTransposeIndex.size() != 2)
1125
0
            {
1126
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1127
0
                         "Invalid transpose option: %s", pszValue);
1128
0
                return nullptr;
1129
0
            }
1130
0
            int index_x = atoi(aosTransposeIndex[0]);
1131
0
            int index_y = atoi(aosTransposeIndex[1]);
1132
1133
0
            auto poMDimDS = std::unique_ptr<GDALDataset>(
1134
0
                GDALDataset::Open(osFilename, GDAL_OF_MULTIDIM_RASTER));
1135
0
            if (!poMDimDS)
1136
0
                return nullptr;
1137
0
            auto poMdimGroup = poMDimDS->GetRootGroup();
1138
0
            if (!poMdimGroup)
1139
0
            {
1140
0
                return nullptr;
1141
0
            }
1142
0
            auto poArray =
1143
0
                poMdimGroup->OpenMDArrayFromFullname(aosTransposeTokens[0]);
1144
0
            if (!poArray)
1145
0
            {
1146
0
                return nullptr;
1147
0
            }
1148
1149
0
            auto poClassicDS = poArray->AsClassicDataset(index_x, index_y);
1150
1151
0
            if (!poClassicDS)
1152
0
                return nullptr;
1153
0
            poSrcDS =
1154
0
                std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser>(
1155
0
                    poClassicDS);
1156
0
        }
1157
0
    }
1158
    // scan for sd_name/sd in tokens, close the source dataset and reopen if found/valid
1159
0
    bool bFound_subdataset = false;
1160
0
    for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(aosTokens))
1161
0
    {
1162
0
        if (EQUAL(pszKey, "sd_name"))
1163
0
        {
1164
0
            if (bFound_transpose)
1165
0
            {
1166
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1167
0
                         "'sd_name' is mutually exclusive with option "
1168
0
                         "'transpose'");
1169
0
                return nullptr;
1170
0
            }
1171
0
            if (bFound_subdataset)
1172
0
            {
1173
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1174
0
                         "'sd_name' is mutually exclusive with option "
1175
0
                         "'sd'");
1176
0
                return nullptr;
1177
0
            }
1178
0
            char **papszSubdatasets = poSrcDS->GetMetadata("SUBDATASETS");
1179
0
            int nSubdatasets = CSLCount(papszSubdatasets);
1180
1181
0
            if (nSubdatasets > 0)
1182
0
            {
1183
0
                bool bFound = false;
1184
0
                for (int j = 0; j < nSubdatasets && papszSubdatasets[j]; j += 2)
1185
0
                {
1186
0
                    const char *pszEqual = strchr(papszSubdatasets[j], '=');
1187
0
                    if (!pszEqual)
1188
0
                    {
1189
0
                        CPLError(CE_Failure, CPLE_IllegalArg,
1190
0
                                 "'sd_name:' failed to obtain "
1191
0
                                 "subdataset string ");
1192
0
                        return nullptr;
1193
0
                    }
1194
0
                    const char *pszSubdatasetSource = pszEqual + 1;
1195
0
                    GDALSubdatasetInfoH info =
1196
0
                        GDALGetSubdatasetInfo(pszSubdatasetSource);
1197
0
                    char *component =
1198
0
                        info ? GDALSubdatasetInfoGetSubdatasetComponent(info)
1199
0
                             : nullptr;
1200
1201
0
                    bFound = component && EQUAL(pszValue, component);
1202
0
                    bFound_subdataset = true;
1203
0
                    CPLFree(component);
1204
0
                    GDALDestroySubdatasetInfo(info);
1205
0
                    if (bFound)
1206
0
                    {
1207
0
                        poSrcDS.reset(GDALDataset::Open(
1208
0
                            pszSubdatasetSource,
1209
0
                            GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
1210
0
                            aosAllowedDrivers.List(), aosOpenOptions.List(),
1211
0
                            nullptr));
1212
0
                        if (poSrcDS == nullptr)
1213
0
                        {
1214
0
                            return nullptr;
1215
0
                        }
1216
1217
0
                        break;
1218
0
                    }
1219
0
                }
1220
1221
0
                if (!bFound)
1222
0
                {
1223
0
                    CPLError(CE_Failure, CPLE_IllegalArg,
1224
0
                             "'sd_name' option should be be a valid "
1225
0
                             "subdataset component name");
1226
0
                    return nullptr;
1227
0
                }
1228
0
            }
1229
0
        }
1230
1231
0
        if (EQUAL(pszKey, "sd"))
1232
0
        {
1233
0
            if (bFound_transpose)
1234
0
            {
1235
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1236
0
                         "'sd' is mutually exclusive with option "
1237
0
                         "'transpose'");
1238
0
                return nullptr;
1239
0
            }
1240
0
            if (bFound_subdataset)
1241
0
            {
1242
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1243
0
                         "'sd' is mutually exclusive with option "
1244
0
                         "'sd_name'");
1245
0
                return nullptr;
1246
0
            }
1247
0
            CSLConstList papszSubdatasets = poSrcDS->GetMetadata("SUBDATASETS");
1248
0
            int nSubdatasets = CSLCount(papszSubdatasets);
1249
1250
0
            if (nSubdatasets > 0)
1251
0
            {
1252
0
                int iSubdataset = atoi(pszValue);
1253
0
                if (iSubdataset < 1 || iSubdataset > (nSubdatasets) / 2)
1254
0
                {
1255
0
                    CPLError(CE_Failure, CPLE_IllegalArg,
1256
0
                             "'sd' option should indicate a valid "
1257
0
                             "subdataset component number (starting with 1)");
1258
0
                    return nullptr;
1259
0
                }
1260
0
                const std::string osSubdatasetSource(
1261
0
                    strstr(papszSubdatasets[(iSubdataset - 1) * 2], "=") + 1);
1262
0
                if (osSubdatasetSource.empty())
1263
0
                {
1264
0
                    CPLError(CE_Failure, CPLE_IllegalArg,
1265
0
                             "'sd:' failed to obtain subdataset "
1266
0
                             "string ");
1267
0
                    return nullptr;
1268
0
                }
1269
1270
0
                poSrcDS.reset(GDALDataset::Open(
1271
0
                    osSubdatasetSource.c_str(),
1272
0
                    GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
1273
0
                    aosAllowedDrivers.List(), aosOpenOptions.List(), nullptr));
1274
0
                if (poSrcDS == nullptr)
1275
0
                {
1276
0
                    return nullptr;
1277
0
                }
1278
0
                bFound_subdataset = true;
1279
0
            }
1280
0
        }
1281
0
    }
1282
1283
0
    std::vector<int> anBands;
1284
1285
0
    CPLStringList argv;
1286
0
    argv.AddString("-of");
1287
0
    argv.AddString("VRT");
1288
1289
0
    for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(aosTokens))
1290
0
    {
1291
0
        if (EQUAL(pszKey, "bands"))
1292
0
        {
1293
0
            const CPLStringList aosBands(CSLTokenizeString2(pszValue, ",", 0));
1294
0
            for (int j = 0; j < aosBands.size(); j++)
1295
0
            {
1296
0
                if (EQUAL(aosBands[j], "mask"))
1297
0
                {
1298
0
                    anBands.push_back(0);
1299
0
                }
1300
0
                else
1301
0
                {
1302
0
                    const int nBand = atoi(aosBands[j]);
1303
0
                    if (nBand <= 0 || nBand > poSrcDS->GetRasterCount())
1304
0
                    {
1305
0
                        CPLError(CE_Failure, CPLE_IllegalArg,
1306
0
                                 "Invalid band number: %s", aosBands[j]);
1307
0
                        return nullptr;
1308
0
                    }
1309
0
                    anBands.push_back(nBand);
1310
0
                }
1311
0
            }
1312
1313
0
            for (const int nBand : anBands)
1314
0
            {
1315
0
                argv.AddString("-b");
1316
0
                argv.AddString(nBand == 0 ? "mask" : CPLSPrintf("%d", nBand));
1317
0
            }
1318
0
        }
1319
1320
0
        else if (EQUAL(pszKey, "a_nodata"))
1321
0
        {
1322
0
            argv.AddString("-a_nodata");
1323
0
            argv.AddString(pszValue);
1324
0
        }
1325
1326
0
        else if (EQUAL(pszKey, "a_srs"))
1327
0
        {
1328
0
            argv.AddString("-a_srs");
1329
0
            argv.AddString(pszValue);
1330
0
        }
1331
1332
0
        else if (EQUAL(pszKey, "a_ullr"))
1333
0
        {
1334
            // Parse the limits
1335
0
            const CPLStringList aosUllr(CSLTokenizeString2(pszValue, ",", 0));
1336
            // fail if not four values
1337
0
            if (aosUllr.size() != 4)
1338
0
            {
1339
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1340
0
                         "Invalid a_ullr option: %s", pszValue);
1341
0
                return nullptr;
1342
0
            }
1343
1344
0
            argv.AddString("-a_ullr");
1345
0
            argv.AddString(aosUllr[0]);
1346
0
            argv.AddString(aosUllr[1]);
1347
0
            argv.AddString(aosUllr[2]);
1348
0
            argv.AddString(aosUllr[3]);
1349
0
        }
1350
1351
0
        else if (EQUAL(pszKey, "ovr"))
1352
0
        {
1353
0
            argv.AddString("-ovr");
1354
0
            argv.AddString(pszValue);
1355
0
        }
1356
0
        else if (EQUAL(pszKey, "expand"))
1357
0
        {
1358
0
            argv.AddString("-expand");
1359
0
            argv.AddString(pszValue);
1360
0
        }
1361
0
        else if (EQUAL(pszKey, "a_scale"))
1362
0
        {
1363
0
            argv.AddString("-a_scale");
1364
0
            argv.AddString(pszValue);
1365
0
        }
1366
0
        else if (EQUAL(pszKey, "a_offset"))
1367
0
        {
1368
0
            argv.AddString("-a_offset");
1369
0
            argv.AddString(pszValue);
1370
0
        }
1371
0
        else if (EQUAL(pszKey, "ot"))
1372
0
        {
1373
0
            argv.AddString("-ot");
1374
0
            argv.AddString(pszValue);
1375
0
        }
1376
0
        else if (EQUAL(pszKey, "gcp"))
1377
0
        {
1378
0
            const CPLStringList aosGCP(CSLTokenizeString2(pszValue, ",", 0));
1379
1380
0
            if (aosGCP.size() < 4 || aosGCP.size() > 5)
1381
0
            {
1382
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1383
0
                         "Invalid value for GCP: %s\n  need 4, or 5 "
1384
0
                         "numbers, comma separated: "
1385
0
                         "'gcp=<pixel>,<line>,<easting>,<northing>[,<"
1386
0
                         "elevation>]'",
1387
0
                         pszValue);
1388
0
                return nullptr;
1389
0
            }
1390
0
            argv.AddString("-gcp");
1391
0
            for (int j = 0; j < aosGCP.size(); j++)
1392
0
            {
1393
0
                argv.AddString(aosGCP[j]);
1394
0
            }
1395
0
        }
1396
0
        else if (EQUAL(pszKey, "scale") || STARTS_WITH_CI(pszKey, "scale_"))
1397
0
        {
1398
0
            const CPLStringList aosScaleParams(
1399
0
                CSLTokenizeString2(pszValue, ",", 0));
1400
1401
0
            if (!(aosScaleParams.size() == 2) &&
1402
0
                !(aosScaleParams.size() == 4) && !(aosScaleParams.size() == 1))
1403
0
            {
1404
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1405
0
                         "Invalid value for scale, (or scale_bn): "
1406
0
                         "%s\n  need 'scale=true', or 2 or 4 "
1407
0
                         "numbers, comma separated: "
1408
0
                         "'scale=src_min,src_max[,dst_min,dst_max]' or "
1409
0
                         "'scale_bn=src_min,src_max[,dst_min,dst_max]'",
1410
0
                         pszValue);
1411
0
                return nullptr;
1412
0
            }
1413
1414
            // -scale because scale=true or scale=min,max or scale=min,max,dstmin,dstmax
1415
0
            if (aosScaleParams.size() == 1 && CPLTestBool(aosScaleParams[0]))
1416
0
            {
1417
0
                argv.AddString(CPLSPrintf("-%s", pszKey));
1418
0
            }
1419
            // add remaining params (length 2 or 4)
1420
0
            if (aosScaleParams.size() > 1)
1421
0
            {
1422
0
                argv.AddString(CPLSPrintf("-%s", pszKey));
1423
0
                for (int j = 0; j < aosScaleParams.size(); j++)
1424
0
                {
1425
0
                    argv.AddString(aosScaleParams[j]);
1426
0
                }
1427
0
            }
1428
0
        }
1429
0
        else if (EQUAL(pszKey, "exponent") ||
1430
0
                 STARTS_WITH_CI(pszKey, "exponent_"))
1431
0
        {
1432
0
            argv.AddString(CPLSPrintf("-%s", pszKey));
1433
0
            argv.AddString(pszValue);
1434
0
        }
1435
0
        else if (EQUAL(pszKey, "outsize"))
1436
0
        {
1437
0
            const CPLStringList aosOutSize(
1438
0
                CSLTokenizeString2(pszValue, ",", 0));
1439
0
            if (aosOutSize.size() != 2)
1440
0
            {
1441
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1442
0
                         "Invalid outsize option: %s, must be two"
1443
0
                         "values separated by comma pixel,line or two "
1444
0
                         "fraction values with percent symbol",
1445
0
                         pszValue);
1446
0
                return nullptr;
1447
0
            }
1448
0
            argv.AddString("-outsize");
1449
0
            argv.AddString(aosOutSize[0]);
1450
0
            argv.AddString(aosOutSize[1]);
1451
0
        }
1452
0
        else if (EQUAL(pszKey, "projwin"))
1453
0
        {
1454
            // Parse the limits
1455
0
            const CPLStringList aosProjWin(
1456
0
                CSLTokenizeString2(pszValue, ",", 0));
1457
            // fail if not four values
1458
0
            if (aosProjWin.size() != 4)
1459
0
            {
1460
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1461
0
                         "Invalid projwin option: %s", pszValue);
1462
0
                return nullptr;
1463
0
            }
1464
1465
0
            argv.AddString("-projwin");
1466
0
            argv.AddString(aosProjWin[0]);
1467
0
            argv.AddString(aosProjWin[1]);
1468
0
            argv.AddString(aosProjWin[2]);
1469
0
            argv.AddString(aosProjWin[3]);
1470
0
        }
1471
0
        else if (EQUAL(pszKey, "projwin_srs"))
1472
0
        {
1473
0
            argv.AddString("-projwin_srs");
1474
0
            argv.AddString(pszValue);
1475
0
        }
1476
0
        else if (EQUAL(pszKey, "tr"))
1477
0
        {
1478
0
            const CPLStringList aosTargetResolution(
1479
0
                CSLTokenizeString2(pszValue, ",", 0));
1480
0
            if (aosTargetResolution.size() != 2)
1481
0
            {
1482
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1483
0
                         "Invalid tr option: %s, must be two "
1484
0
                         "values separated by comma xres,yres",
1485
0
                         pszValue);
1486
0
                return nullptr;
1487
0
            }
1488
0
            argv.AddString("-tr");
1489
0
            argv.AddString(aosTargetResolution[0]);
1490
0
            argv.AddString(aosTargetResolution[1]);
1491
0
        }
1492
0
        else if (EQUAL(pszKey, "r"))
1493
0
        {
1494
0
            argv.AddString("-r");
1495
0
            argv.AddString(pszValue);
1496
0
        }
1497
1498
0
        else if (EQUAL(pszKey, "srcwin"))
1499
0
        {
1500
            // Parse the limits
1501
0
            const CPLStringList aosSrcWin(CSLTokenizeString2(pszValue, ",", 0));
1502
            // fail if not four values
1503
0
            if (aosSrcWin.size() != 4)
1504
0
            {
1505
0
                CPLError(CE_Failure, CPLE_IllegalArg,
1506
0
                         "Invalid srcwin option: %s, must be four "
1507
0
                         "values separated by comma xoff,yoff,xsize,ysize",
1508
0
                         pszValue);
1509
0
                return nullptr;
1510
0
            }
1511
1512
0
            argv.AddString("-srcwin");
1513
0
            argv.AddString(aosSrcWin[0]);
1514
0
            argv.AddString(aosSrcWin[1]);
1515
0
            argv.AddString(aosSrcWin[2]);
1516
0
            argv.AddString(aosSrcWin[3]);
1517
0
        }
1518
1519
0
        else if (EQUAL(pszKey, "a_gt"))
1520
0
        {
1521
            // Parse the limits
1522
0
            const CPLStringList aosAGeoTransform(
1523
0
                CSLTokenizeString2(pszValue, ",", 0));
1524
            // fail if not six values
1525
0
            if (aosAGeoTransform.size() != 6)
1526
0
            {
1527
0
                CPLError(CE_Failure, CPLE_IllegalArg, "Invalid a_gt option: %s",
1528
0
                         pszValue);
1529
0
                return nullptr;
1530
0
            }
1531
1532
0
            argv.AddString("-a_gt");
1533
0
            argv.AddString(aosAGeoTransform[0]);
1534
0
            argv.AddString(aosAGeoTransform[1]);
1535
0
            argv.AddString(aosAGeoTransform[2]);
1536
0
            argv.AddString(aosAGeoTransform[3]);
1537
0
            argv.AddString(aosAGeoTransform[4]);
1538
0
            argv.AddString(aosAGeoTransform[5]);
1539
0
        }
1540
0
        else if (EQUAL(pszKey, "oo"))
1541
0
        {
1542
            // do nothing, we passed this in earlier
1543
0
        }
1544
0
        else if (EQUAL(pszKey, "if"))
1545
0
        {
1546
            // do nothing, we passed this in earlier
1547
0
        }
1548
0
        else if (EQUAL(pszKey, "sd_name"))
1549
0
        {
1550
            // do nothing, we passed this in earlier
1551
0
        }
1552
0
        else if (EQUAL(pszKey, "sd"))
1553
0
        {
1554
            // do nothing, we passed this in earlier
1555
0
        }
1556
0
        else if (EQUAL(pszKey, "transpose"))
1557
0
        {
1558
            // do nothing, we passed this in earlier
1559
0
        }
1560
0
        else if (EQUAL(pszKey, "unscale"))
1561
0
        {
1562
0
            if (CPLTestBool(pszValue))
1563
0
            {
1564
0
                argv.AddString("-unscale");
1565
0
            }
1566
0
        }
1567
0
        else if (EQUAL(pszKey, "a_coord_epoch"))
1568
0
        {
1569
0
            argv.AddString("-a_coord_epoch");
1570
0
            argv.AddString(pszValue);
1571
0
        }
1572
0
        else if (EQUAL(pszKey, "nogcp"))
1573
0
        {
1574
0
            if (CPLTestBool(pszValue))
1575
0
            {
1576
0
                argv.AddString("-nogcp");
1577
0
            }
1578
0
        }
1579
0
        else if (EQUAL(pszKey, "epo"))
1580
0
        {
1581
0
            if (CPLTestBool(pszValue))
1582
0
            {
1583
0
                argv.AddString("-epo");
1584
0
            }
1585
0
        }
1586
0
        else if (EQUAL(pszKey, "eco"))
1587
0
        {
1588
0
            if (CPLTestBool(pszValue))
1589
0
            {
1590
0
                argv.AddString("-eco");
1591
0
            }
1592
0
        }
1593
1594
0
        else
1595
0
        {
1596
0
            CPLError(CE_Failure, CPLE_NotSupported, "Unknown option: %s",
1597
0
                     pszKey);
1598
0
            return nullptr;
1599
0
        }
1600
0
    }
1601
1602
0
    GDALTranslateOptions *psOptions =
1603
0
        GDALTranslateOptionsNew(argv.List(), nullptr);
1604
1605
0
    auto hRet = GDALTranslate("", GDALDataset::ToHandle(poSrcDS.get()),
1606
0
                              psOptions, nullptr);
1607
1608
0
    GDALTranslateOptionsFree(psOptions);
1609
1610
    // Situation where we open a http://.jp2 file with the JP2OpenJPEG driver
1611
    // through the HTTP driver, which returns a /vsimem/ file
1612
0
    const bool bPatchSourceFilename =
1613
0
        (STARTS_WITH(osFilename.c_str(), "http://") ||
1614
0
         STARTS_WITH(osFilename.c_str(), "https://")) &&
1615
0
        osFilename != poSrcDS->GetDescription();
1616
1617
0
    poSrcDS.reset();
1618
1619
0
    auto poDS = dynamic_cast<VRTDataset *>(GDALDataset::FromHandle(hRet));
1620
0
    if (poDS)
1621
0
    {
1622
0
        if (bPatchSourceFilename)
1623
0
        {
1624
0
            for (int i = 0; i < poDS->nBands; ++i)
1625
0
            {
1626
0
                auto poBand =
1627
0
                    dynamic_cast<VRTSourcedRasterBand *>(poDS->papoBands[i]);
1628
0
                if (poBand && poBand->m_papoSources.size() == 1 &&
1629
0
                    poBand->m_papoSources[0]->IsSimpleSource())
1630
0
                {
1631
0
                    auto poSource = cpl::down_cast<VRTSimpleSource *>(
1632
0
                        poBand->m_papoSources[0].get());
1633
0
                    poSource->m_bRelativeToVRTOri = 0;
1634
0
                    poSource->m_osSourceFileNameOri = osFilename;
1635
0
                }
1636
0
            }
1637
0
        }
1638
0
        poDS->SetDescription(pszSpec);
1639
0
        poDS->SetWritable(false);
1640
0
    }
1641
0
    return poDS;
1642
0
}
1643
1644
/************************************************************************/
1645
/*                              OpenXML()                               */
1646
/*                                                                      */
1647
/*      Create an open VRTDataset from a supplied XML representation    */
1648
/*      of the dataset.                                                 */
1649
/************************************************************************/
1650
1651
std::unique_ptr<VRTDataset> VRTDataset::OpenXML(const char *pszXML,
1652
                                                const char *pszVRTPath,
1653
                                                GDALAccess eAccessIn)
1654
1655
0
{
1656
    /* -------------------------------------------------------------------- */
1657
    /*      Parse the XML.                                                  */
1658
    /* -------------------------------------------------------------------- */
1659
0
    CPLXMLTreeCloser psTree(CPLParseXMLString(pszXML));
1660
0
    if (psTree == nullptr)
1661
0
        return nullptr;
1662
1663
0
    CPLXMLNode *psRoot = CPLGetXMLNode(psTree.get(), "=VRTDataset");
1664
0
    if (psRoot == nullptr)
1665
0
    {
1666
0
        CPLError(CE_Failure, CPLE_AppDefined, "Missing VRTDataset element.");
1667
0
        return nullptr;
1668
0
    }
1669
1670
0
    const char *pszSubClass = CPLGetXMLValue(psRoot, "subClass", "");
1671
1672
0
    const bool bIsPansharpened =
1673
0
        strcmp(pszSubClass, "VRTPansharpenedDataset") == 0;
1674
0
    const bool bIsProcessed = strcmp(pszSubClass, "VRTProcessedDataset") == 0;
1675
1676
0
    if (!bIsPansharpened && !bIsProcessed &&
1677
0
        CPLGetXMLNode(psRoot, "Group") == nullptr &&
1678
0
        (CPLGetXMLNode(psRoot, "rasterXSize") == nullptr ||
1679
0
         CPLGetXMLNode(psRoot, "rasterYSize") == nullptr ||
1680
0
         CPLGetXMLNode(psRoot, "VRTRasterBand") == nullptr))
1681
0
    {
1682
0
        CPLError(CE_Failure, CPLE_AppDefined,
1683
0
                 "Missing one of rasterXSize, rasterYSize or bands on"
1684
0
                 " VRTDataset.");
1685
0
        return nullptr;
1686
0
    }
1687
1688
    /* -------------------------------------------------------------------- */
1689
    /*      Create the new virtual dataset object.                          */
1690
    /* -------------------------------------------------------------------- */
1691
0
    const int nXSize = atoi(CPLGetXMLValue(psRoot, "rasterXSize", "0"));
1692
0
    const int nYSize = atoi(CPLGetXMLValue(psRoot, "rasterYSize", "0"));
1693
1694
0
    if (!bIsPansharpened && !bIsProcessed &&
1695
0
        CPLGetXMLNode(psRoot, "VRTRasterBand") != nullptr &&
1696
0
        !GDALCheckDatasetDimensions(nXSize, nYSize))
1697
0
    {
1698
0
        return nullptr;
1699
0
    }
1700
1701
0
    std::unique_ptr<VRTDataset> poDS;
1702
0
    if (strcmp(pszSubClass, "VRTWarpedDataset") == 0)
1703
0
        poDS = std::make_unique<VRTWarpedDataset>(nXSize, nYSize);
1704
0
    else if (bIsPansharpened)
1705
0
        poDS = std::make_unique<VRTPansharpenedDataset>(nXSize, nYSize);
1706
0
    else if (bIsProcessed)
1707
0
        poDS = std::make_unique<VRTProcessedDataset>(nXSize, nYSize);
1708
0
    else
1709
0
    {
1710
0
        poDS = std::make_unique<VRTDataset>(nXSize, nYSize);
1711
0
        poDS->eAccess = eAccessIn;
1712
0
    }
1713
1714
0
    if (poDS->XMLInit(psRoot, pszVRTPath) != CE_None)
1715
0
    {
1716
0
        poDS.reset();
1717
0
    }
1718
1719
    /* -------------------------------------------------------------------- */
1720
    /*      Try to return a regular handle on the file.                     */
1721
    /* -------------------------------------------------------------------- */
1722
1723
0
    return poDS;
1724
0
}
1725
1726
/************************************************************************/
1727
/*                              AddBand()                               */
1728
/************************************************************************/
1729
1730
CPLErr VRTDataset::AddBand(GDALDataType eType, char **papszOptions)
1731
1732
0
{
1733
0
    if (eType == GDT_Unknown || eType == GDT_TypeCount)
1734
0
    {
1735
0
        ReportError(CE_Failure, CPLE_IllegalArg,
1736
0
                    "Illegal GDT_Unknown/GDT_TypeCount argument");
1737
0
        return CE_Failure;
1738
0
    }
1739
1740
0
    SetNeedsFlush();
1741
1742
    /* ==================================================================== */
1743
    /*      Handle a new raw band.                                          */
1744
    /* ==================================================================== */
1745
0
    const char *pszSubClass = CSLFetchNameValue(papszOptions, "subclass");
1746
1747
0
    if (pszSubClass != nullptr && EQUAL(pszSubClass, "VRTRawRasterBand"))
1748
0
    {
1749
0
#ifdef GDAL_VRT_ENABLE_RAWRASTERBAND
1750
0
        if (!VRTDataset::IsRawRasterBandEnabled())
1751
0
        {
1752
0
            return CE_Failure;
1753
0
        }
1754
0
        const int nWordDataSize = GDALGetDataTypeSizeBytes(eType);
1755
1756
        /* ---------------------------------------------------------------- */
1757
        /*      Collect required information.                               */
1758
        /* ---------------------------------------------------------------- */
1759
0
        const char *pszImageOffset =
1760
0
            CSLFetchNameValueDef(papszOptions, "ImageOffset", "0");
1761
0
        vsi_l_offset nImageOffset = CPLScanUIntBig(
1762
0
            pszImageOffset, static_cast<int>(strlen(pszImageOffset)));
1763
1764
0
        int nPixelOffset = nWordDataSize;
1765
0
        const char *pszPixelOffset =
1766
0
            CSLFetchNameValue(papszOptions, "PixelOffset");
1767
0
        if (pszPixelOffset != nullptr)
1768
0
            nPixelOffset = atoi(pszPixelOffset);
1769
1770
0
        int nLineOffset;
1771
0
        const char *pszLineOffset =
1772
0
            CSLFetchNameValue(papszOptions, "LineOffset");
1773
0
        if (pszLineOffset != nullptr)
1774
0
            nLineOffset = atoi(pszLineOffset);
1775
0
        else
1776
0
        {
1777
0
            if (nPixelOffset > INT_MAX / GetRasterXSize() ||
1778
0
                nPixelOffset < INT_MIN / GetRasterXSize())
1779
0
            {
1780
0
                CPLError(CE_Failure, CPLE_AppDefined, "Int overflow");
1781
0
                return CE_Failure;
1782
0
            }
1783
0
            nLineOffset = nPixelOffset * GetRasterXSize();
1784
0
        }
1785
1786
0
        const char *pszByteOrder = CSLFetchNameValue(papszOptions, "ByteOrder");
1787
1788
0
        const char *pszFilename =
1789
0
            CSLFetchNameValue(papszOptions, "SourceFilename");
1790
0
        if (pszFilename == nullptr)
1791
0
        {
1792
0
            CPLError(CE_Failure, CPLE_AppDefined,
1793
0
                     "AddBand() requires a SourceFilename option for "
1794
0
                     "VRTRawRasterBands.");
1795
0
            return CE_Failure;
1796
0
        }
1797
1798
0
        const bool bRelativeToVRT =
1799
0
            CPLFetchBool(papszOptions, "relativeToVRT", false);
1800
1801
        /* --------------------------------------------------------------- */
1802
        /*      Create and initialize the band.                            */
1803
        /* --------------------------------------------------------------- */
1804
1805
0
        auto poBand = std::make_unique<VRTRawRasterBand>(
1806
0
            this, GetRasterCount() + 1, eType);
1807
1808
0
        const std::string osPath = CPLGetPathSafe(GetDescription());
1809
0
        const CPLErr eErr = poBand->SetRawLink(
1810
0
            pszFilename, osPath.empty() ? nullptr : osPath.c_str(),
1811
0
            bRelativeToVRT, nImageOffset, nPixelOffset, nLineOffset,
1812
0
            pszByteOrder);
1813
0
        if (eErr == CE_None)
1814
0
            SetBand(GetRasterCount() + 1, std::move(poBand));
1815
1816
0
        return eErr;
1817
#else
1818
        CPLError(CE_Failure, CPLE_NotSupported,
1819
                 "VRTDataset::AddBand(): cannot instantiate VRTRawRasterBand, "
1820
                 "because disabled in this GDAL build");
1821
        return CE_Failure;
1822
#endif
1823
0
    }
1824
1825
    /* ==================================================================== */
1826
    /*      Handle a new "sourced" band.                                    */
1827
    /* ==================================================================== */
1828
0
    else
1829
0
    {
1830
0
        VRTSourcedRasterBand *poBand = nullptr;
1831
1832
0
        int nBlockXSizeIn =
1833
0
            atoi(CSLFetchNameValueDef(papszOptions, "BLOCKXSIZE", "0"));
1834
0
        int nBlockYSizeIn =
1835
0
            atoi(CSLFetchNameValueDef(papszOptions, "BLOCKYSIZE", "0"));
1836
0
        if (nBlockXSizeIn == 0 && nBlockYSizeIn == 0)
1837
0
        {
1838
0
            nBlockXSizeIn = m_nBlockXSize;
1839
0
            nBlockYSizeIn = m_nBlockYSize;
1840
0
        }
1841
1842
        /* ---- Check for our sourced band 'derived' subclass ---- */
1843
0
        if (pszSubClass != nullptr &&
1844
0
            EQUAL(pszSubClass, "VRTDerivedRasterBand"))
1845
0
        {
1846
1847
            /* We'll need a pointer to the subclass in case we need */
1848
            /* to set the new band's pixel function below. */
1849
0
            VRTDerivedRasterBand *poDerivedBand = new VRTDerivedRasterBand(
1850
0
                this, GetRasterCount() + 1, eType, GetRasterXSize(),
1851
0
                GetRasterYSize(), nBlockXSizeIn, nBlockYSizeIn);
1852
1853
            /* Set the pixel function options it provided. */
1854
0
            const char *pszFuncName =
1855
0
                CSLFetchNameValue(papszOptions, "PixelFunctionType");
1856
0
            if (pszFuncName != nullptr)
1857
0
                poDerivedBand->SetPixelFunctionName(pszFuncName);
1858
1859
0
            const char *pszLanguage =
1860
0
                CSLFetchNameValue(papszOptions, "PixelFunctionLanguage");
1861
0
            if (pszLanguage != nullptr)
1862
0
                poDerivedBand->SetPixelFunctionLanguage(pszLanguage);
1863
1864
0
            const char *pszSkipNonContributingSources =
1865
0
                CSLFetchNameValue(papszOptions, "SkipNonContributingSources");
1866
0
            if (pszSkipNonContributingSources != nullptr)
1867
0
            {
1868
0
                poDerivedBand->SetSkipNonContributingSources(
1869
0
                    CPLTestBool(pszSkipNonContributingSources));
1870
0
            }
1871
0
            for (const auto &[pszKey, pszValue] :
1872
0
                 cpl::IterateNameValue(static_cast<CSLConstList>(papszOptions)))
1873
0
            {
1874
0
                if (STARTS_WITH(pszKey, "_PIXELFN_ARG_"))
1875
0
                {
1876
0
                    poDerivedBand->AddPixelFunctionArgument(pszKey + 13,
1877
0
                                                            pszValue);
1878
0
                }
1879
0
            }
1880
1881
0
            const char *pszTransferTypeName =
1882
0
                CSLFetchNameValue(papszOptions, "SourceTransferType");
1883
0
            if (pszTransferTypeName != nullptr)
1884
0
            {
1885
0
                const GDALDataType eTransferType =
1886
0
                    GDALGetDataTypeByName(pszTransferTypeName);
1887
0
                if (eTransferType == GDT_Unknown)
1888
0
                {
1889
0
                    CPLError(CE_Failure, CPLE_AppDefined,
1890
0
                             "invalid SourceTransferType: \"%s\".",
1891
0
                             pszTransferTypeName);
1892
0
                    delete poDerivedBand;
1893
0
                    return CE_Failure;
1894
0
                }
1895
0
                poDerivedBand->SetSourceTransferType(eTransferType);
1896
0
            }
1897
1898
            /* We're done with the derived band specific stuff, so */
1899
            /* we can assign the base class pointer now. */
1900
0
            poBand = poDerivedBand;
1901
0
        }
1902
0
        else
1903
0
        {
1904
            /* ---- Standard sourced band ---- */
1905
0
            poBand = new VRTSourcedRasterBand(
1906
0
                this, GetRasterCount() + 1, eType, GetRasterXSize(),
1907
0
                GetRasterYSize(), nBlockXSizeIn, nBlockYSizeIn);
1908
0
        }
1909
1910
0
        SetBand(GetRasterCount() + 1, poBand);
1911
1912
0
        for (int i = 0; papszOptions != nullptr && papszOptions[i] != nullptr;
1913
0
             i++)
1914
0
        {
1915
0
            if (STARTS_WITH_CI(papszOptions[i], "AddFuncSource="))
1916
0
            {
1917
0
                char **papszTokens = CSLTokenizeStringComplex(
1918
0
                    papszOptions[i] + 14, ",", TRUE, FALSE);
1919
0
                if (CSLCount(papszTokens) < 1)
1920
0
                {
1921
0
                    CPLError(CE_Failure, CPLE_AppDefined,
1922
0
                             "AddFuncSource(): required argument missing.");
1923
                    // TODO: How should this error be handled?  Return
1924
                    // CE_Failure?
1925
0
                }
1926
1927
0
                VRTImageReadFunc pfnReadFunc = nullptr;
1928
0
                sscanf(papszTokens[0], "%p", &pfnReadFunc);
1929
1930
0
                void *pCBData = nullptr;
1931
0
                if (CSLCount(papszTokens) > 1)
1932
0
                    sscanf(papszTokens[1], "%p", &pCBData);
1933
1934
0
                const double dfNoDataValue = (CSLCount(papszTokens) > 2)
1935
0
                                                 ? CPLAtof(papszTokens[2])
1936
0
                                                 : VRT_NODATA_UNSET;
1937
1938
0
                poBand->AddFuncSource(pfnReadFunc, pCBData, dfNoDataValue);
1939
1940
0
                CSLDestroy(papszTokens);
1941
0
            }
1942
0
        }
1943
1944
0
        return CE_None;
1945
0
    }
1946
0
}
1947
1948
/*! @endcond */
1949
/************************************************************************/
1950
/*                              VRTAddBand()                            */
1951
/************************************************************************/
1952
1953
/**
1954
 * @see VRTDataset::VRTAddBand().
1955
 *
1956
 * @note The return type of this function is int, but the actual values
1957
 * returned are of type CPLErr.
1958
 */
1959
1960
int CPL_STDCALL VRTAddBand(VRTDatasetH hDataset, GDALDataType eType,
1961
                           char **papszOptions)
1962
1963
0
{
1964
0
    VALIDATE_POINTER1(hDataset, "VRTAddBand", 0);
1965
1966
0
    return static_cast<VRTDataset *>(GDALDataset::FromHandle(hDataset))
1967
0
        ->AddBand(eType, papszOptions);
1968
0
}
1969
1970
/*! @cond Doxygen_Suppress */
1971
1972
/************************************************************************/
1973
/*                               Create()                               */
1974
/************************************************************************/
1975
1976
GDALDataset *VRTDataset::Create(const char *pszName, int nXSize, int nYSize,
1977
                                int nBandsIn, GDALDataType eType,
1978
                                char **papszOptions)
1979
1980
0
{
1981
0
    return CreateVRTDataset(pszName, nXSize, nYSize, nBandsIn, eType,
1982
0
                            const_cast<CSLConstList>(papszOptions))
1983
0
        .release();
1984
0
}
1985
1986
/************************************************************************/
1987
/*                            CreateVRTDataset()                        */
1988
/************************************************************************/
1989
1990
std::unique_ptr<VRTDataset>
1991
VRTDataset::CreateVRTDataset(const char *pszName, int nXSize, int nYSize,
1992
                             int nBandsIn, GDALDataType eType,
1993
                             CSLConstList papszOptions)
1994
1995
0
{
1996
0
    if (STARTS_WITH_CI(pszName, "<VRTDataset"))
1997
0
    {
1998
0
        auto poDS = OpenXML(pszName, nullptr, GA_Update);
1999
0
        if (poDS != nullptr)
2000
0
            poDS->SetDescription("<FromXML>");
2001
0
        return poDS;
2002
0
    }
2003
2004
0
    const char *pszSubclass = CSLFetchNameValue(papszOptions, "SUBCLASS");
2005
2006
0
    std::unique_ptr<VRTDataset> poDS;
2007
2008
0
    const int nBlockXSize =
2009
0
        atoi(CSLFetchNameValueDef(papszOptions, "BLOCKXSIZE", "0"));
2010
0
    const int nBlockYSize =
2011
0
        atoi(CSLFetchNameValueDef(papszOptions, "BLOCKYSIZE", "0"));
2012
0
    if (pszSubclass == nullptr || EQUAL(pszSubclass, "VRTDataset"))
2013
0
        poDS = std::make_unique<VRTDataset>(nXSize, nYSize, nBlockXSize,
2014
0
                                            nBlockYSize);
2015
0
    else if (EQUAL(pszSubclass, "VRTWarpedDataset"))
2016
0
    {
2017
0
        poDS = std::make_unique<VRTWarpedDataset>(nXSize, nYSize, nBlockXSize,
2018
0
                                                  nBlockYSize);
2019
0
    }
2020
0
    else
2021
0
    {
2022
0
        CPLError(CE_Failure, CPLE_AppDefined, "SUBCLASS=%s not recognised.",
2023
0
                 pszSubclass);
2024
0
        return nullptr;
2025
0
    }
2026
0
    poDS->eAccess = GA_Update;
2027
2028
0
    poDS->SetDescription(pszName);
2029
2030
0
    for (int iBand = 0; iBand < nBandsIn; iBand++)
2031
0
        poDS->AddBand(eType, nullptr);
2032
2033
0
    poDS->SetNeedsFlush();
2034
2035
0
    poDS->oOvManager.Initialize(poDS.get(), pszName);
2036
2037
0
    return poDS;
2038
0
}
2039
2040
/************************************************************************/
2041
/*                     CreateVRTMultiDimensional()                      */
2042
/************************************************************************/
2043
2044
std::unique_ptr<VRTDataset>
2045
VRTDataset::CreateVRTMultiDimensional(const char *pszFilename,
2046
                                      CSLConstList /*papszRootGroupOptions*/,
2047
                                      CSLConstList /*papszOptions*/)
2048
0
{
2049
0
    auto poDS = std::make_unique<VRTDataset>(0, 0);
2050
0
    poDS->eAccess = GA_Update;
2051
0
    poDS->SetDescription(pszFilename);
2052
0
    poDS->m_poRootGroup = VRTGroup::Create(std::string(), "/");
2053
0
    poDS->m_poRootGroup->SetIsRootGroup();
2054
0
    poDS->m_poRootGroup->SetFilename(pszFilename);
2055
0
    poDS->m_poRootGroup->SetDirty();
2056
2057
0
    return poDS;
2058
0
}
2059
2060
/************************************************************************/
2061
/*                     CreateMultiDimensional()                         */
2062
/************************************************************************/
2063
2064
GDALDataset *
2065
VRTDataset::CreateMultiDimensional(const char *pszFilename,
2066
                                   CSLConstList papszRootGroupOptions,
2067
                                   CSLConstList papszOptions)
2068
0
{
2069
0
    return CreateVRTMultiDimensional(pszFilename, papszRootGroupOptions,
2070
0
                                     papszOptions)
2071
0
        .release();
2072
0
}
2073
2074
/************************************************************************/
2075
/*                            GetFileList()                             */
2076
/************************************************************************/
2077
2078
char **VRTDataset::GetFileList()
2079
0
{
2080
0
    char **papszFileList = GDALDataset::GetFileList();
2081
2082
0
    int nSize = CSLCount(papszFileList);
2083
0
    int nMaxSize = nSize;
2084
2085
    // Do not need an element deallocator as each string points to an
2086
    // element of the papszFileList.
2087
0
    CPLHashSet *hSetFiles =
2088
0
        CPLHashSetNew(CPLHashSetHashStr, CPLHashSetEqualStr, nullptr);
2089
2090
0
    for (int iBand = 0; iBand < nBands; iBand++)
2091
0
    {
2092
0
        static_cast<VRTRasterBand *>(papoBands[iBand])
2093
0
            ->GetFileList(&papszFileList, &nSize, &nMaxSize, hSetFiles);
2094
0
    }
2095
2096
0
    CPLHashSetDestroy(hSetFiles);
2097
2098
0
    return papszFileList;
2099
0
}
2100
2101
/************************************************************************/
2102
/*                              Delete()                                */
2103
/************************************************************************/
2104
2105
/* We implement Delete() to avoid that the default implementation */
2106
/* in GDALDriver::Delete() destroys the source files listed by GetFileList(),*/
2107
/* which would be an undesired effect... */
2108
CPLErr VRTDataset::Delete(const char *pszFilename)
2109
0
{
2110
0
    GDALDriverH hDriver = GDALIdentifyDriver(pszFilename, nullptr);
2111
2112
0
    if (!hDriver || !EQUAL(GDALGetDriverShortName(hDriver), "VRT"))
2113
0
        return CE_Failure;
2114
2115
0
    if (strstr(pszFilename, "<VRTDataset") == nullptr &&
2116
0
        VSIUnlink(pszFilename) != 0)
2117
0
    {
2118
0
        CPLError(CE_Failure, CPLE_AppDefined, "Deleting %s failed:\n%s",
2119
0
                 pszFilename, VSIStrerror(errno));
2120
0
        return CE_Failure;
2121
0
    }
2122
2123
0
    return CE_None;
2124
0
}
2125
2126
/************************************************************************/
2127
/*                          CreateMaskBand()                            */
2128
/************************************************************************/
2129
2130
CPLErr VRTDataset::CreateMaskBand(int)
2131
0
{
2132
0
    if (m_poMaskBand != nullptr)
2133
0
    {
2134
0
        CPLError(CE_Failure, CPLE_AppDefined,
2135
0
                 "This VRT dataset has already a mask band");
2136
0
        return CE_Failure;
2137
0
    }
2138
2139
0
    SetMaskBand(std::make_unique<VRTSourcedRasterBand>(this, 0));
2140
2141
0
    return CE_None;
2142
0
}
2143
2144
/************************************************************************/
2145
/*                           SetMaskBand()                              */
2146
/************************************************************************/
2147
2148
void VRTDataset::SetMaskBand(std::unique_ptr<VRTRasterBand> poMaskBandIn)
2149
0
{
2150
0
    m_poMaskBand = std::move(poMaskBandIn);
2151
0
    m_poMaskBand->SetIsMaskBand();
2152
0
}
2153
2154
/************************************************************************/
2155
/*                        CloseDependentDatasets()                      */
2156
/************************************************************************/
2157
2158
int VRTDataset::CloseDependentDatasets()
2159
0
{
2160
    /* We need to call it before removing the sources, otherwise */
2161
    /* we would remove them from the serizalized VRT */
2162
0
    FlushCache(true);
2163
2164
0
    int bHasDroppedRef = GDALDataset::CloseDependentDatasets();
2165
2166
0
    for (int iBand = 0; iBand < nBands; iBand++)
2167
0
    {
2168
0
        bHasDroppedRef |= static_cast<VRTRasterBand *>(papoBands[iBand])
2169
0
                              ->CloseDependentDatasets();
2170
0
    }
2171
2172
0
    return bHasDroppedRef;
2173
0
}
2174
2175
/************************************************************************/
2176
/*                      CheckCompatibleForDatasetIO()                   */
2177
/************************************************************************/
2178
2179
/* We will return TRUE only if all the bands are VRTSourcedRasterBands */
2180
/* made of identical sources, that are strictly VRTSimpleSource, and that */
2181
/* the band number of each source is the band number of the */
2182
/* VRTSourcedRasterBand. */
2183
2184
bool VRTDataset::CheckCompatibleForDatasetIO() const
2185
0
{
2186
0
    size_t nSources = 0;
2187
0
    const std::unique_ptr<VRTSource> *papoSources = nullptr;
2188
0
    CPLString osResampling;
2189
2190
0
    if (m_nCompatibleForDatasetIO >= 0)
2191
0
    {
2192
0
        return CPL_TO_BOOL(m_nCompatibleForDatasetIO);
2193
0
    }
2194
2195
0
    m_nCompatibleForDatasetIO = false;
2196
2197
0
    GDALDataset *poFirstBandSourceDS = nullptr;
2198
0
    for (int iBand = 0; iBand < nBands; iBand++)
2199
0
    {
2200
0
        auto poVRTBand = static_cast<VRTRasterBand *>(papoBands[iBand]);
2201
0
        assert(poVRTBand);
2202
0
        if (!poVRTBand->IsSourcedRasterBand())
2203
0
            return false;
2204
2205
0
        const VRTSourcedRasterBand *poBand =
2206
0
            static_cast<const VRTSourcedRasterBand *>(poVRTBand);
2207
2208
        // Do not allow VRTDerivedRasterBand for example
2209
0
        if (typeid(*poBand) != typeid(VRTSourcedRasterBand))
2210
0
            return false;
2211
2212
0
        if (iBand == 0)
2213
0
        {
2214
0
            nSources = poBand->m_papoSources.size();
2215
0
            papoSources = poBand->m_papoSources.data();
2216
0
            for (auto &poSource : poBand->m_papoSources)
2217
0
            {
2218
0
                if (!poSource->IsSimpleSource())
2219
0
                    return false;
2220
2221
0
                const VRTSimpleSource *poSimpleSource =
2222
0
                    static_cast<const VRTSimpleSource *>(poSource.get());
2223
0
                if (poSimpleSource->GetType() !=
2224
0
                    VRTSimpleSource::GetTypeStatic())
2225
0
                    return false;
2226
2227
0
                if (poSimpleSource->m_nBand != iBand + 1 ||
2228
0
                    poSimpleSource->m_bGetMaskBand ||
2229
0
                    (nSources > 1 && poSimpleSource->m_osSrcDSName.empty()))
2230
0
                {
2231
0
                    return false;
2232
0
                }
2233
0
                if (nSources == 1 && poSimpleSource->m_osSrcDSName.empty())
2234
0
                {
2235
0
                    if (auto poSourceBand = poSimpleSource->GetRasterBand())
2236
0
                    {
2237
0
                        poFirstBandSourceDS = poSourceBand->GetDataset();
2238
0
                    }
2239
0
                    else
2240
0
                    {
2241
0
                        return false;
2242
0
                    }
2243
0
                }
2244
0
                osResampling = poSimpleSource->GetResampling();
2245
0
            }
2246
0
        }
2247
0
        else if (nSources != poBand->m_papoSources.size())
2248
0
        {
2249
0
            return false;
2250
0
        }
2251
0
        else
2252
0
        {
2253
0
            for (size_t iSource = 0; iSource < nSources; iSource++)
2254
0
            {
2255
0
                if (!poBand->m_papoSources[iSource]->IsSimpleSource())
2256
0
                    return false;
2257
0
                const VRTSimpleSource *poRefSource =
2258
0
                    static_cast<const VRTSimpleSource *>(
2259
0
                        papoSources[iSource].get());
2260
2261
0
                const VRTSimpleSource *poSource =
2262
0
                    static_cast<const VRTSimpleSource *>(
2263
0
                        poBand->m_papoSources[iSource].get());
2264
0
                if (poSource->GetType() != VRTSimpleSource::GetTypeStatic())
2265
0
                    return false;
2266
0
                if (poSource->m_nBand != iBand + 1 ||
2267
0
                    poSource->m_bGetMaskBand ||
2268
0
                    (nSources > 1 && poSource->m_osSrcDSName.empty()))
2269
0
                    return false;
2270
0
                if (!poSource->IsSameExceptBandNumber(poRefSource))
2271
0
                    return false;
2272
0
                if (osResampling.compare(poSource->GetResampling()) != 0)
2273
0
                    return false;
2274
0
                if (nSources == 1 && poSource->m_osSrcDSName.empty())
2275
0
                {
2276
0
                    auto poSourceBand = poSource->GetRasterBand();
2277
0
                    if (!poSourceBand ||
2278
0
                        poFirstBandSourceDS != poSourceBand->GetDataset())
2279
0
                    {
2280
0
                        return false;
2281
0
                    }
2282
0
                }
2283
0
            }
2284
0
        }
2285
0
    }
2286
2287
0
    m_nCompatibleForDatasetIO = nSources != 0;
2288
0
    return CPL_TO_BOOL(m_nCompatibleForDatasetIO);
2289
0
}
2290
2291
/************************************************************************/
2292
/*                         GetSingleSimpleSource()                      */
2293
/*                                                                      */
2294
/* Returns a non-NULL dataset if the VRT is made of a single source     */
2295
/* that is a simple source, in its full extent, and with all of its     */
2296
/* bands. Basically something produced by :                             */
2297
/*   gdal_translate src dst.vrt -of VRT (-a_srs / -a_ullr)              */
2298
/************************************************************************/
2299
2300
GDALDataset *VRTDataset::GetSingleSimpleSource()
2301
0
{
2302
0
    if (!CheckCompatibleForDatasetIO())
2303
0
        return nullptr;
2304
2305
0
    VRTSourcedRasterBand *poVRTBand =
2306
0
        static_cast<VRTSourcedRasterBand *>(papoBands[0]);
2307
0
    if (poVRTBand->m_papoSources.size() != 1)
2308
0
        return nullptr;
2309
2310
0
    VRTSimpleSource *poSource =
2311
0
        static_cast<VRTSimpleSource *>(poVRTBand->m_papoSources[0].get());
2312
2313
0
    GDALRasterBand *poBand = poSource->GetRasterBand();
2314
0
    if (poBand == nullptr || poSource->GetMaskBandMainBand() != nullptr)
2315
0
        return nullptr;
2316
2317
0
    GDALDataset *poSrcDS = poBand->GetDataset();
2318
0
    if (poSrcDS == nullptr)
2319
0
        return nullptr;
2320
2321
    /* Check that it uses the full source dataset */
2322
0
    double dfReqXOff = 0.0;
2323
0
    double dfReqYOff = 0.0;
2324
0
    double dfReqXSize = 0.0;
2325
0
    double dfReqYSize = 0.0;
2326
0
    int nReqXOff = 0;
2327
0
    int nReqYOff = 0;
2328
0
    int nReqXSize = 0;
2329
0
    int nReqYSize = 0;
2330
0
    int nOutXOff = 0;
2331
0
    int nOutYOff = 0;
2332
0
    int nOutXSize = 0;
2333
0
    int nOutYSize = 0;
2334
0
    bool bError = false;
2335
0
    if (!poSource->GetSrcDstWindow(
2336
0
            0, 0, poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize(),
2337
0
            poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize(), &dfReqXOff,
2338
0
            &dfReqYOff, &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff,
2339
0
            &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff, &nOutXSize,
2340
0
            &nOutYSize, bError))
2341
0
        return nullptr;
2342
2343
0
    if (nReqXOff != 0 || nReqYOff != 0 ||
2344
0
        nReqXSize != poSrcDS->GetRasterXSize() ||
2345
0
        nReqYSize != poSrcDS->GetRasterYSize())
2346
0
        return nullptr;
2347
2348
0
    if (nOutXOff != 0 || nOutYOff != 0 ||
2349
0
        nOutXSize != poSrcDS->GetRasterXSize() ||
2350
0
        nOutYSize != poSrcDS->GetRasterYSize())
2351
0
        return nullptr;
2352
2353
0
    return poSrcDS;
2354
0
}
2355
2356
/************************************************************************/
2357
/*                             AdviseRead()                             */
2358
/************************************************************************/
2359
2360
CPLErr VRTDataset::AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
2361
                              int nBufXSize, int nBufYSize, GDALDataType eDT,
2362
                              int nBandCount, int *panBandList,
2363
                              char **papszOptions)
2364
0
{
2365
0
    if (!CheckCompatibleForDatasetIO())
2366
0
        return CE_None;
2367
2368
0
    VRTSourcedRasterBand *poVRTBand =
2369
0
        static_cast<VRTSourcedRasterBand *>(papoBands[0]);
2370
0
    if (poVRTBand->m_papoSources.size() != 1)
2371
0
        return CE_None;
2372
2373
0
    VRTSimpleSource *poSource =
2374
0
        static_cast<VRTSimpleSource *>(poVRTBand->m_papoSources[0].get());
2375
2376
    /* Find source window and buffer size */
2377
0
    double dfReqXOff = 0.0;
2378
0
    double dfReqYOff = 0.0;
2379
0
    double dfReqXSize = 0.0;
2380
0
    double dfReqYSize = 0.0;
2381
0
    int nReqXOff = 0;
2382
0
    int nReqYOff = 0;
2383
0
    int nReqXSize = 0;
2384
0
    int nReqYSize = 0;
2385
0
    int nOutXOff = 0;
2386
0
    int nOutYOff = 0;
2387
0
    int nOutXSize = 0;
2388
0
    int nOutYSize = 0;
2389
0
    bool bError = false;
2390
0
    if (!poSource->GetSrcDstWindow(nXOff, nYOff, nXSize, nYSize, nBufXSize,
2391
0
                                   nBufYSize, &dfReqXOff, &dfReqYOff,
2392
0
                                   &dfReqXSize, &dfReqYSize, &nReqXOff,
2393
0
                                   &nReqYOff, &nReqXSize, &nReqYSize, &nOutXOff,
2394
0
                                   &nOutYOff, &nOutXSize, &nOutYSize, bError))
2395
0
    {
2396
0
        return bError ? CE_Failure : CE_None;
2397
0
    }
2398
2399
0
    GDALRasterBand *poBand = poSource->GetRasterBand();
2400
0
    if (poBand == nullptr || poSource->GetMaskBandMainBand() != nullptr)
2401
0
        return CE_None;
2402
2403
0
    GDALDataset *poSrcDS = poBand->GetDataset();
2404
0
    if (poSrcDS == nullptr)
2405
0
        return CE_None;
2406
2407
0
    return poSrcDS->AdviseRead(nReqXOff, nReqYOff, nReqXSize, nReqYSize,
2408
0
                               nOutXSize, nOutYSize, eDT, nBandCount,
2409
0
                               panBandList, papszOptions);
2410
0
}
2411
2412
/************************************************************************/
2413
/*                           GetNumThreads()                            */
2414
/************************************************************************/
2415
2416
/* static */ int VRTDataset::GetNumThreads(GDALDataset *poDS)
2417
0
{
2418
0
    const char *pszNumThreads = nullptr;
2419
0
    if (poDS)
2420
0
        pszNumThreads = CSLFetchNameValueDef(poDS->GetOpenOptions(),
2421
0
                                             "NUM_THREADS", nullptr);
2422
0
    if (!pszNumThreads)
2423
0
        pszNumThreads = CPLGetConfigOption("VRT_NUM_THREADS", nullptr);
2424
0
    if (!pszNumThreads)
2425
0
        pszNumThreads = CPLGetConfigOption("GDAL_NUM_THREADS", "ALL_CPUS");
2426
0
    if (EQUAL(pszNumThreads, "0") || EQUAL(pszNumThreads, "1"))
2427
0
        return atoi(pszNumThreads);
2428
0
    const int nMaxPoolSize = GDALGetMaxDatasetPoolSize();
2429
0
    const int nLimit = std::min(CPLGetNumCPUs(), nMaxPoolSize);
2430
0
    if (EQUAL(pszNumThreads, "ALL_CPUS"))
2431
0
        return nLimit;
2432
0
    return std::min(atoi(pszNumThreads), nLimit);
2433
0
}
2434
2435
/************************************************************************/
2436
/*                       VRTDatasetRasterIOJob                          */
2437
/************************************************************************/
2438
2439
/** Structure used to declare a threaded job to satisfy IRasterIO()
2440
 * on a given source.
2441
 */
2442
struct VRTDatasetRasterIOJob
2443
{
2444
    std::atomic<int> *pnCompletedJobs = nullptr;
2445
    std::atomic<bool> *pbSuccess = nullptr;
2446
    CPLErrorAccumulator *poErrorAccumulator = nullptr;
2447
2448
    GDALDataType eVRTBandDataType = GDT_Unknown;
2449
    int nXOff = 0;
2450
    int nYOff = 0;
2451
    int nXSize = 0;
2452
    int nYSize = 0;
2453
    void *pData = nullptr;
2454
    int nBufXSize = 0;
2455
    int nBufYSize = 0;
2456
    int nBandCount = 0;
2457
    BANDMAP_TYPE panBandMap = nullptr;
2458
    GDALDataType eBufType = GDT_Unknown;
2459
    GSpacing nPixelSpace = 0;
2460
    GSpacing nLineSpace = 0;
2461
    GSpacing nBandSpace = 0;
2462
    GDALRasterIOExtraArg *psExtraArg = nullptr;
2463
    VRTSimpleSource *poSource = nullptr;
2464
2465
    static void Func(void *pData);
2466
};
2467
2468
/************************************************************************/
2469
/*                     VRTDatasetRasterIOJob::Func()                    */
2470
/************************************************************************/
2471
2472
void VRTDatasetRasterIOJob::Func(void *pData)
2473
0
{
2474
0
    auto psJob = std::unique_ptr<VRTDatasetRasterIOJob>(
2475
0
        static_cast<VRTDatasetRasterIOJob *>(pData));
2476
0
    if (*psJob->pbSuccess)
2477
0
    {
2478
0
        GDALRasterIOExtraArg sArg = *(psJob->psExtraArg);
2479
0
        sArg.pfnProgress = nullptr;
2480
0
        sArg.pProgressData = nullptr;
2481
2482
0
        auto oAccumulator = psJob->poErrorAccumulator->InstallForCurrentScope();
2483
0
        CPL_IGNORE_RET_VAL(oAccumulator);
2484
2485
0
        if (psJob->poSource->DatasetRasterIO(
2486
0
                psJob->eVRTBandDataType, psJob->nXOff, psJob->nYOff,
2487
0
                psJob->nXSize, psJob->nYSize, psJob->pData, psJob->nBufXSize,
2488
0
                psJob->nBufYSize, psJob->eBufType, psJob->nBandCount,
2489
0
                psJob->panBandMap, psJob->nPixelSpace, psJob->nLineSpace,
2490
0
                psJob->nBandSpace, &sArg) != CE_None)
2491
0
        {
2492
0
            *psJob->pbSuccess = false;
2493
0
        }
2494
0
    }
2495
2496
0
    ++(*psJob->pnCompletedJobs);
2497
0
}
2498
2499
/************************************************************************/
2500
/*                              IRasterIO()                             */
2501
/************************************************************************/
2502
2503
CPLErr VRTDataset::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
2504
                             int nXSize, int nYSize, void *pData, int nBufXSize,
2505
                             int nBufYSize, GDALDataType eBufType,
2506
                             int nBandCount, BANDMAP_TYPE panBandMap,
2507
                             GSpacing nPixelSpace, GSpacing nLineSpace,
2508
                             GSpacing nBandSpace,
2509
                             GDALRasterIOExtraArg *psExtraArg)
2510
0
{
2511
0
    m_bMultiThreadedRasterIOLastUsed = false;
2512
2513
0
    if (nBands == 1 && nBandCount == 1)
2514
0
    {
2515
0
        VRTSourcedRasterBand *poBand =
2516
0
            dynamic_cast<VRTSourcedRasterBand *>(papoBands[0]);
2517
0
        if (poBand)
2518
0
        {
2519
0
            return poBand->IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
2520
0
                                     pData, nBufXSize, nBufYSize, eBufType,
2521
0
                                     nPixelSpace, nLineSpace, psExtraArg);
2522
0
        }
2523
0
    }
2524
2525
0
    bool bLocalCompatibleForDatasetIO =
2526
0
        CPL_TO_BOOL(CheckCompatibleForDatasetIO());
2527
0
    if (bLocalCompatibleForDatasetIO && eRWFlag == GF_Read &&
2528
0
        (nBufXSize < nXSize || nBufYSize < nYSize) && m_apoOverviews.empty())
2529
0
    {
2530
0
        int bTried = FALSE;
2531
0
        const CPLErr eErr = TryOverviewRasterIO(
2532
0
            eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
2533
0
            eBufType, nBandCount, panBandMap, nPixelSpace, nLineSpace,
2534
0
            nBandSpace, psExtraArg, &bTried);
2535
2536
0
        if (bTried)
2537
0
        {
2538
0
            return eErr;
2539
0
        }
2540
2541
0
        for (int iBand = 0; iBand < nBands; iBand++)
2542
0
        {
2543
0
            VRTSourcedRasterBand *poBand =
2544
0
                static_cast<VRTSourcedRasterBand *>(papoBands[iBand]);
2545
2546
            // If there are overviews, let VRTSourcedRasterBand::IRasterIO()
2547
            // do the job.
2548
0
            if (poBand->GetOverviewCount() != 0)
2549
0
            {
2550
0
                bLocalCompatibleForDatasetIO = false;
2551
0
                break;
2552
0
            }
2553
0
        }
2554
0
    }
2555
2556
    // If resampling with non-nearest neighbour, we need to be careful
2557
    // if the VRT band exposes a nodata value, but the sources do not have it.
2558
    // To also avoid edge effects on sources when downsampling, use the
2559
    // base implementation of IRasterIO() (that is acquiring sources at their
2560
    // nominal resolution, and then downsampling), but only if none of the
2561
    // contributing sources have overviews.
2562
0
    if (bLocalCompatibleForDatasetIO && eRWFlag == GF_Read &&
2563
0
        (nXSize != nBufXSize || nYSize != nBufYSize) &&
2564
0
        psExtraArg->eResampleAlg != GRIORA_NearestNeighbour)
2565
0
    {
2566
0
        for (int iBandIndex = 0; iBandIndex < nBandCount; iBandIndex++)
2567
0
        {
2568
0
            VRTSourcedRasterBand *poBand = static_cast<VRTSourcedRasterBand *>(
2569
0
                GetRasterBand(panBandMap[iBandIndex]));
2570
0
            if (!poBand->CanIRasterIOBeForwardedToEachSource(
2571
0
                    eRWFlag, nXOff, nYOff, nXSize, nYSize, nBufXSize, nBufYSize,
2572
0
                    psExtraArg))
2573
0
            {
2574
0
                bLocalCompatibleForDatasetIO = false;
2575
0
                break;
2576
0
            }
2577
0
        }
2578
0
    }
2579
2580
0
    if (bLocalCompatibleForDatasetIO && eRWFlag == GF_Read)
2581
0
    {
2582
0
        for (int iBandIndex = 0; iBandIndex < nBandCount; iBandIndex++)
2583
0
        {
2584
0
            VRTSourcedRasterBand *poBand = static_cast<VRTSourcedRasterBand *>(
2585
0
                GetRasterBand(panBandMap[iBandIndex]));
2586
2587
            /* Dirty little trick to initialize the buffer without doing */
2588
            /* any real I/O */
2589
0
            std::vector<std::unique_ptr<VRTSource>> papoSavedSources;
2590
0
            std::swap(papoSavedSources, poBand->m_papoSources);
2591
2592
0
            GDALProgressFunc pfnProgressGlobal = psExtraArg->pfnProgress;
2593
0
            psExtraArg->pfnProgress = nullptr;
2594
2595
0
            GByte *pabyBandData =
2596
0
                static_cast<GByte *>(pData) + iBandIndex * nBandSpace;
2597
2598
0
            poBand->IRasterIO(GF_Read, nXOff, nYOff, nXSize, nYSize,
2599
0
                              pabyBandData, nBufXSize, nBufYSize, eBufType,
2600
0
                              nPixelSpace, nLineSpace, psExtraArg);
2601
2602
0
            psExtraArg->pfnProgress = pfnProgressGlobal;
2603
2604
0
            std::swap(papoSavedSources, poBand->m_papoSources);
2605
0
        }
2606
2607
0
        CPLErr eErr = CE_None;
2608
2609
        // Use the last band, because when sources reference a GDALProxyDataset,
2610
        // they don't necessary instantiate all underlying rasterbands.
2611
0
        VRTSourcedRasterBand *poBand =
2612
0
            static_cast<VRTSourcedRasterBand *>(papoBands[nBands - 1]);
2613
2614
0
        double dfXOff = nXOff;
2615
0
        double dfYOff = nYOff;
2616
0
        double dfXSize = nXSize;
2617
0
        double dfYSize = nYSize;
2618
0
        if (psExtraArg->bFloatingPointWindowValidity)
2619
0
        {
2620
0
            dfXOff = psExtraArg->dfXOff;
2621
0
            dfYOff = psExtraArg->dfYOff;
2622
0
            dfXSize = psExtraArg->dfXSize;
2623
0
            dfYSize = psExtraArg->dfYSize;
2624
0
        }
2625
2626
0
        int nContributingSources = 0;
2627
0
        int nMaxThreads = 0;
2628
0
        constexpr int MINIMUM_PIXEL_COUNT_FOR_THREADED_IO = 1000 * 1000;
2629
0
        if ((static_cast<int64_t>(nBufXSize) * nBufYSize >=
2630
0
                 MINIMUM_PIXEL_COUNT_FOR_THREADED_IO ||
2631
0
             static_cast<int64_t>(nXSize) * nYSize >=
2632
0
                 MINIMUM_PIXEL_COUNT_FOR_THREADED_IO) &&
2633
0
            poBand->CanMultiThreadRasterIO(dfXOff, dfYOff, dfXSize, dfYSize,
2634
0
                                           nContributingSources) &&
2635
0
            nContributingSources > 1 &&
2636
0
            (nMaxThreads = VRTDataset::GetNumThreads(this)) > 1)
2637
0
        {
2638
0
            m_bMultiThreadedRasterIOLastUsed = true;
2639
0
            m_oMapSharedSources.InitMutex();
2640
2641
0
            CPLErrorAccumulator errorAccumulator;
2642
0
            std::atomic<bool> bSuccess = true;
2643
0
            CPLWorkerThreadPool *psThreadPool = GDALGetGlobalThreadPool(
2644
0
                std::min(nContributingSources, nMaxThreads));
2645
2646
0
            CPLDebugOnly(
2647
0
                "VRT",
2648
0
                "IRasterIO(): use optimized "
2649
0
                "multi-threaded code path for mosaic. "
2650
0
                "Using %d threads",
2651
0
                std::min(nContributingSources, psThreadPool->GetThreadCount()));
2652
2653
0
            auto oQueue = psThreadPool->CreateJobQueue();
2654
0
            std::atomic<int> nCompletedJobs = 0;
2655
0
            for (auto &poSource : poBand->m_papoSources)
2656
0
            {
2657
0
                if (!poSource->IsSimpleSource())
2658
0
                    continue;
2659
0
                auto poSimpleSource =
2660
0
                    cpl::down_cast<VRTSimpleSource *>(poSource.get());
2661
0
                if (poSimpleSource->DstWindowIntersects(dfXOff, dfYOff, dfXSize,
2662
0
                                                        dfYSize))
2663
0
                {
2664
0
                    auto psJob = new VRTDatasetRasterIOJob();
2665
0
                    psJob->pbSuccess = &bSuccess;
2666
0
                    psJob->poErrorAccumulator = &errorAccumulator;
2667
0
                    psJob->pnCompletedJobs = &nCompletedJobs;
2668
0
                    psJob->eVRTBandDataType = poBand->GetRasterDataType();
2669
0
                    psJob->nXOff = nXOff;
2670
0
                    psJob->nYOff = nYOff;
2671
0
                    psJob->nXSize = nXSize;
2672
0
                    psJob->nYSize = nYSize;
2673
0
                    psJob->pData = pData;
2674
0
                    psJob->nBufXSize = nBufXSize;
2675
0
                    psJob->nBufYSize = nBufYSize;
2676
0
                    psJob->eBufType = eBufType;
2677
0
                    psJob->nBandCount = nBandCount;
2678
0
                    psJob->panBandMap = panBandMap;
2679
0
                    psJob->nPixelSpace = nPixelSpace;
2680
0
                    psJob->nLineSpace = nLineSpace;
2681
0
                    psJob->nBandSpace = nBandSpace;
2682
0
                    psJob->psExtraArg = psExtraArg;
2683
0
                    psJob->poSource = poSimpleSource;
2684
2685
0
                    if (!oQueue->SubmitJob(VRTDatasetRasterIOJob::Func, psJob))
2686
0
                    {
2687
0
                        delete psJob;
2688
0
                        bSuccess = false;
2689
0
                        break;
2690
0
                    }
2691
0
                }
2692
0
            }
2693
2694
0
            while (oQueue->WaitEvent())
2695
0
            {
2696
                // Quite rough progress callback. We could do better by counting
2697
                // the number of contributing pixels.
2698
0
                if (psExtraArg->pfnProgress)
2699
0
                {
2700
0
                    psExtraArg->pfnProgress(double(nCompletedJobs.load()) /
2701
0
                                                nContributingSources,
2702
0
                                            "", psExtraArg->pProgressData);
2703
0
                }
2704
0
            }
2705
2706
0
            errorAccumulator.ReplayErrors();
2707
0
            eErr = bSuccess ? CE_None : CE_Failure;
2708
0
        }
2709
0
        else
2710
0
        {
2711
0
            GDALProgressFunc pfnProgressGlobal = psExtraArg->pfnProgress;
2712
0
            void *pProgressDataGlobal = psExtraArg->pProgressData;
2713
2714
0
            const int nSources = static_cast<int>(poBand->m_papoSources.size());
2715
0
            for (int iSource = 0; eErr == CE_None && iSource < nSources;
2716
0
                 iSource++)
2717
0
            {
2718
0
                psExtraArg->pfnProgress = GDALScaledProgress;
2719
0
                psExtraArg->pProgressData = GDALCreateScaledProgress(
2720
0
                    1.0 * iSource / nSources, 1.0 * (iSource + 1) / nSources,
2721
0
                    pfnProgressGlobal, pProgressDataGlobal);
2722
2723
0
                VRTSimpleSource *poSource = static_cast<VRTSimpleSource *>(
2724
0
                    poBand->m_papoSources[iSource].get());
2725
2726
0
                eErr = poSource->DatasetRasterIO(
2727
0
                    poBand->GetRasterDataType(), nXOff, nYOff, nXSize, nYSize,
2728
0
                    pData, nBufXSize, nBufYSize, eBufType, nBandCount,
2729
0
                    panBandMap, nPixelSpace, nLineSpace, nBandSpace,
2730
0
                    psExtraArg);
2731
2732
0
                GDALDestroyScaledProgress(psExtraArg->pProgressData);
2733
0
            }
2734
2735
0
            psExtraArg->pfnProgress = pfnProgressGlobal;
2736
0
            psExtraArg->pProgressData = pProgressDataGlobal;
2737
0
        }
2738
2739
0
        if (eErr == CE_None && psExtraArg->pfnProgress)
2740
0
        {
2741
0
            psExtraArg->pfnProgress(1.0, "", psExtraArg->pProgressData);
2742
0
        }
2743
2744
0
        return eErr;
2745
0
    }
2746
2747
0
    CPLErr eErr;
2748
0
    if (eRWFlag == GF_Read &&
2749
0
        psExtraArg->eResampleAlg != GRIORA_NearestNeighbour &&
2750
0
        nBufXSize < nXSize && nBufYSize < nYSize && nBandCount > 1)
2751
0
    {
2752
        // Force going through VRTSourcedRasterBand::IRasterIO(), otherwise
2753
        // GDALDataset::IRasterIOResampled() would be used without source
2754
        // overviews being potentially used.
2755
0
        eErr = GDALDataset::BandBasedRasterIO(
2756
0
            eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
2757
0
            eBufType, nBandCount, panBandMap, nPixelSpace, nLineSpace,
2758
0
            nBandSpace, psExtraArg);
2759
0
    }
2760
0
    else
2761
0
    {
2762
0
        eErr = GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
2763
0
                                      pData, nBufXSize, nBufYSize, eBufType,
2764
0
                                      nBandCount, panBandMap, nPixelSpace,
2765
0
                                      nLineSpace, nBandSpace, psExtraArg);
2766
0
    }
2767
0
    return eErr;
2768
0
}
2769
2770
/************************************************************************/
2771
/*                  UnsetPreservedRelativeFilenames()                   */
2772
/************************************************************************/
2773
2774
void VRTDataset::UnsetPreservedRelativeFilenames()
2775
0
{
2776
0
    for (int iBand = 0; iBand < nBands; iBand++)
2777
0
    {
2778
0
        if (!static_cast<VRTRasterBand *>(papoBands[iBand])
2779
0
                 ->IsSourcedRasterBand())
2780
0
            continue;
2781
2782
0
        VRTSourcedRasterBand *poBand =
2783
0
            static_cast<VRTSourcedRasterBand *>(papoBands[iBand]);
2784
0
        for (auto &poSource : poBand->m_papoSources)
2785
0
        {
2786
0
            if (!poSource->IsSimpleSource())
2787
0
                continue;
2788
2789
0
            VRTSimpleSource *poSimpleSource =
2790
0
                static_cast<VRTSimpleSource *>(poSource.get());
2791
0
            poSimpleSource->UnsetPreservedRelativeFilenames();
2792
0
        }
2793
0
    }
2794
0
}
2795
2796
/************************************************************************/
2797
/*                        BuildVirtualOverviews()                       */
2798
/************************************************************************/
2799
2800
static bool CheckBandForOverview(GDALRasterBand *poBand,
2801
                                 GDALRasterBand *&poFirstBand, int &nOverviews,
2802
                                 std::set<std::pair<int, int>> &oSetOvrSizes,
2803
                                 std::vector<GDALDataset *> &apoOverviewsBak)
2804
0
{
2805
0
    if (!cpl::down_cast<VRTRasterBand *>(poBand)->IsSourcedRasterBand())
2806
0
        return false;
2807
2808
0
    VRTSourcedRasterBand *poVRTBand =
2809
0
        cpl::down_cast<VRTSourcedRasterBand *>(poBand);
2810
0
    if (poVRTBand->m_papoSources.size() != 1)
2811
0
        return false;
2812
0
    if (!poVRTBand->m_papoSources[0]->IsSimpleSource())
2813
0
        return false;
2814
2815
0
    VRTSimpleSource *poSource =
2816
0
        cpl::down_cast<VRTSimpleSource *>(poVRTBand->m_papoSources[0].get());
2817
0
    const char *pszType = poSource->GetType();
2818
0
    if (pszType != VRTSimpleSource::GetTypeStatic() &&
2819
0
        pszType != VRTComplexSource::GetTypeStatic())
2820
0
    {
2821
0
        return false;
2822
0
    }
2823
0
    GDALRasterBand *poSrcBand = poSource->GetMaskBandMainBand();
2824
0
    if (!poSrcBand)
2825
0
        poSrcBand = poSource->GetRasterBand();
2826
0
    if (poSrcBand == nullptr)
2827
0
        return false;
2828
2829
    // To prevent recursion
2830
0
    apoOverviewsBak.push_back(nullptr);
2831
0
    const int nOvrCount = poSrcBand->GetOverviewCount();
2832
0
    oSetOvrSizes.insert(
2833
0
        std::pair<int, int>(poSrcBand->GetXSize(), poSrcBand->GetYSize()));
2834
0
    for (int i = 0; i < nOvrCount; ++i)
2835
0
    {
2836
0
        auto poSrcOvrBand = poSrcBand->GetOverview(i);
2837
0
        if (poSrcOvrBand)
2838
0
        {
2839
0
            oSetOvrSizes.insert(std::pair<int, int>(poSrcOvrBand->GetXSize(),
2840
0
                                                    poSrcOvrBand->GetYSize()));
2841
0
        }
2842
0
    }
2843
0
    apoOverviewsBak.resize(0);
2844
2845
0
    if (nOvrCount == 0)
2846
0
        return false;
2847
0
    if (poFirstBand == nullptr)
2848
0
    {
2849
0
        if (poSrcBand->GetXSize() == 0 || poSrcBand->GetYSize() == 0)
2850
0
            return false;
2851
0
        poFirstBand = poSrcBand;
2852
0
        nOverviews = nOvrCount;
2853
0
    }
2854
0
    else if (nOvrCount < nOverviews)
2855
0
        nOverviews = nOvrCount;
2856
0
    return true;
2857
0
}
2858
2859
void VRTDataset::BuildVirtualOverviews()
2860
0
{
2861
    // Currently we expose virtual overviews only if the dataset is made of
2862
    // a single SimpleSource/ComplexSource, in each band.
2863
    // And if the underlying sources have overviews of course
2864
0
    if (!m_apoOverviews.empty() || !m_apoOverviewsBak.empty())
2865
0
        return;
2866
2867
0
    int nOverviews = 0;
2868
0
    GDALRasterBand *poFirstBand = nullptr;
2869
0
    std::set<std::pair<int, int>> oSetOvrSizes;
2870
2871
0
    for (int iBand = 0; iBand < nBands; iBand++)
2872
0
    {
2873
0
        if (!CheckBandForOverview(papoBands[iBand], poFirstBand, nOverviews,
2874
0
                                  oSetOvrSizes, m_apoOverviewsBak))
2875
0
            return;
2876
0
    }
2877
2878
0
    if (m_poMaskBand)
2879
0
    {
2880
0
        if (!CheckBandForOverview(m_poMaskBand.get(), poFirstBand, nOverviews,
2881
0
                                  oSetOvrSizes, m_apoOverviewsBak))
2882
0
            return;
2883
0
    }
2884
0
    if (poFirstBand == nullptr)
2885
0
    {
2886
        // to make cppcheck happy
2887
0
        CPLAssert(false);
2888
0
        return;
2889
0
    }
2890
2891
0
    VRTSourcedRasterBand *l_poVRTBand =
2892
0
        cpl::down_cast<VRTSourcedRasterBand *>(papoBands[0]);
2893
0
    VRTSimpleSource *poSource =
2894
0
        cpl::down_cast<VRTSimpleSource *>(l_poVRTBand->m_papoSources[0].get());
2895
0
    const double dfDstToSrcXRatio =
2896
0
        poSource->m_dfDstXSize / poSource->m_dfSrcXSize;
2897
0
    const double dfDstToSrcYRatio =
2898
0
        poSource->m_dfDstYSize / poSource->m_dfSrcYSize;
2899
2900
0
    for (int j = 0; j < nOverviews; j++)
2901
0
    {
2902
0
        auto poOvrBand = poFirstBand->GetOverview(j);
2903
0
        if (!poOvrBand)
2904
0
            return;
2905
0
        const double dfXRatio = static_cast<double>(poOvrBand->GetXSize()) /
2906
0
                                poFirstBand->GetXSize();
2907
0
        const double dfYRatio = static_cast<double>(poOvrBand->GetYSize()) /
2908
0
                                poFirstBand->GetYSize();
2909
0
        if (dfXRatio >= dfDstToSrcXRatio || dfYRatio >= dfDstToSrcYRatio)
2910
0
        {
2911
0
            continue;
2912
0
        }
2913
0
        int nOvrXSize = static_cast<int>(0.5 + nRasterXSize * dfXRatio);
2914
0
        int nOvrYSize = static_cast<int>(0.5 + nRasterYSize * dfYRatio);
2915
2916
        // Look for a source overview whose size is very close to the
2917
        // theoretical computed one.
2918
0
        bool bSrcOvrMatchFound = false;
2919
0
        for (const auto &ovrSize : oSetOvrSizes)
2920
0
        {
2921
0
            if (std::abs(ovrSize.first - nOvrXSize) <= 1 &&
2922
0
                std::abs(ovrSize.second - nOvrYSize) <= 1)
2923
0
            {
2924
0
                bSrcOvrMatchFound = true;
2925
0
                nOvrXSize = ovrSize.first;
2926
0
                nOvrYSize = ovrSize.second;
2927
0
                break;
2928
0
            }
2929
0
        }
2930
2931
0
        if (!bSrcOvrMatchFound &&
2932
0
            (nOvrXSize < DEFAULT_BLOCK_SIZE || nOvrYSize < DEFAULT_BLOCK_SIZE))
2933
0
        {
2934
0
            break;
2935
0
        }
2936
2937
0
        int nBlockXSize = 0;
2938
0
        int nBlockYSize = 0;
2939
0
        l_poVRTBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
2940
0
        if (VRTDataset::IsDefaultBlockSize(nBlockXSize, nRasterXSize))
2941
0
            nBlockXSize = 0;
2942
0
        if (VRTDataset::IsDefaultBlockSize(nBlockYSize, nRasterYSize))
2943
0
            nBlockYSize = 0;
2944
2945
0
        VRTDataset *poOvrVDS =
2946
0
            new VRTDataset(nOvrXSize, nOvrYSize, nBlockXSize, nBlockYSize);
2947
0
        m_apoOverviews.push_back(poOvrVDS);
2948
2949
0
        const auto CreateOverviewBand =
2950
0
            [&poOvrVDS, nOvrXSize, nOvrYSize, dfXRatio,
2951
0
             dfYRatio](VRTSourcedRasterBand *poVRTBand)
2952
0
        {
2953
0
            auto poOvrVRTBand = std::make_unique<VRTSourcedRasterBand>(
2954
0
                poOvrVDS, poVRTBand->GetBand(), poVRTBand->GetRasterDataType(),
2955
0
                nOvrXSize, nOvrYSize);
2956
0
            poOvrVRTBand->CopyCommonInfoFrom(poVRTBand);
2957
0
            poOvrVRTBand->m_bNoDataValueSet = poVRTBand->m_bNoDataValueSet;
2958
0
            poOvrVRTBand->m_dfNoDataValue = poVRTBand->m_dfNoDataValue;
2959
0
            poOvrVRTBand->m_bHideNoDataValue = poVRTBand->m_bHideNoDataValue;
2960
2961
0
            VRTSimpleSource *poSrcSource = cpl::down_cast<VRTSimpleSource *>(
2962
0
                poVRTBand->m_papoSources[0].get());
2963
0
            std::unique_ptr<VRTSimpleSource> poNewSource;
2964
0
            const char *pszType = poSrcSource->GetType();
2965
0
            if (pszType == VRTSimpleSource::GetTypeStatic())
2966
0
            {
2967
0
                poNewSource = std::make_unique<VRTSimpleSource>(
2968
0
                    poSrcSource, dfXRatio, dfYRatio);
2969
0
            }
2970
0
            else if (pszType == VRTComplexSource::GetTypeStatic())
2971
0
            {
2972
0
                poNewSource = std::make_unique<VRTComplexSource>(
2973
0
                    cpl::down_cast<VRTComplexSource *>(poSrcSource), dfXRatio,
2974
0
                    dfYRatio);
2975
0
            }
2976
0
            else
2977
0
            {
2978
0
                CPLAssert(false);
2979
0
            }
2980
0
            if (poNewSource)
2981
0
            {
2982
0
                poOvrVRTBand->AddSource(std::move(poNewSource));
2983
0
            }
2984
2985
0
            return poOvrVRTBand;
2986
0
        };
2987
2988
0
        for (int i = 0; i < nBands; i++)
2989
0
        {
2990
0
            VRTSourcedRasterBand *poSrcBand =
2991
0
                cpl::down_cast<VRTSourcedRasterBand *>(GetRasterBand(i + 1));
2992
0
            poOvrVDS->SetBand(poOvrVDS->GetRasterCount() + 1,
2993
0
                              CreateOverviewBand(poSrcBand));
2994
0
        }
2995
2996
0
        if (m_poMaskBand)
2997
0
        {
2998
0
            VRTSourcedRasterBand *poSrcBand =
2999
0
                cpl::down_cast<VRTSourcedRasterBand *>(m_poMaskBand.get());
3000
0
            poOvrVDS->SetMaskBand(CreateOverviewBand(poSrcBand));
3001
0
        }
3002
0
    }
3003
0
}
3004
3005
/************************************************************************/
3006
/*                        AddVirtualOverview()                          */
3007
/************************************************************************/
3008
3009
bool VRTDataset::AddVirtualOverview(int nOvFactor, const char *pszResampling)
3010
0
{
3011
0
    if (nRasterXSize / nOvFactor == 0 || nRasterYSize / nOvFactor == 0)
3012
0
    {
3013
0
        return false;
3014
0
    }
3015
3016
0
    CPLStringList argv;
3017
0
    argv.AddString("-of");
3018
0
    argv.AddString("VRT");
3019
0
    argv.AddString("-outsize");
3020
0
    argv.AddString(CPLSPrintf("%d", nRasterXSize / nOvFactor));
3021
0
    argv.AddString(CPLSPrintf("%d", nRasterYSize / nOvFactor));
3022
0
    argv.AddString("-r");
3023
0
    argv.AddString(pszResampling);
3024
3025
0
    int nBlockXSize = 0;
3026
0
    int nBlockYSize = 0;
3027
0
    GetRasterBand(1)->GetBlockSize(&nBlockXSize, &nBlockYSize);
3028
0
    if (!VRTDataset::IsDefaultBlockSize(nBlockXSize, nRasterXSize))
3029
0
    {
3030
0
        argv.AddString("-co");
3031
0
        argv.AddString(CPLSPrintf("BLOCKXSIZE=%d", nBlockXSize));
3032
0
    }
3033
0
    if (!VRTDataset::IsDefaultBlockSize(nBlockYSize, nRasterYSize))
3034
0
    {
3035
0
        argv.AddString("-co");
3036
0
        argv.AddString(CPLSPrintf("BLOCKYSIZE=%d", nBlockYSize));
3037
0
    }
3038
3039
0
    GDALTranslateOptions *psOptions =
3040
0
        GDALTranslateOptionsNew(argv.List(), nullptr);
3041
3042
    // Add a dummy overview so that BuildVirtualOverviews() doesn't trigger
3043
0
    m_apoOverviews.push_back(nullptr);
3044
0
    CPLAssert(m_bCanTakeRef);
3045
0
    m_bCanTakeRef =
3046
0
        false;  // we don't want hOverviewDS to take a reference on ourselves.
3047
0
    GDALDatasetH hOverviewDS =
3048
0
        GDALTranslate("", GDALDataset::ToHandle(this), psOptions, nullptr);
3049
0
    m_bCanTakeRef = true;
3050
0
    m_apoOverviews.pop_back();
3051
3052
0
    GDALTranslateOptionsFree(psOptions);
3053
0
    if (hOverviewDS == nullptr)
3054
0
        return false;
3055
3056
0
    m_anOverviewFactors.push_back(nOvFactor);
3057
0
    m_apoOverviews.push_back(GDALDataset::FromHandle(hOverviewDS));
3058
0
    return true;
3059
0
}
3060
3061
/************************************************************************/
3062
/*                          IBuildOverviews()                           */
3063
/************************************************************************/
3064
3065
CPLErr VRTDataset::IBuildOverviews(const char *pszResampling, int nOverviews,
3066
                                   const int *panOverviewList, int nListBands,
3067
                                   const int *panBandList,
3068
                                   GDALProgressFunc pfnProgress,
3069
                                   void *pProgressData,
3070
                                   CSLConstList papszOptions)
3071
0
{
3072
0
    if (CPLTestBool(CSLFetchNameValueDef(
3073
0
            papszOptions, "VIRTUAL",
3074
0
            CPLGetConfigOption("VRT_VIRTUAL_OVERVIEWS", "NO"))))
3075
0
    {
3076
0
        SetNeedsFlush();
3077
0
        if (nOverviews == 0 ||
3078
0
            (!m_apoOverviews.empty() && m_anOverviewFactors.empty()))
3079
0
        {
3080
0
            m_anOverviewFactors.clear();
3081
0
            m_apoOverviewsBak.insert(m_apoOverviewsBak.end(),
3082
0
                                     m_apoOverviews.begin(),
3083
0
                                     m_apoOverviews.end());
3084
0
            m_apoOverviews.clear();
3085
0
        }
3086
0
        m_osOverviewResampling = pszResampling;
3087
0
        for (int i = 0; i < nOverviews; i++)
3088
0
        {
3089
0
            if (std::find(m_anOverviewFactors.begin(),
3090
0
                          m_anOverviewFactors.end(),
3091
0
                          panOverviewList[i]) == m_anOverviewFactors.end())
3092
0
            {
3093
0
                AddVirtualOverview(panOverviewList[i], pszResampling);
3094
0
            }
3095
0
        }
3096
0
        return CE_None;
3097
0
    }
3098
3099
0
    if (!oOvManager.IsInitialized())
3100
0
    {
3101
0
        const char *pszDesc = GetDescription();
3102
0
        if (pszDesc[0])
3103
0
        {
3104
0
            oOvManager.Initialize(this, pszDesc);
3105
0
        }
3106
0
    }
3107
3108
    // Make implicit overviews invisible, but do not destroy them in case they
3109
    // are already used.  Should the client do that?  Behavior might undefined
3110
    // in GDAL API?
3111
0
    if (!m_apoOverviews.empty())
3112
0
    {
3113
0
        m_apoOverviewsBak.insert(m_apoOverviewsBak.end(),
3114
0
                                 m_apoOverviews.begin(), m_apoOverviews.end());
3115
0
        m_apoOverviews.clear();
3116
0
    }
3117
0
    else
3118
0
    {
3119
        // Add a dummy overview so that GDALDataset::IBuildOverviews()
3120
        // doesn't manage to get a virtual implicit overview.
3121
0
        m_apoOverviews.push_back(nullptr);
3122
0
    }
3123
3124
0
    CPLErr eErr = GDALDataset::IBuildOverviews(
3125
0
        pszResampling, nOverviews, panOverviewList, nListBands, panBandList,
3126
0
        pfnProgress, pProgressData, papszOptions);
3127
3128
0
    m_apoOverviews.clear();
3129
0
    return eErr;
3130
0
}
3131
3132
/************************************************************************/
3133
/*                         GetShiftedDataset()                          */
3134
/*                                                                      */
3135
/* Returns true if the VRT is made of a single source that is a simple  */
3136
/* in its full resolution.                                              */
3137
/************************************************************************/
3138
3139
bool VRTDataset::GetShiftedDataset(int nXOff, int nYOff, int nXSize, int nYSize,
3140
                                   GDALDataset *&poSrcDataset, int &nSrcXOff,
3141
                                   int &nSrcYOff)
3142
0
{
3143
0
    if (!CheckCompatibleForDatasetIO())
3144
0
        return false;
3145
3146
0
    VRTSourcedRasterBand *poVRTBand =
3147
0
        static_cast<VRTSourcedRasterBand *>(papoBands[0]);
3148
0
    if (poVRTBand->m_papoSources.size() != 1)
3149
0
        return false;
3150
3151
0
    VRTSimpleSource *poSource =
3152
0
        static_cast<VRTSimpleSource *>(poVRTBand->m_papoSources[0].get());
3153
3154
0
    GDALRasterBand *poBand = poSource->GetRasterBand();
3155
0
    if (!poBand || poSource->GetMaskBandMainBand() ||
3156
0
        poBand->GetRasterDataType() != poVRTBand->GetRasterDataType())
3157
0
        return false;
3158
3159
0
    poSrcDataset = poBand->GetDataset();
3160
0
    if (!poSrcDataset)
3161
0
        return false;
3162
3163
0
    double dfReqXOff = 0.0;
3164
0
    double dfReqYOff = 0.0;
3165
0
    double dfReqXSize = 0.0;
3166
0
    double dfReqYSize = 0.0;
3167
0
    int nReqXOff = 0;
3168
0
    int nReqYOff = 0;
3169
0
    int nReqXSize = 0;
3170
0
    int nReqYSize = 0;
3171
0
    int nOutXOff = 0;
3172
0
    int nOutYOff = 0;
3173
0
    int nOutXSize = 0;
3174
0
    int nOutYSize = 0;
3175
0
    bool bError = false;
3176
0
    if (!poSource->GetSrcDstWindow(nXOff, nYOff, nXSize, nYSize, nXSize, nYSize,
3177
0
                                   &dfReqXOff, &dfReqYOff, &dfReqXSize,
3178
0
                                   &dfReqYSize, &nReqXOff, &nReqYOff,
3179
0
                                   &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff,
3180
0
                                   &nOutXSize, &nOutYSize, bError))
3181
0
        return false;
3182
3183
0
    if (nReqXSize != nXSize || nReqYSize != nYSize || nReqXSize != nOutXSize ||
3184
0
        nReqYSize != nOutYSize)
3185
0
        return false;
3186
3187
0
    nSrcXOff = nReqXOff;
3188
0
    nSrcYOff = nReqYOff;
3189
0
    return true;
3190
0
}
3191
3192
/************************************************************************/
3193
/*                       GetCompressionFormats()                        */
3194
/************************************************************************/
3195
3196
CPLStringList VRTDataset::GetCompressionFormats(int nXOff, int nYOff,
3197
                                                int nXSize, int nYSize,
3198
                                                int nBandCount,
3199
                                                const int *panBandList)
3200
0
{
3201
0
    GDALDataset *poSrcDataset;
3202
0
    int nSrcXOff;
3203
0
    int nSrcYOff;
3204
0
    if (!GetShiftedDataset(nXOff, nYOff, nXSize, nYSize, poSrcDataset, nSrcXOff,
3205
0
                           nSrcYOff))
3206
0
        return CPLStringList();
3207
0
    return poSrcDataset->GetCompressionFormats(nSrcXOff, nSrcYOff, nXSize,
3208
0
                                               nYSize, nBandCount, panBandList);
3209
0
}
3210
3211
/************************************************************************/
3212
/*                       ReadCompressedData()                           */
3213
/************************************************************************/
3214
3215
CPLErr VRTDataset::ReadCompressedData(const char *pszFormat, int nXOff,
3216
                                      int nYOff, int nXSize, int nYSize,
3217
                                      int nBandCount, const int *panBandList,
3218
                                      void **ppBuffer, size_t *pnBufferSize,
3219
                                      char **ppszDetailedFormat)
3220
0
{
3221
0
    GDALDataset *poSrcDataset;
3222
0
    int nSrcXOff;
3223
0
    int nSrcYOff;
3224
0
    if (!GetShiftedDataset(nXOff, nYOff, nXSize, nYSize, poSrcDataset, nSrcXOff,
3225
0
                           nSrcYOff))
3226
0
        return CE_Failure;
3227
0
    return poSrcDataset->ReadCompressedData(
3228
0
        pszFormat, nSrcXOff, nSrcYOff, nXSize, nYSize, nBandCount, panBandList,
3229
0
        ppBuffer, pnBufferSize, ppszDetailedFormat);
3230
0
}
3231
3232
/************************************************************************/
3233
/*                          ClearStatistics()                           */
3234
/************************************************************************/
3235
3236
void VRTDataset::ClearStatistics()
3237
0
{
3238
0
    for (int i = 1; i <= nBands; ++i)
3239
0
    {
3240
0
        bool bChanged = false;
3241
0
        GDALRasterBand *poBand = GetRasterBand(i);
3242
0
        CSLConstList papszOldMD = poBand->GetMetadata();
3243
0
        CPLStringList aosNewMD;
3244
0
        for (const char *pszMDItem : cpl::Iterate(papszOldMD))
3245
0
        {
3246
0
            if (STARTS_WITH_CI(pszMDItem, "STATISTICS_"))
3247
0
            {
3248
0
                bChanged = true;
3249
0
            }
3250
0
            else
3251
0
            {
3252
0
                aosNewMD.AddString(pszMDItem);
3253
0
            }
3254
0
        }
3255
0
        if (bChanged)
3256
0
        {
3257
0
            poBand->SetMetadata(aosNewMD.List());
3258
0
        }
3259
0
    }
3260
3261
0
    GDALDataset::ClearStatistics();
3262
0
}
3263
3264
/************************************************************************/
3265
/*                 VRTMapSharedResources::LockGuard()                   */
3266
/************************************************************************/
3267
3268
std::unique_ptr<std::lock_guard<std::mutex>>
3269
VRTMapSharedResources::LockGuard() const
3270
0
{
3271
0
    std::unique_ptr<std::lock_guard<std::mutex>> poLockGuard;
3272
0
    if (m_bUseMutex)
3273
0
    {
3274
0
        poLockGuard = std::make_unique<std::lock_guard<std::mutex>>(m_oMutex);
3275
0
    }
3276
0
    return poLockGuard;
3277
0
}
3278
3279
/************************************************************************/
3280
/*                   VRTMapSharedResources::Get()                       */
3281
/************************************************************************/
3282
3283
GDALDataset *VRTMapSharedResources::Get(const std::string &osKey) const
3284
0
{
3285
0
    auto poLockGuard = LockGuard();
3286
0
    CPL_IGNORE_RET_VAL(poLockGuard);
3287
0
    auto oIter = m_oMap.find(osKey);
3288
0
    GDALDataset *poRet = nullptr;
3289
0
    if (oIter != m_oMap.end())
3290
0
        poRet = oIter->second;
3291
0
    return poRet;
3292
0
}
3293
3294
/************************************************************************/
3295
/*                   VRTMapSharedResources::Insert()                    */
3296
/************************************************************************/
3297
3298
void VRTMapSharedResources::Insert(const std::string &osKey, GDALDataset *poDS)
3299
0
{
3300
0
    auto poLockGuard = LockGuard();
3301
0
    CPL_IGNORE_RET_VAL(poLockGuard);
3302
0
    m_oMap[osKey] = poDS;
3303
0
}
3304
3305
/************************************************************************/
3306
/*                   VRTMapSharedResources::InitMutex()                 */
3307
/************************************************************************/
3308
3309
void VRTMapSharedResources::InitMutex()
3310
0
{
3311
0
    m_bUseMutex = true;
3312
0
}
3313
3314
/*! @endcond */