LCOV - code coverage report
Current view: top level - apps - gdalalg_raster_write.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 44 44 100.0 %
Date: 2026-04-05 12:09:15 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  "write" step of "raster pipeline"
       5             :  * Author:   Even Rouault <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "gdalalg_raster_write.h"
      14             : 
      15             : #include "cpl_string.h"
      16             : #include "gdal_utils.h"
      17             : #include "gdal_priv.h"
      18             : 
      19             : //! @cond Doxygen_Suppress
      20             : 
      21             : /************************************************************************/
      22             : /*         GDALRasterWriteAlgorithm::GDALRasterWriteAlgorithm()         */
      23             : /************************************************************************/
      24             : 
      25         693 : GDALRasterWriteAlgorithm::GDALRasterWriteAlgorithm()
      26             :     : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
      27         693 :                                       /* standaloneStep =*/false)
      28             : {
      29         693 :     AddRasterOutputArgs(/* hiddenForCLI = */ false);
      30         693 : }
      31             : 
      32             : /************************************************************************/
      33             : /*                 GDALRasterWriteAlgorithm::RunStep()                  */
      34             : /************************************************************************/
      35             : 
      36         441 : bool GDALRasterWriteAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
      37             : {
      38         441 :     auto pfnProgress = ctxt.m_pfnProgress;
      39         441 :     auto pProgressData = ctxt.m_pProgressData;
      40         441 :     auto poSrcDS = m_inputDataset[0].GetDatasetRef();
      41         441 :     CPLAssert(poSrcDS);
      42         441 :     CPLAssert(!m_outputDataset.GetDatasetRef());
      43             : 
      44         441 :     if (m_format == "stream")
      45             :     {
      46         184 :         m_outputDataset.Set(poSrcDS);
      47         184 :         return true;
      48             :     }
      49             : 
      50         514 :     CPLStringList aosOptions;
      51         257 :     if (!m_overwrite)
      52             :     {
      53         239 :         aosOptions.AddString("--no-overwrite");
      54             :     }
      55         257 :     if (m_appendRaster)
      56             :     {
      57           6 :         CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
      58           6 :         if (std::unique_ptr<GDALDataset>(GDALDataset::Open(
      59           6 :                 m_outputDataset.GetName().c_str(), GDAL_OF_RASTER)))
      60             :         {
      61           2 :             aosOptions.AddString("-co");
      62           2 :             aosOptions.AddString("APPEND_SUBDATASET=YES");
      63             :         }
      64             :     }
      65         257 :     if (!m_format.empty())
      66             :     {
      67         137 :         aosOptions.AddString("-of");
      68         137 :         aosOptions.AddString(m_format.c_str());
      69             :     }
      70         274 :     for (const auto &co : m_creationOptions)
      71             :     {
      72          17 :         aosOptions.AddString("-co");
      73          17 :         aosOptions.AddString(co.c_str());
      74             :     }
      75             : 
      76             :     GDALTranslateOptions *psOptions =
      77         257 :         GDALTranslateOptionsNew(aosOptions.List(), nullptr);
      78         257 :     GDALTranslateOptionsSetProgress(psOptions, pfnProgress, pProgressData);
      79             : 
      80             :     // Backup error state since GDALTranslate() resets it multiple times
      81         257 :     const auto nLastErrorNum = CPLGetLastErrorNo();
      82         257 :     const auto nLastErrorType = CPLGetLastErrorType();
      83         514 :     const std::string osLastErrorMsg = CPLGetLastErrorMsg();
      84         257 :     const auto nLastErrorCounter = CPLGetErrorCounter();
      85             : 
      86         257 :     GDALDatasetH hSrcDS = GDALDataset::ToHandle(poSrcDS);
      87         257 :     auto poRetDS = GDALDataset::FromHandle(GDALTranslate(
      88         257 :         m_outputDataset.GetName().c_str(), hSrcDS, psOptions, nullptr));
      89         257 :     GDALTranslateOptionsFree(psOptions);
      90             : 
      91         257 :     if (nLastErrorCounter > 0 && CPLGetErrorCounter() == 0)
      92             :     {
      93           5 :         CPLErrorSetState(nLastErrorType, nLastErrorNum, osLastErrorMsg.c_str(),
      94             :                          &nLastErrorCounter);
      95             :     }
      96             : 
      97         257 :     if (!poRetDS)
      98           4 :         return false;
      99             : 
     100         253 :     m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS));
     101             : 
     102         253 :     return true;
     103             : }
     104             : 
     105             : //! @endcond

Generated by: LCOV version 1.14