LCOV - code coverage report
Current view: top level - apps - gdalalg_raster_write.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 26 26 100.0 %
Date: 2025-01-18 12:42:00 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          47 : GDALRasterWriteAlgorithm::GDALRasterWriteAlgorithm()
      26             :     : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
      27          47 :                                       /* standaloneStep =*/false)
      28             : {
      29          47 :     AddOutputArgs(/* hiddenForCLI = */ false);
      30          47 : }
      31             : 
      32             : /************************************************************************/
      33             : /*                  GDALRasterWriteAlgorithm::RunStep()                 */
      34             : /************************************************************************/
      35             : 
      36          24 : bool GDALRasterWriteAlgorithm::RunStep(GDALProgressFunc pfnProgress,
      37             :                                        void *pProgressData)
      38             : {
      39          24 :     CPLAssert(m_inputDataset.GetDatasetRef());
      40          24 :     CPLAssert(!m_outputDataset.GetDatasetRef());
      41             : 
      42          48 :     CPLStringList aosOptions;
      43          24 :     if (!m_overwrite)
      44             :     {
      45          13 :         aosOptions.AddString("--no-overwrite");
      46             :     }
      47          24 :     if (!m_format.empty())
      48             :     {
      49           1 :         aosOptions.AddString("-of");
      50           1 :         aosOptions.AddString(m_format.c_str());
      51             :     }
      52          25 :     for (const auto &co : m_creationOptions)
      53             :     {
      54           1 :         aosOptions.AddString("-co");
      55           1 :         aosOptions.AddString(co.c_str());
      56             :     }
      57             : 
      58             :     GDALTranslateOptions *psOptions =
      59          24 :         GDALTranslateOptionsNew(aosOptions.List(), nullptr);
      60          24 :     GDALTranslateOptionsSetProgress(psOptions, pfnProgress, pProgressData);
      61             : 
      62          24 :     GDALDatasetH hSrcDS = GDALDataset::ToHandle(m_inputDataset.GetDatasetRef());
      63          24 :     auto poRetDS = GDALDataset::FromHandle(GDALTranslate(
      64          24 :         m_outputDataset.GetName().c_str(), hSrcDS, psOptions, nullptr));
      65          24 :     GDALTranslateOptionsFree(psOptions);
      66          24 :     if (!poRetDS)
      67           1 :         return false;
      68             : 
      69          23 :     m_outputDataset.Set(std::unique_ptr<GDALDataset>(poRetDS));
      70             : 
      71          23 :     return true;
      72             : }
      73             : 
      74             : //! @endcond

Generated by: LCOV version 1.14