Coverage Report

Created: 2025-12-31 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/gtiff/cogdriver.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  COG Driver
4
 * Purpose:  Cloud optimized GeoTIFF write support.
5
 * Author:   Even Rouault <even dot rouault at spatialys dot com>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2019, Even Rouault <even dot rouault at spatialys dot com>
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12
13
#include "cpl_port.h"
14
15
#include "gdal_proxy.h"
16
#include "gdal_priv.h"
17
#include "gdal_frmts.h"
18
#include "gtiff.h"
19
#include "gtiffdataset.h"
20
#include "gt_overview.h"
21
#include "gdal_utils.h"
22
#include "gdalwarper.h"
23
#include "cogdriver.h"
24
#include "geotiff.h"
25
26
#include "tilematrixset.hpp"
27
28
#include <algorithm>
29
#include <memory>
30
#include <mutex>
31
#include <vector>
32
33
static bool gbHasLZW = false;
34
35
/************************************************************************/
36
/*                        HasZSTDCompression()                          */
37
/************************************************************************/
38
39
static bool HasZSTDCompression()
40
0
{
41
0
    TIFFCodec *codecs = TIFFGetConfiguredCODECs();
42
0
    bool bHasZSTD = false;
43
0
    for (TIFFCodec *c = codecs; c->name; ++c)
44
0
    {
45
0
        if (c->scheme == COMPRESSION_ZSTD)
46
0
        {
47
0
            bHasZSTD = true;
48
0
            break;
49
0
        }
50
0
    }
51
0
    _TIFFfree(codecs);
52
0
    return bHasZSTD;
53
0
}
54
55
/************************************************************************/
56
/*                           GetTmpFilename()                           */
57
/************************************************************************/
58
59
static CPLString GetTmpFilename(const char *pszFilename, const char *pszExt)
60
0
{
61
0
    const bool bSupportsRandomWrite =
62
0
        VSISupportsRandomWrite(pszFilename, false);
63
0
    CPLString osTmpFilename;
64
0
    if (!bSupportsRandomWrite ||
65
0
        CPLGetConfigOption("CPL_TMPDIR", nullptr) != nullptr)
66
0
    {
67
0
        osTmpFilename = CPLGenerateTempFilenameSafe(
68
0
            CPLGetBasenameSafe(pszFilename).c_str());
69
0
    }
70
0
    else
71
0
        osTmpFilename = pszFilename;
72
0
    osTmpFilename += '.';
73
0
    osTmpFilename += pszExt;
74
0
    VSIUnlink(osTmpFilename);
75
0
    return osTmpFilename;
76
0
}
77
78
/************************************************************************/
79
/*                             GetResampling()                          */
80
/************************************************************************/
81
82
static const char *GetResampling(GDALDataset *poSrcDS)
83
0
{
84
0
    return poSrcDS->GetRasterBand(1)->GetColorTable() ||
85
0
                   GDALDataTypeIsComplex(
86
0
                       poSrcDS->GetRasterBand(1)->GetRasterDataType())
87
0
               ? "NEAREST"
88
0
               : "CUBIC";
89
0
}
90
91
/************************************************************************/
92
/*                             GetPredictor()                          */
93
/************************************************************************/
94
static const char *GetPredictor(GDALDataset *poSrcDS, const char *pszPredictor)
95
0
{
96
0
    if (pszPredictor == nullptr)
97
0
        return nullptr;
98
99
0
    if (EQUAL(pszPredictor, "YES") || EQUAL(pszPredictor, "ON") ||
100
0
        EQUAL(pszPredictor, "TRUE"))
101
0
    {
102
0
        if (GDALDataTypeIsFloating(
103
0
                poSrcDS->GetRasterBand(1)->GetRasterDataType()))
104
0
            return "3";
105
0
        else
106
0
            return "2";
107
0
    }
108
0
    else if (EQUAL(pszPredictor, "STANDARD") || EQUAL(pszPredictor, "2"))
109
0
    {
110
0
        return "2";
111
0
    }
112
0
    else if (EQUAL(pszPredictor, "FLOATING_POINT") || EQUAL(pszPredictor, "3"))
113
0
    {
114
0
        return "3";
115
0
    }
116
0
    return nullptr;
117
0
}
118
119
/************************************************************************/
120
/*                            COGGetTargetSRS()                         */
121
/************************************************************************/
122
123
static bool COGGetTargetSRS(const char *const *papszOptions,
124
                            CPLString &osTargetSRS,
125
                            std::unique_ptr<gdal::TileMatrixSet> &poTM)
126
0
{
127
0
    osTargetSRS = CSLFetchNameValueDef(papszOptions, "TARGET_SRS", "");
128
0
    CPLString osTilingScheme(
129
0
        CSLFetchNameValueDef(papszOptions, "TILING_SCHEME", "CUSTOM"));
130
0
    if (EQUAL(osTargetSRS, "") && EQUAL(osTilingScheme, "CUSTOM"))
131
0
        return false;
132
133
0
    if (!EQUAL(osTilingScheme, "CUSTOM"))
134
0
    {
135
0
        poTM = gdal::TileMatrixSet::parse(osTilingScheme);
136
0
        if (poTM == nullptr)
137
0
            return false;
138
0
        if (!poTM->haveAllLevelsSameTopLeft())
139
0
        {
140
0
            CPLError(CE_Failure, CPLE_NotSupported,
141
0
                     "Unsupported tiling scheme: not all zoom levels have same "
142
0
                     "top left corner");
143
0
            return false;
144
0
        }
145
0
        if (!poTM->haveAllLevelsSameTileSize())
146
0
        {
147
0
            CPLError(CE_Failure, CPLE_NotSupported,
148
0
                     "Unsupported tiling scheme: not all zoom levels have same "
149
0
                     "tile size");
150
0
            return false;
151
0
        }
152
0
        if (poTM->hasVariableMatrixWidth())
153
0
        {
154
0
            CPLError(CE_Failure, CPLE_NotSupported,
155
0
                     "Unsupported tiling scheme: some levels have variable "
156
0
                     "matrix width");
157
0
            return false;
158
0
        }
159
0
        if (!osTargetSRS.empty())
160
0
        {
161
0
            CPLError(CE_Warning, CPLE_AppDefined, "Ignoring TARGET_SRS option");
162
0
        }
163
0
        osTargetSRS = poTM->crs();
164
165
        // "Normalize" SRS as AUTH:CODE
166
0
        OGRSpatialReference oTargetSRS;
167
0
        oTargetSRS.SetFromUserInput(
168
0
            osTargetSRS,
169
0
            OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get());
170
0
        const char *pszAuthCode = oTargetSRS.GetAuthorityCode(nullptr);
171
0
        const char *pszAuthName = oTargetSRS.GetAuthorityName(nullptr);
172
0
        if (pszAuthName && pszAuthCode)
173
0
        {
174
0
            osTargetSRS = pszAuthName;
175
0
            osTargetSRS += ':';
176
0
            osTargetSRS += pszAuthCode;
177
0
        }
178
0
    }
179
180
0
    return true;
181
0
}
182
183
// Used by gdalwarp
184
bool COGGetTargetSRS(const char *const *papszOptions, CPLString &osTargetSRS)
185
0
{
186
0
    std::unique_ptr<gdal::TileMatrixSet> poTM;
187
0
    return COGGetTargetSRS(papszOptions, osTargetSRS, poTM);
188
0
}
189
190
// Used by gdalwarp
191
std::string COGGetResampling(GDALDataset *poSrcDS,
192
                             const char *const *papszOptions)
193
0
{
194
0
    return CSLFetchNameValueDef(papszOptions, "WARP_RESAMPLING",
195
0
                                CSLFetchNameValueDef(papszOptions, "RESAMPLING",
196
0
                                                     GetResampling(poSrcDS)));
197
0
}
198
199
/************************************************************************/
200
/*                     COGGetWarpingCharacteristics()                   */
201
/************************************************************************/
202
203
static bool COGGetWarpingCharacteristics(
204
    GDALDataset *poSrcDS, const char *const *papszOptions,
205
    CPLString &osResampling, CPLString &osTargetSRS, int &nXSize, int &nYSize,
206
    double &dfMinX, double &dfMinY, double &dfMaxX, double &dfMaxY,
207
    double &dfRes, std::unique_ptr<gdal::TileMatrixSet> &poTM, int &nZoomLevel,
208
    int &nAlignedLevels)
209
0
{
210
0
    if (!COGGetTargetSRS(papszOptions, osTargetSRS, poTM))
211
0
        return false;
212
213
0
    CPLStringList aosTO;
214
0
    aosTO.SetNameValue("DST_SRS", osTargetSRS);
215
0
    void *hTransformArg = nullptr;
216
217
0
    OGRSpatialReference oTargetSRS;
218
0
    oTargetSRS.SetFromUserInput(
219
0
        osTargetSRS,
220
0
        OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get());
221
0
    const char *pszAuthCode = oTargetSRS.GetAuthorityCode(nullptr);
222
0
    const int nEPSGCode = pszAuthCode ? atoi(pszAuthCode) : 0;
223
224
    // Hack to compensate for GDALSuggestedWarpOutput2() failure (or not
225
    // ideal suggestion with PROJ 8) when reprojecting latitude = +/- 90 to
226
    // EPSG:3857.
227
0
    GDALGeoTransform srcGT;
228
0
    std::unique_ptr<GDALDataset> poTmpDS;
229
0
    if (nEPSGCode == 3857 && poSrcDS->GetGeoTransform(srcGT) == CE_None &&
230
0
        srcGT[2] == 0 && srcGT[4] == 0 && srcGT[5] < 0)
231
0
    {
232
0
        const auto poSrcSRS = poSrcDS->GetSpatialRef();
233
0
        if (poSrcSRS && poSrcSRS->IsGeographic() &&
234
0
            !poSrcSRS->IsDerivedGeographic())
235
0
        {
236
0
            double maxLat = srcGT[3];
237
0
            double minLat = srcGT[3] + poSrcDS->GetRasterYSize() * srcGT[5];
238
            // Corresponds to the latitude of below MAX_GM
239
0
            constexpr double MAX_LAT = 85.0511287798066;
240
0
            bool bModified = false;
241
0
            if (maxLat > MAX_LAT)
242
0
            {
243
0
                maxLat = MAX_LAT;
244
0
                bModified = true;
245
0
            }
246
0
            if (minLat < -MAX_LAT)
247
0
            {
248
0
                minLat = -MAX_LAT;
249
0
                bModified = true;
250
0
            }
251
0
            if (bModified)
252
0
            {
253
0
                CPLStringList aosOptions;
254
0
                aosOptions.AddString("-of");
255
0
                aosOptions.AddString("VRT");
256
0
                aosOptions.AddString("-projwin");
257
0
                aosOptions.AddString(CPLSPrintf("%.17g", srcGT[0]));
258
0
                aosOptions.AddString(CPLSPrintf("%.17g", maxLat));
259
0
                aosOptions.AddString(CPLSPrintf(
260
0
                    "%.17g", srcGT[0] + poSrcDS->GetRasterXSize() * srcGT[1]));
261
0
                aosOptions.AddString(CPLSPrintf("%.17g", minLat));
262
0
                auto psOptions =
263
0
                    GDALTranslateOptionsNew(aosOptions.List(), nullptr);
264
0
                poTmpDS.reset(GDALDataset::FromHandle(GDALTranslate(
265
0
                    "", GDALDataset::ToHandle(poSrcDS), psOptions, nullptr)));
266
0
                GDALTranslateOptionsFree(psOptions);
267
0
                if (poTmpDS)
268
0
                {
269
0
                    hTransformArg = GDALCreateGenImgProjTransformer2(
270
0
                        GDALDataset::FromHandle(poTmpDS.get()), nullptr,
271
0
                        aosTO.List());
272
0
                    if (hTransformArg == nullptr)
273
0
                    {
274
0
                        return false;
275
0
                    }
276
0
                }
277
0
            }
278
0
        }
279
0
    }
280
0
    if (hTransformArg == nullptr)
281
0
    {
282
0
        hTransformArg =
283
0
            GDALCreateGenImgProjTransformer2(poSrcDS, nullptr, aosTO.List());
284
0
        if (hTransformArg == nullptr)
285
0
        {
286
0
            return false;
287
0
        }
288
0
    }
289
290
0
    GDALTransformerInfo *psInfo =
291
0
        static_cast<GDALTransformerInfo *>(hTransformArg);
292
0
    double adfGeoTransform[6];
293
0
    double adfExtent[4];
294
295
0
    if (GDALSuggestedWarpOutput2(poTmpDS ? poTmpDS.get() : poSrcDS,
296
0
                                 psInfo->pfnTransform, hTransformArg,
297
0
                                 adfGeoTransform, &nXSize, &nYSize, adfExtent,
298
0
                                 0) != CE_None)
299
0
    {
300
0
        GDALDestroyGenImgProjTransformer(hTransformArg);
301
0
        return false;
302
0
    }
303
304
0
    GDALDestroyGenImgProjTransformer(hTransformArg);
305
0
    hTransformArg = nullptr;
306
0
    poTmpDS.reset();
307
308
0
    dfMinX = adfExtent[0];
309
0
    dfMinY = adfExtent[1];
310
0
    dfMaxX = adfExtent[2];
311
0
    dfMaxY = adfExtent[3];
312
0
    dfRes = adfGeoTransform[1];
313
314
0
    const CPLString osExtent(CSLFetchNameValueDef(papszOptions, "EXTENT", ""));
315
0
    const CPLString osRes(CSLFetchNameValueDef(papszOptions, "RES", ""));
316
0
    if (poTM)
317
0
    {
318
0
        if (!osExtent.empty())
319
0
        {
320
0
            CPLError(CE_Warning, CPLE_AppDefined, "Ignoring EXTENT option");
321
0
        }
322
0
        if (!osRes.empty())
323
0
        {
324
0
            CPLError(CE_Warning, CPLE_AppDefined, "Ignoring RES option");
325
0
        }
326
0
        const bool bInvertAxis =
327
0
            oTargetSRS.EPSGTreatsAsLatLong() != FALSE ||
328
0
            oTargetSRS.EPSGTreatsAsNorthingEasting() != FALSE;
329
330
0
        const auto &bbox = poTM->bbox();
331
0
        if (bbox.mCrs == poTM->crs())
332
0
        {
333
0
            if (dfMaxX <
334
0
                    (bInvertAxis ? bbox.mLowerCornerY : bbox.mLowerCornerX) ||
335
0
                dfMinX >
336
0
                    (bInvertAxis ? bbox.mUpperCornerY : bbox.mUpperCornerX) ||
337
0
                dfMaxY <
338
0
                    (bInvertAxis ? bbox.mLowerCornerX : bbox.mLowerCornerY) ||
339
0
                dfMinY >
340
0
                    (bInvertAxis ? bbox.mUpperCornerX : bbox.mUpperCornerY))
341
0
            {
342
0
                CPLError(CE_Failure, CPLE_AppDefined,
343
0
                         "Raster extent completely outside of tile matrix set "
344
0
                         "bounding box");
345
0
                return false;
346
0
            }
347
0
        }
348
349
0
        const auto &tmList = poTM->tileMatrixList();
350
0
        const int nBlockSize = atoi(CSLFetchNameValueDef(
351
0
            papszOptions, "BLOCKSIZE", CPLSPrintf("%d", tmList[0].mTileWidth)));
352
0
        dfRes = 0.0;
353
354
0
        const char *pszZoomLevel =
355
0
            CSLFetchNameValue(papszOptions, "ZOOM_LEVEL");
356
0
        if (pszZoomLevel)
357
0
        {
358
0
            nZoomLevel = atoi(pszZoomLevel);
359
0
            if (nZoomLevel < 0 || nZoomLevel >= static_cast<int>(tmList.size()))
360
0
            {
361
0
                CPLError(CE_Failure, CPLE_AppDefined,
362
0
                         "Invalid zoom level: should be in [0,%d]",
363
0
                         static_cast<int>(tmList.size()) - 1);
364
0
                return false;
365
0
            }
366
0
        }
367
0
        else
368
0
        {
369
0
            double dfComputedRes = adfGeoTransform[1];
370
0
            double dfPrevRes = 0.0;
371
0
            for (; nZoomLevel < static_cast<int>(tmList.size()); nZoomLevel++)
372
0
            {
373
0
                dfRes = tmList[nZoomLevel].mResX * tmList[0].mTileWidth /
374
0
                        nBlockSize;
375
0
                if (dfComputedRes > dfRes ||
376
0
                    fabs(dfComputedRes - dfRes) / dfRes <= 1e-8)
377
0
                    break;
378
0
                dfPrevRes = dfRes;
379
0
            }
380
0
            if (nZoomLevel == static_cast<int>(tmList.size()))
381
0
            {
382
0
                CPLError(CE_Failure, CPLE_AppDefined,
383
0
                         "Could not find an appropriate zoom level");
384
0
                return false;
385
0
            }
386
387
0
            if (nZoomLevel > 0 && fabs(dfComputedRes - dfRes) / dfRes > 1e-8)
388
0
            {
389
0
                const char *pszZoomLevelStrategy = CSLFetchNameValueDef(
390
0
                    papszOptions, "ZOOM_LEVEL_STRATEGY", "AUTO");
391
0
                if (EQUAL(pszZoomLevelStrategy, "LOWER"))
392
0
                {
393
0
                    nZoomLevel--;
394
0
                }
395
0
                else if (EQUAL(pszZoomLevelStrategy, "UPPER"))
396
0
                {
397
                    /* do nothing */
398
0
                }
399
0
                else
400
0
                {
401
0
                    if (dfPrevRes / dfComputedRes < dfComputedRes / dfRes)
402
0
                        nZoomLevel--;
403
0
                }
404
0
            }
405
0
        }
406
0
        CPLDebug("COG", "Using ZOOM_LEVEL %d", nZoomLevel);
407
0
        dfRes = tmList[nZoomLevel].mResX * tmList[0].mTileWidth / nBlockSize;
408
409
0
        const double dfOriX =
410
0
            bInvertAxis ? tmList[0].mTopLeftY : tmList[0].mTopLeftX;
411
0
        const double dfOriY =
412
0
            bInvertAxis ? tmList[0].mTopLeftX : tmList[0].mTopLeftY;
413
0
        const double dfTileExtent = dfRes * nBlockSize;
414
0
        constexpr double TOLERANCE_IN_PIXEL = 0.499;
415
0
        const double dfEps = TOLERANCE_IN_PIXEL * dfRes;
416
0
        int nTLTileX = static_cast<int>(
417
0
            std::floor((dfMinX - dfOriX + dfEps) / dfTileExtent));
418
0
        int nTLTileY = static_cast<int>(
419
0
            std::floor((dfOriY - dfMaxY + dfEps) / dfTileExtent));
420
0
        int nBRTileX = static_cast<int>(
421
0
            std::ceil((dfMaxX - dfOriX - dfEps) / dfTileExtent));
422
0
        int nBRTileY = static_cast<int>(
423
0
            std::ceil((dfOriY - dfMinY - dfEps) / dfTileExtent));
424
425
0
        nAlignedLevels =
426
0
            std::min(std::min(10, atoi(CSLFetchNameValueDef(
427
0
                                      papszOptions, "ALIGNED_LEVELS", "0"))),
428
0
                     nZoomLevel);
429
0
        int nAccDivisor = 1;
430
0
        for (int i = 0; i < nAlignedLevels - 1; i++)
431
0
        {
432
0
            const int nCurLevel = nZoomLevel - i;
433
0
            const double dfResRatio =
434
0
                tmList[nCurLevel - 1].mResX / tmList[nCurLevel].mResX;
435
            // Magical number that has a great number of divisors
436
            // For example if previous scale denom was 50K and current one
437
            // is 20K, then dfResRatio = 2.5 and dfScaledInvResRatio = 24
438
            // We must then simplify 60 / 24 as 5 / 2, and make sure to
439
            // align tile coordinates on multiple of the 5 numerator
440
0
            constexpr int MAGICAL = 60;
441
0
            const double dfScaledInvResRatio = MAGICAL / dfResRatio;
442
0
            if (dfScaledInvResRatio < 1 || dfScaledInvResRatio > 60 ||
443
0
                std::abs(std::round(dfScaledInvResRatio) -
444
0
                         dfScaledInvResRatio) > 1e-10)
445
0
            {
446
0
                CPLError(CE_Failure, CPLE_AppDefined,
447
0
                         "Unsupported ratio of resolution for "
448
0
                         "ALIGNED_LEVELS between zoom level %d and %d = %g",
449
0
                         nCurLevel - 1, nCurLevel, dfResRatio);
450
0
                return false;
451
0
            }
452
0
            const int nScaledInvResRatio =
453
0
                static_cast<int>(std::round(dfScaledInvResRatio));
454
0
            int nNumerator = 0;
455
0
            for (int nDivisor = nScaledInvResRatio; nDivisor >= 2; --nDivisor)
456
0
            {
457
0
                if ((MAGICAL % nDivisor) == 0 &&
458
0
                    (nScaledInvResRatio % nDivisor) == 0)
459
0
                {
460
0
                    nNumerator = MAGICAL / nDivisor;
461
0
                    break;
462
0
                }
463
0
            }
464
0
            if (nNumerator == 0)
465
0
            {
466
0
                CPLError(CE_Failure, CPLE_AppDefined,
467
0
                         "Unsupported ratio of resolution for "
468
0
                         "ALIGNED_LEVELS between zoom level %d and %d = %g",
469
0
                         nCurLevel - 1, nCurLevel, dfResRatio);
470
0
                return false;
471
0
            }
472
0
            nAccDivisor *= nNumerator;
473
0
        }
474
0
        if (nAccDivisor > 1)
475
0
        {
476
0
            nTLTileX = (nTLTileX / nAccDivisor) * nAccDivisor;
477
0
            nTLTileY = (nTLTileY / nAccDivisor) * nAccDivisor;
478
0
            nBRTileY = DIV_ROUND_UP(nBRTileY, nAccDivisor) * nAccDivisor;
479
0
            nBRTileX = DIV_ROUND_UP(nBRTileX, nAccDivisor) * nAccDivisor;
480
0
        }
481
482
0
        if (nTLTileX < 0 || nTLTileY < 0 ||
483
0
            nBRTileX > tmList[nZoomLevel].mMatrixWidth ||
484
0
            nBRTileY > tmList[nZoomLevel].mMatrixHeight)
485
0
        {
486
0
            CPLError(CE_Warning, CPLE_AppDefined,
487
0
                     "Raster extent partially outside of tile matrix "
488
0
                     "bounding box. Clamping it to it");
489
0
        }
490
0
        nTLTileX = std::max(0, nTLTileX);
491
0
        nTLTileY = std::max(0, nTLTileY);
492
0
        nBRTileX = std::min(tmList[nZoomLevel].mMatrixWidth, nBRTileX);
493
0
        nBRTileY = std::min(tmList[nZoomLevel].mMatrixHeight, nBRTileY);
494
495
0
        dfMinX = dfOriX + nTLTileX * dfTileExtent;
496
0
        dfMinY = dfOriY - nBRTileY * dfTileExtent;
497
0
        dfMaxX = dfOriX + nBRTileX * dfTileExtent;
498
0
        dfMaxY = dfOriY - nTLTileY * dfTileExtent;
499
0
    }
500
0
    else if (!osExtent.empty() || !osRes.empty())
501
0
    {
502
0
        CPLStringList aosTokens(CSLTokenizeString2(osExtent, ",", 0));
503
0
        if (aosTokens.size() != 4)
504
0
        {
505
0
            CPLError(CE_Failure, CPLE_AppDefined, "Invalid value for EXTENT");
506
0
            return false;
507
0
        }
508
0
        dfMinX = CPLAtof(aosTokens[0]);
509
0
        dfMinY = CPLAtof(aosTokens[1]);
510
0
        dfMaxX = CPLAtof(aosTokens[2]);
511
0
        dfMaxY = CPLAtof(aosTokens[3]);
512
0
        if (!osRes.empty())
513
0
            dfRes = CPLAtof(osRes);
514
0
    }
515
516
0
    nXSize = static_cast<int>(std::round((dfMaxX - dfMinX) / dfRes));
517
0
    nYSize = static_cast<int>(std::round((dfMaxY - dfMinY) / dfRes));
518
519
0
    osResampling = COGGetResampling(poSrcDS, papszOptions);
520
521
0
    return true;
522
0
}
523
524
// Used by gdalwarp
525
bool COGGetWarpingCharacteristics(GDALDataset *poSrcDS,
526
                                  const char *const *papszOptions,
527
                                  CPLString &osResampling,
528
                                  CPLString &osTargetSRS, int &nXSize,
529
                                  int &nYSize, double &dfMinX, double &dfMinY,
530
                                  double &dfMaxX, double &dfMaxY)
531
0
{
532
0
    std::unique_ptr<gdal::TileMatrixSet> poTM;
533
0
    int nZoomLevel = 0;
534
0
    int nAlignedLevels = 0;
535
0
    double dfRes;
536
0
    return COGGetWarpingCharacteristics(poSrcDS, papszOptions, osResampling,
537
0
                                        osTargetSRS, nXSize, nYSize, dfMinX,
538
0
                                        dfMinY, dfMaxX, dfMaxY, dfRes, poTM,
539
0
                                        nZoomLevel, nAlignedLevels);
540
0
}
541
542
/************************************************************************/
543
/*                        COGHasWarpingOptions()                        */
544
/************************************************************************/
545
546
bool COGHasWarpingOptions(CSLConstList papszOptions)
547
0
{
548
0
    return CSLFetchNameValue(papszOptions, "TARGET_SRS") != nullptr ||
549
0
           !EQUAL(CSLFetchNameValueDef(papszOptions, "TILING_SCHEME", "CUSTOM"),
550
0
                  "CUSTOM");
551
0
}
552
553
/************************************************************************/
554
/*                      COGRemoveWarpingOptions()                       */
555
/************************************************************************/
556
557
void COGRemoveWarpingOptions(CPLStringList &aosOptions)
558
0
{
559
0
    aosOptions.SetNameValue("TARGET_SRS", nullptr);
560
0
    aosOptions.SetNameValue("TILING_SCHEME", nullptr);
561
0
    aosOptions.SetNameValue("EXTENT", nullptr);
562
0
    aosOptions.SetNameValue("RES", nullptr);
563
0
    aosOptions.SetNameValue("ALIGNED_LEVELS", nullptr);
564
0
    aosOptions.SetNameValue("ZOOM_LEVEL_STRATEGY", nullptr);
565
0
}
566
567
/************************************************************************/
568
/*                        CreateReprojectedDS()                         */
569
/************************************************************************/
570
571
static std::unique_ptr<GDALDataset> CreateReprojectedDS(
572
    const char *pszDstFilename, GDALDataset *poSrcDS,
573
    const char *const *papszOptions, const CPLString &osResampling,
574
    const CPLString &osTargetSRS, const int nXSize, const int nYSize,
575
    const double dfMinX, const double dfMinY, const double dfMaxX,
576
    const double dfMaxY, const double dfRes, GDALProgressFunc pfnProgress,
577
    void *pProgressData, double &dfCurPixels, double &dfTotalPixelsToProcess)
578
0
{
579
0
    char **papszArg = nullptr;
580
    // We could have done a warped VRT, but overview building on it might be
581
    // slow, so materialize as GTiff
582
0
    papszArg = CSLAddString(papszArg, "-of");
583
0
    papszArg = CSLAddString(papszArg, "GTiff");
584
0
    papszArg = CSLAddString(papszArg, "-co");
585
0
    papszArg = CSLAddString(papszArg, "TILED=YES");
586
0
    papszArg = CSLAddString(papszArg, "-co");
587
0
    papszArg = CSLAddString(papszArg, "SPARSE_OK=YES");
588
0
    const char *pszBIGTIFF = CSLFetchNameValue(papszOptions, "BIGTIFF");
589
0
    if (pszBIGTIFF)
590
0
    {
591
0
        papszArg = CSLAddString(papszArg, "-co");
592
0
        papszArg = CSLAddString(papszArg,
593
0
                                (CPLString("BIGTIFF=") + pszBIGTIFF).c_str());
594
0
    }
595
0
    papszArg = CSLAddString(papszArg, "-co");
596
0
    papszArg = CSLAddString(papszArg, HasZSTDCompression() ? "COMPRESS=ZSTD"
597
0
                                                           : "COMPRESS=LZW");
598
0
    papszArg = CSLAddString(papszArg, "-t_srs");
599
0
    papszArg = CSLAddString(papszArg, osTargetSRS);
600
0
    papszArg = CSLAddString(papszArg, "-te");
601
0
    papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfMinX));
602
0
    papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfMinY));
603
0
    papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfMaxX));
604
0
    papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfMaxY));
605
0
    papszArg = CSLAddString(papszArg, "-ts");
606
0
    papszArg = CSLAddString(papszArg, CPLSPrintf("%d", nXSize));
607
0
    papszArg = CSLAddString(papszArg, CPLSPrintf("%d", nYSize));
608
609
    // to be kept in sync with gdalwarp_lib.cpp
610
0
    constexpr double RELATIVE_ERROR_RES_SHARED_BY_COG_AND_GDALWARP = 1e-8;
611
0
    if (fabs((dfMaxX - dfMinX) / dfRes - nXSize) <=
612
0
            RELATIVE_ERROR_RES_SHARED_BY_COG_AND_GDALWARP &&
613
0
        fabs((dfMaxY - dfMinY) / dfRes - nYSize) <=
614
0
            RELATIVE_ERROR_RES_SHARED_BY_COG_AND_GDALWARP)
615
0
    {
616
        // Try to produce exactly square pixels
617
0
        papszArg = CSLAddString(papszArg, "-tr");
618
0
        papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfRes));
619
0
        papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfRes));
620
0
    }
621
0
    else
622
0
    {
623
0
        CPLDebug("COG", "Cannot pass -tr option to GDALWarp() due to extent, "
624
0
                        "size and resolution not consistent enough");
625
0
    }
626
627
0
    int bHasNoData = FALSE;
628
0
    poSrcDS->GetRasterBand(1)->GetNoDataValue(&bHasNoData);
629
0
    if (!bHasNoData &&
630
0
        CPLTestBool(CSLFetchNameValueDef(papszOptions, "ADD_ALPHA", "YES")))
631
0
    {
632
0
        papszArg = CSLAddString(papszArg, "-dstalpha");
633
0
    }
634
0
    papszArg = CSLAddString(papszArg, "-r");
635
0
    papszArg = CSLAddString(papszArg, osResampling);
636
0
    papszArg = CSLAddString(papszArg, "-wo");
637
0
    papszArg = CSLAddString(papszArg, "SAMPLE_GRID=YES");
638
0
    const char *pszNumThreads = CSLFetchNameValue(papszOptions, "NUM_THREADS");
639
0
    if (pszNumThreads)
640
0
    {
641
0
        papszArg = CSLAddString(papszArg, "-wo");
642
0
        papszArg = CSLAddString(
643
0
            papszArg, (CPLString("NUM_THREADS=") + pszNumThreads).c_str());
644
0
    }
645
646
0
    const auto poFirstBand = poSrcDS->GetRasterBand(1);
647
0
    const bool bHasMask = poFirstBand->GetMaskFlags() == GMF_PER_DATASET;
648
649
0
    const int nBands = poSrcDS->GetRasterCount();
650
0
    const char *pszOverviews =
651
0
        CSLFetchNameValueDef(papszOptions, "OVERVIEWS", "AUTO");
652
0
    const bool bUseExistingOrNone = EQUAL(pszOverviews, "FORCE_USE_EXISTING") ||
653
0
                                    EQUAL(pszOverviews, "NONE");
654
0
    dfTotalPixelsToProcess =
655
0
        double(nXSize) * nYSize * (nBands + (bHasMask ? 1 : 0)) +
656
0
        ((bHasMask && !bUseExistingOrNone) ? double(nXSize) * nYSize / 3 : 0) +
657
0
        (!bUseExistingOrNone ? double(nXSize) * nYSize * nBands / 3 : 0) +
658
0
        double(nXSize) * nYSize * (nBands + (bHasMask ? 1 : 0)) * 4. / 3;
659
660
0
    auto psOptions = GDALWarpAppOptionsNew(papszArg, nullptr);
661
0
    CSLDestroy(papszArg);
662
0
    if (psOptions == nullptr)
663
0
        return nullptr;
664
665
0
    const double dfNextPixels =
666
0
        double(nXSize) * nYSize * (nBands + (bHasMask ? 1 : 0));
667
0
    void *pScaledProgress = GDALCreateScaledProgress(
668
0
        dfCurPixels / dfTotalPixelsToProcess,
669
0
        dfNextPixels / dfTotalPixelsToProcess, pfnProgress, pProgressData);
670
0
    dfCurPixels = dfNextPixels;
671
672
0
    CPLDebug("COG", "Reprojecting source dataset: start");
673
0
    GDALWarpAppOptionsSetProgress(psOptions, GDALScaledProgress,
674
0
                                  pScaledProgress);
675
0
    CPLString osTmpFile(GetTmpFilename(pszDstFilename, "warped.tif.tmp"));
676
0
    auto hSrcDS = GDALDataset::ToHandle(poSrcDS);
677
678
0
    std::unique_ptr<CPLConfigOptionSetter> poWarpThreadSetter;
679
0
    if (pszNumThreads)
680
0
    {
681
0
        poWarpThreadSetter.reset(new CPLConfigOptionSetter(
682
0
            "GDAL_NUM_THREADS", pszNumThreads, false));
683
0
    }
684
685
0
    auto hRet = GDALWarp(osTmpFile, nullptr, 1, &hSrcDS, psOptions, nullptr);
686
0
    GDALWarpAppOptionsFree(psOptions);
687
0
    CPLDebug("COG", "Reprojecting source dataset: end");
688
689
0
    GDALDestroyScaledProgress(pScaledProgress);
690
691
0
    return std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(hRet));
692
0
}
693
694
/************************************************************************/
695
/*                            GDALCOGCreator                            */
696
/************************************************************************/
697
698
struct GDALCOGCreator final
699
{
700
    std::unique_ptr<GDALDataset> m_poReprojectedDS{};
701
    std::unique_ptr<GDALDataset> m_poRGBMaskDS{};
702
    std::unique_ptr<GDALDataset> m_poVRTWithOrWithoutStats{};
703
    CPLString m_osTmpOverviewFilename{};
704
    CPLString m_osTmpMskOverviewFilename{};
705
706
    ~GDALCOGCreator();
707
708
    std::unique_ptr<GDALDataset> Create(const char *pszFilename,
709
                                        GDALDataset *const poSrcDS,
710
                                        CSLConstList papszOptions,
711
                                        GDALProgressFunc pfnProgress,
712
                                        void *pProgressData);
713
};
714
715
/************************************************************************/
716
/*                    GDALCOGCreator::~GDALCOGCreator()                 */
717
/************************************************************************/
718
719
GDALCOGCreator::~GDALCOGCreator()
720
0
{
721
    // Destroy m_poRGBMaskDS before m_poReprojectedDS since the former
722
    // may reference the later
723
0
    m_poRGBMaskDS.reset();
724
725
    // Config option just for testing purposes
726
0
    const bool bDeleteTempFiles =
727
0
        CPLTestBool(CPLGetConfigOption("COG_DELETE_TEMP_FILES", "YES"));
728
0
    if (bDeleteTempFiles)
729
0
    {
730
0
        if (m_poReprojectedDS)
731
0
        {
732
0
            CPLString osProjectedDSName(m_poReprojectedDS->GetDescription());
733
0
            m_poReprojectedDS.reset();
734
0
            VSIUnlink(osProjectedDSName);
735
0
        }
736
0
        if (!m_osTmpOverviewFilename.empty())
737
0
        {
738
0
            VSIUnlink(m_osTmpOverviewFilename);
739
0
        }
740
0
        if (!m_osTmpMskOverviewFilename.empty())
741
0
        {
742
0
            VSIUnlink(m_osTmpMskOverviewFilename);
743
0
        }
744
0
    }
745
0
}
746
747
/************************************************************************/
748
/*                    GDALCOGCreator::Create()                          */
749
/************************************************************************/
750
751
std::unique_ptr<GDALDataset>
752
GDALCOGCreator::Create(const char *pszFilename, GDALDataset *const poSrcDS,
753
                       CSLConstList papszOptions, GDALProgressFunc pfnProgress,
754
                       void *pProgressData)
755
0
{
756
0
    if (pfnProgress == nullptr)
757
0
        pfnProgress = GDALDummyProgress;
758
759
0
    if (poSrcDS->GetRasterCount() == 0)
760
0
    {
761
0
        CPLError(CE_Failure, CPLE_NotSupported,
762
0
                 "COG driver does not support 0-band source raster");
763
0
        return nullptr;
764
0
    }
765
766
0
    const CPLString osCompress = CSLFetchNameValueDef(
767
0
        papszOptions, "COMPRESS", gbHasLZW ? "LZW" : "NONE");
768
769
0
    const char *pszInterleave =
770
0
        CSLFetchNameValueDef(papszOptions, "INTERLEAVE", "PIXEL");
771
0
    if (EQUAL(osCompress, "WEBP"))
772
0
    {
773
0
        if (!EQUAL(pszInterleave, "PIXEL"))
774
0
        {
775
0
            CPLError(CE_Failure, CPLE_NotSupported,
776
0
                     "COMPRESS=WEBP only supported for INTERLEAVE=PIXEL");
777
0
            return nullptr;
778
0
        }
779
0
    }
780
781
0
    CPLConfigOptionSetter oSetterReportDirtyBlockFlushing(
782
0
        "GDAL_REPORT_DIRTY_BLOCK_FLUSHING", "NO", true);
783
784
0
    const char *pszStatistics =
785
0
        CSLFetchNameValueDef(papszOptions, "STATISTICS", "AUTO");
786
0
    auto poSrcFirstBand = poSrcDS->GetRasterBand(1);
787
0
    const bool bSrcHasStatistics =
788
0
        poSrcFirstBand->GetMetadataItem("STATISTICS_MINIMUM") &&
789
0
        poSrcFirstBand->GetMetadataItem("STATISTICS_MAXIMUM") &&
790
0
        poSrcFirstBand->GetMetadataItem("STATISTICS_MEAN") &&
791
0
        poSrcFirstBand->GetMetadataItem("STATISTICS_STDDEV");
792
0
    bool bNeedStats = false;
793
0
    bool bRemoveStats = false;
794
0
    bool bWrkHasStatistics = bSrcHasStatistics;
795
0
    if (EQUAL(pszStatistics, "AUTO"))
796
0
    {
797
        // nothing
798
0
    }
799
0
    else if (CPLTestBool(pszStatistics))
800
0
    {
801
0
        bNeedStats = true;
802
0
    }
803
0
    else
804
0
    {
805
0
        bRemoveStats = true;
806
0
    }
807
808
0
    double dfCurPixels = 0;
809
0
    double dfTotalPixelsToProcess = 0;
810
0
    GDALDataset *poCurDS = poSrcDS;
811
812
0
    std::unique_ptr<gdal::TileMatrixSet> poTM;
813
0
    int nZoomLevel = 0;
814
0
    int nAlignedLevels = 0;
815
0
    if (COGHasWarpingOptions(papszOptions))
816
0
    {
817
0
        CPLString osTargetResampling;
818
0
        CPLString osTargetSRS;
819
0
        int nTargetXSize = 0;
820
0
        int nTargetYSize = 0;
821
0
        double dfTargetMinX = 0;
822
0
        double dfTargetMinY = 0;
823
0
        double dfTargetMaxX = 0;
824
0
        double dfTargetMaxY = 0;
825
0
        double dfRes = 0;
826
0
        if (!COGGetWarpingCharacteristics(
827
0
                poCurDS, papszOptions, osTargetResampling, osTargetSRS,
828
0
                nTargetXSize, nTargetYSize, dfTargetMinX, dfTargetMinY,
829
0
                dfTargetMaxX, dfTargetMaxY, dfRes, poTM, nZoomLevel,
830
0
                nAlignedLevels))
831
0
        {
832
0
            return nullptr;
833
0
        }
834
835
        // Collect information on source dataset to see if it already
836
        // matches the warping specifications
837
0
        CPLString osSrcSRS;
838
0
        const auto poSrcSRS = poCurDS->GetSpatialRef();
839
0
        if (poSrcSRS)
840
0
        {
841
0
            const char *pszAuthName = poSrcSRS->GetAuthorityName(nullptr);
842
0
            const char *pszAuthCode = poSrcSRS->GetAuthorityCode(nullptr);
843
0
            if (pszAuthName && pszAuthCode)
844
0
            {
845
0
                osSrcSRS = pszAuthName;
846
0
                osSrcSRS += ':';
847
0
                osSrcSRS += pszAuthCode;
848
0
            }
849
0
        }
850
0
        double dfSrcMinX = 0;
851
0
        double dfSrcMinY = 0;
852
0
        double dfSrcMaxX = 0;
853
0
        double dfSrcMaxY = 0;
854
0
        GDALGeoTransform srcGT;
855
0
        const int nSrcXSize = poCurDS->GetRasterXSize();
856
0
        const int nSrcYSize = poCurDS->GetRasterYSize();
857
0
        if (poCurDS->GetGeoTransform(srcGT) == CE_None)
858
0
        {
859
0
            dfSrcMinX = srcGT[0];
860
0
            dfSrcMaxY = srcGT[3];
861
0
            dfSrcMaxX = srcGT[0] + nSrcXSize * srcGT[1];
862
0
            dfSrcMinY = srcGT[3] + nSrcYSize * srcGT[5];
863
0
        }
864
865
0
        if (nTargetXSize == nSrcXSize && nTargetYSize == nSrcYSize &&
866
0
            osTargetSRS == osSrcSRS &&
867
0
            fabs(dfSrcMinX - dfTargetMinX) < 1e-10 * fabs(dfSrcMinX) &&
868
0
            fabs(dfSrcMinY - dfTargetMinY) < 1e-10 * fabs(dfSrcMinY) &&
869
0
            fabs(dfSrcMaxX - dfTargetMaxX) < 1e-10 * fabs(dfSrcMaxX) &&
870
0
            fabs(dfSrcMaxY - dfTargetMaxY) < 1e-10 * fabs(dfSrcMaxY))
871
0
        {
872
0
            CPLDebug("COG",
873
0
                     "Skipping reprojection step: "
874
0
                     "source dataset matches reprojection specifications");
875
0
        }
876
0
        else
877
0
        {
878
0
            m_poReprojectedDS = CreateReprojectedDS(
879
0
                pszFilename, poCurDS, papszOptions, osTargetResampling,
880
0
                osTargetSRS, nTargetXSize, nTargetYSize, dfTargetMinX,
881
0
                dfTargetMinY, dfTargetMaxX, dfTargetMaxY, dfRes, pfnProgress,
882
0
                pProgressData, dfCurPixels, dfTotalPixelsToProcess);
883
0
            if (!m_poReprojectedDS)
884
0
                return nullptr;
885
0
            poCurDS = m_poReprojectedDS.get();
886
887
0
            if (bSrcHasStatistics && !bNeedStats && !bRemoveStats)
888
0
            {
889
0
                bNeedStats = true;
890
0
            }
891
0
            bWrkHasStatistics = false;
892
0
        }
893
0
    }
894
895
0
    if (EQUAL(osCompress, "JPEG") && EQUAL(pszInterleave, "PIXEL") &&
896
0
        (poCurDS->GetRasterCount() == 2 || poCurDS->GetRasterCount() == 4) &&
897
0
        poCurDS->GetRasterBand(poCurDS->GetRasterCount())
898
0
                ->GetColorInterpretation() == GCI_AlphaBand)
899
0
    {
900
0
        char **papszArg = nullptr;
901
0
        papszArg = CSLAddString(papszArg, "-of");
902
0
        papszArg = CSLAddString(papszArg, "VRT");
903
0
        papszArg = CSLAddString(papszArg, "-b");
904
0
        papszArg = CSLAddString(papszArg, "1");
905
0
        if (poCurDS->GetRasterCount() == 2)
906
0
        {
907
0
            papszArg = CSLAddString(papszArg, "-mask");
908
0
            papszArg = CSLAddString(papszArg, "2");
909
0
        }
910
0
        else
911
0
        {
912
0
            CPLAssert(poCurDS->GetRasterCount() == 4);
913
0
            papszArg = CSLAddString(papszArg, "-b");
914
0
            papszArg = CSLAddString(papszArg, "2");
915
0
            papszArg = CSLAddString(papszArg, "-b");
916
0
            papszArg = CSLAddString(papszArg, "3");
917
0
            papszArg = CSLAddString(papszArg, "-mask");
918
0
            papszArg = CSLAddString(papszArg, "4");
919
0
        }
920
0
        GDALTranslateOptions *psOptions =
921
0
            GDALTranslateOptionsNew(papszArg, nullptr);
922
0
        CSLDestroy(papszArg);
923
0
        GDALDatasetH hRGBMaskDS = GDALTranslate(
924
0
            "", GDALDataset::ToHandle(poCurDS), psOptions, nullptr);
925
0
        GDALTranslateOptionsFree(psOptions);
926
0
        if (!hRGBMaskDS)
927
0
        {
928
0
            return nullptr;
929
0
        }
930
0
        m_poRGBMaskDS.reset(GDALDataset::FromHandle(hRGBMaskDS));
931
0
        poCurDS = m_poRGBMaskDS.get();
932
933
0
        if (bSrcHasStatistics && !bNeedStats && !bRemoveStats)
934
0
        {
935
0
            bNeedStats = true;
936
0
        }
937
0
        else if (bRemoveStats && bWrkHasStatistics)
938
0
        {
939
0
            poCurDS->ClearStatistics();
940
0
            bRemoveStats = false;
941
0
        }
942
0
    }
943
944
0
    const int nBands = poCurDS->GetRasterCount();
945
0
    const int nXSize = poCurDS->GetRasterXSize();
946
0
    const int nYSize = poCurDS->GetRasterYSize();
947
948
0
    const auto CreateVRTWithOrWithoutStats = [this, &poCurDS]()
949
0
    {
950
0
        const char *const apszOptions[] = {"-of", "VRT", nullptr};
951
0
        GDALTranslateOptions *psOptions =
952
0
            GDALTranslateOptionsNew(const_cast<char **>(apszOptions), nullptr);
953
0
        GDALDatasetH hVRTDS = GDALTranslate("", GDALDataset::ToHandle(poCurDS),
954
0
                                            psOptions, nullptr);
955
0
        GDALTranslateOptionsFree(psOptions);
956
0
        if (!hVRTDS)
957
0
            return false;
958
0
        m_poVRTWithOrWithoutStats.reset(GDALDataset::FromHandle(hVRTDS));
959
0
        poCurDS = m_poVRTWithOrWithoutStats.get();
960
0
        return true;
961
0
    };
962
963
0
    if (bNeedStats)
964
0
    {
965
0
        if (poSrcDS == poCurDS && !CreateVRTWithOrWithoutStats())
966
0
        {
967
0
            return nullptr;
968
0
        }
969
0
        poCurDS->ClearStatistics();
970
971
        // Avoid source files to be modified
972
0
        CPLConfigOptionSetter enablePamDirtyDisabler(
973
0
            "GDAL_PAM_ENABLE_MARK_DIRTY", "NO", true);
974
975
0
        for (int i = 1; i <= nBands; ++i)
976
0
        {
977
0
            poCurDS->GetRasterBand(i)->ComputeStatistics(
978
0
                /*bApproxOK=*/FALSE, nullptr, nullptr, nullptr, nullptr,
979
0
                nullptr, nullptr);
980
0
        }
981
0
    }
982
0
    else if (bRemoveStats && bWrkHasStatistics)
983
0
    {
984
0
        if (!CreateVRTWithOrWithoutStats())
985
0
            return nullptr;
986
987
0
        m_poVRTWithOrWithoutStats->ClearStatistics();
988
0
    }
989
990
0
    CPLString osBlockSize(CSLFetchNameValueDef(papszOptions, "BLOCKSIZE", ""));
991
0
    if (osBlockSize.empty())
992
0
    {
993
0
        if (poTM)
994
0
        {
995
0
            osBlockSize.Printf("%d", poTM->tileMatrixList()[0].mTileWidth);
996
0
        }
997
0
        else
998
0
        {
999
0
            osBlockSize = "512";
1000
0
        }
1001
0
    }
1002
1003
0
    const int nOvrThresholdSize = atoi(osBlockSize);
1004
1005
0
    const auto poFirstBand = poCurDS->GetRasterBand(1);
1006
0
    const bool bHasMask = poFirstBand->GetMaskFlags() == GMF_PER_DATASET;
1007
1008
0
    CPLString osOverviews =
1009
0
        CSLFetchNameValueDef(papszOptions, "OVERVIEWS", "AUTO");
1010
0
    const bool bUseExistingOrNone =
1011
0
        EQUAL(osOverviews, "FORCE_USE_EXISTING") || EQUAL(osOverviews, "NONE");
1012
1013
0
    const int nOverviewCount =
1014
0
        atoi(CSLFetchNameValueDef(papszOptions, "OVERVIEW_COUNT", "-1"));
1015
1016
0
    const bool bGenerateMskOvr =
1017
0
        !bUseExistingOrNone && bHasMask &&
1018
0
        (nXSize > nOvrThresholdSize || nYSize > nOvrThresholdSize ||
1019
0
         nOverviewCount > 0) &&
1020
0
        (EQUAL(osOverviews, "IGNORE_EXISTING") ||
1021
0
         poFirstBand->GetMaskBand()->GetOverviewCount() == 0);
1022
0
    const bool bGenerateOvr =
1023
0
        !bUseExistingOrNone &&
1024
0
        (nXSize > nOvrThresholdSize || nYSize > nOvrThresholdSize ||
1025
0
         nOverviewCount > 0) &&
1026
0
        (EQUAL(osOverviews, "IGNORE_EXISTING") ||
1027
0
         poFirstBand->GetOverviewCount() == 0);
1028
1029
0
    std::vector<std::pair<int, int>> asOverviewDims;
1030
0
    int nTmpXSize = nXSize;
1031
0
    int nTmpYSize = nYSize;
1032
0
    if (poTM)
1033
0
    {
1034
0
        const auto &tmList = poTM->tileMatrixList();
1035
0
        int nCurLevel = nZoomLevel;
1036
0
        while (true)
1037
0
        {
1038
0
            if (nOverviewCount < 0)
1039
0
            {
1040
0
                if (nTmpXSize <= nOvrThresholdSize &&
1041
0
                    nTmpYSize <= nOvrThresholdSize)
1042
0
                    break;
1043
0
            }
1044
0
            else if (static_cast<int>(asOverviewDims.size()) ==
1045
0
                         nOverviewCount ||
1046
0
                     (nTmpXSize == 1 && nTmpYSize == 1))
1047
0
            {
1048
0
                break;
1049
0
            }
1050
0
            const double dfResRatio =
1051
0
                (nCurLevel >= 1)
1052
0
                    ? tmList[nCurLevel - 1].mResX / tmList[nCurLevel].mResX
1053
0
                    : 2;
1054
0
            nTmpXSize = static_cast<int>(nTmpXSize / dfResRatio + 0.5);
1055
0
            nTmpYSize = static_cast<int>(nTmpYSize / dfResRatio + 0.5);
1056
0
            if (nTmpXSize == 0)
1057
0
                nTmpXSize = 1;
1058
0
            if (nTmpYSize == 0)
1059
0
                nTmpYSize = 1;
1060
0
            asOverviewDims.emplace_back(std::pair(nTmpXSize, nTmpYSize));
1061
0
            nCurLevel--;
1062
0
        }
1063
0
    }
1064
0
    else if (bGenerateMskOvr || bGenerateOvr)
1065
0
    {
1066
0
        if (!bGenerateOvr)
1067
0
        {
1068
            // If generating only .msk.ovr, use the exact overview size as
1069
            // the overviews of the imagery.
1070
0
            int nIters = poFirstBand->GetOverviewCount();
1071
0
            if (nOverviewCount >= 0 && nOverviewCount < nIters)
1072
0
                nIters = nOverviewCount;
1073
0
            for (int i = 0; i < nIters; i++)
1074
0
            {
1075
0
                auto poOvrBand = poFirstBand->GetOverview(i);
1076
0
                asOverviewDims.emplace_back(
1077
0
                    std::pair(poOvrBand->GetXSize(), poOvrBand->GetYSize()));
1078
0
            }
1079
0
        }
1080
0
        else
1081
0
        {
1082
0
            while (true)
1083
0
            {
1084
0
                if (nOverviewCount < 0)
1085
0
                {
1086
0
                    if (nTmpXSize <= nOvrThresholdSize &&
1087
0
                        nTmpYSize <= nOvrThresholdSize)
1088
0
                        break;
1089
0
                }
1090
0
                else if (static_cast<int>(asOverviewDims.size()) ==
1091
0
                             nOverviewCount ||
1092
0
                         (nTmpXSize == 1 && nTmpYSize == 1))
1093
0
                {
1094
0
                    break;
1095
0
                }
1096
0
                nTmpXSize /= 2;
1097
0
                nTmpYSize /= 2;
1098
0
                if (nTmpXSize == 0)
1099
0
                    nTmpXSize = 1;
1100
0
                if (nTmpYSize == 0)
1101
0
                    nTmpYSize = 1;
1102
0
                asOverviewDims.emplace_back(std::pair(nTmpXSize, nTmpYSize));
1103
0
            }
1104
0
        }
1105
0
    }
1106
1107
0
    if (dfTotalPixelsToProcess == 0.0)
1108
0
    {
1109
0
        dfTotalPixelsToProcess =
1110
0
            (bGenerateMskOvr ? double(nXSize) * nYSize / 3 : 0) +
1111
0
            (bGenerateOvr ? double(nXSize) * nYSize * nBands / 3 : 0) +
1112
0
            double(nXSize) * nYSize * (nBands + (bHasMask ? 1 : 0)) * 4. / 3;
1113
0
    }
1114
1115
0
    CPLStringList aosOverviewOptions;
1116
0
    aosOverviewOptions.SetNameValue(
1117
0
        "COMPRESS",
1118
0
        CPLGetConfigOption("COG_TMP_COMPRESSION",  // only for debug purposes
1119
0
                           HasZSTDCompression() ? "ZSTD" : "LZW"));
1120
0
    aosOverviewOptions.SetNameValue(
1121
0
        "NUM_THREADS", CSLFetchNameValue(papszOptions, "NUM_THREADS"));
1122
0
    aosOverviewOptions.SetNameValue("BIGTIFF", "YES");
1123
0
    aosOverviewOptions.SetNameValue("SPARSE_OK", "YES");
1124
1125
0
    if (bGenerateMskOvr)
1126
0
    {
1127
0
        CPLDebug("COG", "Generating overviews of the mask: start");
1128
0
        m_osTmpMskOverviewFilename = GetTmpFilename(pszFilename, "msk.ovr.tmp");
1129
0
        GDALRasterBand *poSrcMask = poFirstBand->GetMaskBand();
1130
0
        const char *pszResampling = CSLFetchNameValueDef(
1131
0
            papszOptions, "OVERVIEW_RESAMPLING",
1132
0
            CSLFetchNameValueDef(papszOptions, "RESAMPLING",
1133
0
                                 GetResampling(poSrcDS)));
1134
1135
0
        double dfNextPixels = dfCurPixels + double(nXSize) * nYSize / 3;
1136
0
        void *pScaledProgress = GDALCreateScaledProgress(
1137
0
            dfCurPixels / dfTotalPixelsToProcess,
1138
0
            dfNextPixels / dfTotalPixelsToProcess, pfnProgress, pProgressData);
1139
0
        dfCurPixels = dfNextPixels;
1140
1141
0
        CPLErr eErr = GTIFFBuildOverviewsEx(
1142
0
            m_osTmpMskOverviewFilename, 1, &poSrcMask,
1143
0
            static_cast<int>(asOverviewDims.size()), nullptr,
1144
0
            asOverviewDims.data(), pszResampling, aosOverviewOptions.List(),
1145
0
            GDALScaledProgress, pScaledProgress);
1146
0
        CPLDebug("COG", "Generating overviews of the mask: end");
1147
1148
0
        GDALDestroyScaledProgress(pScaledProgress);
1149
0
        if (eErr != CE_None)
1150
0
        {
1151
0
            return nullptr;
1152
0
        }
1153
0
    }
1154
1155
0
    std::string osOverviewResampling;
1156
0
    if (bGenerateOvr)
1157
0
    {
1158
0
        CPLDebug("COG", "Generating overviews of the imagery: start");
1159
0
        m_osTmpOverviewFilename = GetTmpFilename(pszFilename, "ovr.tmp");
1160
0
        std::vector<GDALRasterBand *> apoSrcBands;
1161
0
        for (int i = 0; i < nBands; i++)
1162
0
            apoSrcBands.push_back(poCurDS->GetRasterBand(i + 1));
1163
0
        osOverviewResampling = CSLFetchNameValueDef(
1164
0
            papszOptions, "OVERVIEW_RESAMPLING",
1165
0
            CSLFetchNameValueDef(papszOptions, "RESAMPLING",
1166
0
                                 GetResampling(poSrcDS)));
1167
1168
0
        double dfNextPixels =
1169
0
            dfCurPixels + double(nXSize) * nYSize * nBands / 3;
1170
0
        void *pScaledProgress = GDALCreateScaledProgress(
1171
0
            dfCurPixels / dfTotalPixelsToProcess,
1172
0
            dfNextPixels / dfTotalPixelsToProcess, pfnProgress, pProgressData);
1173
0
        dfCurPixels = dfNextPixels;
1174
1175
0
        if (nBands > 1)
1176
0
        {
1177
0
            aosOverviewOptions.SetNameValue("INTERLEAVE", "PIXEL");
1178
0
        }
1179
0
        if (!m_osTmpMskOverviewFilename.empty())
1180
0
        {
1181
0
            aosOverviewOptions.SetNameValue("MASK_OVERVIEW_DATASET",
1182
0
                                            m_osTmpMskOverviewFilename);
1183
0
        }
1184
0
        CPLErr eErr = GTIFFBuildOverviewsEx(
1185
0
            m_osTmpOverviewFilename, nBands, &apoSrcBands[0],
1186
0
            static_cast<int>(asOverviewDims.size()), nullptr,
1187
0
            asOverviewDims.data(), osOverviewResampling.c_str(),
1188
0
            aosOverviewOptions.List(), GDALScaledProgress, pScaledProgress);
1189
0
        CPLDebug("COG", "Generating overviews of the imagery: end");
1190
1191
0
        GDALDestroyScaledProgress(pScaledProgress);
1192
0
        if (eErr != CE_None)
1193
0
        {
1194
0
            return nullptr;
1195
0
        }
1196
0
    }
1197
0
    else if (poSrcDS->GetRasterBand(1)->GetOverviewCount() > 0)
1198
0
    {
1199
0
        const char *pszResampling =
1200
0
            poSrcDS->GetRasterBand(1)->GetOverview(0)->GetMetadataItem(
1201
0
                "RESAMPLING");
1202
0
        if (pszResampling)
1203
0
            osOverviewResampling = pszResampling;
1204
0
    }
1205
1206
0
    CPLStringList aosOptions;
1207
0
    aosOptions.SetNameValue("COPY_SRC_OVERVIEWS", "YES");
1208
0
    aosOptions.SetNameValue("COMPRESS", osCompress);
1209
0
    aosOptions.SetNameValue("TILED", "YES");
1210
0
    aosOptions.SetNameValue("BLOCKXSIZE", osBlockSize);
1211
0
    aosOptions.SetNameValue("BLOCKYSIZE", osBlockSize);
1212
0
    const char *pszPredictor =
1213
0
        CSLFetchNameValueDef(papszOptions, "PREDICTOR", "FALSE");
1214
0
    const char *pszPredictorValue = GetPredictor(poSrcDS, pszPredictor);
1215
0
    if (pszPredictorValue != nullptr)
1216
0
    {
1217
0
        aosOptions.SetNameValue("PREDICTOR", pszPredictorValue);
1218
0
    }
1219
1220
0
    const char *pszQuality = CSLFetchNameValue(papszOptions, "QUALITY");
1221
0
    if (EQUAL(osCompress, "JPEG"))
1222
0
    {
1223
0
        aosOptions.SetNameValue("JPEG_QUALITY", pszQuality);
1224
0
        if (nBands == 3 && EQUAL(pszInterleave, "PIXEL"))
1225
0
            aosOptions.SetNameValue("PHOTOMETRIC", "YCBCR");
1226
0
    }
1227
0
    else if (EQUAL(osCompress, "WEBP"))
1228
0
    {
1229
0
        if (pszQuality && atoi(pszQuality) == 100)
1230
0
            aosOptions.SetNameValue("WEBP_LOSSLESS", "YES");
1231
0
        aosOptions.SetNameValue("WEBP_LEVEL", pszQuality);
1232
0
    }
1233
0
    else if (EQUAL(osCompress, "DEFLATE") || EQUAL(osCompress, "LERC_DEFLATE"))
1234
0
    {
1235
0
        aosOptions.SetNameValue("ZLEVEL",
1236
0
                                CSLFetchNameValue(papszOptions, "LEVEL"));
1237
0
    }
1238
0
    else if (EQUAL(osCompress, "ZSTD") || EQUAL(osCompress, "LERC_ZSTD"))
1239
0
    {
1240
0
        aosOptions.SetNameValue("ZSTD_LEVEL",
1241
0
                                CSLFetchNameValue(papszOptions, "LEVEL"));
1242
0
    }
1243
0
    else if (EQUAL(osCompress, "LZMA"))
1244
0
    {
1245
0
        aosOptions.SetNameValue("LZMA_PRESET",
1246
0
                                CSLFetchNameValue(papszOptions, "LEVEL"));
1247
0
    }
1248
1249
0
    if (STARTS_WITH_CI(osCompress, "LERC"))
1250
0
    {
1251
0
        aosOptions.SetNameValue("MAX_Z_ERROR",
1252
0
                                CSLFetchNameValue(papszOptions, "MAX_Z_ERROR"));
1253
0
        aosOptions.SetNameValue(
1254
0
            "MAX_Z_ERROR_OVERVIEW",
1255
0
            CSLFetchNameValue(papszOptions, "MAX_Z_ERROR_OVERVIEW"));
1256
0
    }
1257
1258
0
    if (STARTS_WITH_CI(osCompress, "JXL"))
1259
0
    {
1260
0
        for (const char *pszKey : {"JXL_LOSSLESS", "JXL_EFFORT", "JXL_DISTANCE",
1261
0
                                   "JXL_ALPHA_DISTANCE"})
1262
0
        {
1263
0
            const char *pszValue = CSLFetchNameValue(papszOptions, pszKey);
1264
0
            if (pszValue)
1265
0
                aosOptions.SetNameValue(pszKey, pszValue);
1266
0
        }
1267
0
    }
1268
1269
0
    aosOptions.SetNameValue("BIGTIFF",
1270
0
                            CSLFetchNameValue(papszOptions, "BIGTIFF"));
1271
0
    aosOptions.SetNameValue("NUM_THREADS",
1272
0
                            CSLFetchNameValue(papszOptions, "NUM_THREADS"));
1273
0
    aosOptions.SetNameValue("GEOTIFF_VERSION",
1274
0
                            CSLFetchNameValue(papszOptions, "GEOTIFF_VERSION"));
1275
0
    aosOptions.SetNameValue("SPARSE_OK",
1276
0
                            CSLFetchNameValue(papszOptions, "SPARSE_OK"));
1277
0
    aosOptions.SetNameValue("NBITS", CSLFetchNameValue(papszOptions, "NBITS"));
1278
1279
0
    if (EQUAL(osOverviews, "NONE"))
1280
0
    {
1281
0
        aosOptions.SetNameValue("@OVERVIEW_DATASET", "");
1282
0
    }
1283
0
    else
1284
0
    {
1285
0
        if (!osOverviewResampling.empty())
1286
0
        {
1287
0
            aosOptions.SetNameValue("@OVERVIEW_RESAMPLING",
1288
0
                                    osOverviewResampling.c_str());
1289
0
        }
1290
0
        if (!m_osTmpOverviewFilename.empty())
1291
0
        {
1292
0
            aosOptions.SetNameValue("@OVERVIEW_DATASET",
1293
0
                                    m_osTmpOverviewFilename);
1294
0
        }
1295
0
        if (!m_osTmpMskOverviewFilename.empty())
1296
0
        {
1297
0
            aosOptions.SetNameValue("@MASK_OVERVIEW_DATASET",
1298
0
                                    m_osTmpMskOverviewFilename);
1299
0
        }
1300
0
        aosOptions.SetNameValue(
1301
0
            "@OVERVIEW_COUNT",
1302
0
            CSLFetchNameValue(papszOptions, "OVERVIEW_COUNT"));
1303
0
    }
1304
1305
0
    const CPLString osTilingScheme(
1306
0
        CSLFetchNameValueDef(papszOptions, "TILING_SCHEME", "CUSTOM"));
1307
0
    if (osTilingScheme != "CUSTOM")
1308
0
    {
1309
0
        aosOptions.SetNameValue("@TILING_SCHEME_NAME", osTilingScheme);
1310
0
        aosOptions.SetNameValue("@TILING_SCHEME_ZOOM_LEVEL",
1311
0
                                CPLSPrintf("%d", nZoomLevel));
1312
0
        if (nAlignedLevels > 0)
1313
0
        {
1314
0
            aosOptions.SetNameValue("@TILING_SCHEME_ALIGNED_LEVELS",
1315
0
                                    CPLSPrintf("%d", nAlignedLevels));
1316
0
        }
1317
0
    }
1318
0
    const char *pszOverviewCompress = CSLFetchNameValueDef(
1319
0
        papszOptions, "OVERVIEW_COMPRESS", osCompress.c_str());
1320
1321
0
    CPLConfigOptionSetter ovrCompressSetter("COMPRESS_OVERVIEW",
1322
0
                                            pszOverviewCompress, true);
1323
0
    const char *pszOverviewQuality =
1324
0
        CSLFetchNameValue(papszOptions, "OVERVIEW_QUALITY");
1325
0
    CPLConfigOptionSetter ovrQualityJpegSetter("JPEG_QUALITY_OVERVIEW",
1326
0
                                               pszOverviewQuality, true);
1327
1328
0
    std::unique_ptr<CPLConfigOptionSetter> poWebpLosslessSetter;
1329
0
    std::unique_ptr<CPLConfigOptionSetter> poWebpLevelSetter;
1330
0
    if (EQUAL(pszOverviewCompress, "WEBP"))
1331
0
    {
1332
0
        if (pszOverviewQuality && CPLAtof(pszOverviewQuality) == 100.0)
1333
0
        {
1334
0
            poWebpLosslessSetter.reset(new CPLConfigOptionSetter(
1335
0
                "WEBP_LOSSLESS_OVERVIEW", "TRUE", true));
1336
0
        }
1337
0
        else
1338
0
        {
1339
0
            poWebpLosslessSetter.reset(new CPLConfigOptionSetter(
1340
0
                "WEBP_LOSSLESS_OVERVIEW", "FALSE", true));
1341
0
            poWebpLevelSetter.reset(new CPLConfigOptionSetter(
1342
0
                "WEBP_LEVEL_OVERVIEW", pszOverviewQuality, true));
1343
0
        }
1344
0
    }
1345
1346
0
    std::unique_ptr<CPLConfigOptionSetter> poPhotometricSetter;
1347
0
    if (nBands == 3 && EQUAL(pszOverviewCompress, "JPEG") &&
1348
0
        EQUAL(pszInterleave, "PIXEL"))
1349
0
    {
1350
0
        poPhotometricSetter.reset(
1351
0
            new CPLConfigOptionSetter("PHOTOMETRIC_OVERVIEW", "YCBCR", true));
1352
0
    }
1353
1354
0
    const char *osOvrPredictor =
1355
0
        CSLFetchNameValueDef(papszOptions, "OVERVIEW_PREDICTOR", "FALSE");
1356
0
    const char *pszOvrPredictorValue = GetPredictor(poSrcDS, osOvrPredictor);
1357
0
    CPLConfigOptionSetter ovrPredictorSetter("PREDICTOR_OVERVIEW",
1358
0
                                             pszOvrPredictorValue, true);
1359
1360
0
    GDALDriver *poGTiffDrv =
1361
0
        GDALDriver::FromHandle(GDALGetDriverByName("GTiff"));
1362
0
    if (!poGTiffDrv)
1363
0
        return nullptr;
1364
0
    void *pScaledProgress = GDALCreateScaledProgress(
1365
0
        dfCurPixels / dfTotalPixelsToProcess, 1.0, pfnProgress, pProgressData);
1366
1367
0
    CPLConfigOptionSetter oSetterInternalMask("GDAL_TIFF_INTERNAL_MASK", "YES",
1368
0
                                              false);
1369
1370
0
    const char *pszCopySrcMDD = CSLFetchNameValue(papszOptions, "COPY_SRC_MDD");
1371
0
    if (pszCopySrcMDD)
1372
0
        aosOptions.SetNameValue("COPY_SRC_MDD", pszCopySrcMDD);
1373
0
    char **papszSrcMDD = CSLFetchNameValueMultiple(papszOptions, "SRC_MDD");
1374
0
    for (CSLConstList papszSrcMDDIter = papszSrcMDD;
1375
0
         papszSrcMDDIter && *papszSrcMDDIter; ++papszSrcMDDIter)
1376
0
        aosOptions.AddNameValue("SRC_MDD", *papszSrcMDDIter);
1377
0
    CSLDestroy(papszSrcMDD);
1378
1379
0
    if (EQUAL(pszInterleave, "TILE"))
1380
0
    {
1381
0
        aosOptions.SetNameValue("INTERLEAVE", "BAND");
1382
0
        aosOptions.SetNameValue("@TILE_INTERLEAVE", "YES");
1383
0
    }
1384
0
    else
1385
0
    {
1386
0
        aosOptions.SetNameValue("INTERLEAVE", pszInterleave);
1387
0
    }
1388
1389
0
    aosOptions.SetNameValue("@FLUSHCACHE", "YES");
1390
0
    aosOptions.SetNameValue("@SUPPRESS_ASAP",
1391
0
                            CSLFetchNameValue(papszOptions, "@SUPPRESS_ASAP"));
1392
1393
0
    CPLDebug("COG", "Generating final product: start");
1394
0
    auto poRet = std::unique_ptr<GDALDataset>(
1395
0
        poGTiffDrv->CreateCopy(pszFilename, poCurDS, false, aosOptions.List(),
1396
0
                               GDALScaledProgress, pScaledProgress));
1397
1398
0
    GDALDestroyScaledProgress(pScaledProgress);
1399
1400
0
    CPLDebug("COG", "Generating final product: end");
1401
0
    return poRet;
1402
0
}
1403
1404
/************************************************************************/
1405
/*                            COGCreateCopy()                           */
1406
/************************************************************************/
1407
1408
static GDALDataset *COGCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
1409
                                  int /*bStrict*/, char **papszOptions,
1410
                                  GDALProgressFunc pfnProgress,
1411
                                  void *pProgressData)
1412
0
{
1413
0
    return GDALCOGCreator()
1414
0
        .Create(pszFilename, poSrcDS, papszOptions, pfnProgress, pProgressData)
1415
0
        .release();
1416
0
}
1417
1418
/************************************************************************/
1419
/*                              COGProxyDataset                         */
1420
/************************************************************************/
1421
1422
class COGProxyDataset final : public GDALProxyDataset
1423
{
1424
  public:
1425
    COGProxyDataset(GDALDriver *poCOGDriver, const char *pszFilename,
1426
                    CSLConstList papszOptions,
1427
                    std::unique_ptr<GDALDataset> poGTiffTmpDS)
1428
0
        : m_poCOGDriver(poCOGDriver), m_osFilename(pszFilename),
1429
0
          m_aosOptions(papszOptions), m_poGTiffTmpDS(std::move(poGTiffTmpDS))
1430
0
    {
1431
0
        eAccess = GA_Update;
1432
0
        nRasterXSize = m_poGTiffTmpDS->GetRasterXSize();
1433
0
        nRasterYSize = m_poGTiffTmpDS->GetRasterYSize();
1434
0
        nBands = m_poGTiffTmpDS->GetRasterCount();
1435
0
        papoBands = static_cast<GDALRasterBand **>(
1436
0
            CPLMalloc(sizeof(GDALRasterBand *) * nBands));
1437
0
        for (int i = 0; i < nBands; ++i)
1438
0
            papoBands[i] = m_poGTiffTmpDS->GetRasterBand(i + 1);
1439
0
    }
1440
1441
    ~COGProxyDataset() override;
1442
1443
    CPLErr Close(GDALProgressFunc pfnProgress = nullptr,
1444
                 void *pProgressData = nullptr) override;
1445
1446
    bool GetCloseReportsProgress() const override
1447
0
    {
1448
0
        return true;
1449
0
    }
1450
1451
    GDALDriver *GetDriver() override
1452
0
    {
1453
0
        return m_poCOGDriver;
1454
0
    }
1455
1456
  protected:
1457
    GDALDataset *RefUnderlyingDataset() const override
1458
0
    {
1459
0
        return m_poGTiffTmpDS.get();
1460
0
    }
1461
1462
  private:
1463
    GDALDriver *const m_poCOGDriver;
1464
    const std::string m_osFilename;
1465
    const CPLStringList m_aosOptions;
1466
    std::unique_ptr<GDALDataset> m_poGTiffTmpDS;
1467
1468
    CPL_DISALLOW_COPY_ASSIGN(COGProxyDataset)
1469
};
1470
1471
/************************************************************************/
1472
/*                    COGProxyDataset::~COGProxyDataset()               */
1473
/************************************************************************/
1474
1475
COGProxyDataset::~COGProxyDataset()
1476
0
{
1477
0
    COGProxyDataset::Close();
1478
0
}
1479
1480
/************************************************************************/
1481
/*                              Close()                                 */
1482
/************************************************************************/
1483
1484
CPLErr COGProxyDataset::Close(GDALProgressFunc pfnProgress, void *pProgressData)
1485
0
{
1486
0
    CPLErr eErr = CE_None;
1487
0
    if (nOpenFlags != OPEN_FLAGS_CLOSED)
1488
0
    {
1489
0
        if (IsMarkedSuppressOnClose())
1490
0
        {
1491
0
            m_poGTiffTmpDS->MarkSuppressOnClose();
1492
0
        }
1493
0
        else if (!GDALCOGCreator().Create(
1494
0
                     m_osFilename.c_str(), m_poGTiffTmpDS.get(),
1495
0
                     m_aosOptions.List(), pfnProgress, pProgressData))
1496
1497
0
        {
1498
0
            eErr = CE_Failure;
1499
0
        }
1500
1501
0
        eErr = GDAL::Combine(eErr, m_poGTiffTmpDS->Close());
1502
0
        m_poGTiffTmpDS.reset();
1503
1504
0
        CPLFree(papoBands);
1505
0
        papoBands = nullptr;
1506
0
        nBands = 0;
1507
1508
0
        eErr = GDAL::Combine(eErr, GDALDataset::Close());
1509
0
    }
1510
0
    return eErr;
1511
0
}
1512
1513
/************************************************************************/
1514
/*                              COGCreate()                             */
1515
/************************************************************************/
1516
1517
static GDALDataset *COGCreate(const char *pszFilename, int nXSize, int nYSize,
1518
                              int nBands, GDALDataType eType,
1519
                              char **papszOptions)
1520
0
{
1521
0
    const std::string osTmpFile(GetTmpFilename(pszFilename, "create.tif"));
1522
0
    CPLStringList aosOptions;
1523
0
    aosOptions.SetNameValue("@CREATE_ONLY_VISIBLE_AT_CLOSE_TIME", "YES");
1524
0
    aosOptions.SetNameValue("@SUPPRESS_ASAP", "YES");
1525
0
    aosOptions.SetNameValue("TILED", "YES");
1526
0
    const char *pszBlockSize =
1527
0
        CSLFetchNameValueDef(papszOptions, "BLOCKSIZE", "512");
1528
0
    aosOptions.SetNameValue("BLOCKXSIZE", pszBlockSize);
1529
0
    aosOptions.SetNameValue("BLOCKYSIZE", pszBlockSize);
1530
1531
0
    bool bHasLZW = false;
1532
0
    bool bHasDEFLATE = false;
1533
0
    bool bHasLZMA = false;
1534
0
    bool bHasZSTD = false;
1535
0
    bool bHasJPEG = false;
1536
0
    bool bHasWebP = false;
1537
0
    bool bHasLERC = false;
1538
0
    CPL_IGNORE_RET_VAL(GTiffGetCompressValues(bHasLZW, bHasDEFLATE, bHasLZMA,
1539
0
                                              bHasZSTD, bHasJPEG, bHasWebP,
1540
0
                                              bHasLERC, true /* bForCOG */));
1541
0
    aosOptions.SetNameValue("COMPRESS", bHasZSTD ? "ZSTD" : "LZW");
1542
1543
0
    auto poGTiffTmpDS = std::unique_ptr<GDALDataset>(GTiffDataset::Create(
1544
0
        osTmpFile.c_str(), nXSize, nYSize, nBands, eType, aosOptions.List()));
1545
0
    if (!poGTiffTmpDS)
1546
0
        return nullptr;
1547
1548
0
    auto poCOGDriver = GetGDALDriverManager()->GetDriverByName("COG");
1549
0
    return std::make_unique<COGProxyDataset>(
1550
0
               poCOGDriver, pszFilename, papszOptions, std::move(poGTiffTmpDS))
1551
0
        .release();
1552
0
}
1553
1554
/************************************************************************/
1555
/*                          GDALRegister_COG()                          */
1556
/************************************************************************/
1557
1558
class GDALCOGDriver final : public GDALDriver
1559
{
1560
    std::recursive_mutex m_oMutex{};
1561
    bool m_bInitialized = false;
1562
1563
    bool bHasLZW = false;
1564
    bool bHasDEFLATE = false;
1565
    bool bHasLZMA = false;
1566
    bool bHasZSTD = false;
1567
    bool bHasJPEG = false;
1568
    bool bHasWebP = false;
1569
    bool bHasLERC = false;
1570
    std::string osCompressValues{};
1571
1572
    void InitializeCreationOptionList();
1573
1574
  public:
1575
    GDALCOGDriver();
1576
1577
    const char *GetMetadataItem(const char *pszName,
1578
                                const char *pszDomain) override;
1579
1580
    char **GetMetadata(const char *pszDomain) override
1581
0
    {
1582
0
        std::lock_guard oLock(m_oMutex);
1583
0
        InitializeCreationOptionList();
1584
0
        return GDALDriver::GetMetadata(pszDomain);
1585
0
    }
1586
};
1587
1588
GDALCOGDriver::GDALCOGDriver()
1589
0
{
1590
    // We could defer this in InitializeCreationOptionList() but with currently
1591
    // released libtiff versions where there was a bug (now fixed) in
1592
    // TIFFGetConfiguredCODECs(), this wouldn't work properly if the LERC codec
1593
    // had been registered in between
1594
0
    osCompressValues = GTiffGetCompressValues(bHasLZW, bHasDEFLATE, bHasLZMA,
1595
0
                                              bHasZSTD, bHasJPEG, bHasWebP,
1596
0
                                              bHasLERC, true /* bForCOG */);
1597
0
    gbHasLZW = bHasLZW;
1598
0
}
1599
1600
const char *GDALCOGDriver::GetMetadataItem(const char *pszName,
1601
                                           const char *pszDomain)
1602
0
{
1603
0
    std::lock_guard oLock(m_oMutex);
1604
0
    if (EQUAL(pszName, GDAL_DMD_CREATIONOPTIONLIST))
1605
0
    {
1606
0
        InitializeCreationOptionList();
1607
0
    }
1608
0
    return GDALDriver::GetMetadataItem(pszName, pszDomain);
1609
0
}
1610
1611
void GDALCOGDriver::InitializeCreationOptionList()
1612
0
{
1613
0
    if (m_bInitialized)
1614
0
        return;
1615
0
    m_bInitialized = true;
1616
1617
0
    CPLString osOptions;
1618
0
    osOptions = "<CreationOptionList>"
1619
0
                "   <Option name='COMPRESS' type='string-select' default='";
1620
0
    osOptions += bHasLZW ? "LZW" : "NONE";
1621
0
    osOptions += "'>";
1622
0
    osOptions += osCompressValues;
1623
0
    osOptions += "   </Option>";
1624
1625
0
    osOptions +=
1626
0
        "   <Option name='OVERVIEW_COMPRESS' type='string-select' default='";
1627
0
    osOptions += bHasLZW ? "LZW" : "NONE";
1628
0
    osOptions += "'>";
1629
0
    osOptions += osCompressValues;
1630
0
    osOptions += "   </Option>";
1631
1632
0
    if (bHasLZW || bHasDEFLATE || bHasZSTD || bHasLZMA)
1633
0
    {
1634
0
        const char *osPredictorOptions =
1635
0
            "     <Value>YES</Value>"
1636
0
            "     <Value>NO</Value>"
1637
0
            "     <Value alias='2'>STANDARD</Value>"
1638
0
            "     <Value alias='3'>FLOATING_POINT</Value>";
1639
1640
0
        osOptions +=
1641
0
            "   <Option name='LEVEL' type='int' "
1642
0
            "description='DEFLATE/ZSTD/LZMA compression level: 1 (fastest)'/>";
1643
1644
0
        osOptions +=
1645
0
            "   <Option name='PREDICTOR' type='string-select' default='FALSE'>";
1646
0
        osOptions += osPredictorOptions;
1647
0
        osOptions += "   </Option>"
1648
0
                     "   <Option name='OVERVIEW_PREDICTOR' "
1649
0
                     "type='string-select' default='FALSE'>";
1650
0
        osOptions += osPredictorOptions;
1651
0
        osOptions += "   </Option>";
1652
0
    }
1653
0
    if (bHasJPEG || bHasWebP)
1654
0
    {
1655
0
        std::string osJPEG_WEBP;
1656
0
        if (bHasJPEG)
1657
0
            osJPEG_WEBP = "JPEG";
1658
0
        if (bHasWebP)
1659
0
        {
1660
0
            if (!osJPEG_WEBP.empty())
1661
0
                osJPEG_WEBP += '/';
1662
0
            osJPEG_WEBP += "WEBP";
1663
0
        }
1664
0
        osOptions += "   <Option name='QUALITY' type='int' "
1665
0
                     "description='" +
1666
0
                     osJPEG_WEBP +
1667
0
                     " quality 1-100' min='1' max='100' default='75'/>"
1668
0
                     "   <Option name='OVERVIEW_QUALITY' type='int' "
1669
0
                     "description='Overview " +
1670
0
                     osJPEG_WEBP +
1671
0
                     " quality 1-100' min='1' max='100' "
1672
0
                     "default='75'/>";
1673
0
    }
1674
0
    if (bHasLERC)
1675
0
    {
1676
0
        osOptions +=
1677
0
            ""
1678
0
            "   <Option name='MAX_Z_ERROR' type='float' description='Maximum "
1679
0
            "error for LERC compression' default='0'/>"
1680
0
            "   <Option name='MAX_Z_ERROR_OVERVIEW' type='float' "
1681
0
            "description='Maximum error for LERC compression in overviews' "
1682
0
            "default='0'/>";
1683
0
    }
1684
#ifdef HAVE_JXL
1685
    osOptions +=
1686
        ""
1687
        "   <Option name='JXL_LOSSLESS' type='boolean' description='Whether "
1688
        "JPEGXL compression should be lossless' default='YES'/>"
1689
        "   <Option name='JXL_EFFORT' type='int' description='Level of effort "
1690
        "1(fast)-9(slow)' min='1' max='9' default='5'/>"
1691
        "   <Option name='JXL_DISTANCE' type='float' description='Distance "
1692
        "level for lossy compression (0=mathematically lossless, 1.0=visually "
1693
        "lossless, usual range [0.5,3])' default='1.0' min='0.01' max='25.0'/>";
1694
#ifdef HAVE_JxlEncoderSetExtraChannelDistance
1695
    osOptions += "   <Option name='JXL_ALPHA_DISTANCE' type='float' "
1696
                 "description='Distance level for alpha channel "
1697
                 "(-1=same as non-alpha channels, "
1698
                 "0=mathematically lossless, 1.0=visually lossless, "
1699
                 "usual range [0.5,3])' default='-1' min='-1' max='25.0'/>";
1700
#endif
1701
#endif
1702
0
    osOptions +=
1703
0
        "   <Option name='NUM_THREADS' type='string' "
1704
0
        "description='Number of worker threads for compression. "
1705
0
        "Can be set to ALL_CPUS' default='1'/>"
1706
0
        "   <Option name='NBITS' type='int' description='BITS for sub-byte "
1707
0
        "files (1-7), sub-uint16_t (9-15), sub-uint32_t (17-31), or float32 "
1708
0
        "(16)'/>"
1709
0
        "   <Option name='BLOCKSIZE' type='int' "
1710
0
        "description='Tile size in pixels' min='128' default='512'/>"
1711
0
        "   <Option name='INTERLEAVE' type='string-select' default='PIXEL'>"
1712
0
        "       <Value>BAND</Value>"
1713
0
        "       <Value>PIXEL</Value>"
1714
0
        "       <Value>TILE</Value>"
1715
0
        "   </Option>"
1716
0
        "   <Option name='BIGTIFF' type='string-select' description='"
1717
0
        "Force creation of BigTIFF file'>"
1718
0
        "     <Value>YES</Value>"
1719
0
        "     <Value>NO</Value>"
1720
0
        "     <Value>IF_NEEDED</Value>"
1721
0
        "     <Value>IF_SAFER</Value>"
1722
0
        "   </Option>"
1723
0
        "   <Option name='RESAMPLING' type='string' "
1724
0
        "description='Resampling method for overviews or warping'/>"
1725
0
        "   <Option name='OVERVIEW_RESAMPLING' type='string' "
1726
0
        "description='Resampling method for overviews'/>"
1727
0
        "   <Option name='WARP_RESAMPLING' type='string' "
1728
0
        "description='Resampling method for warping'/>"
1729
0
        "   <Option name='OVERVIEWS' type='string-select' description='"
1730
0
        "Behavior regarding overviews'>"
1731
0
        "     <Value>AUTO</Value>"
1732
0
        "     <Value>IGNORE_EXISTING</Value>"
1733
0
        "     <Value>FORCE_USE_EXISTING</Value>"
1734
0
        "     <Value>NONE</Value>"
1735
0
        "   </Option>"
1736
0
        "  <Option name='OVERVIEW_COUNT' type='int' min='0' "
1737
0
        "description='Number of overviews'/>"
1738
0
        "  <Option name='TILING_SCHEME' type='string-select' description='"
1739
0
        "Which tiling scheme to use pre-defined value or custom inline/outline "
1740
0
        "JSON definition' default='CUSTOM'>"
1741
0
        "    <Value>CUSTOM</Value>";
1742
1743
0
    const auto tmsList = gdal::TileMatrixSet::listPredefinedTileMatrixSets();
1744
0
    for (const auto &tmsName : tmsList)
1745
0
    {
1746
0
        const auto poTM = gdal::TileMatrixSet::parse(tmsName.c_str());
1747
0
        if (poTM && poTM->haveAllLevelsSameTopLeft() &&
1748
0
            poTM->haveAllLevelsSameTileSize() &&
1749
0
            !poTM->hasVariableMatrixWidth())
1750
0
        {
1751
0
            osOptions += "    <Value>";
1752
0
            osOptions += tmsName;
1753
0
            osOptions += "</Value>";
1754
0
        }
1755
0
    }
1756
1757
0
    osOptions +=
1758
0
        "  </Option>"
1759
0
        "  <Option name='ZOOM_LEVEL' type='int' description='Target zoom "
1760
0
        "level. "
1761
0
        "Only used for TILING_SCHEME != CUSTOM'/>"
1762
0
        "  <Option name='ZOOM_LEVEL_STRATEGY' type='string-select' "
1763
0
        "description='Strategy to determine zoom level. "
1764
0
        "Only used for TILING_SCHEME != CUSTOM' default='AUTO'>"
1765
0
        "    <Value>AUTO</Value>"
1766
0
        "    <Value>LOWER</Value>"
1767
0
        "    <Value>UPPER</Value>"
1768
0
        "  </Option>"
1769
0
        "   <Option name='TARGET_SRS' type='string' "
1770
0
        "description='Target SRS as EPSG:XXXX, WKT or PROJ string for "
1771
0
        "reprojection'/>"
1772
0
        "  <Option name='RES' type='float' description='"
1773
0
        "Target resolution for reprojection'/>"
1774
0
        "  <Option name='EXTENT' type='string' description='"
1775
0
        "Target extent as minx,miny,maxx,maxy for reprojection'/>"
1776
0
        "  <Option name='ALIGNED_LEVELS' type='int' description='"
1777
0
        "Number of resolution levels for which the tiles from GeoTIFF and the "
1778
0
        "specified tiling scheme match'/>"
1779
0
        "  <Option name='ADD_ALPHA' type='boolean' description='Can be set to "
1780
0
        "NO to "
1781
0
        "disable the addition of an alpha band in case of reprojection' "
1782
0
        "default='YES'/>"
1783
0
#if LIBGEOTIFF_VERSION >= 1600
1784
0
        "   <Option name='GEOTIFF_VERSION' type='string-select' default='AUTO' "
1785
0
        "description='Which version of GeoTIFF must be used'>"
1786
0
        "       <Value>AUTO</Value>"
1787
0
        "       <Value>1.0</Value>"
1788
0
        "       <Value>1.1</Value>"
1789
0
        "   </Option>"
1790
0
#endif
1791
0
        "   <Option name='SPARSE_OK' type='boolean' description='Should empty "
1792
0
        "blocks be omitted on disk?' default='FALSE'/>"
1793
0
        "   <Option name='STATISTICS' type='string-select' default='AUTO' "
1794
0
        "description='Which to add statistics to the output file'>"
1795
0
        "       <Value>AUTO</Value>"
1796
0
        "       <Value>YES</Value>"
1797
0
        "       <Value>NO</Value>"
1798
0
        "   </Option>"
1799
0
        "</CreationOptionList>";
1800
1801
0
    SetMetadataItem(GDAL_DMD_CREATIONOPTIONLIST, osOptions.c_str());
1802
0
}
1803
1804
void GDALRegister_COG()
1805
1806
0
{
1807
0
    if (GDALGetDriverByName("COG") != nullptr)
1808
0
        return;
1809
1810
0
    auto poDriver = new GDALCOGDriver();
1811
0
    poDriver->SetDescription("COG");
1812
0
    poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
1813
0
    poDriver->SetMetadataItem(GDAL_DMD_LONGNAME,
1814
0
                              "Cloud optimized GeoTIFF generator");
1815
0
    poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/cog.html");
1816
0
    poDriver->SetMetadataItem(GDAL_DMD_EXTENSIONS, "tif tiff");
1817
0
    poDriver->SetMetadataItem(GDAL_DMD_OVERVIEW_CREATIONOPTIONLIST,
1818
0
                              "<OverviewCreationOptionList>"
1819
0
                              "</OverviewCreationOptionList>");
1820
1821
0
    poDriver->SetMetadataItem(
1822
0
        GDAL_DMD_CREATIONDATATYPES,
1823
0
        "Byte Int8 UInt16 Int16 UInt32 Int32 UInt64 Int64 Float32 "
1824
0
        "Float64 CInt16 CInt32 CFloat32 CFloat64");
1825
1826
0
    poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
1827
0
    poDriver->SetMetadataItem(GDAL_DCAP_CREATE_ONLY_VISIBLE_AT_CLOSE_TIME,
1828
0
                              "YES");
1829
0
    poDriver->SetMetadataItem(GDAL_DCAP_CAN_READ_AFTER_DELETE, "YES");
1830
1831
0
    poDriver->SetMetadataItem(GDAL_DCAP_COORDINATE_EPOCH, "YES");
1832
1833
0
    poDriver->pfnCreateCopy = COGCreateCopy;
1834
0
    poDriver->pfnCreate = COGCreate;
1835
1836
0
    GetGDALDriverManager()->RegisterDriver(poDriver);
1837
0
}