Coverage Report

Created: 2026-07-25 06:50

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