LCOV - code coverage report
Current view: top level - apps - gdalalg_raster_resize.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 60 60 100.0 %
Date: 2026-11-17 20:26:34 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  "resize" step of "raster pipeline"
       5             :  * Author:   Even Rouault <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "gdalalg_raster_resize.h"
      14             : 
      15             : #include "gdal_priv.h"
      16             : #include "gdal_utils.h"
      17             : 
      18             : //! @cond Doxygen_Suppress
      19             : 
      20             : #ifndef _
      21             : #define _(x) (x)
      22             : #endif
      23             : 
      24             : /************************************************************************/
      25             : /*        GDALRasterResizeAlgorithm::GDALRasterResizeAlgorithm()        */
      26             : /************************************************************************/
      27             : 
      28          96 : GDALRasterResizeAlgorithm::GDALRasterResizeAlgorithm(bool standaloneStep)
      29             :     : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
      30          96 :                                       standaloneStep)
      31             : {
      32             :     AddArg("resolution", 0, _("Target resolution (in destination CRS units)"),
      33         192 :            &m_resolution)
      34          96 :         .SetMinCount(2)
      35          96 :         .SetMaxCount(2)
      36          96 :         .SetMinValueExcluded(0)
      37          96 :         .SetRepeatedArgAllowed(false)
      38          96 :         .SetDisplayHintAboutRepetition(false)
      39         192 :         .SetMetaVar("<xres>,<yres>")
      40          96 :         .SetMutualExclusionGroup("resolution-size");
      41             : 
      42             :     // The same logic is applied in gdalalg_raster_create.cpp
      43             :     AddArg("size", 0,
      44             :            _("Target size in pixels (or percentage if using '%' suffix)"),
      45         192 :            &m_size)
      46          96 :         .SetMinCount(2)
      47          96 :         .SetMaxCount(2)
      48          96 :         .SetRequired()
      49          96 :         .SetMinValueIncluded(0)
      50          96 :         .SetRepeatedArgAllowed(false)
      51          96 :         .SetDisplayHintAboutRepetition(false)
      52         192 :         .SetMetaVar("<width[%]>,<height[%]>")
      53         192 :         .SetMutualExclusionGroup("resolution-size")
      54             :         .AddValidationAction(
      55          28 :             [this]()
      56             :             {
      57          59 :                 for (const auto &s : m_size)
      58             :                 {
      59          41 :                     auto trimmed = cpl::trim(s);
      60          41 :                     if (!trimmed.empty() && trimmed.back() == '%')
      61             :                     {
      62           6 :                         trimmed = trimmed.substr(0, trimmed.size() - 1);
      63             :                     }
      64             : 
      65          41 :                     if (cpl::strict_parse<int>(trimmed).value_or(-1) < 0)
      66             :                     {
      67           5 :                         ReportError(CE_Failure, CPLE_IllegalArg,
      68             :                                     "Invalid size value: %s'", s.c_str());
      69           5 :                         return false;
      70             :                     }
      71             :                 }
      72          18 :                 return true;
      73          96 :             });
      74         192 :     AddArg("resampling", 'r', _("Resampling method"), &m_resampling)
      75             :         .SetChoices("nearest", "bilinear", "cubic", "cubicspline", "lanczos",
      76          96 :                     "average", "mode")
      77          96 :         .SetDefault("nearest")
      78          96 :         .SetHiddenChoices("near");
      79          96 : }
      80             : 
      81             : /************************************************************************/
      82             : /*                 GDALRasterResizeAlgorithm::RunStep()                 */
      83             : /************************************************************************/
      84             : 
      85           7 : bool GDALRasterResizeAlgorithm::RunStep(GDALPipelineStepRunContext &)
      86             : {
      87           7 :     const auto poSrcDS = m_inputDataset[0].GetDatasetRef();
      88           7 :     CPLAssert(poSrcDS);
      89           7 :     CPLAssert(m_outputDataset.GetName().empty());
      90           7 :     CPLAssert(!m_outputDataset.GetDatasetRef());
      91             : 
      92          14 :     CPLStringList aosOptions;
      93           7 :     aosOptions.AddString("-of");
      94           7 :     aosOptions.AddString("VRT");
      95           7 :     if (!m_size.empty())
      96             :     {
      97           6 :         aosOptions.AddString("-outsize");
      98           6 :         aosOptions.AddString(m_size[0]);
      99           6 :         aosOptions.AddString(m_size[1]);
     100             :     }
     101           7 :     if (!m_resolution.empty())
     102             :     {
     103           1 :         aosOptions.AddString("-tr");
     104           1 :         aosOptions.AddString(m_resolution[0]);
     105           1 :         aosOptions.AddString(m_resolution[1]);
     106             :     }
     107           7 :     if (!m_resampling.empty())
     108             :     {
     109           7 :         aosOptions.AddString("-r");
     110           7 :         aosOptions.AddString(m_resampling.c_str());
     111             :     }
     112             : 
     113             :     GDALTranslateOptions *psOptions =
     114           7 :         GDALTranslateOptionsNew(aosOptions.List(), nullptr);
     115             : 
     116             :     auto poOutDS = std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(
     117           7 :         GDALTranslate("", GDALDataset::ToHandle(poSrcDS), psOptions, nullptr)));
     118           7 :     GDALTranslateOptionsFree(psOptions);
     119           7 :     const bool bRet = poOutDS != nullptr;
     120           7 :     if (poOutDS)
     121             :     {
     122           7 :         m_outputDataset.Set(std::move(poOutDS));
     123             :     }
     124             : 
     125          14 :     return bRet;
     126             : }
     127             : 
     128             : GDALRasterResizeAlgorithmStandalone::~GDALRasterResizeAlgorithmStandalone() =
     129             :     default;
     130             : 
     131             : //! @endcond

Generated by: LCOV version 1.14