Coverage Report

Created: 2026-03-30 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/gdalbuildvrt_lib.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL Utilities
4
 * Purpose:  Command line application to build VRT datasets from raster products
5
 *           or content of SHP tile index
6
 * Author:   Even Rouault, <even dot rouault at spatialys dot com>
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 2007-2016, Even Rouault <even dot rouault at spatialys dot com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "ogr_api.h"
15
#include "ogr_srs_api.h"
16
17
#include "cpl_port.h"
18
#include "gdal_utils.h"
19
#include "gdal_utils_priv.h"
20
#include "gdalargumentparser.h"
21
22
#include <cassert>
23
#include <cmath>
24
#include <cstdio>
25
#include <cstdlib>
26
#include <cstring>
27
28
#include <algorithm>
29
#include <memory>
30
#include <set>
31
#include <string>
32
#include <vector>
33
34
#include "commonutils.h"
35
#include "cpl_conv.h"
36
#include "cpl_error.h"
37
#include "cpl_float.h"
38
#include "cpl_progress.h"
39
#include "cpl_string.h"
40
#include "cpl_vsi.h"
41
#include "cpl_vsi_virtual.h"
42
#include "gdal.h"
43
#include "gdal_vrt.h"
44
#include "gdal_priv.h"
45
#include "gdal_proxy.h"
46
#include "ogr_api.h"
47
#include "ogr_core.h"
48
#include "ogr_srs_api.h"
49
#include "ogr_spatialref.h"
50
#include "ogrsf_frmts.h"
51
#include "vrtdataset.h"
52
53
0
#define GEOTRSFRM_TOPLEFT_X 0
54
0
#define GEOTRSFRM_WE_RES 1
55
0
#define GEOTRSFRM_ROTATION_PARAM1 2
56
0
#define GEOTRSFRM_TOPLEFT_Y 3
57
0
#define GEOTRSFRM_ROTATION_PARAM2 4
58
0
#define GEOTRSFRM_NS_RES 5
59
60
namespace gdal::GDALBuildVRT
61
{
62
typedef enum
63
{
64
    LOWEST_RESOLUTION,
65
    HIGHEST_RESOLUTION,
66
    AVERAGE_RESOLUTION,
67
    SAME_RESOLUTION,
68
    USER_RESOLUTION,
69
    COMMON_RESOLUTION,
70
} ResolutionStrategy;
71
72
struct DatasetProperty
73
{
74
    int isFileOK = FALSE;
75
    int nRasterXSize = 0;
76
    int nRasterYSize = 0;
77
    GDALGeoTransform gt{};
78
    int nBlockXSize = 0;
79
    int nBlockYSize = 0;
80
    std::vector<GDALDataType> aeBandType{};
81
    std::vector<bool> abHasNoData{};
82
    std::vector<double> adfNoDataValues{};
83
    std::vector<bool> abHasOffset{};
84
    std::vector<double> adfOffset{};
85
    std::vector<bool> abHasScale{};
86
    std::vector<bool> abHasMaskBand{};
87
    std::vector<double> adfScale{};
88
    int bHasDatasetMask = 0;
89
    bool bLastBandIsAlpha = false;
90
    int nMaskBlockXSize = 0;
91
    int nMaskBlockYSize = 0;
92
    std::vector<int> anOverviewFactors{};
93
    std::vector<std::string> aosDescriptions{};
94
    std::map<int, std::map<std::string, std::string>> mapBandMetadata{};
95
};
96
97
struct BandProperty
98
{
99
    GDALColorInterp colorInterpretation = GCI_Undefined;
100
    GDALDataType dataType = GDT_Unknown;
101
    std::unique_ptr<GDALColorTable> colorTable{};
102
    bool bHasNoData = false;
103
    double noDataValue = 0;
104
    bool bHasOffset = false;
105
    double dfOffset = 0;
106
    bool bHasScale = false;
107
    double dfScale = 0;
108
    std::string osDescription = "";
109
    std::map<std::string, std::string> mapBandMetadata{};
110
};
111
}  // namespace gdal::GDALBuildVRT
112
113
using namespace gdal::GDALBuildVRT;
114
115
/************************************************************************/
116
/*                            GetSrcDstWin()                            */
117
/************************************************************************/
118
119
static int GetSrcDstWin(DatasetProperty *psDP, double we_res, double ns_res,
120
                        double minX, double minY, double maxX, double maxY,
121
                        int nTargetXSize, int nTargetYSize, double *pdfSrcXOff,
122
                        double *pdfSrcYOff, double *pdfSrcXSize,
123
                        double *pdfSrcYSize, double *pdfDstXOff,
124
                        double *pdfDstYOff, double *pdfDstXSize,
125
                        double *pdfDstYSize)
126
0
{
127
0
    if (we_res == 0 || ns_res == 0)
128
0
    {
129
        // should not happen. to please Coverity
130
0
        return FALSE;
131
0
    }
132
133
    /* Check that the destination bounding box intersects the source bounding
134
     * box */
135
0
    if (psDP->gt[GEOTRSFRM_TOPLEFT_X] +
136
0
            psDP->nRasterXSize * psDP->gt[GEOTRSFRM_WE_RES] <=
137
0
        minX)
138
0
        return FALSE;
139
0
    if (psDP->gt[GEOTRSFRM_TOPLEFT_X] >= maxX)
140
0
        return FALSE;
141
0
    if (psDP->gt[GEOTRSFRM_TOPLEFT_Y] +
142
0
            psDP->nRasterYSize * psDP->gt[GEOTRSFRM_NS_RES] >=
143
0
        maxY)
144
0
        return FALSE;
145
0
    if (psDP->gt[GEOTRSFRM_TOPLEFT_Y] <= minY)
146
0
        return FALSE;
147
148
0
    if (psDP->gt[GEOTRSFRM_TOPLEFT_X] < minX)
149
0
    {
150
0
        *pdfSrcXOff =
151
0
            (minX - psDP->gt[GEOTRSFRM_TOPLEFT_X]) / psDP->gt[GEOTRSFRM_WE_RES];
152
0
        *pdfDstXOff = 0.0;
153
0
    }
154
0
    else
155
0
    {
156
0
        *pdfSrcXOff = 0.0;
157
0
        *pdfDstXOff = ((psDP->gt[GEOTRSFRM_TOPLEFT_X] - minX) / we_res);
158
0
    }
159
0
    if (maxY < psDP->gt[GEOTRSFRM_TOPLEFT_Y])
160
0
    {
161
0
        *pdfSrcYOff = (psDP->gt[GEOTRSFRM_TOPLEFT_Y] - maxY) /
162
0
                      -psDP->gt[GEOTRSFRM_NS_RES];
163
0
        *pdfDstYOff = 0.0;
164
0
    }
165
0
    else
166
0
    {
167
0
        *pdfSrcYOff = 0.0;
168
0
        *pdfDstYOff = ((maxY - psDP->gt[GEOTRSFRM_TOPLEFT_Y]) / -ns_res);
169
0
    }
170
171
0
    *pdfSrcXSize = psDP->nRasterXSize;
172
0
    *pdfSrcYSize = psDP->nRasterYSize;
173
0
    if (*pdfSrcXOff > 0)
174
0
        *pdfSrcXSize -= *pdfSrcXOff;
175
0
    if (*pdfSrcYOff > 0)
176
0
        *pdfSrcYSize -= *pdfSrcYOff;
177
178
0
    const double dfSrcToDstXSize = psDP->gt[GEOTRSFRM_WE_RES] / we_res;
179
0
    *pdfDstXSize = *pdfSrcXSize * dfSrcToDstXSize;
180
0
    const double dfSrcToDstYSize = psDP->gt[GEOTRSFRM_NS_RES] / ns_res;
181
0
    *pdfDstYSize = *pdfSrcYSize * dfSrcToDstYSize;
182
183
0
    if (*pdfDstXOff + *pdfDstXSize > nTargetXSize)
184
0
    {
185
0
        *pdfDstXSize = nTargetXSize - *pdfDstXOff;
186
0
        *pdfSrcXSize = *pdfDstXSize / dfSrcToDstXSize;
187
0
    }
188
189
0
    if (*pdfDstYOff + *pdfDstYSize > nTargetYSize)
190
0
    {
191
0
        *pdfDstYSize = nTargetYSize - *pdfDstYOff;
192
0
        *pdfSrcYSize = *pdfDstYSize / dfSrcToDstYSize;
193
0
    }
194
195
0
    return *pdfSrcXSize > 0 && *pdfDstXSize > 0 && *pdfSrcYSize > 0 &&
196
0
           *pdfDstYSize > 0;
197
0
}
198
199
/************************************************************************/
200
/*                              VRTBuilder                              */
201
/************************************************************************/
202
203
class VRTBuilder
204
{
205
    /* Input parameters */
206
    bool bStrict = false;
207
    char *pszOutputFilename = nullptr;
208
    int nInputFiles = 0;
209
    char **ppszInputFilenames = nullptr;
210
    int nSrcDSCount = 0;
211
    GDALDatasetH *pahSrcDS = nullptr;
212
    int nTotalBands = 0;
213
    bool bLastBandIsAlpha = false;
214
    bool bExplicitBandList = false;
215
    int nMaxSelectedBandNo = 0;
216
    int nSelectedBands = 0;
217
    int *panSelectedBandList = nullptr;
218
    ResolutionStrategy resolutionStrategy = AVERAGE_RESOLUTION;
219
    int nCountValid = 0;
220
    double we_res = 0;
221
    double ns_res = 0;
222
    int bTargetAlignedPixels = 0;
223
    double minX = 0;
224
    double minY = 0;
225
    double maxX = 0;
226
    double maxY = 0;
227
    int bSeparate = 0;
228
    int bAllowProjectionDifference = 0;
229
    int bAddAlpha = 0;
230
    int bHideNoData = 0;
231
    int nSubdataset = 0;
232
    char *pszSrcNoData = nullptr;
233
    char *pszVRTNoData = nullptr;
234
    char *pszOutputSRS = nullptr;
235
    char *pszResampling = nullptr;
236
    char **papszOpenOptions = nullptr;
237
    bool bUseSrcMaskBand = true;
238
    bool bNoDataFromMask = false;
239
    double dfMaskValueThreshold = 0;
240
    const CPLStringList aosCreateOptions;
241
    std::string osPixelFunction{};
242
    const CPLStringList aosPixelFunctionArgs;
243
    const bool bWriteAbsolutePath;
244
245
    /* Internal variables */
246
    char *pszProjectionRef = nullptr;
247
    std::vector<BandProperty> asBandProperties{};
248
    int bFirst = TRUE;
249
    int bHasGeoTransform = 0;
250
    int nRasterXSize = 0;
251
    int nRasterYSize = 0;
252
    std::vector<DatasetProperty> asDatasetProperties{};
253
    int bUserExtent = 0;
254
    int bAllowSrcNoData = TRUE;
255
    double *padfSrcNoData = nullptr;
256
    int nSrcNoDataCount = 0;
257
    int bAllowVRTNoData = TRUE;
258
    double *padfVRTNoData = nullptr;
259
    int nVRTNoDataCount = 0;
260
    int bHasRunBuild = 0;
261
    int bHasDatasetMask = 0;
262
263
    std::string AnalyseRaster(GDALDatasetH hDS,
264
                              DatasetProperty *psDatasetProperties);
265
266
    void CreateVRTSeparate(VRTDataset *poVTDS);
267
    void CreateVRTNonSeparate(VRTDataset *poVRTDS);
268
269
    CPL_DISALLOW_COPY_ASSIGN(VRTBuilder)
270
271
  public:
272
    VRTBuilder(bool bStrictIn, const char *pszOutputFilename, int nInputFiles,
273
               const char *const *ppszInputFilenames, GDALDatasetH *pahSrcDSIn,
274
               const int *panSelectedBandListIn, int nBandCount,
275
               ResolutionStrategy resolutionStrategy, double we_res,
276
               double ns_res, int bTargetAlignedPixels, double minX,
277
               double minY, double maxX, double maxY, int bSeparate,
278
               int bAllowProjectionDifference, int bAddAlpha, int bHideNoData,
279
               int nSubdataset, const char *pszSrcNoData,
280
               const char *pszVRTNoData, bool bUseSrcMaskBand,
281
               bool bNoDataFromMask, double dfMaskValueThreshold,
282
               const char *pszOutputSRS, const char *pszResampling,
283
               const char *pszPixelFunctionName,
284
               const CPLStringList &aosPixelFunctionArgs,
285
               const char *const *papszOpenOptionsIn,
286
               const CPLStringList &aosCreateOptionsIn,
287
               bool bWriteAbsolutePathIn);
288
289
    ~VRTBuilder();
290
291
    std::unique_ptr<GDALDataset> Build(GDALProgressFunc pfnProgress,
292
                                       void *pProgressData);
293
294
    std::string m_osProgramName{};
295
};
296
297
/************************************************************************/
298
/*                             VRTBuilder()                             */
299
/************************************************************************/
300
301
VRTBuilder::VRTBuilder(
302
    bool bStrictIn, const char *pszOutputFilenameIn, int nInputFilesIn,
303
    const char *const *ppszInputFilenamesIn, GDALDatasetH *pahSrcDSIn,
304
    const int *panSelectedBandListIn, int nBandCount,
305
    ResolutionStrategy resolutionStrategyIn, double we_resIn, double ns_resIn,
306
    int bTargetAlignedPixelsIn, double minXIn, double minYIn, double maxXIn,
307
    double maxYIn, int bSeparateIn, int bAllowProjectionDifferenceIn,
308
    int bAddAlphaIn, int bHideNoDataIn, int nSubdatasetIn,
309
    const char *pszSrcNoDataIn, const char *pszVRTNoDataIn,
310
    bool bUseSrcMaskBandIn, bool bNoDataFromMaskIn,
311
    double dfMaskValueThresholdIn, const char *pszOutputSRSIn,
312
    const char *pszResamplingIn, const char *pszPixelFunctionIn,
313
    const CPLStringList &aosPixelFunctionArgsIn,
314
    const char *const *papszOpenOptionsIn,
315
    const CPLStringList &aosCreateOptionsIn, bool bWriteAbsolutePathIn)
316
0
    : bStrict(bStrictIn), aosCreateOptions(aosCreateOptionsIn),
317
0
      aosPixelFunctionArgs(aosPixelFunctionArgsIn),
318
0
      bWriteAbsolutePath(bWriteAbsolutePathIn)
319
0
{
320
0
    pszOutputFilename = CPLStrdup(pszOutputFilenameIn);
321
0
    nInputFiles = nInputFilesIn;
322
0
    papszOpenOptions = CSLDuplicate(const_cast<char **>(papszOpenOptionsIn));
323
324
0
    if (pszPixelFunctionIn != nullptr)
325
0
    {
326
0
        osPixelFunction = pszPixelFunctionIn;
327
0
    }
328
329
0
    if (ppszInputFilenamesIn)
330
0
    {
331
0
        ppszInputFilenames =
332
0
            static_cast<char **>(CPLMalloc(nInputFiles * sizeof(char *)));
333
0
        for (int i = 0; i < nInputFiles; i++)
334
0
        {
335
0
            ppszInputFilenames[i] = CPLStrdup(ppszInputFilenamesIn[i]);
336
0
        }
337
0
    }
338
0
    else if (pahSrcDSIn)
339
0
    {
340
0
        nSrcDSCount = nInputFiles;
341
0
        pahSrcDS = static_cast<GDALDatasetH *>(
342
0
            CPLMalloc(nInputFiles * sizeof(GDALDatasetH)));
343
0
        memcpy(pahSrcDS, pahSrcDSIn, nInputFiles * sizeof(GDALDatasetH));
344
0
        ppszInputFilenames =
345
0
            static_cast<char **>(CPLMalloc(nInputFiles * sizeof(char *)));
346
0
        for (int i = 0; i < nInputFiles; i++)
347
0
        {
348
0
            ppszInputFilenames[i] =
349
0
                CPLStrdup(GDALGetDescription(pahSrcDSIn[i]));
350
0
        }
351
0
    }
352
353
0
    bExplicitBandList = nBandCount != 0;
354
0
    nSelectedBands = nBandCount;
355
0
    if (nBandCount)
356
0
    {
357
0
        panSelectedBandList =
358
0
            static_cast<int *>(CPLMalloc(nSelectedBands * sizeof(int)));
359
0
        memcpy(panSelectedBandList, panSelectedBandListIn,
360
0
               nSelectedBands * sizeof(int));
361
0
    }
362
363
0
    resolutionStrategy = resolutionStrategyIn;
364
0
    we_res = we_resIn;
365
0
    ns_res = ns_resIn;
366
0
    bTargetAlignedPixels = bTargetAlignedPixelsIn;
367
0
    minX = minXIn;
368
0
    minY = minYIn;
369
0
    maxX = maxXIn;
370
0
    maxY = maxYIn;
371
0
    bSeparate = bSeparateIn;
372
0
    bAllowProjectionDifference = bAllowProjectionDifferenceIn;
373
0
    bAddAlpha = bAddAlphaIn;
374
0
    bHideNoData = bHideNoDataIn;
375
0
    nSubdataset = nSubdatasetIn;
376
0
    pszSrcNoData = (pszSrcNoDataIn) ? CPLStrdup(pszSrcNoDataIn) : nullptr;
377
0
    pszVRTNoData = (pszVRTNoDataIn) ? CPLStrdup(pszVRTNoDataIn) : nullptr;
378
0
    pszOutputSRS = (pszOutputSRSIn) ? CPLStrdup(pszOutputSRSIn) : nullptr;
379
0
    pszResampling = (pszResamplingIn) ? CPLStrdup(pszResamplingIn) : nullptr;
380
0
    bUseSrcMaskBand = bUseSrcMaskBandIn;
381
0
    bNoDataFromMask = bNoDataFromMaskIn;
382
0
    dfMaskValueThreshold = dfMaskValueThresholdIn;
383
0
}
384
385
/************************************************************************/
386
/*                            ~VRTBuilder()                             */
387
/************************************************************************/
388
389
VRTBuilder::~VRTBuilder()
390
0
{
391
0
    CPLFree(pszOutputFilename);
392
0
    CPLFree(pszSrcNoData);
393
0
    CPLFree(pszVRTNoData);
394
0
    CPLFree(panSelectedBandList);
395
396
0
    if (ppszInputFilenames)
397
0
    {
398
0
        for (int i = 0; i < nInputFiles; i++)
399
0
        {
400
0
            CPLFree(ppszInputFilenames[i]);
401
0
        }
402
0
    }
403
0
    CPLFree(ppszInputFilenames);
404
0
    CPLFree(pahSrcDS);
405
406
0
    CPLFree(pszProjectionRef);
407
0
    CPLFree(padfSrcNoData);
408
0
    CPLFree(padfVRTNoData);
409
0
    CPLFree(pszOutputSRS);
410
0
    CPLFree(pszResampling);
411
0
    CSLDestroy(papszOpenOptions);
412
0
}
413
414
/************************************************************************/
415
/*                            ProjAreEqual()                            */
416
/************************************************************************/
417
418
static int ProjAreEqual(const char *pszWKT1, const char *pszWKT2)
419
0
{
420
0
    if (EQUAL(pszWKT1, pszWKT2))
421
0
        return TRUE;
422
423
0
    OGRSpatialReferenceH hSRS1 = OSRNewSpatialReference(pszWKT1);
424
0
    OGRSpatialReferenceH hSRS2 = OSRNewSpatialReference(pszWKT2);
425
0
    int bRet = hSRS1 != nullptr && hSRS2 != nullptr && OSRIsSame(hSRS1, hSRS2);
426
0
    if (hSRS1)
427
0
        OSRDestroySpatialReference(hSRS1);
428
0
    if (hSRS2)
429
0
        OSRDestroySpatialReference(hSRS2);
430
0
    return bRet;
431
0
}
432
433
/************************************************************************/
434
/*                         GetProjectionName()                          */
435
/************************************************************************/
436
437
static CPLString GetProjectionName(const char *pszProjection)
438
0
{
439
0
    if (!pszProjection)
440
0
        return "(null)";
441
442
0
    OGRSpatialReference oSRS;
443
0
    oSRS.SetFromUserInput(pszProjection);
444
0
    const char *pszRet = nullptr;
445
0
    if (oSRS.IsProjected())
446
0
        pszRet = oSRS.GetAttrValue("PROJCS");
447
0
    else if (oSRS.IsGeographic())
448
0
        pszRet = oSRS.GetAttrValue("GEOGCS");
449
0
    return pszRet ? pszRet : "(null)";
450
0
}
451
452
/************************************************************************/
453
/*                           AnalyseRaster()                            */
454
/************************************************************************/
455
456
static void checkNoDataValues(const std::vector<BandProperty> &asProperties)
457
0
{
458
0
    for (const auto &oProps : asProperties)
459
0
    {
460
0
        if (oProps.bHasNoData && GDALDataTypeIsInteger(oProps.dataType) &&
461
0
            !GDALIsValueExactAs(oProps.noDataValue, oProps.dataType))
462
0
        {
463
0
            CPLError(CE_Warning, CPLE_NotSupported,
464
0
                     "Band data type of %s cannot represent the specified "
465
0
                     "NoData value of %g",
466
0
                     GDALGetDataTypeName(oProps.dataType), oProps.noDataValue);
467
0
        }
468
0
    }
469
0
}
470
471
std::string VRTBuilder::AnalyseRaster(GDALDatasetH hDS,
472
                                      DatasetProperty *psDatasetProperties)
473
0
{
474
0
    GDALDataset *poDS = GDALDataset::FromHandle(hDS);
475
0
    const char *dsFileName = poDS->GetDescription();
476
0
    CSLConstList papszMetadata = poDS->GetMetadata("SUBDATASETS");
477
0
    if (CSLCount(papszMetadata) > 0 && poDS->GetRasterCount() == 0)
478
0
    {
479
0
        ppszInputFilenames = static_cast<char **>(CPLRealloc(
480
0
            ppszInputFilenames,
481
0
            sizeof(char *) * (nInputFiles + CSLCount(papszMetadata))));
482
0
        if (nSubdataset < 0)
483
0
        {
484
0
            int count = 1;
485
0
            char subdatasetNameKey[80];
486
0
            snprintf(subdatasetNameKey, sizeof(subdatasetNameKey),
487
0
                     "SUBDATASET_%d_NAME", count);
488
0
            while (*papszMetadata != nullptr)
489
0
            {
490
0
                if (EQUALN(*papszMetadata, subdatasetNameKey,
491
0
                           strlen(subdatasetNameKey)))
492
0
                {
493
0
                    asDatasetProperties.resize(nInputFiles + 1);
494
0
                    ppszInputFilenames[nInputFiles] = CPLStrdup(
495
0
                        *papszMetadata + strlen(subdatasetNameKey) + 1);
496
0
                    nInputFiles++;
497
0
                    count++;
498
0
                    snprintf(subdatasetNameKey, sizeof(subdatasetNameKey),
499
0
                             "SUBDATASET_%d_NAME", count);
500
0
                }
501
0
                papszMetadata++;
502
0
            }
503
0
        }
504
0
        else
505
0
        {
506
0
            char subdatasetNameKey[80];
507
0
            const char *pszSubdatasetName;
508
509
0
            snprintf(subdatasetNameKey, sizeof(subdatasetNameKey),
510
0
                     "SUBDATASET_%d_NAME", nSubdataset);
511
0
            pszSubdatasetName =
512
0
                CSLFetchNameValue(papszMetadata, subdatasetNameKey);
513
0
            if (pszSubdatasetName)
514
0
            {
515
0
                asDatasetProperties.resize(nInputFiles + 1);
516
0
                ppszInputFilenames[nInputFiles] = CPLStrdup(pszSubdatasetName);
517
0
                nInputFiles++;
518
0
            }
519
0
        }
520
0
        return "SILENTLY_IGNORE";
521
0
    }
522
523
0
    const char *proj = poDS->GetProjectionRef();
524
0
    auto &gt = psDatasetProperties->gt;
525
0
    int bGotGeoTransform = poDS->GetGeoTransform(gt) == CE_None;
526
0
    if (bSeparate)
527
0
    {
528
0
        std::string osProgramName(m_osProgramName);
529
0
        if (osProgramName == "gdalbuildvrt")
530
0
            osProgramName += " -separate";
531
532
0
        if (bFirst)
533
0
        {
534
0
            bHasGeoTransform = bGotGeoTransform;
535
0
            if (!bHasGeoTransform)
536
0
            {
537
0
                if (bUserExtent)
538
0
                {
539
0
                    CPLError(CE_Warning, CPLE_NotSupported, "%s",
540
0
                             ("User extent ignored by " + osProgramName +
541
0
                              "with ungeoreferenced images.")
542
0
                                 .c_str());
543
0
                }
544
0
                if (resolutionStrategy == USER_RESOLUTION)
545
0
                {
546
0
                    CPLError(CE_Warning, CPLE_NotSupported, "%s",
547
0
                             ("User resolution ignored by " + osProgramName +
548
0
                              " with ungeoreferenced images.")
549
0
                                 .c_str());
550
0
                }
551
0
            }
552
0
        }
553
0
        else if (bHasGeoTransform != bGotGeoTransform)
554
0
        {
555
0
            return osProgramName + " cannot stack ungeoreferenced and "
556
0
                                   "georeferenced images.";
557
0
        }
558
0
        else if (!bHasGeoTransform && (nRasterXSize != poDS->GetRasterXSize() ||
559
0
                                       nRasterYSize != poDS->GetRasterYSize()))
560
0
        {
561
0
            return osProgramName + " cannot stack ungeoreferenced images "
562
0
                                   "that have not the same dimensions.";
563
0
        }
564
0
    }
565
0
    else
566
0
    {
567
0
        if (!bGotGeoTransform)
568
0
        {
569
0
            return m_osProgramName + " does not support ungeoreferenced image.";
570
0
        }
571
0
        bHasGeoTransform = TRUE;
572
0
    }
573
574
0
    if (bGotGeoTransform)
575
0
    {
576
0
        if (gt[GEOTRSFRM_ROTATION_PARAM1] != 0 ||
577
0
            gt[GEOTRSFRM_ROTATION_PARAM2] != 0)
578
0
        {
579
0
            return m_osProgramName +
580
0
                   " does not support rotated geo transforms.";
581
0
        }
582
0
        if (gt[GEOTRSFRM_NS_RES] >= 0)
583
0
        {
584
0
            return m_osProgramName +
585
0
                   " does not support positive NS resolution.";
586
0
        }
587
0
    }
588
589
0
    psDatasetProperties->nRasterXSize = poDS->GetRasterXSize();
590
0
    psDatasetProperties->nRasterYSize = poDS->GetRasterYSize();
591
0
    if (bFirst && bSeparate && !bGotGeoTransform)
592
0
    {
593
0
        nRasterXSize = poDS->GetRasterXSize();
594
0
        nRasterYSize = poDS->GetRasterYSize();
595
0
    }
596
597
0
    double ds_minX = gt[GEOTRSFRM_TOPLEFT_X];
598
0
    double ds_maxY = gt[GEOTRSFRM_TOPLEFT_Y];
599
0
    double ds_maxX = ds_minX + GDALGetRasterXSize(hDS) * gt[GEOTRSFRM_WE_RES];
600
0
    double ds_minY = ds_maxY + GDALGetRasterYSize(hDS) * gt[GEOTRSFRM_NS_RES];
601
602
0
    int _nBands = GDALGetRasterCount(hDS);
603
0
    if (_nBands == 0)
604
0
    {
605
0
        return "Dataset has no bands";
606
0
    }
607
0
    if (bNoDataFromMask &&
608
0
        poDS->GetRasterBand(_nBands)->GetColorInterpretation() == GCI_AlphaBand)
609
0
        _nBands--;
610
611
0
    GDALRasterBand *poFirstBand = poDS->GetRasterBand(1);
612
0
    poFirstBand->GetBlockSize(&psDatasetProperties->nBlockXSize,
613
0
                              &psDatasetProperties->nBlockYSize);
614
615
    /* For the -separate case */
616
0
    psDatasetProperties->aeBandType.resize(_nBands);
617
0
    psDatasetProperties->aosDescriptions.resize(_nBands);
618
619
0
    psDatasetProperties->adfNoDataValues.resize(_nBands);
620
0
    psDatasetProperties->abHasNoData.resize(_nBands);
621
622
0
    psDatasetProperties->adfOffset.resize(_nBands);
623
0
    psDatasetProperties->abHasOffset.resize(_nBands);
624
625
0
    psDatasetProperties->adfScale.resize(_nBands);
626
0
    psDatasetProperties->abHasScale.resize(_nBands);
627
628
0
    psDatasetProperties->abHasMaskBand.resize(_nBands);
629
630
0
    psDatasetProperties->bHasDatasetMask =
631
0
        poFirstBand->GetMaskFlags() == GMF_PER_DATASET;
632
0
    if (psDatasetProperties->bHasDatasetMask)
633
0
        bHasDatasetMask = TRUE;
634
0
    poFirstBand->GetMaskBand()->GetBlockSize(
635
0
        &psDatasetProperties->nMaskBlockXSize,
636
0
        &psDatasetProperties->nMaskBlockYSize);
637
638
0
    psDatasetProperties->bLastBandIsAlpha = false;
639
0
    if (poDS->GetRasterBand(_nBands)->GetColorInterpretation() == GCI_AlphaBand)
640
0
        psDatasetProperties->bLastBandIsAlpha = true;
641
642
    // Collect overview factors. We only handle power-of-two situations for now
643
0
    const int nOverviews = poFirstBand->GetOverviewCount();
644
0
    int nExpectedOvFactor = 2;
645
0
    for (int j = 0; j < nOverviews; j++)
646
0
    {
647
0
        GDALRasterBand *poOverview = poFirstBand->GetOverview(j);
648
0
        if (!poOverview)
649
0
            continue;
650
0
        if (poOverview->GetXSize() < 128 && poOverview->GetYSize() < 128)
651
0
        {
652
0
            break;
653
0
        }
654
655
0
        const int nOvFactor = GDALComputeOvFactor(
656
0
            poOverview->GetXSize(), poFirstBand->GetXSize(),
657
0
            poOverview->GetYSize(), poFirstBand->GetYSize());
658
659
0
        if (nOvFactor != nExpectedOvFactor)
660
0
            break;
661
662
0
        psDatasetProperties->anOverviewFactors.push_back(nOvFactor);
663
0
        nExpectedOvFactor *= 2;
664
0
    }
665
666
0
    for (int j = 0; j < _nBands; j++)
667
0
    {
668
0
        GDALRasterBand *poBand = poDS->GetRasterBand(j + 1);
669
670
0
        psDatasetProperties->aeBandType[j] = poBand->GetRasterDataType();
671
672
        // Only used by separate mode
673
0
        if (bSeparate)
674
0
        {
675
0
            psDatasetProperties->aosDescriptions[j] = poBand->GetDescription();
676
            // Add metadata items
677
0
            CSLConstList papszMD(poBand->GetMetadata());
678
0
            for (const auto &[pszKey, pszValue] :
679
0
                 cpl::IterateNameValue(papszMD))
680
0
            {
681
0
                psDatasetProperties->mapBandMetadata[j][pszKey] = pszValue;
682
0
            }
683
0
        }
684
685
0
        if (!bSeparate && nSrcNoDataCount > 0)
686
0
        {
687
0
            psDatasetProperties->abHasNoData[j] = true;
688
0
            if (j < nSrcNoDataCount)
689
0
                psDatasetProperties->adfNoDataValues[j] = padfSrcNoData[j];
690
0
            else
691
0
                psDatasetProperties->adfNoDataValues[j] =
692
0
                    padfSrcNoData[nSrcNoDataCount - 1];
693
0
        }
694
0
        else
695
0
        {
696
0
            int bHasNoData = false;
697
0
            psDatasetProperties->adfNoDataValues[j] =
698
0
                poBand->GetNoDataValue(&bHasNoData);
699
0
            psDatasetProperties->abHasNoData[j] = bHasNoData != 0;
700
0
        }
701
702
0
        int bHasOffset = false;
703
0
        psDatasetProperties->adfOffset[j] = poBand->GetOffset(&bHasOffset);
704
0
        psDatasetProperties->abHasOffset[j] =
705
0
            bHasOffset != 0 && psDatasetProperties->adfOffset[j] != 0.0;
706
707
0
        int bHasScale = false;
708
0
        psDatasetProperties->adfScale[j] = poBand->GetScale(&bHasScale);
709
0
        psDatasetProperties->abHasScale[j] =
710
0
            bHasScale != 0 && psDatasetProperties->adfScale[j] != 1.0;
711
712
0
        const int nMaskFlags = poBand->GetMaskFlags();
713
0
        psDatasetProperties->abHasMaskBand[j] =
714
0
            (nMaskFlags != GMF_ALL_VALID && nMaskFlags != GMF_NODATA) ||
715
0
            poBand->GetColorInterpretation() == GCI_AlphaBand;
716
0
    }
717
718
0
    if (bSeparate)
719
0
    {
720
0
        for (int j = 0; j < nSelectedBands; j++)
721
0
        {
722
0
            if (panSelectedBandList[j] > _nBands)
723
0
            {
724
0
                return CPLSPrintf("%s has %d bands, but %d is requested",
725
0
                                  dsFileName, _nBands, panSelectedBandList[j]);
726
0
            }
727
0
        }
728
0
    }
729
730
0
    if (bFirst)
731
0
    {
732
0
        nTotalBands = _nBands;
733
0
        if (bAddAlpha && psDatasetProperties->bLastBandIsAlpha)
734
0
        {
735
0
            bLastBandIsAlpha = true;
736
0
            nTotalBands--;
737
0
        }
738
739
0
        if (proj)
740
0
            pszProjectionRef = CPLStrdup(proj);
741
0
        if (!bUserExtent)
742
0
        {
743
0
            minX = ds_minX;
744
0
            minY = ds_minY;
745
0
            maxX = ds_maxX;
746
0
            maxY = ds_maxY;
747
0
        }
748
749
0
        if (!bSeparate)
750
0
        {
751
            // if not provided an explicit band list, take the one of the first
752
            // dataset
753
0
            if (nSelectedBands == 0)
754
0
            {
755
0
                nSelectedBands = nTotalBands;
756
0
                CPLFree(panSelectedBandList);
757
0
                panSelectedBandList =
758
0
                    static_cast<int *>(CPLMalloc(nSelectedBands * sizeof(int)));
759
0
                for (int j = 0; j < nSelectedBands; j++)
760
0
                {
761
0
                    panSelectedBandList[j] = j + 1;
762
0
                }
763
0
            }
764
0
            for (int j = 0; j < nSelectedBands; j++)
765
0
            {
766
0
                nMaxSelectedBandNo =
767
0
                    std::max(nMaxSelectedBandNo, panSelectedBandList[j]);
768
0
            }
769
770
0
            asBandProperties.resize(nSelectedBands);
771
0
            for (int j = 0; j < nSelectedBands; j++)
772
0
            {
773
0
                const int nSelBand = panSelectedBandList[j];
774
0
                if (nSelBand <= 0 || nSelBand > nTotalBands)
775
0
                {
776
0
                    return CPLSPrintf("Invalid band number: %d", nSelBand);
777
0
                }
778
0
                GDALRasterBand *poBand = poDS->GetRasterBand(nSelBand);
779
0
                asBandProperties[j].colorInterpretation =
780
0
                    poBand->GetColorInterpretation();
781
0
                asBandProperties[j].dataType = poBand->GetRasterDataType();
782
0
                asBandProperties[j].osDescription = poBand->GetDescription();
783
                // Add metadata items
784
0
                const CSLConstList aosMD(poBand->GetMetadata());
785
0
                for (const auto &[pszKey, pszValue] :
786
0
                     cpl::IterateNameValue(aosMD))
787
0
                {
788
0
                    asBandProperties[j].mapBandMetadata[pszKey] = pszValue;
789
0
                }
790
791
0
                if (asBandProperties[j].colorInterpretation == GCI_PaletteIndex)
792
0
                {
793
0
                    auto colorTable = poBand->GetColorTable();
794
0
                    if (colorTable)
795
0
                    {
796
0
                        asBandProperties[j].colorTable.reset(
797
0
                            colorTable->Clone());
798
0
                    }
799
0
                }
800
0
                else
801
0
                    asBandProperties[j].colorTable = nullptr;
802
803
0
                if (nVRTNoDataCount > 0)
804
0
                {
805
0
                    asBandProperties[j].bHasNoData = true;
806
0
                    if (j < nVRTNoDataCount)
807
0
                        asBandProperties[j].noDataValue = padfVRTNoData[j];
808
0
                    else
809
0
                        asBandProperties[j].noDataValue =
810
0
                            padfVRTNoData[nVRTNoDataCount - 1];
811
0
                }
812
0
                else
813
0
                {
814
0
                    int bHasNoData = false;
815
0
                    asBandProperties[j].noDataValue =
816
0
                        poBand->GetNoDataValue(&bHasNoData);
817
0
                    asBandProperties[j].bHasNoData = bHasNoData != 0;
818
0
                }
819
820
0
                int bHasOffset = false;
821
0
                asBandProperties[j].dfOffset = poBand->GetOffset(&bHasOffset);
822
0
                asBandProperties[j].bHasOffset =
823
0
                    bHasOffset != 0 && asBandProperties[j].dfOffset != 0.0;
824
825
0
                int bHasScale = false;
826
0
                asBandProperties[j].dfScale = poBand->GetScale(&bHasScale);
827
0
                asBandProperties[j].bHasScale =
828
0
                    bHasScale != 0 && asBandProperties[j].dfScale != 1.0;
829
0
            }
830
0
        }
831
0
    }
832
0
    else
833
0
    {
834
0
        if ((proj != nullptr && pszProjectionRef == nullptr) ||
835
0
            (proj == nullptr && pszProjectionRef != nullptr) ||
836
0
            (proj != nullptr && pszProjectionRef != nullptr &&
837
0
             ProjAreEqual(proj, pszProjectionRef) == FALSE))
838
0
        {
839
0
            if (!bAllowProjectionDifference)
840
0
            {
841
0
                CPLString osExpected = GetProjectionName(pszProjectionRef);
842
0
                CPLString osGot = GetProjectionName(proj);
843
0
                return m_osProgramName +
844
0
                       CPLSPrintf(" does not support heterogeneous "
845
0
                                  "projection: expected %s, got %s.",
846
0
                                  osExpected.c_str(), osGot.c_str());
847
0
            }
848
0
        }
849
0
        if (!bSeparate)
850
0
        {
851
0
            if (!bExplicitBandList && _nBands != nTotalBands)
852
0
            {
853
0
                if (bAddAlpha && _nBands == nTotalBands + 1 &&
854
0
                    psDatasetProperties->bLastBandIsAlpha)
855
0
                {
856
0
                    bLastBandIsAlpha = true;
857
0
                }
858
0
                else
859
0
                {
860
0
                    return m_osProgramName +
861
0
                           CPLSPrintf(" does not support heterogeneous band "
862
0
                                      "numbers: expected %d, got %d.",
863
0
                                      nTotalBands, _nBands);
864
0
                }
865
0
            }
866
0
            else if (bExplicitBandList && _nBands < nMaxSelectedBandNo)
867
0
            {
868
0
                return m_osProgramName +
869
0
                       CPLSPrintf(" does not support heterogeneous band "
870
0
                                  "numbers: expected at least %d, got %d.",
871
0
                                  nMaxSelectedBandNo, _nBands);
872
0
            }
873
874
0
            for (int j = 0; j < nSelectedBands; j++)
875
0
            {
876
0
                const int nSelBand = panSelectedBandList[j];
877
0
                CPLAssert(nSelBand >= 1 && nSelBand <= _nBands);
878
0
                GDALRasterBand *poBand = poDS->GetRasterBand(nSelBand);
879
                // In normal mode we only preserve description if identical across
880
0
                if (asBandProperties[j].osDescription !=
881
0
                    poBand->GetDescription())
882
0
                {
883
0
                    asBandProperties[j].osDescription = "";
884
0
                }
885
                // same for metadata
886
0
                const CPLStringList aosMD(poBand->GetMetadata());
887
0
                std::vector<std::string> keysToErase;
888
0
                for (const auto &[pszKey, pszValue] :
889
0
                     cpl::IterateNameValue(aosMD))
890
0
                {
891
0
                    const auto &existingValue =
892
0
                        asBandProperties[j].mapBandMetadata[pszKey];
893
0
                    if (existingValue.empty() ||
894
0
                        !EQUAL(existingValue.c_str(), pszValue))
895
0
                    {
896
0
                        keysToErase.push_back(pszKey);
897
0
                    }
898
0
                }
899
                // Also expand keysToErase to those that are not in the current band
900
0
                for (const auto &pair : asBandProperties[j].mapBandMetadata)
901
0
                {
902
0
                    if (aosMD.FetchNameValue(pair.first.c_str()) == nullptr)
903
0
                    {
904
0
                        keysToErase.push_back(pair.first);
905
0
                    }
906
0
                }
907
908
0
                for (const auto &key : keysToErase)
909
0
                {
910
0
                    asBandProperties[j].mapBandMetadata.erase(key);
911
0
                }
912
913
0
                if (asBandProperties[j].colorInterpretation !=
914
0
                    poBand->GetColorInterpretation())
915
0
                {
916
0
                    return m_osProgramName +
917
0
                           CPLSPrintf(
918
0
                               " does not support heterogeneous "
919
0
                               "band color interpretation: expected %s, got "
920
0
                               "%s.",
921
0
                               GDALGetColorInterpretationName(
922
0
                                   asBandProperties[j].colorInterpretation),
923
0
                               GDALGetColorInterpretationName(
924
0
                                   poBand->GetColorInterpretation()));
925
0
                }
926
0
                if (asBandProperties[j].dataType != poBand->GetRasterDataType())
927
0
                {
928
0
                    return m_osProgramName +
929
0
                           CPLSPrintf(" does not support heterogeneous "
930
0
                                      "band data type: expected %s, got %s.",
931
0
                                      GDALGetDataTypeName(
932
0
                                          asBandProperties[j].dataType),
933
0
                                      GDALGetDataTypeName(
934
0
                                          poBand->GetRasterDataType()));
935
0
                }
936
0
                if (asBandProperties[j].colorTable)
937
0
                {
938
0
                    const GDALColorTable *colorTable = poBand->GetColorTable();
939
0
                    int nRefColorEntryCount =
940
0
                        asBandProperties[j].colorTable->GetColorEntryCount();
941
0
                    if (colorTable == nullptr ||
942
0
                        (colorTable->GetColorEntryCount() !=
943
0
                             nRefColorEntryCount &&
944
                         // For NITF CADRG tiles that may have 256 or 257 colors,
945
                         // with the 257th color when present being transparent
946
0
                         !(std::min(colorTable->GetColorEntryCount(),
947
0
                                    nRefColorEntryCount) +
948
0
                               1 ==
949
0
                           std::max(colorTable->GetColorEntryCount(),
950
0
                                    nRefColorEntryCount))))
951
952
0
                    {
953
0
                        return m_osProgramName +
954
0
                               " does not support rasters with "
955
0
                               "different color tables (different number of "
956
0
                               "color table entries)";
957
0
                    }
958
959
                    /* Check that the palette are the same too */
960
                    /* We just warn and still process the file. It is not a
961
                     * technical no-go, but the user */
962
                    /* should check that the end result is OK for him. */
963
0
                    for (int i = 0;
964
0
                         i < std::min(nRefColorEntryCount,
965
0
                                      colorTable->GetColorEntryCount());
966
0
                         i++)
967
0
                    {
968
0
                        const GDALColorEntry *psEntry =
969
0
                            colorTable->GetColorEntry(i);
970
0
                        const GDALColorEntry *psEntryRef =
971
0
                            asBandProperties[j].colorTable->GetColorEntry(i);
972
0
                        if (psEntry->c1 != psEntryRef->c1 ||
973
0
                            psEntry->c2 != psEntryRef->c2 ||
974
0
                            psEntry->c3 != psEntryRef->c3 ||
975
0
                            psEntry->c4 != psEntryRef->c4)
976
0
                        {
977
0
                            static int bFirstWarningPCT = TRUE;
978
0
                            if (bFirstWarningPCT)
979
0
                                CPLError(
980
0
                                    CE_Warning, CPLE_NotSupported,
981
0
                                    "%s has different values than the first "
982
0
                                    "raster for some entries in the color "
983
0
                                    "table.\n"
984
0
                                    "The end result might produce weird "
985
0
                                    "colors.\n"
986
0
                                    "You're advised to pre-process your "
987
0
                                    "rasters with other tools, such as "
988
0
                                    "pct2rgb.py or gdal_translate -expand RGB\n"
989
0
                                    "to operate %s on RGB rasters "
990
0
                                    "instead",
991
0
                                    dsFileName, m_osProgramName.c_str());
992
0
                            else
993
0
                                CPLError(CE_Warning, CPLE_NotSupported,
994
0
                                         "%s has different values than the "
995
0
                                         "first raster for some entries in the "
996
0
                                         "color table.",
997
0
                                         dsFileName);
998
0
                            bFirstWarningPCT = FALSE;
999
0
                            break;
1000
0
                        }
1001
0
                    }
1002
1003
                    // For NITF CADRG tiles that may have 256 or 257 colors,
1004
                    // with the 257th color when present being transparent
1005
0
                    if (nRefColorEntryCount + 1 ==
1006
0
                            colorTable->GetColorEntryCount() &&
1007
0
                        colorTable->GetColorEntry(nRefColorEntryCount)->c1 ==
1008
0
                            0 &&
1009
0
                        colorTable->GetColorEntry(nRefColorEntryCount)->c2 ==
1010
0
                            0 &&
1011
0
                        colorTable->GetColorEntry(nRefColorEntryCount)->c3 ==
1012
0
                            0 &&
1013
0
                        colorTable->GetColorEntry(nRefColorEntryCount)->c4 == 0)
1014
0
                    {
1015
0
                        int bHasNoData = false;
1016
0
                        const double dfNoData =
1017
0
                            poBand->GetNoDataValue(&bHasNoData);
1018
0
                        if (bHasNoData && !asBandProperties[j].bHasNoData &&
1019
0
                            dfNoData == nRefColorEntryCount)
1020
0
                        {
1021
0
                            asBandProperties[j].bHasNoData = true;
1022
0
                            asBandProperties[j].noDataValue = dfNoData;
1023
0
                        }
1024
1025
0
                        asBandProperties[j].colorTable.reset(
1026
0
                            colorTable->Clone());
1027
0
                    }
1028
0
                }
1029
1030
0
                if (psDatasetProperties->abHasOffset[j] !=
1031
0
                        asBandProperties[j].bHasOffset ||
1032
0
                    (asBandProperties[j].bHasOffset &&
1033
0
                     psDatasetProperties->adfOffset[j] !=
1034
0
                         asBandProperties[j].dfOffset))
1035
0
                {
1036
0
                    return m_osProgramName +
1037
0
                           CPLSPrintf(
1038
0
                               " does not support heterogeneous "
1039
0
                               "band offset: expected (%d,%f), got (%d,%f).",
1040
0
                               static_cast<int>(asBandProperties[j].bHasOffset),
1041
0
                               asBandProperties[j].dfOffset,
1042
0
                               static_cast<int>(
1043
0
                                   psDatasetProperties->abHasOffset[j]),
1044
0
                               psDatasetProperties->adfOffset[j]);
1045
0
                }
1046
1047
0
                if (psDatasetProperties->abHasScale[j] !=
1048
0
                        asBandProperties[j].bHasScale ||
1049
0
                    (asBandProperties[j].bHasScale &&
1050
0
                     psDatasetProperties->adfScale[j] !=
1051
0
                         asBandProperties[j].dfScale))
1052
0
                {
1053
0
                    return m_osProgramName +
1054
0
                           CPLSPrintf(
1055
0
                               " does not support heterogeneous "
1056
0
                               "band scale: expected (%d,%f), got (%d,%f).",
1057
0
                               static_cast<int>(asBandProperties[j].bHasScale),
1058
0
                               asBandProperties[j].dfScale,
1059
0
                               static_cast<int>(
1060
0
                                   psDatasetProperties->abHasScale[j]),
1061
0
                               psDatasetProperties->adfScale[j]);
1062
0
                }
1063
0
            }
1064
0
        }
1065
0
        if (!bUserExtent)
1066
0
        {
1067
0
            if (ds_minX < minX)
1068
0
                minX = ds_minX;
1069
0
            if (ds_minY < minY)
1070
0
                minY = ds_minY;
1071
0
            if (ds_maxX > maxX)
1072
0
                maxX = ds_maxX;
1073
0
            if (ds_maxY > maxY)
1074
0
                maxY = ds_maxY;
1075
0
        }
1076
0
    }
1077
1078
0
    if (resolutionStrategy == AVERAGE_RESOLUTION)
1079
0
    {
1080
0
        ++nCountValid;
1081
0
        {
1082
0
            const double dfDelta = gt[GEOTRSFRM_WE_RES] - we_res;
1083
0
            we_res += dfDelta / nCountValid;
1084
0
        }
1085
0
        {
1086
0
            const double dfDelta = gt[GEOTRSFRM_NS_RES] - ns_res;
1087
0
            ns_res += dfDelta / nCountValid;
1088
0
        }
1089
0
    }
1090
0
    else if (resolutionStrategy == SAME_RESOLUTION)
1091
0
    {
1092
0
        if (bFirst)
1093
0
        {
1094
0
            we_res = gt[GEOTRSFRM_WE_RES];
1095
0
            ns_res = gt[GEOTRSFRM_NS_RES];
1096
0
        }
1097
0
        else if (we_res != gt[GEOTRSFRM_WE_RES] ||
1098
0
                 ns_res != gt[GEOTRSFRM_NS_RES])
1099
0
        {
1100
0
            return CPLSPrintf(
1101
0
                "Dataset %s has resolution %.17g x %.17g, whereas "
1102
0
                "previous sources have resolution %.17g x %.17g. To mosaic "
1103
0
                "these data sources, a different resolution strategy should be "
1104
0
                "specified.",
1105
0
                dsFileName, gt[GEOTRSFRM_WE_RES], gt[GEOTRSFRM_NS_RES], we_res,
1106
0
                ns_res);
1107
0
        }
1108
0
    }
1109
0
    else if (resolutionStrategy != USER_RESOLUTION)
1110
0
    {
1111
0
        if (bFirst)
1112
0
        {
1113
0
            we_res = gt[GEOTRSFRM_WE_RES];
1114
0
            ns_res = gt[GEOTRSFRM_NS_RES];
1115
0
        }
1116
0
        else if (resolutionStrategy == HIGHEST_RESOLUTION)
1117
0
        {
1118
0
            we_res = std::min(we_res, gt[GEOTRSFRM_WE_RES]);
1119
            // ns_res is negative, the highest resolution is the max value.
1120
0
            ns_res = std::max(ns_res, gt[GEOTRSFRM_NS_RES]);
1121
0
        }
1122
0
        else if (resolutionStrategy == COMMON_RESOLUTION)
1123
0
        {
1124
0
            we_res = CPLGreatestCommonDivisor(we_res, gt[GEOTRSFRM_WE_RES]);
1125
0
            if (we_res == 0)
1126
0
            {
1127
0
                return "Failed to get common resolution";
1128
0
            }
1129
0
            ns_res = CPLGreatestCommonDivisor(ns_res, gt[GEOTRSFRM_NS_RES]);
1130
0
            if (ns_res == 0)
1131
0
            {
1132
0
                return "Failed to get common resolution";
1133
0
            }
1134
0
        }
1135
0
        else
1136
0
        {
1137
0
            we_res = std::max(we_res, gt[GEOTRSFRM_WE_RES]);
1138
            // ns_res is negative, the lowest resolution is the min value.
1139
0
            ns_res = std::min(ns_res, gt[GEOTRSFRM_NS_RES]);
1140
0
        }
1141
0
    }
1142
1143
0
    checkNoDataValues(asBandProperties);
1144
1145
0
    return "";
1146
0
}
1147
1148
/************************************************************************/
1149
/*                         WriteAbsolutePath()                          */
1150
/************************************************************************/
1151
1152
static void WriteAbsolutePath(VRTSimpleSource *poSource, const char *dsFileName)
1153
0
{
1154
0
    if (dsFileName[0])
1155
0
    {
1156
0
        if (CPLIsFilenameRelative(dsFileName))
1157
0
        {
1158
0
            VSIStatBufL sStat;
1159
0
            if (VSIStatL(dsFileName, &sStat) == 0)
1160
0
            {
1161
0
                if (char *pszCurDir = CPLGetCurrentDir())
1162
0
                {
1163
0
                    poSource->SetSourceDatasetName(
1164
0
                        CPLFormFilenameSafe(pszCurDir, dsFileName, nullptr)
1165
0
                            .c_str(),
1166
0
                        false);
1167
0
                    CPLFree(pszCurDir);
1168
0
                }
1169
0
            }
1170
0
        }
1171
0
        else
1172
0
        {
1173
0
            poSource->SetSourceDatasetName(dsFileName, false);
1174
0
        }
1175
0
    }
1176
0
}
1177
1178
/************************************************************************/
1179
/*                         CreateVRTSeparate()                          */
1180
/************************************************************************/
1181
1182
void VRTBuilder::CreateVRTSeparate(VRTDataset *poVRTDS)
1183
0
{
1184
0
    int iBand = 1;
1185
0
    for (int i = 0; ppszInputFilenames != nullptr && i < nInputFiles; i++)
1186
0
    {
1187
0
        DatasetProperty *psDatasetProperties = &asDatasetProperties[i];
1188
1189
0
        if (psDatasetProperties->isFileOK == FALSE)
1190
0
            continue;
1191
1192
0
        const char *dsFileName = ppszInputFilenames[i];
1193
1194
0
        double dfSrcXOff, dfSrcYOff, dfSrcXSize, dfSrcYSize, dfDstXOff,
1195
0
            dfDstYOff, dfDstXSize, dfDstYSize;
1196
0
        if (bHasGeoTransform)
1197
0
        {
1198
0
            if (!GetSrcDstWin(psDatasetProperties, we_res, ns_res, minX, minY,
1199
0
                              maxX, maxY, nRasterXSize, nRasterYSize,
1200
0
                              &dfSrcXOff, &dfSrcYOff, &dfSrcXSize, &dfSrcYSize,
1201
0
                              &dfDstXOff, &dfDstYOff, &dfDstXSize, &dfDstYSize))
1202
0
            {
1203
0
                CPLDebug("BuildVRT",
1204
0
                         "Skipping %s as not intersecting area of interest",
1205
0
                         dsFileName);
1206
0
                continue;
1207
0
            }
1208
0
        }
1209
0
        else
1210
0
        {
1211
0
            dfSrcXOff = dfSrcYOff = dfDstXOff = dfDstYOff = 0;
1212
0
            dfSrcXSize = dfDstXSize = nRasterXSize;
1213
0
            dfSrcYSize = dfDstYSize = nRasterYSize;
1214
0
        }
1215
1216
0
        GDALDatasetH hSourceDS;
1217
0
        bool bDropRef = false;
1218
0
        if (nSrcDSCount == nInputFiles &&
1219
0
            GDALGetDatasetDriver(pahSrcDS[i]) != nullptr &&
1220
0
            (dsFileName[0] == '\0' ||  // could be a unnamed VRT file
1221
0
             EQUAL(GDALGetDescription(GDALGetDatasetDriver(pahSrcDS[i])),
1222
0
                   "MEM")))
1223
0
        {
1224
0
            hSourceDS = pahSrcDS[i];
1225
0
        }
1226
0
        else
1227
0
        {
1228
0
            bDropRef = true;
1229
0
            GDALProxyPoolDatasetH hProxyDS = GDALProxyPoolDatasetCreate(
1230
0
                dsFileName, psDatasetProperties->nRasterXSize,
1231
0
                psDatasetProperties->nRasterYSize, GA_ReadOnly, TRUE,
1232
0
                pszProjectionRef, psDatasetProperties->gt.data());
1233
0
            hSourceDS = static_cast<GDALDatasetH>(hProxyDS);
1234
0
            cpl::down_cast<GDALProxyPoolDataset *>(
1235
0
                GDALDataset::FromHandle(hProxyDS))
1236
0
                ->SetOpenOptions(papszOpenOptions);
1237
1238
0
            for (int jBand = 0;
1239
0
                 jBand <
1240
0
                 static_cast<int>(psDatasetProperties->aeBandType.size());
1241
0
                 ++jBand)
1242
0
            {
1243
0
                GDALProxyPoolDatasetAddSrcBandDescription(
1244
0
                    hProxyDS, psDatasetProperties->aeBandType[jBand],
1245
0
                    psDatasetProperties->nBlockXSize,
1246
0
                    psDatasetProperties->nBlockYSize);
1247
0
            }
1248
0
        }
1249
1250
0
        const int nBandsToIter =
1251
0
            nSelectedBands > 0
1252
0
                ? nSelectedBands
1253
0
                : static_cast<int>(psDatasetProperties->aeBandType.size());
1254
0
        for (int iBandToIter = 0; iBandToIter < nBandsToIter; ++iBandToIter)
1255
0
        {
1256
            // 0-based
1257
0
            const int nSrcBandIdx = nSelectedBands > 0
1258
0
                                        ? panSelectedBandList[iBandToIter] - 1
1259
0
                                        : iBandToIter;
1260
0
            assert(nSrcBandIdx >= 0);
1261
0
            poVRTDS->AddBand(psDatasetProperties->aeBandType[nSrcBandIdx],
1262
0
                             nullptr);
1263
1264
0
            VRTSourcedRasterBand *poVRTBand =
1265
0
                static_cast<VRTSourcedRasterBand *>(
1266
0
                    poVRTDS->GetRasterBand(iBand));
1267
1268
0
            poVRTBand->SetDescription(
1269
0
                psDatasetProperties->aosDescriptions[nSrcBandIdx].c_str());
1270
0
            if (!psDatasetProperties->mapBandMetadata[nSrcBandIdx].empty())
1271
0
            {
1272
0
                for (const auto &[key, value] :
1273
0
                     psDatasetProperties->mapBandMetadata[nSrcBandIdx])
1274
0
                {
1275
0
                    poVRTBand->SetMetadataItem(key.c_str(), value.c_str());
1276
0
                }
1277
0
            }
1278
1279
0
            if (bHideNoData)
1280
0
                poVRTBand->SetMetadataItem("HideNoDataValue", "1", nullptr);
1281
1282
0
            if (bAllowVRTNoData)
1283
0
            {
1284
0
                if (nVRTNoDataCount > 0)
1285
0
                {
1286
0
                    if (iBand - 1 < nVRTNoDataCount)
1287
0
                        poVRTBand->SetNoDataValue(padfVRTNoData[iBand - 1]);
1288
0
                    else
1289
0
                        poVRTBand->SetNoDataValue(
1290
0
                            padfVRTNoData[nVRTNoDataCount - 1]);
1291
0
                }
1292
0
                else if (psDatasetProperties->abHasNoData[nSrcBandIdx])
1293
0
                {
1294
0
                    poVRTBand->SetNoDataValue(
1295
0
                        psDatasetProperties->adfNoDataValues[nSrcBandIdx]);
1296
0
                }
1297
0
            }
1298
1299
0
            VRTSimpleSource *poSimpleSource;
1300
0
            if (bAllowSrcNoData &&
1301
0
                (nSrcNoDataCount > 0 ||
1302
0
                 psDatasetProperties->abHasNoData[nSrcBandIdx]))
1303
0
            {
1304
0
                auto poComplexSource = new VRTComplexSource();
1305
0
                poSimpleSource = poComplexSource;
1306
0
                if (nSrcNoDataCount > 0)
1307
0
                {
1308
0
                    if (iBand - 1 < nSrcNoDataCount)
1309
0
                        poComplexSource->SetNoDataValue(
1310
0
                            padfSrcNoData[iBand - 1]);
1311
0
                    else
1312
0
                        poComplexSource->SetNoDataValue(
1313
0
                            padfSrcNoData[nSrcNoDataCount - 1]);
1314
0
                }
1315
0
                else /* if (psDatasetProperties->abHasNoData[nSrcBandIdx]) */
1316
0
                {
1317
0
                    poComplexSource->SetNoDataValue(
1318
0
                        psDatasetProperties->adfNoDataValues[nSrcBandIdx]);
1319
0
                }
1320
0
            }
1321
0
            else if (bUseSrcMaskBand &&
1322
0
                     psDatasetProperties->abHasMaskBand[nSrcBandIdx])
1323
0
            {
1324
0
                auto poSource = new VRTComplexSource();
1325
0
                poSource->SetUseMaskBand(true);
1326
0
                poSimpleSource = poSource;
1327
0
            }
1328
0
            else
1329
0
                poSimpleSource = new VRTSimpleSource();
1330
1331
0
            if (pszResampling)
1332
0
                poSimpleSource->SetResampling(pszResampling);
1333
0
            poVRTBand->ConfigureSource(
1334
0
                poSimpleSource,
1335
0
                static_cast<GDALRasterBand *>(
1336
0
                    GDALGetRasterBand(hSourceDS, nSrcBandIdx + 1)),
1337
0
                FALSE, dfSrcXOff, dfSrcYOff, dfSrcXSize, dfSrcYSize, dfDstXOff,
1338
0
                dfDstYOff, dfDstXSize, dfDstYSize);
1339
1340
0
            if (bWriteAbsolutePath)
1341
0
                WriteAbsolutePath(poSimpleSource, dsFileName);
1342
1343
0
            if (psDatasetProperties->abHasOffset[nSrcBandIdx])
1344
0
                poVRTBand->SetOffset(
1345
0
                    psDatasetProperties->adfOffset[nSrcBandIdx]);
1346
1347
0
            if (psDatasetProperties->abHasScale[nSrcBandIdx])
1348
0
                poVRTBand->SetScale(psDatasetProperties->adfScale[nSrcBandIdx]);
1349
1350
0
            poVRTBand->AddSource(poSimpleSource);
1351
1352
0
            iBand++;
1353
0
        }
1354
1355
0
        if (bDropRef)
1356
0
        {
1357
0
            GDALDereferenceDataset(hSourceDS);
1358
0
        }
1359
0
    }
1360
0
}
1361
1362
/************************************************************************/
1363
/*                        CreateVRTNonSeparate()                        */
1364
/************************************************************************/
1365
1366
void VRTBuilder::CreateVRTNonSeparate(VRTDataset *poVRTDS)
1367
0
{
1368
0
    CPLStringList aosOptions;
1369
1370
0
    if (!osPixelFunction.empty())
1371
0
    {
1372
0
        aosOptions.AddNameValue("subclass", "VRTDerivedRasterBand");
1373
0
        aosOptions.AddNameValue("PixelFunctionType", osPixelFunction.c_str());
1374
0
        aosOptions.AddNameValue("SkipNonContributingSources", "1");
1375
0
        CPLString osName;
1376
0
        for (const auto &[pszKey, pszValue] :
1377
0
             cpl::IterateNameValue(aosPixelFunctionArgs))
1378
0
        {
1379
0
            osName.Printf("_PIXELFN_ARG_%s", pszKey);
1380
0
            aosOptions.AddNameValue(osName.c_str(), pszValue);
1381
0
        }
1382
0
    }
1383
1384
0
    for (int j = 0; j < nSelectedBands; j++)
1385
0
    {
1386
0
        const char *pszSourceTransferType = "Float64";
1387
0
        if (osPixelFunction == "mean" || osPixelFunction == "min" ||
1388
0
            osPixelFunction == "max")
1389
0
        {
1390
0
            pszSourceTransferType =
1391
0
                GDALGetDataTypeName(asBandProperties[j].dataType);
1392
0
        }
1393
0
        aosOptions.AddNameValue("SourceTransferType", pszSourceTransferType);
1394
1395
0
        poVRTDS->AddBand(asBandProperties[j].dataType, aosOptions.List());
1396
0
        GDALRasterBand *poBand = poVRTDS->GetRasterBand(j + 1);
1397
0
        poBand->SetColorInterpretation(asBandProperties[j].colorInterpretation);
1398
0
        poBand->SetDescription(asBandProperties[j].osDescription.c_str());
1399
0
        if (!asBandProperties[j].mapBandMetadata.empty())
1400
0
        {
1401
0
            for (const auto &[key, value] : asBandProperties[j].mapBandMetadata)
1402
0
            {
1403
0
                poBand->SetMetadataItem(key.c_str(), value.c_str());
1404
0
            }
1405
0
        }
1406
0
        if (asBandProperties[j].colorInterpretation == GCI_PaletteIndex)
1407
0
        {
1408
0
            poBand->SetColorTable(asBandProperties[j].colorTable.get());
1409
0
        }
1410
0
        if (bAllowVRTNoData && asBandProperties[j].bHasNoData)
1411
0
            poBand->SetNoDataValue(asBandProperties[j].noDataValue);
1412
0
        if (bHideNoData)
1413
0
            poBand->SetMetadataItem("HideNoDataValue", "1");
1414
1415
0
        if (asBandProperties[j].bHasOffset)
1416
0
            poBand->SetOffset(asBandProperties[j].dfOffset);
1417
1418
0
        if (asBandProperties[j].bHasScale)
1419
0
            poBand->SetScale(asBandProperties[j].dfScale);
1420
0
    }
1421
1422
0
    VRTSourcedRasterBand *poMaskVRTBand = nullptr;
1423
0
    if (bAddAlpha)
1424
0
    {
1425
0
        poVRTDS->AddBand(GDT_UInt8);
1426
0
        GDALRasterBand *poBand = poVRTDS->GetRasterBand(nSelectedBands + 1);
1427
0
        poBand->SetColorInterpretation(GCI_AlphaBand);
1428
0
    }
1429
0
    else if (bHasDatasetMask)
1430
0
    {
1431
0
        poVRTDS->CreateMaskBand(GMF_PER_DATASET);
1432
0
        poMaskVRTBand = static_cast<VRTSourcedRasterBand *>(
1433
0
            poVRTDS->GetRasterBand(1)->GetMaskBand());
1434
0
    }
1435
1436
0
    bool bCanCollectOverviewFactors = true;
1437
0
    std::set<int> anOverviewFactorsSet;
1438
0
    std::vector<int> anIdxValidDatasets;
1439
1440
0
    for (int i = 0; ppszInputFilenames != nullptr && i < nInputFiles; i++)
1441
0
    {
1442
0
        DatasetProperty *psDatasetProperties = &asDatasetProperties[i];
1443
1444
0
        if (psDatasetProperties->isFileOK == FALSE)
1445
0
            continue;
1446
1447
0
        const char *dsFileName = ppszInputFilenames[i];
1448
1449
0
        double dfSrcXOff;
1450
0
        double dfSrcYOff;
1451
0
        double dfSrcXSize;
1452
0
        double dfSrcYSize;
1453
0
        double dfDstXOff;
1454
0
        double dfDstYOff;
1455
0
        double dfDstXSize;
1456
0
        double dfDstYSize;
1457
0
        if (!GetSrcDstWin(psDatasetProperties, we_res, ns_res, minX, minY, maxX,
1458
0
                          maxY, nRasterXSize, nRasterYSize, &dfSrcXOff,
1459
0
                          &dfSrcYOff, &dfSrcXSize, &dfSrcYSize, &dfDstXOff,
1460
0
                          &dfDstYOff, &dfDstXSize, &dfDstYSize))
1461
0
        {
1462
0
            CPLDebug("BuildVRT",
1463
0
                     "Skipping %s as not intersecting area of interest",
1464
0
                     dsFileName);
1465
0
            continue;
1466
0
        }
1467
1468
0
        anIdxValidDatasets.push_back(i);
1469
1470
0
        if (bCanCollectOverviewFactors)
1471
0
        {
1472
0
            if (std::abs(psDatasetProperties->gt.xscale - we_res) >
1473
0
                    1e-8 * std::abs(we_res) ||
1474
0
                std::abs(psDatasetProperties->gt.yscale - ns_res) >
1475
0
                    1e-8 * std::abs(ns_res))
1476
0
            {
1477
0
                bCanCollectOverviewFactors = false;
1478
0
                anOverviewFactorsSet.clear();
1479
0
            }
1480
0
        }
1481
0
        if (bCanCollectOverviewFactors)
1482
0
        {
1483
0
            for (int nOvFactor : psDatasetProperties->anOverviewFactors)
1484
0
                anOverviewFactorsSet.insert(nOvFactor);
1485
0
        }
1486
1487
0
        GDALDatasetH hSourceDS;
1488
0
        bool bDropRef = false;
1489
1490
0
        if (nSrcDSCount == nInputFiles &&
1491
0
            GDALGetDatasetDriver(pahSrcDS[i]) != nullptr &&
1492
0
            (dsFileName[0] == '\0' ||  // could be a unnamed VRT file
1493
0
             EQUAL(GDALGetDescription(GDALGetDatasetDriver(pahSrcDS[i])),
1494
0
                   "MEM")))
1495
0
        {
1496
0
            hSourceDS = pahSrcDS[i];
1497
0
        }
1498
0
        else
1499
0
        {
1500
0
            bDropRef = true;
1501
0
            GDALProxyPoolDatasetH hProxyDS = GDALProxyPoolDatasetCreate(
1502
0
                dsFileName, psDatasetProperties->nRasterXSize,
1503
0
                psDatasetProperties->nRasterYSize, GA_ReadOnly, TRUE,
1504
0
                pszProjectionRef, psDatasetProperties->gt.data());
1505
0
            cpl::down_cast<GDALProxyPoolDataset *>(
1506
0
                GDALDataset::FromHandle(hProxyDS))
1507
0
                ->SetOpenOptions(papszOpenOptions);
1508
1509
0
            for (int j = 0;
1510
0
                 j < nMaxSelectedBandNo +
1511
0
                         (bAddAlpha && psDatasetProperties->bLastBandIsAlpha
1512
0
                              ? 1
1513
0
                              : 0);
1514
0
                 j++)
1515
0
            {
1516
0
                GDALProxyPoolDatasetAddSrcBandDescription(
1517
0
                    hProxyDS,
1518
0
                    j < static_cast<int>(asBandProperties.size())
1519
0
                        ? asBandProperties[j].dataType
1520
0
                        : GDT_UInt8,
1521
0
                    psDatasetProperties->nBlockXSize,
1522
0
                    psDatasetProperties->nBlockYSize);
1523
0
            }
1524
0
            if (bHasDatasetMask && !bAddAlpha)
1525
0
            {
1526
0
                static_cast<GDALProxyPoolRasterBand *>(
1527
0
                    cpl::down_cast<GDALProxyPoolDataset *>(
1528
0
                        GDALDataset::FromHandle(hProxyDS))
1529
0
                        ->GetRasterBand(1))
1530
0
                    ->AddSrcMaskBandDescription(
1531
0
                        GDT_UInt8, psDatasetProperties->nMaskBlockXSize,
1532
0
                        psDatasetProperties->nMaskBlockYSize);
1533
0
            }
1534
1535
0
            hSourceDS = static_cast<GDALDatasetH>(hProxyDS);
1536
0
        }
1537
1538
0
        for (int j = 0;
1539
0
             j <
1540
0
             nSelectedBands +
1541
0
                 (bAddAlpha && psDatasetProperties->bLastBandIsAlpha ? 1 : 0);
1542
0
             j++)
1543
0
        {
1544
0
            VRTSourcedRasterBandH hVRTBand = static_cast<VRTSourcedRasterBandH>(
1545
0
                poVRTDS->GetRasterBand(j + 1));
1546
0
            const int nSelBand = j == nSelectedBands ? nSelectedBands + 1
1547
0
                                                     : panSelectedBandList[j];
1548
1549
            /* Place the raster band at the right position in the VRT */
1550
0
            VRTSourcedRasterBand *poVRTBand =
1551
0
                static_cast<VRTSourcedRasterBand *>(hVRTBand);
1552
1553
0
            VRTSimpleSource *poSimpleSource;
1554
0
            if (bNoDataFromMask)
1555
0
            {
1556
0
                auto poNoDataFromMaskSource = new VRTNoDataFromMaskSource();
1557
0
                poSimpleSource = poNoDataFromMaskSource;
1558
0
                poNoDataFromMaskSource->SetParameters(
1559
0
                    (nVRTNoDataCount > 0)
1560
0
                        ? ((j < nVRTNoDataCount)
1561
0
                               ? padfVRTNoData[j]
1562
0
                               : padfVRTNoData[nVRTNoDataCount - 1])
1563
0
                        : 0,
1564
0
                    dfMaskValueThreshold);
1565
0
            }
1566
0
            else if (bAllowSrcNoData &&
1567
0
                     psDatasetProperties->abHasNoData[nSelBand - 1])
1568
0
            {
1569
0
                auto poComplexSource = new VRTComplexSource();
1570
0
                poSimpleSource = poComplexSource;
1571
0
                poComplexSource->SetNoDataValue(
1572
0
                    psDatasetProperties->adfNoDataValues[nSelBand - 1]);
1573
0
            }
1574
0
            else if (bUseSrcMaskBand &&
1575
0
                     psDatasetProperties->abHasMaskBand[nSelBand - 1])
1576
0
            {
1577
0
                auto poSource = new VRTComplexSource();
1578
0
                poSource->SetUseMaskBand(true);
1579
0
                poSimpleSource = poSource;
1580
0
            }
1581
0
            else
1582
0
                poSimpleSource = new VRTSimpleSource();
1583
0
            if (pszResampling)
1584
0
                poSimpleSource->SetResampling(pszResampling);
1585
0
            auto poSrcBand = GDALRasterBand::FromHandle(
1586
0
                GDALGetRasterBand(hSourceDS, nSelBand));
1587
0
            poVRTBand->ConfigureSource(poSimpleSource, poSrcBand, FALSE,
1588
0
                                       dfSrcXOff, dfSrcYOff, dfSrcXSize,
1589
0
                                       dfSrcYSize, dfDstXOff, dfDstYOff,
1590
0
                                       dfDstXSize, dfDstYSize);
1591
1592
0
            if (bWriteAbsolutePath)
1593
0
                WriteAbsolutePath(poSimpleSource, dsFileName);
1594
1595
0
            poVRTBand->AddSource(poSimpleSource);
1596
0
        }
1597
1598
0
        if (bAddAlpha && !psDatasetProperties->bLastBandIsAlpha)
1599
0
        {
1600
0
            VRTSourcedRasterBand *poVRTBand =
1601
0
                static_cast<VRTSourcedRasterBand *>(
1602
0
                    poVRTDS->GetRasterBand(nSelectedBands + 1));
1603
0
            if (psDatasetProperties->bHasDatasetMask && bUseSrcMaskBand)
1604
0
            {
1605
0
                auto poComplexSource = new VRTComplexSource();
1606
0
                poComplexSource->SetUseMaskBand(true);
1607
0
                poVRTBand->ConfigureSource(
1608
0
                    poComplexSource,
1609
0
                    GDALRasterBand::FromHandle(GDALGetRasterBand(hSourceDS, 1)),
1610
0
                    TRUE, dfSrcXOff, dfSrcYOff, dfSrcXSize, dfSrcYSize,
1611
0
                    dfDstXOff, dfDstYOff, dfDstXSize, dfDstYSize);
1612
1613
0
                if (bWriteAbsolutePath)
1614
0
                    WriteAbsolutePath(poComplexSource, dsFileName);
1615
1616
0
                poVRTBand->AddSource(poComplexSource);
1617
0
            }
1618
0
            else
1619
0
            {
1620
                /* Little trick : we use an offset of 255 and a scaling of 0, so
1621
                 * that in areas covered */
1622
                /* by the source, the value of the alpha band will be 255, otherwise
1623
                 * it will be 0 */
1624
0
                poVRTBand->AddComplexSource(
1625
0
                    GDALRasterBand::FromHandle(GDALGetRasterBand(hSourceDS, 1)),
1626
0
                    dfSrcXOff, dfSrcYOff, dfSrcXSize, dfSrcYSize, dfDstXOff,
1627
0
                    dfDstYOff, dfDstXSize, dfDstYSize, 255, 0,
1628
0
                    VRT_NODATA_UNSET);
1629
0
            }
1630
0
        }
1631
0
        else if (bHasDatasetMask)
1632
0
        {
1633
0
            VRTSimpleSource *poSource;
1634
0
            if (bUseSrcMaskBand)
1635
0
            {
1636
0
                auto poComplexSource = new VRTComplexSource();
1637
0
                poComplexSource->SetUseMaskBand(true);
1638
0
                poSource = poComplexSource;
1639
0
            }
1640
0
            else
1641
0
            {
1642
0
                poSource = new VRTSimpleSource();
1643
0
            }
1644
0
            if (pszResampling)
1645
0
                poSource->SetResampling(pszResampling);
1646
0
            assert(poMaskVRTBand);
1647
0
            poMaskVRTBand->ConfigureSource(
1648
0
                poSource,
1649
0
                GDALRasterBand::FromHandle(GDALGetRasterBand(hSourceDS, 1)),
1650
0
                TRUE, dfSrcXOff, dfSrcYOff, dfSrcXSize, dfSrcYSize, dfDstXOff,
1651
0
                dfDstYOff, dfDstXSize, dfDstYSize);
1652
1653
0
            if (bWriteAbsolutePath)
1654
0
                WriteAbsolutePath(poSource, dsFileName);
1655
1656
0
            poMaskVRTBand->AddSource(poSource);
1657
0
        }
1658
1659
0
        if (bDropRef)
1660
0
        {
1661
0
            GDALDereferenceDataset(hSourceDS);
1662
0
        }
1663
0
    }
1664
1665
0
    for (int i : anIdxValidDatasets)
1666
0
    {
1667
0
        const DatasetProperty *psDatasetProperties = &asDatasetProperties[i];
1668
0
        for (auto oIter = anOverviewFactorsSet.begin();
1669
0
             oIter != anOverviewFactorsSet.end();)
1670
0
        {
1671
0
            const int nGlobalOvrFactor = *oIter;
1672
0
            auto oIterNext = oIter;
1673
0
            ++oIterNext;
1674
1675
0
            if (psDatasetProperties->nRasterXSize / nGlobalOvrFactor < 128 &&
1676
0
                psDatasetProperties->nRasterYSize / nGlobalOvrFactor < 128)
1677
0
            {
1678
0
                break;
1679
0
            }
1680
0
            if (std::find(psDatasetProperties->anOverviewFactors.begin(),
1681
0
                          psDatasetProperties->anOverviewFactors.end(),
1682
0
                          nGlobalOvrFactor) ==
1683
0
                psDatasetProperties->anOverviewFactors.end())
1684
0
            {
1685
0
                anOverviewFactorsSet.erase(oIter);
1686
0
            }
1687
1688
0
            oIter = oIterNext;
1689
0
        }
1690
0
    }
1691
0
    if (!anOverviewFactorsSet.empty() &&
1692
0
        CPLTestBool(CPLGetConfigOption("VRT_VIRTUAL_OVERVIEWS", "YES")))
1693
0
    {
1694
0
        std::vector<int> anOverviewFactors;
1695
0
        anOverviewFactors.insert(anOverviewFactors.end(),
1696
0
                                 anOverviewFactorsSet.begin(),
1697
0
                                 anOverviewFactorsSet.end());
1698
0
        const char *const apszOptions[] = {"VRT_VIRTUAL_OVERVIEWS=YES",
1699
0
                                           nullptr};
1700
0
        poVRTDS->BuildOverviews(pszResampling ? pszResampling : "nearest",
1701
0
                                static_cast<int>(anOverviewFactors.size()),
1702
0
                                &anOverviewFactors[0], 0, nullptr, nullptr,
1703
0
                                nullptr, apszOptions);
1704
0
    }
1705
0
}
1706
1707
/************************************************************************/
1708
/*                               Build()                                */
1709
/************************************************************************/
1710
1711
std::unique_ptr<GDALDataset> VRTBuilder::Build(GDALProgressFunc pfnProgress,
1712
                                               void *pProgressData)
1713
0
{
1714
0
    if (bHasRunBuild)
1715
0
        return nullptr;
1716
0
    bHasRunBuild = TRUE;
1717
1718
0
    if (pfnProgress == nullptr)
1719
0
        pfnProgress = GDALDummyProgress;
1720
1721
0
    bUserExtent = (minX != 0 || minY != 0 || maxX != 0 || maxY != 0);
1722
0
    if (bUserExtent)
1723
0
    {
1724
0
        if (minX >= maxX || minY >= maxY)
1725
0
        {
1726
0
            CPLError(CE_Failure, CPLE_IllegalArg, "Invalid user extent");
1727
0
            return nullptr;
1728
0
        }
1729
0
    }
1730
1731
0
    if (resolutionStrategy == USER_RESOLUTION)
1732
0
    {
1733
0
        if (we_res <= 0 || ns_res <= 0)
1734
0
        {
1735
0
            CPLError(CE_Failure, CPLE_IllegalArg, "Invalid user resolution");
1736
0
            return nullptr;
1737
0
        }
1738
1739
        /* We work with negative north-south resolution in all the following
1740
         * code */
1741
0
        ns_res = -ns_res;
1742
0
    }
1743
0
    else
1744
0
    {
1745
0
        we_res = ns_res = 0;
1746
0
    }
1747
1748
0
    asDatasetProperties.resize(nInputFiles);
1749
1750
0
    if (pszSrcNoData != nullptr)
1751
0
    {
1752
0
        if (EQUAL(pszSrcNoData, "none"))
1753
0
        {
1754
0
            bAllowSrcNoData = FALSE;
1755
0
        }
1756
0
        else
1757
0
        {
1758
0
            char **papszTokens = CSLTokenizeString(pszSrcNoData);
1759
0
            nSrcNoDataCount = CSLCount(papszTokens);
1760
0
            padfSrcNoData = static_cast<double *>(
1761
0
                CPLMalloc(sizeof(double) * nSrcNoDataCount));
1762
0
            for (int i = 0; i < nSrcNoDataCount; i++)
1763
0
            {
1764
0
                if (!ArgIsNumeric(papszTokens[i]) &&
1765
0
                    !EQUAL(papszTokens[i], "nan") &&
1766
0
                    !EQUAL(papszTokens[i], "-inf") &&
1767
0
                    !EQUAL(papszTokens[i], "inf"))
1768
0
                {
1769
0
                    CPLError(CE_Failure, CPLE_IllegalArg,
1770
0
                             "Invalid -srcnodata value");
1771
0
                    CSLDestroy(papszTokens);
1772
0
                    return nullptr;
1773
0
                }
1774
0
                padfSrcNoData[i] = CPLAtofM(papszTokens[i]);
1775
0
            }
1776
0
            CSLDestroy(papszTokens);
1777
0
        }
1778
0
    }
1779
1780
0
    if (pszVRTNoData != nullptr)
1781
0
    {
1782
0
        if (EQUAL(pszVRTNoData, "none"))
1783
0
        {
1784
0
            bAllowVRTNoData = FALSE;
1785
0
        }
1786
0
        else
1787
0
        {
1788
0
            char **papszTokens = CSLTokenizeString(pszVRTNoData);
1789
0
            nVRTNoDataCount = CSLCount(papszTokens);
1790
0
            padfVRTNoData = static_cast<double *>(
1791
0
                CPLMalloc(sizeof(double) * nVRTNoDataCount));
1792
0
            for (int i = 0; i < nVRTNoDataCount; i++)
1793
0
            {
1794
0
                if (!ArgIsNumeric(papszTokens[i]) &&
1795
0
                    !EQUAL(papszTokens[i], "nan") &&
1796
0
                    !EQUAL(papszTokens[i], "-inf") &&
1797
0
                    !EQUAL(papszTokens[i], "inf"))
1798
0
                {
1799
0
                    CPLError(CE_Failure, CPLE_IllegalArg,
1800
0
                             "Invalid -vrtnodata value");
1801
0
                    CSLDestroy(papszTokens);
1802
0
                    return nullptr;
1803
0
                }
1804
0
                padfVRTNoData[i] = CPLAtofM(papszTokens[i]);
1805
0
            }
1806
0
            CSLDestroy(papszTokens);
1807
0
        }
1808
0
    }
1809
1810
0
    bool bFoundValid = false;
1811
0
    for (int i = 0; ppszInputFilenames != nullptr && i < nInputFiles; i++)
1812
0
    {
1813
0
        const char *dsFileName = ppszInputFilenames[i];
1814
1815
0
        if (!pfnProgress(1.0 * (i + 1) / nInputFiles, nullptr, pProgressData))
1816
0
        {
1817
0
            return nullptr;
1818
0
        }
1819
1820
0
        GDALDatasetH hDS = (pahSrcDS)
1821
0
                               ? pahSrcDS[i]
1822
0
                               : GDALOpenEx(dsFileName, GDAL_OF_RASTER, nullptr,
1823
0
                                            papszOpenOptions, nullptr);
1824
0
        asDatasetProperties[i].isFileOK = FALSE;
1825
1826
0
        if (hDS)
1827
0
        {
1828
0
            const auto osErrorMsg = AnalyseRaster(hDS, &asDatasetProperties[i]);
1829
0
            if (osErrorMsg.empty())
1830
0
            {
1831
0
                asDatasetProperties[i].isFileOK = TRUE;
1832
0
                bFoundValid = true;
1833
0
                bFirst = FALSE;
1834
0
            }
1835
0
            if (pahSrcDS == nullptr)
1836
0
                GDALClose(hDS);
1837
0
            if (!osErrorMsg.empty() && osErrorMsg != "SILENTLY_IGNORE")
1838
0
            {
1839
0
                if (bStrict)
1840
0
                {
1841
0
                    CPLError(CE_Failure, CPLE_AppDefined, "%s",
1842
0
                             osErrorMsg.c_str());
1843
0
                    return nullptr;
1844
0
                }
1845
0
                else
1846
0
                {
1847
0
                    CPLError(CE_Warning, CPLE_AppDefined, "%s Skipping %s",
1848
0
                             osErrorMsg.c_str(), dsFileName);
1849
0
                }
1850
0
            }
1851
0
        }
1852
0
        else
1853
0
        {
1854
0
            if (bStrict)
1855
0
            {
1856
0
                CPLError(CE_Failure, CPLE_AppDefined, "Can't open %s.",
1857
0
                         dsFileName);
1858
0
                return nullptr;
1859
0
            }
1860
0
            else
1861
0
            {
1862
0
                CPLError(CE_Warning, CPLE_AppDefined,
1863
0
                         "Can't open %s. Skipping it", dsFileName);
1864
0
            }
1865
0
        }
1866
0
    }
1867
1868
0
    if (!bFoundValid)
1869
0
        return nullptr;
1870
1871
0
    if (bHasGeoTransform)
1872
0
    {
1873
0
        if (bTargetAlignedPixels)
1874
0
        {
1875
0
            minX = floor(minX / we_res) * we_res;
1876
0
            maxX = ceil(maxX / we_res) * we_res;
1877
0
            minY = floor(minY / -ns_res) * -ns_res;
1878
0
            maxY = ceil(maxY / -ns_res) * -ns_res;
1879
0
        }
1880
1881
0
        nRasterXSize = static_cast<int>(0.5 + (maxX - minX) / we_res);
1882
0
        nRasterYSize = static_cast<int>(0.5 + (maxY - minY) / -ns_res);
1883
0
    }
1884
1885
0
    if (nRasterXSize == 0 || nRasterYSize == 0)
1886
0
    {
1887
0
        CPLError(CE_Failure, CPLE_AppDefined,
1888
0
                 "Computed VRT dimension is invalid. You've probably "
1889
0
                 "specified inappropriate resolution.");
1890
0
        return nullptr;
1891
0
    }
1892
1893
0
    auto poDS = VRTDataset::CreateVRTDataset(pszOutputFilename, nRasterXSize,
1894
0
                                             nRasterYSize, 0, GDT_Unknown,
1895
0
                                             aosCreateOptions.List());
1896
0
    if (!poDS)
1897
0
    {
1898
0
        return nullptr;
1899
0
    }
1900
1901
0
    if (pszOutputSRS)
1902
0
    {
1903
0
        poDS->SetProjection(pszOutputSRS);
1904
0
    }
1905
0
    else if (pszProjectionRef)
1906
0
    {
1907
0
        poDS->SetProjection(pszProjectionRef);
1908
0
    }
1909
1910
0
    if (bHasGeoTransform)
1911
0
    {
1912
0
        GDALGeoTransform gt;
1913
0
        gt[GEOTRSFRM_TOPLEFT_X] = minX;
1914
0
        gt[GEOTRSFRM_WE_RES] = we_res;
1915
0
        gt[GEOTRSFRM_ROTATION_PARAM1] = 0;
1916
0
        gt[GEOTRSFRM_TOPLEFT_Y] = maxY;
1917
0
        gt[GEOTRSFRM_ROTATION_PARAM2] = 0;
1918
0
        gt[GEOTRSFRM_NS_RES] = ns_res;
1919
0
        poDS->SetGeoTransform(gt);
1920
0
    }
1921
1922
0
    if (bSeparate)
1923
0
    {
1924
0
        CreateVRTSeparate(poDS.get());
1925
0
    }
1926
0
    else
1927
0
    {
1928
0
        CreateVRTNonSeparate(poDS.get());
1929
0
    }
1930
1931
0
    return poDS;
1932
0
}
1933
1934
/************************************************************************/
1935
/*                          add_file_to_list()                          */
1936
/************************************************************************/
1937
1938
static bool add_file_to_list(const char *filename, const char *tile_index,
1939
                             CPLStringList &aosList)
1940
0
{
1941
1942
0
    if (EQUAL(CPLGetExtensionSafe(filename).c_str(), "SHP"))
1943
0
    {
1944
        /* Handle gdaltindex Shapefile as a special case */
1945
0
        auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(filename));
1946
0
        if (poDS == nullptr)
1947
0
        {
1948
0
            CPLError(CE_Failure, CPLE_AppDefined,
1949
0
                     "Unable to open shapefile `%s'.", filename);
1950
0
            return false;
1951
0
        }
1952
1953
0
        auto poLayer = poDS->GetLayer(0);
1954
0
        const auto poFDefn = poLayer->GetLayerDefn();
1955
1956
0
        if (poFDefn->GetFieldIndex("LOCATION") >= 0 &&
1957
0
            strcmp("LOCATION", tile_index) != 0)
1958
0
        {
1959
0
            CPLError(CE_Failure, CPLE_AppDefined,
1960
0
                     "This shapefile seems to be a tile index of "
1961
0
                     "OGR features and not GDAL products.");
1962
0
        }
1963
0
        const int ti_field = poFDefn->GetFieldIndex(tile_index);
1964
0
        if (ti_field < 0)
1965
0
        {
1966
0
            CPLError(CE_Failure, CPLE_AppDefined,
1967
0
                     "Unable to find field `%s' in DBF file `%s'.", tile_index,
1968
0
                     filename);
1969
0
            return false;
1970
0
        }
1971
1972
        /* Load in memory existing file names in SHP */
1973
0
        const auto nTileIndexFiles = poLayer->GetFeatureCount(TRUE);
1974
0
        if (nTileIndexFiles == 0)
1975
0
        {
1976
0
            CPLError(CE_Warning, CPLE_AppDefined,
1977
0
                     "Tile index %s is empty. Skipping it.", filename);
1978
0
            return true;
1979
0
        }
1980
0
        if (nTileIndexFiles > 100 * 1024 * 1024)
1981
0
        {
1982
0
            CPLError(CE_Failure, CPLE_AppDefined,
1983
0
                     "Too large feature count in tile index");
1984
0
            return false;
1985
0
        }
1986
1987
0
        for (auto &&poFeature : poLayer)
1988
0
        {
1989
0
            aosList.AddString(poFeature->GetFieldAsString(ti_field));
1990
0
        }
1991
0
    }
1992
0
    else
1993
0
    {
1994
0
        aosList.AddString(filename);
1995
0
    }
1996
1997
0
    return true;
1998
0
}
1999
2000
/************************************************************************/
2001
/*                         GDALBuildVRTOptions                          */
2002
/************************************************************************/
2003
2004
/** Options for use with GDALBuildVRT(). GDALBuildVRTOptions* must be allocated
2005
 * and freed with GDALBuildVRTOptionsNew() and GDALBuildVRTOptionsFree()
2006
 * respectively.
2007
 */
2008
struct GDALBuildVRTOptions
2009
{
2010
    std::string osProgramName = "gdalbuildvrt";
2011
    std::string osTileIndex = "location";
2012
    bool bStrict = false;
2013
    std::string osResolution{};
2014
    bool bSeparate = false;
2015
    bool bAllowProjectionDifference = false;
2016
    double we_res = 0;
2017
    double ns_res = 0;
2018
    bool bTargetAlignedPixels = false;
2019
    double xmin = 0;
2020
    double ymin = 0;
2021
    double xmax = 0;
2022
    double ymax = 0;
2023
    bool bAddAlpha = false;
2024
    bool bHideNoData = false;
2025
    int nSubdataset = -1;
2026
    std::string osSrcNoData{};
2027
    std::string osVRTNoData{};
2028
    std::string osOutputSRS{};
2029
    std::vector<int> anSelectedBandList{};
2030
    std::string osResampling{};
2031
    CPLStringList aosOpenOptions{};
2032
    CPLStringList aosCreateOptions{};
2033
    bool bUseSrcMaskBand = true;
2034
    bool bNoDataFromMask = false;
2035
    double dfMaskValueThreshold = 0;
2036
    bool bWriteAbsolutePath = false;
2037
    std::string osPixelFunction{};
2038
    CPLStringList aosPixelFunctionArgs{};
2039
2040
    /*! allow or suppress progress monitor and other non-error output */
2041
    bool bQuiet = true;
2042
2043
    /*! the progress function to use */
2044
    GDALProgressFunc pfnProgress = GDALDummyProgress;
2045
2046
    /*! pointer to the progress data variable */
2047
    void *pProgressData = nullptr;
2048
};
2049
2050
/************************************************************************/
2051
/*                            GDALBuildVRT()                            */
2052
/************************************************************************/
2053
2054
/* clang-format off */
2055
/**
2056
 * Build a VRT from a list of datasets.
2057
 *
2058
 * This is the equivalent of the
2059
 * <a href="/programs/gdalbuildvrt.html">gdalbuildvrt</a> utility.
2060
 *
2061
 * GDALBuildVRTOptions* must be allocated and freed with
2062
 * GDALBuildVRTOptionsNew() and GDALBuildVRTOptionsFree() respectively. pahSrcDS
2063
 * and papszSrcDSNames cannot be used at the same time.
2064
 *
2065
 * @param pszDest the destination dataset path.
2066
 * @param nSrcCount the number of input datasets.
2067
 * @param pahSrcDS the list of input datasets (or NULL, exclusive with
2068
 * papszSrcDSNames). For practical purposes, the type
2069
 * of this argument should be considered as "const GDALDatasetH* const*", that
2070
 * is neither the array nor its values are mutated by this function.
2071
 * @param papszSrcDSNames the list of input dataset names (or NULL, exclusive
2072
 * with pahSrcDS)
2073
 * @param psOptionsIn the options struct returned by GDALBuildVRTOptionsNew() or
2074
 * NULL.
2075
 * @param pbUsageError pointer to a integer output variable to store if any
2076
 * usage error has occurred.
2077
 * @return the output dataset (new dataset that must be closed using
2078
 * GDALClose()) or NULL in case of error. If using pahSrcDS, the returned VRT
2079
 * dataset has a reference to each pahSrcDS[] element. Hence pahSrcDS[] elements
2080
 * should be closed after the returned dataset if using GDALClose().
2081
 * A safer alternative is to use GDALReleaseDataset() instead of using
2082
 * GDALClose(), in which case you can close datasets in any order.
2083
2084
 *
2085
 * @since GDAL 2.1
2086
 */
2087
/* clang-format on */
2088
2089
GDALDatasetH GDALBuildVRT(const char *pszDest, int nSrcCount,
2090
                          GDALDatasetH *pahSrcDS,
2091
                          const char *const *papszSrcDSNames,
2092
                          const GDALBuildVRTOptions *psOptionsIn,
2093
                          int *pbUsageError)
2094
0
{
2095
0
    if (pszDest == nullptr)
2096
0
        pszDest = "";
2097
2098
0
    if (nSrcCount == 0)
2099
0
    {
2100
0
        CPLError(CE_Failure, CPLE_AppDefined, "No input dataset specified.");
2101
2102
0
        if (pbUsageError)
2103
0
            *pbUsageError = TRUE;
2104
0
        return nullptr;
2105
0
    }
2106
2107
    // cppcheck-suppress unreadVariable
2108
0
    GDALBuildVRTOptions sOptions(psOptionsIn ? *psOptionsIn
2109
0
                                             : GDALBuildVRTOptions());
2110
2111
0
    if (sOptions.we_res != 0 && sOptions.ns_res != 0 &&
2112
0
        !sOptions.osResolution.empty() &&
2113
0
        !EQUAL(sOptions.osResolution.c_str(), "user"))
2114
0
    {
2115
0
        CPLError(CE_Failure, CPLE_NotSupported,
2116
0
                 "-tr option is not compatible with -resolution %s",
2117
0
                 sOptions.osResolution.c_str());
2118
0
        if (pbUsageError)
2119
0
            *pbUsageError = TRUE;
2120
0
        return nullptr;
2121
0
    }
2122
2123
0
    if (sOptions.bTargetAlignedPixels && sOptions.we_res == 0 &&
2124
0
        sOptions.ns_res == 0)
2125
0
    {
2126
0
        CPLError(CE_Failure, CPLE_NotSupported,
2127
0
                 "-tap option cannot be used without using -tr");
2128
0
        if (pbUsageError)
2129
0
            *pbUsageError = TRUE;
2130
0
        return nullptr;
2131
0
    }
2132
2133
0
    if (sOptions.bAddAlpha && sOptions.bSeparate)
2134
0
    {
2135
0
        CPLError(CE_Failure, CPLE_NotSupported,
2136
0
                 "-addalpha option is not compatible with -separate.");
2137
0
        if (pbUsageError)
2138
0
            *pbUsageError = TRUE;
2139
0
        return nullptr;
2140
0
    }
2141
2142
0
    ResolutionStrategy eStrategy = AVERAGE_RESOLUTION;
2143
0
    if (sOptions.osResolution.empty() ||
2144
0
        EQUAL(sOptions.osResolution.c_str(), "user"))
2145
0
    {
2146
0
        if (sOptions.we_res != 0 || sOptions.ns_res != 0)
2147
0
            eStrategy = USER_RESOLUTION;
2148
0
        else if (EQUAL(sOptions.osResolution.c_str(), "user"))
2149
0
        {
2150
0
            CPLError(CE_Failure, CPLE_NotSupported,
2151
0
                     "-tr option must be used with -resolution user.");
2152
0
            if (pbUsageError)
2153
0
                *pbUsageError = TRUE;
2154
0
            return nullptr;
2155
0
        }
2156
0
    }
2157
0
    else if (EQUAL(sOptions.osResolution.c_str(), "average"))
2158
0
        eStrategy = AVERAGE_RESOLUTION;
2159
0
    else if (EQUAL(sOptions.osResolution.c_str(), "highest"))
2160
0
        eStrategy = HIGHEST_RESOLUTION;
2161
0
    else if (EQUAL(sOptions.osResolution.c_str(), "lowest"))
2162
0
        eStrategy = LOWEST_RESOLUTION;
2163
0
    else if (EQUAL(sOptions.osResolution.c_str(), "same"))
2164
0
        eStrategy = SAME_RESOLUTION;
2165
0
    else if (EQUAL(sOptions.osResolution.c_str(), "common"))
2166
0
        eStrategy = COMMON_RESOLUTION;
2167
2168
    /* If -srcnodata is specified, use it as the -vrtnodata if the latter is not
2169
     */
2170
    /* specified */
2171
0
    if (!sOptions.osSrcNoData.empty() && sOptions.osVRTNoData.empty())
2172
0
        sOptions.osVRTNoData = sOptions.osSrcNoData;
2173
2174
0
    VRTBuilder oBuilder(
2175
0
        sOptions.bStrict, pszDest, nSrcCount, papszSrcDSNames, pahSrcDS,
2176
0
        sOptions.anSelectedBandList.empty()
2177
0
            ? nullptr
2178
0
            : sOptions.anSelectedBandList.data(),
2179
0
        static_cast<int>(sOptions.anSelectedBandList.size()), eStrategy,
2180
0
        sOptions.we_res, sOptions.ns_res, sOptions.bTargetAlignedPixels,
2181
0
        sOptions.xmin, sOptions.ymin, sOptions.xmax, sOptions.ymax,
2182
0
        sOptions.bSeparate, sOptions.bAllowProjectionDifference,
2183
0
        sOptions.bAddAlpha, sOptions.bHideNoData, sOptions.nSubdataset,
2184
0
        sOptions.osSrcNoData.empty() ? nullptr : sOptions.osSrcNoData.c_str(),
2185
0
        sOptions.osVRTNoData.empty() ? nullptr : sOptions.osVRTNoData.c_str(),
2186
0
        sOptions.bUseSrcMaskBand, sOptions.bNoDataFromMask,
2187
0
        sOptions.dfMaskValueThreshold,
2188
0
        sOptions.osOutputSRS.empty() ? nullptr : sOptions.osOutputSRS.c_str(),
2189
0
        sOptions.osResampling.empty() ? nullptr : sOptions.osResampling.c_str(),
2190
0
        sOptions.osPixelFunction.empty() ? nullptr
2191
0
                                         : sOptions.osPixelFunction.c_str(),
2192
0
        sOptions.aosPixelFunctionArgs, sOptions.aosOpenOptions.List(),
2193
0
        sOptions.aosCreateOptions, sOptions.bWriteAbsolutePath);
2194
0
    oBuilder.m_osProgramName = sOptions.osProgramName;
2195
2196
0
    return GDALDataset::ToHandle(
2197
0
        oBuilder.Build(sOptions.pfnProgress, sOptions.pProgressData).release());
2198
0
}
2199
2200
/************************************************************************/
2201
/*                             SanitizeSRS                              */
2202
/************************************************************************/
2203
2204
static char *SanitizeSRS(const char *pszUserInput)
2205
2206
0
{
2207
0
    OGRSpatialReferenceH hSRS;
2208
0
    char *pszResult = nullptr;
2209
2210
0
    CPLErrorReset();
2211
2212
0
    hSRS = OSRNewSpatialReference(nullptr);
2213
0
    if (OSRSetFromUserInput(hSRS, pszUserInput) == OGRERR_NONE)
2214
0
        OSRExportToWkt(hSRS, &pszResult);
2215
0
    else
2216
0
    {
2217
0
        CPLError(CE_Failure, CPLE_AppDefined, "Translating SRS failed:\n%s",
2218
0
                 pszUserInput);
2219
0
    }
2220
2221
0
    OSRDestroySpatialReference(hSRS);
2222
2223
0
    return pszResult;
2224
0
}
2225
2226
/************************************************************************/
2227
/*                    GDALBuildVRTOptionsGetParser()                    */
2228
/************************************************************************/
2229
2230
static std::unique_ptr<GDALArgumentParser>
2231
GDALBuildVRTOptionsGetParser(GDALBuildVRTOptions *psOptions,
2232
                             GDALBuildVRTOptionsForBinary *psOptionsForBinary)
2233
0
{
2234
0
    auto argParser = std::make_unique<GDALArgumentParser>(
2235
0
        "gdalbuildvrt", /* bForBinary=*/psOptionsForBinary != nullptr);
2236
2237
0
    argParser->add_description(_("Builds a VRT from a list of datasets."));
2238
2239
0
    argParser->add_epilog(_(
2240
0
        "\n"
2241
0
        "e.g.\n"
2242
0
        "  % gdalbuildvrt doq_index.vrt doq/*.tif\n"
2243
0
        "  % gdalbuildvrt -input_file_list my_list.txt doq_index.vrt\n"
2244
0
        "\n"
2245
0
        "NOTES:\n"
2246
0
        "  o With -separate, each files goes into a separate band in the VRT "
2247
0
        "band.\n"
2248
0
        "    Otherwise, the files are considered as tiles of a larger mosaic.\n"
2249
0
        "  o -b option selects a band to add into vrt.  Multiple bands can be "
2250
0
        "listed.\n"
2251
0
        "    By default all bands are queried.\n"
2252
0
        "  o The default tile index field is 'location' unless otherwise "
2253
0
        "specified by\n"
2254
0
        "    -tileindex.\n"
2255
0
        "  o In case the resolution of all input files is not the same, the "
2256
0
        "-resolution\n"
2257
0
        "    flag enable the user to control the way the output resolution is "
2258
0
        "computed.\n"
2259
0
        "    Average is the default.\n"
2260
0
        "  o Input files may be any valid GDAL dataset or a GDAL raster tile "
2261
0
        "index.\n"
2262
0
        "  o For a GDAL raster tile index, all entries will be added to the "
2263
0
        "VRT.\n"
2264
0
        "  o If one GDAL dataset is made of several subdatasets and has 0 "
2265
0
        "raster bands,\n"
2266
0
        "    its datasets will be added to the VRT rather than the dataset "
2267
0
        "itself.\n"
2268
0
        "    Single subdataset could be selected by its number using the -sd "
2269
0
        "option.\n"
2270
0
        "  o By default, only datasets of same projection and band "
2271
0
        "characteristics\n"
2272
0
        "    may be added to the VRT.\n"
2273
0
        "\n"
2274
0
        "For more details, consult "
2275
0
        "https://gdal.org/programs/gdalbuildvrt.html"));
2276
2277
0
    argParser->add_quiet_argument(
2278
0
        psOptionsForBinary ? &psOptionsForBinary->bQuiet : nullptr);
2279
2280
0
    {
2281
0
        auto &group = argParser->add_mutually_exclusive_group();
2282
2283
0
        group.add_argument("-strict")
2284
0
            .flag()
2285
0
            .store_into(psOptions->bStrict)
2286
0
            .help(_("Turn warnings as failures."));
2287
2288
0
        group.add_argument("-non_strict")
2289
0
            .flag()
2290
0
            .action([psOptions](const std::string &)
2291
0
                    { psOptions->bStrict = false; })
2292
0
            .help(_("Skip source datasets that have issues with warnings, and "
2293
0
                    "continue processing."));
2294
0
    }
2295
2296
0
    argParser->add_argument("-tile_index")
2297
0
        .metavar("<field_name>")
2298
0
        .store_into(psOptions->osTileIndex)
2299
0
        .help(_("Use the specified value as the tile index field, instead of "
2300
0
                "the default value which is 'location'."));
2301
2302
0
    argParser->add_argument("-resolution")
2303
0
        .metavar("user|average|common|highest|lowest|same")
2304
0
        .action(
2305
0
            [psOptions](const std::string &s)
2306
0
            {
2307
0
                psOptions->osResolution = s;
2308
0
                if (!EQUAL(psOptions->osResolution.c_str(), "user") &&
2309
0
                    !EQUAL(psOptions->osResolution.c_str(), "average") &&
2310
0
                    !EQUAL(psOptions->osResolution.c_str(), "highest") &&
2311
0
                    !EQUAL(psOptions->osResolution.c_str(), "lowest") &&
2312
0
                    !EQUAL(psOptions->osResolution.c_str(), "same") &&
2313
0
                    !EQUAL(psOptions->osResolution.c_str(), "common"))
2314
0
                {
2315
0
                    throw std::invalid_argument(
2316
0
                        CPLSPrintf("Illegal resolution value (%s).",
2317
0
                                   psOptions->osResolution.c_str()));
2318
0
                }
2319
0
            })
2320
0
        .help(_("Control the way the output resolution is computed."));
2321
2322
0
    argParser->add_argument("-tr")
2323
0
        .metavar("<xres> <yres>")
2324
0
        .nargs(2)
2325
0
        .scan<'g', double>()
2326
0
        .help(_("Set target resolution."));
2327
2328
0
    if (psOptionsForBinary)
2329
0
    {
2330
0
        argParser->add_argument("-input_file_list")
2331
0
            .metavar("<filename>")
2332
0
            .action(
2333
0
                [psOptions, psOptionsForBinary](const std::string &s)
2334
0
                {
2335
0
                    const char *input_file_list = s.c_str();
2336
0
                    auto f = VSIVirtualHandleUniquePtr(
2337
0
                        VSIFOpenL(input_file_list, "r"));
2338
0
                    if (f)
2339
0
                    {
2340
0
                        while (1)
2341
0
                        {
2342
0
                            const char *filename = CPLReadLineL(f.get());
2343
0
                            if (filename == nullptr)
2344
0
                                break;
2345
0
                            if (!add_file_to_list(
2346
0
                                    filename, psOptions->osTileIndex.c_str(),
2347
0
                                    psOptionsForBinary->aosSrcFiles))
2348
0
                            {
2349
0
                                throw std::invalid_argument(
2350
0
                                    std::string("Cannot add ")
2351
0
                                        .append(filename)
2352
0
                                        .append(" to input file list"));
2353
0
                            }
2354
0
                        }
2355
0
                    }
2356
0
                })
2357
0
            .help(_("Text file with an input filename on each line"));
2358
0
    }
2359
2360
0
    {
2361
0
        auto &group = argParser->add_mutually_exclusive_group();
2362
2363
0
        group.add_argument("-separate")
2364
0
            .flag()
2365
0
            .store_into(psOptions->bSeparate)
2366
0
            .help(_("Place each input file into a separate band."));
2367
2368
0
        group.add_argument("-pixel-function")
2369
0
            .metavar("<function>")
2370
0
            .action(
2371
0
                [psOptions](const std::string &s)
2372
0
                {
2373
0
                    auto *poPixFun =
2374
0
                        VRTDerivedRasterBand::GetPixelFunction(s.c_str());
2375
0
                    if (poPixFun == nullptr)
2376
0
                    {
2377
0
                        throw std::invalid_argument(
2378
0
                            s + " is not a registered pixel function.");
2379
0
                    }
2380
2381
0
                    psOptions->osPixelFunction = s;
2382
0
                })
2383
2384
0
            .help("Function to calculate value from overlapping inputs");
2385
0
    }
2386
2387
0
    argParser->add_argument("-pixel-function-arg")
2388
0
        .metavar("<NAME>=<VALUE>")
2389
0
        .append()
2390
0
        .action([psOptions](const std::string &s)
2391
0
                { psOptions->aosPixelFunctionArgs.AddString(s); })
2392
0
        .help(_("Pixel function argument(s)"));
2393
2394
0
    argParser->add_argument("-allow_projection_difference")
2395
0
        .flag()
2396
0
        .store_into(psOptions->bAllowProjectionDifference)
2397
0
        .help(_("Accept source files not in the same projection (but without "
2398
0
                "reprojecting them!)."));
2399
2400
0
    argParser->add_argument("-sd")
2401
0
        .metavar("<n>")
2402
0
        .store_into(psOptions->nSubdataset)
2403
0
        .help(_("Use subdataset of specified index (starting at 1), instead of "
2404
0
                "the source dataset itself."));
2405
2406
0
    argParser->add_argument("-tap")
2407
0
        .flag()
2408
0
        .store_into(psOptions->bTargetAlignedPixels)
2409
0
        .help(_("Align the coordinates of the extent of the output file to the "
2410
0
                "values of the resolution."));
2411
2412
0
    argParser->add_argument("-te")
2413
0
        .metavar("<xmin> <ymin> <xmax> <ymax>")
2414
0
        .nargs(4)
2415
0
        .scan<'g', double>()
2416
0
        .help(_("Set georeferenced extents of output file to be created."));
2417
2418
0
    argParser->add_argument("-addalpha")
2419
0
        .flag()
2420
0
        .store_into(psOptions->bAddAlpha)
2421
0
        .help(_("Adds an alpha mask band to the VRT when the source raster "
2422
0
                "have none."));
2423
2424
0
    argParser->add_argument("-b")
2425
0
        .metavar("<band>")
2426
0
        .append()
2427
0
        .store_into(psOptions->anSelectedBandList)
2428
0
        .help(_("Specify input band(s) number."));
2429
2430
0
    argParser->add_argument("-hidenodata")
2431
0
        .flag()
2432
0
        .store_into(psOptions->bHideNoData)
2433
0
        .help(_("Makes the VRT band not report the NoData."));
2434
2435
0
    if (psOptionsForBinary)
2436
0
    {
2437
0
        argParser->add_argument("-overwrite")
2438
0
            .flag()
2439
0
            .store_into(psOptionsForBinary->bOverwrite)
2440
0
            .help(_("Overwrite the VRT if it already exists."));
2441
0
    }
2442
2443
0
    argParser->add_argument("-srcnodata")
2444
0
        .metavar("\"<value>[ <value>]...\"")
2445
0
        .store_into(psOptions->osSrcNoData)
2446
0
        .help(_("Set nodata values for input bands."));
2447
2448
0
    argParser->add_argument("-vrtnodata")
2449
0
        .metavar("\"<value>[ <value>]...\"")
2450
0
        .store_into(psOptions->osVRTNoData)
2451
0
        .help(_("Set nodata values at the VRT band level."));
2452
2453
0
    argParser->add_argument("-a_srs")
2454
0
        .metavar("<srs_def>")
2455
0
        .action(
2456
0
            [psOptions](const std::string &s)
2457
0
            {
2458
0
                char *pszSRS = SanitizeSRS(s.c_str());
2459
0
                if (pszSRS == nullptr)
2460
0
                {
2461
0
                    throw std::invalid_argument("Invalid value for -a_srs");
2462
0
                }
2463
0
                psOptions->osOutputSRS = pszSRS;
2464
0
                CPLFree(pszSRS);
2465
0
            })
2466
0
        .help(_("Override the projection for the output file.."));
2467
2468
0
    argParser->add_argument("-r")
2469
0
        .metavar("nearest|bilinear|cubic|cubicspline|lanczos|average|mode")
2470
0
        .store_into(psOptions->osResampling)
2471
0
        .help(_("Resampling algorithm."));
2472
2473
0
    argParser->add_open_options_argument(&psOptions->aosOpenOptions);
2474
2475
0
    argParser->add_creation_options_argument(psOptions->aosCreateOptions);
2476
2477
0
    argParser->add_argument("-write_absolute_path")
2478
0
        .flag()
2479
0
        .store_into(psOptions->bWriteAbsolutePath)
2480
0
        .help(_("Write the absolute path of the raster files in the tile index "
2481
0
                "file."));
2482
2483
0
    argParser->add_argument("-ignore_srcmaskband")
2484
0
        .flag()
2485
0
        .action([psOptions](const std::string &)
2486
0
                { psOptions->bUseSrcMaskBand = false; })
2487
0
        .help(_("Cause mask band of sources will not be taken into account."));
2488
2489
0
    argParser->add_argument("-nodata_max_mask_threshold")
2490
0
        .metavar("<threshold>")
2491
0
        .scan<'g', double>()
2492
0
        .action(
2493
0
            [psOptions](const std::string &s)
2494
0
            {
2495
0
                psOptions->bNoDataFromMask = true;
2496
0
                psOptions->dfMaskValueThreshold = CPLAtofM(s.c_str());
2497
0
            })
2498
0
        .help(_("Replaces the value of the source with the value of -vrtnodata "
2499
0
                "when the value of the mask band of the source is less or "
2500
0
                "equal to the threshold."));
2501
2502
0
    argParser->add_argument("-program_name")
2503
0
        .store_into(psOptions->osProgramName)
2504
0
        .hidden();
2505
2506
0
    if (psOptionsForBinary)
2507
0
    {
2508
0
        if (psOptionsForBinary->osDstFilename.empty())
2509
0
        {
2510
            // We normally go here, unless undocumented -o switch is used
2511
0
            argParser->add_argument("vrt_dataset_name")
2512
0
                .metavar("<vrt_dataset_name>")
2513
0
                .store_into(psOptionsForBinary->osDstFilename)
2514
0
                .help(_("Output VRT."));
2515
0
        }
2516
2517
0
        argParser->add_argument("src_dataset_name")
2518
0
            .metavar("<src_dataset_name>")
2519
0
            .nargs(argparse::nargs_pattern::any)
2520
0
            .action(
2521
0
                [psOptions, psOptionsForBinary](const std::string &s)
2522
0
                {
2523
0
                    if (!add_file_to_list(s.c_str(),
2524
0
                                          psOptions->osTileIndex.c_str(),
2525
0
                                          psOptionsForBinary->aosSrcFiles))
2526
0
                    {
2527
0
                        throw std::invalid_argument(
2528
0
                            std::string("Cannot add ")
2529
0
                                .append(s)
2530
0
                                .append(" to input file list"));
2531
0
                    }
2532
0
                })
2533
0
            .help(_("Input dataset(s)."));
2534
0
    }
2535
2536
0
    return argParser;
2537
0
}
2538
2539
/************************************************************************/
2540
/*                     GDALBuildVRTGetParserUsage()                     */
2541
/************************************************************************/
2542
2543
std::string GDALBuildVRTGetParserUsage()
2544
0
{
2545
0
    try
2546
0
    {
2547
0
        GDALBuildVRTOptions sOptions;
2548
0
        GDALBuildVRTOptionsForBinary sOptionsForBinary;
2549
0
        auto argParser =
2550
0
            GDALBuildVRTOptionsGetParser(&sOptions, &sOptionsForBinary);
2551
0
        return argParser->usage();
2552
0
    }
2553
0
    catch (const std::exception &err)
2554
0
    {
2555
0
        CPLError(CE_Failure, CPLE_AppDefined, "Unexpected exception: %s",
2556
0
                 err.what());
2557
0
        return std::string();
2558
0
    }
2559
0
}
2560
2561
/************************************************************************/
2562
/*                       GDALBuildVRTOptionsNew()                       */
2563
/************************************************************************/
2564
2565
/**
2566
 * Allocates a GDALBuildVRTOptions struct.
2567
 *
2568
 * @param papszArgv NULL terminated list of options (potentially including
2569
 * filename and open options too), or NULL. The accepted options are the ones of
2570
 * the <a href="/programs/gdalbuildvrt.html">gdalbuildvrt</a> utility.
2571
 * @param psOptionsForBinary (output) may be NULL (and should generally be
2572
 * NULL), otherwise (gdalbuildvrt_bin.cpp use case) must be allocated with
2573
 * GDALBuildVRTOptionsForBinaryNew() prior to this function. Will be filled
2574
 * with potentially present filename, open options,...
2575
 * @return pointer to the allocated GDALBuildVRTOptions struct. Must be freed
2576
 * with GDALBuildVRTOptionsFree().
2577
 *
2578
 * @since GDAL 2.1
2579
 */
2580
2581
GDALBuildVRTOptions *
2582
GDALBuildVRTOptionsNew(char **papszArgv,
2583
                       GDALBuildVRTOptionsForBinary *psOptionsForBinary)
2584
0
{
2585
0
    auto psOptions = std::make_unique<GDALBuildVRTOptions>();
2586
2587
0
    CPLStringList aosArgv;
2588
0
    const int nArgc = CSLCount(papszArgv);
2589
0
    for (int i = 0;
2590
0
         i < nArgc && papszArgv != nullptr && papszArgv[i] != nullptr; i++)
2591
0
    {
2592
0
        if (psOptionsForBinary && EQUAL(papszArgv[i], "-o") && i + 1 < nArgc &&
2593
0
            papszArgv[i + 1] != nullptr)
2594
0
        {
2595
            // Undocumented alternate way of specifying the destination file
2596
0
            psOptionsForBinary->osDstFilename = papszArgv[i + 1];
2597
0
            ++i;
2598
0
        }
2599
        // argparser will be confused if the value of a string argument
2600
        // starts with a negative sign.
2601
0
        else if (EQUAL(papszArgv[i], "-srcnodata") && i + 1 < nArgc)
2602
0
        {
2603
0
            ++i;
2604
0
            psOptions->osSrcNoData = papszArgv[i];
2605
0
        }
2606
        // argparser will be confused if the value of a string argument
2607
        // starts with a negative sign.
2608
0
        else if (EQUAL(papszArgv[i], "-vrtnodata") && i + 1 < nArgc)
2609
0
        {
2610
0
            ++i;
2611
0
            psOptions->osVRTNoData = papszArgv[i];
2612
0
        }
2613
2614
0
        else
2615
0
        {
2616
0
            aosArgv.AddString(papszArgv[i]);
2617
0
        }
2618
0
    }
2619
2620
0
    try
2621
0
    {
2622
0
        auto argParser =
2623
0
            GDALBuildVRTOptionsGetParser(psOptions.get(), psOptionsForBinary);
2624
2625
0
        argParser->parse_args_without_binary_name(aosArgv.List());
2626
2627
0
        if (auto adfTargetRes = argParser->present<std::vector<double>>("-tr"))
2628
0
        {
2629
0
            psOptions->we_res = (*adfTargetRes)[0];
2630
0
            psOptions->ns_res = (*adfTargetRes)[1];
2631
0
        }
2632
2633
0
        if (auto oTE = argParser->present<std::vector<double>>("-te"))
2634
0
        {
2635
0
            psOptions->xmin = (*oTE)[0];
2636
0
            psOptions->ymin = (*oTE)[1];
2637
0
            psOptions->xmax = (*oTE)[2];
2638
0
            psOptions->ymax = (*oTE)[3];
2639
0
        }
2640
2641
0
        if (psOptions->osPixelFunction.empty() &&
2642
0
            !psOptions->aosPixelFunctionArgs.empty())
2643
0
        {
2644
0
            throw std::runtime_error(
2645
0
                "Pixel function arguments provided without a pixel function");
2646
0
        }
2647
2648
0
        return psOptions.release();
2649
0
    }
2650
0
    catch (const std::exception &err)
2651
0
    {
2652
0
        CPLError(CE_Failure, CPLE_AppDefined, "%s", err.what());
2653
0
        return nullptr;
2654
0
    }
2655
0
}
2656
2657
/************************************************************************/
2658
/*                      GDALBuildVRTOptionsFree()                       */
2659
/************************************************************************/
2660
2661
/**
2662
 * Frees the GDALBuildVRTOptions struct.
2663
 *
2664
 * @param psOptions the options struct for GDALBuildVRT().
2665
 *
2666
 * @since GDAL 2.1
2667
 */
2668
2669
void GDALBuildVRTOptionsFree(GDALBuildVRTOptions *psOptions)
2670
0
{
2671
0
    delete psOptions;
2672
0
}
2673
2674
/************************************************************************/
2675
/*                   GDALBuildVRTOptionsSetProgress()                   */
2676
/************************************************************************/
2677
2678
/**
2679
 * Set a progress function.
2680
 *
2681
 * @param psOptions the options struct for GDALBuildVRT().
2682
 * @param pfnProgress the progress callback.
2683
 * @param pProgressData the user data for the progress callback.
2684
 *
2685
 * @since GDAL 2.1
2686
 */
2687
2688
void GDALBuildVRTOptionsSetProgress(GDALBuildVRTOptions *psOptions,
2689
                                    GDALProgressFunc pfnProgress,
2690
                                    void *pProgressData)
2691
0
{
2692
0
    psOptions->pfnProgress = pfnProgress ? pfnProgress : GDALDummyProgress;
2693
0
    psOptions->pProgressData = pProgressData;
2694
0
    if (pfnProgress == GDALTermProgress)
2695
0
        psOptions->bQuiet = false;
2696
0
}