LCOV - code coverage report
Current view: top level - apps - gdalalg_raster_convert.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 42 42 100.0 %
Date: 2025-05-15 18:21:54 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  gdal "raster convert" subcommand
       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_convert.h"
      14             : 
      15             : #include "cpl_conv.h"
      16             : #include "gdal_priv.h"
      17             : #include "gdal_utils.h"
      18             : 
      19             : //! @cond Doxygen_Suppress
      20             : 
      21             : #ifndef _
      22             : #define _(x) (x)
      23             : #endif
      24             : 
      25             : /************************************************************************/
      26             : /*          GDALRasterConvertAlgorithm::GDALRasterConvertAlgorithm()    */
      27             : /************************************************************************/
      28             : 
      29          43 : GDALRasterConvertAlgorithm::GDALRasterConvertAlgorithm(
      30          43 :     bool openForMixedRasterVector)
      31          43 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      32             : {
      33          43 :     AddProgressArg();
      34          43 :     AddOutputFormatArg(&m_outputFormat)
      35             :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES,
      36         129 :                          {GDAL_DCAP_RASTER, GDAL_DCAP_CREATECOPY});
      37          43 :     AddOpenOptionsArg(&m_openOptions);
      38          43 :     AddInputFormatsArg(&m_inputFormats)
      39          86 :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_RASTER});
      40             :     AddInputDatasetArg(&m_inputDataset, openForMixedRasterVector
      41             :                                             ? GDAL_OF_RASTER | GDAL_OF_VECTOR
      42          43 :                                             : GDAL_OF_RASTER);
      43          43 :     AddOutputDatasetArg(&m_outputDataset, GDAL_OF_RASTER);
      44          43 :     AddCreationOptionsArg(&m_creationOptions);
      45          43 :     const char *exclusionGroup = "overwrite-append";
      46          43 :     AddOverwriteArg(&m_overwrite).SetMutualExclusionGroup(exclusionGroup);
      47             :     AddArg(GDAL_ARG_NAME_APPEND, 0,
      48          86 :            _("Append as a subdataset to existing output"), &m_append)
      49          43 :         .SetDefault(false)
      50          43 :         .SetMutualExclusionGroup(exclusionGroup);
      51          43 : }
      52             : 
      53             : /************************************************************************/
      54             : /*                  GDALRasterConvertAlgorithm::RunImpl()               */
      55             : /************************************************************************/
      56             : 
      57          12 : bool GDALRasterConvertAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
      58             :                                          void *pProgressData)
      59             : {
      60          12 :     CPLAssert(m_inputDataset.GetDatasetRef());
      61          12 :     CPLAssert(!m_outputDataset.GetDatasetRef());
      62             : 
      63          24 :     CPLStringList aosOptions;
      64          12 :     if (!m_outputFormat.empty())
      65             :     {
      66           1 :         aosOptions.AddString("-of");
      67           1 :         aosOptions.AddString(m_outputFormat.c_str());
      68             :     }
      69          12 :     if (!m_overwrite)
      70             :     {
      71          11 :         aosOptions.AddString("--no-overwrite");
      72             :     }
      73          12 :     if (m_append)
      74             :     {
      75           2 :         aosOptions.AddString("-co");
      76           2 :         aosOptions.AddString("APPEND_SUBDATASET=YES");
      77             :     }
      78          14 :     for (const auto &co : m_creationOptions)
      79             :     {
      80           2 :         aosOptions.AddString("-co");
      81           2 :         aosOptions.AddString(co.c_str());
      82             :     }
      83             : 
      84             :     GDALTranslateOptions *psOptions =
      85          12 :         GDALTranslateOptionsNew(aosOptions.List(), nullptr);
      86          12 :     GDALTranslateOptionsSetProgress(psOptions, pfnProgress, pProgressData);
      87             : 
      88             :     auto poOutDS = std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(
      89          12 :         GDALTranslate(m_outputDataset.GetName().c_str(),
      90             :                       GDALDataset::ToHandle(m_inputDataset.GetDatasetRef()),
      91          36 :                       psOptions, nullptr)));
      92          12 :     GDALTranslateOptionsFree(psOptions);
      93          12 :     if (!poOutDS)
      94           2 :         return false;
      95             : 
      96          10 :     m_outputDataset.Set(std::move(poOutDS));
      97             : 
      98          10 :     return true;
      99             : }
     100             : 
     101             : //! @endcond

Generated by: LCOV version 1.14