LCOV - code coverage report
Current view: top level - apps - gdalalg_raster_convert.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 44 44 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:  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          39 : GDALRasterConvertAlgorithm::GDALRasterConvertAlgorithm(
      30          39 :     bool openForMixedRasterVector)
      31          39 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      32             : {
      33          39 :     AddProgressArg();
      34          39 :     AddOutputFormatArg(&m_outputFormat)
      35             :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES,
      36         117 :                          {GDAL_DCAP_RASTER, GDAL_DCAP_CREATECOPY});
      37          39 :     AddOpenOptionsArg(&m_openOptions);
      38          39 :     AddInputFormatsArg(&m_inputFormats)
      39          78 :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_RASTER});
      40             :     AddInputDatasetArg(&m_inputDataset, openForMixedRasterVector
      41             :                                             ? GDAL_OF_RASTER | GDAL_OF_VECTOR |
      42             :                                                   GDAL_OF_MULTIDIM_RASTER
      43          39 :                                             : GDAL_OF_RASTER);
      44          39 :     AddOutputDatasetArg(&m_outputDataset, GDAL_OF_RASTER);
      45          39 :     AddCreationOptionsArg(&m_creationOptions);
      46          39 :     const char *exclusionGroup = "overwrite-append";
      47          39 :     AddOverwriteArg(&m_overwrite).SetMutualExclusionGroup(exclusionGroup);
      48             :     AddArg("append", 0, _("Append as a subdataset to existing output"),
      49          78 :            &m_append)
      50          39 :         .SetDefault(false)
      51          39 :         .SetMutualExclusionGroup(exclusionGroup);
      52          39 : }
      53             : 
      54             : /************************************************************************/
      55             : /*                  GDALRasterConvertAlgorithm::RunImpl()               */
      56             : /************************************************************************/
      57             : 
      58          12 : bool GDALRasterConvertAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
      59             :                                          void *pProgressData)
      60             : {
      61          12 :     CPLAssert(m_inputDataset.GetDatasetRef());
      62          12 :     if (m_outputDataset.GetDatasetRef())
      63             :     {
      64           1 :         CPLError(CE_Failure, CPLE_NotSupported,
      65             :                  "gdal raster convert does not support outputting to an "
      66             :                  "already opened output dataset");
      67           1 :         return false;
      68             :     }
      69             : 
      70          22 :     CPLStringList aosOptions;
      71          11 :     if (!m_outputFormat.empty())
      72             :     {
      73           1 :         aosOptions.AddString("-of");
      74           1 :         aosOptions.AddString(m_outputFormat.c_str());
      75             :     }
      76          11 :     if (!m_overwrite)
      77             :     {
      78          10 :         aosOptions.AddString("--no-overwrite");
      79             :     }
      80          11 :     if (m_append)
      81             :     {
      82           1 :         aosOptions.AddString("-co");
      83           1 :         aosOptions.AddString("APPEND_SUBDATASET=YES");
      84             :     }
      85          13 :     for (const auto &co : m_creationOptions)
      86             :     {
      87           2 :         aosOptions.AddString("-co");
      88           2 :         aosOptions.AddString(co.c_str());
      89             :     }
      90             : 
      91             :     GDALTranslateOptions *psOptions =
      92          11 :         GDALTranslateOptionsNew(aosOptions.List(), nullptr);
      93          11 :     GDALTranslateOptionsSetProgress(psOptions, pfnProgress, pProgressData);
      94             : 
      95             :     auto poOutDS = std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(
      96          11 :         GDALTranslate(m_outputDataset.GetName().c_str(),
      97             :                       GDALDataset::ToHandle(m_inputDataset.GetDatasetRef()),
      98          33 :                       psOptions, nullptr)));
      99          11 :     GDALTranslateOptionsFree(psOptions);
     100          11 :     if (!poOutDS)
     101           2 :         return false;
     102             : 
     103           9 :     m_outputDataset.Set(std::move(poOutDS));
     104             : 
     105           9 :     return true;
     106             : }
     107             : 
     108             : //! @endcond

Generated by: LCOV version 1.14