LCOV - code coverage report
Current view: top level - apps - gdalalg_raster_slope.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 44 44 100.0 %
Date: 2025-04-16 00:42:22 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  "slope" 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_slope.h"
      14             : 
      15             : #include "gdal_priv.h"
      16             : #include "gdal_utils.h"
      17             : 
      18             : #include <cmath>
      19             : 
      20             : //! @cond Doxygen_Suppress
      21             : 
      22             : #ifndef _
      23             : #define _(x) (x)
      24             : #endif
      25             : 
      26             : /************************************************************************/
      27             : /*       GDALRasterSlopeAlgorithm::GDALRasterSlopeAlgorithm()   */
      28             : /************************************************************************/
      29             : 
      30          22 : GDALRasterSlopeAlgorithm::GDALRasterSlopeAlgorithm(bool standaloneStep)
      31             :     : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
      32          22 :                                       standaloneStep)
      33             : {
      34          22 :     SetOutputVRTCompatible(false);
      35             : 
      36          22 :     AddBandArg(&m_band).SetDefault(m_band);
      37          44 :     AddArg("unit", 0, _("Unit in which to express slopes"), &m_unit)
      38          22 :         .SetChoices("degree", "percent")
      39          22 :         .SetDefault(m_unit);
      40             :     AddArg("xscale", 0, _("Ratio of vertical units to horizontal X axis units"),
      41          44 :            &m_xscale)
      42          22 :         .SetMinValueExcluded(0);
      43             :     AddArg("yscale", 0, _("Ratio of vertical units to horizontal Y axis units"),
      44          44 :            &m_yscale)
      45          22 :         .SetMinValueExcluded(0);
      46             :     AddArg("gradient-alg", 0, _("Algorithm used to compute terrain gradient"),
      47          44 :            &m_gradientAlg)
      48          22 :         .SetChoices("Horn", "ZevenbergenThorne")
      49          22 :         .SetDefault(m_gradientAlg);
      50             :     AddArg("no-edges", 0,
      51             :            _("Do not try to interpolate values at dataset edges or close to "
      52             :              "nodata values"),
      53          22 :            &m_noEdges);
      54          22 : }
      55             : 
      56             : /************************************************************************/
      57             : /*              GDALRasterSlopeAlgorithm::RunStep()                 */
      58             : /************************************************************************/
      59             : 
      60           9 : bool GDALRasterSlopeAlgorithm::RunStep(GDALProgressFunc, void *)
      61             : {
      62           9 :     CPLAssert(m_inputDataset.GetDatasetRef());
      63           9 :     CPLAssert(m_outputDataset.GetName().empty());
      64           9 :     CPLAssert(!m_outputDataset.GetDatasetRef());
      65             : 
      66          18 :     CPLStringList aosOptions;
      67           9 :     aosOptions.AddString("-of");
      68           9 :     aosOptions.AddString("stream");
      69           9 :     aosOptions.AddString("-b");
      70           9 :     aosOptions.AddString(CPLSPrintf("%d", m_band));
      71           9 :     if (!std::isnan(m_xscale))
      72             :     {
      73           2 :         aosOptions.AddString("-xscale");
      74           2 :         aosOptions.AddString(CPLSPrintf("%.17g", m_xscale));
      75             :     }
      76           9 :     if (!std::isnan(m_yscale))
      77             :     {
      78           2 :         aosOptions.AddString("-yscale");
      79           2 :         aosOptions.AddString(CPLSPrintf("%.17g", m_yscale));
      80             :     }
      81           9 :     if (m_unit == "percent")
      82           1 :         aosOptions.AddString("-p");
      83           9 :     aosOptions.AddString("-alg");
      84           9 :     aosOptions.AddString(m_gradientAlg.c_str());
      85             : 
      86           9 :     if (!m_noEdges)
      87           7 :         aosOptions.AddString("-compute_edges");
      88             : 
      89             :     GDALDEMProcessingOptions *psOptions =
      90           9 :         GDALDEMProcessingOptionsNew(aosOptions.List(), nullptr);
      91             : 
      92             :     auto poOutDS =
      93             :         std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(GDALDEMProcessing(
      94             :             "", GDALDataset::ToHandle(m_inputDataset.GetDatasetRef()), "slope",
      95           9 :             nullptr, psOptions, nullptr)));
      96           9 :     GDALDEMProcessingOptionsFree(psOptions);
      97           9 :     const bool bRet = poOutDS != nullptr;
      98           9 :     if (poOutDS)
      99             :     {
     100           9 :         m_outputDataset.Set(std::move(poOutDS));
     101             :     }
     102             : 
     103          18 :     return bRet;
     104             : }
     105             : 
     106             : //! @endcond

Generated by: LCOV version 1.14