LCOV - code coverage report
Current view: top level - apps - gdalalg_raster_write.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 35 35 100.0 %
Date: 2025-05-15 13:16:46 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         286 : GDALRasterWriteAlgorithm::GDALRasterWriteAlgorithm()
      26             :     : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
      27         286 :                                       /* standaloneStep =*/false)
      28             : {
      29         286 :     AddOutputArgs(/* hiddenForCLI = */ false);
      30         286 : }
      31             : 
      32             : /************************************************************************/
      33             : /*                  GDALRasterWriteAlgorithm::RunStep()                 */
      34             : /************************************************************************/
      35             : 
      36         202 : bool GDALRasterWriteAlgorithm::RunStep(GDALProgressFunc pfnProgress,
      37             :                                        void *pProgressData)
      38             : {
      39         202 :     CPLAssert(m_inputDataset.GetDatasetRef());
      40         202 :     CPLAssert(!m_outputDataset.GetDatasetRef());
      41             : 
      42         202 :     if (m_format == "stream")
      43             :     {
      44           8 :         m_outputDataset.Set(m_inputDataset.GetDatasetRef());
      45           8 :         return true;
      46             :     }
      47             : 
      48         388 :     CPLStringList aosOptions;
      49         194 :     if (!m_overwrite)
      50             :     {
      51         176 :         aosOptions.AddString("--no-overwrite");
      52             :     }
      53         194 :     if (!m_format.empty())
      54             :     {
      55         113 :         aosOptions.AddString("-of");
      56         113 :         aosOptions.AddString(m_format.c_str());
      57             :     }
      58         206 :     for (const auto &co : m_creationOptions)
      59             :     {
      60          12 :         aosOptions.AddString("-co");
      61          12 :         aosOptions.AddString(co.c_str());
      62             :     }
      63             : 
      64             :     GDALTranslateOptions *psOptions =
      65         194 :         GDALTranslateOptionsNew(aosOptions.List(), nullptr);
      66         194 :     GDALTranslateOptionsSetProgress(psOptions, pfnProgress, pProgressData);
      67             : 
      68             :     // Backup error state since GDALTranslate() resets it multiple times
      69         194 :     const auto nLastErrorNum = CPLGetLastErrorNo();
      70         194 :     const auto nLastErrorType = CPLGetLastErrorType();
      71         388 :     const std::string osLastErrorMsg = CPLGetLastErrorMsg();
      72         194 :     const auto nLastErrorCounter = CPLGetErrorCounter();
      73             : 
      74         194 :     GDALDatasetH hSrcDS = GDALDataset::ToHandle(m_inputDataset.GetDatasetRef());
      75         194 :     auto poRetDS = GDALDataset::FromHandle(GDALTranslate(
      76         194 :         m_outputDataset.GetName().c_str(), hSrcDS, psOptions, nullptr));
      77         194 :     GDALTranslateOptionsFree(psOptions);
      78             : 
      79         194 :     if (nLastErrorCounter > 0 && CPLGetErrorCounter() == 0)
      80             :     {
      81          10 :         CPLErrorSetState(nLastErrorType, nLastErrorNum, osLastErrorMsg.c_str(),
      82             :                          &nLastErrorCounter);
      83             :     }
      84             : 
      85         194 :     if (!poRetDS)
      86           1 :         return false;
      87             : 
      88         193 :     m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS));
      89             : 
      90         193 :     return true;
      91             : }
      92             : 
      93             : //! @endcond

Generated by: LCOV version 1.14