LCOV - code coverage report
Current view: top level - frmts/gtiff - cogdriver.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 906 966 93.8 %
Date: 2026-03-30 01:17:37 Functions: 32 32 100.0 %

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

Generated by: LCOV version 1.14