Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: "aspect" 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_aspect.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 : /* GDALRasterAspectAlgorithm::GDALRasterAspectAlgorithm() */ 28 : /************************************************************************/ 29 : 30 37 : GDALRasterAspectAlgorithm::GDALRasterAspectAlgorithm(bool standaloneStep) 31 : : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, 32 37 : standaloneStep) 33 : { 34 37 : SetOutputVRTCompatible(false); 35 : 36 37 : AddBandArg(&m_band).SetDefault(m_band); 37 74 : AddArg("convention", 0, _("Convention for output angles"), &m_convention) 38 37 : .SetChoices("azimuth", "trigonometric-angle") 39 37 : .SetDefault(m_convention); 40 : AddArg("gradient-alg", 0, _("Algorithm used to compute terrain gradient"), 41 74 : &m_gradientAlg) 42 37 : .SetChoices("Horn", "ZevenbergenThorne") 43 37 : .SetDefault(m_gradientAlg); 44 : AddArg("zero-for-flat", 0, _("Whether to output zero for flat areas"), 45 37 : &m_zeroForFlat); 46 : AddArg("no-edges", 0, 47 : _("Do not try to interpolate values at dataset edges or close to " 48 : "nodata values"), 49 37 : &m_noEdges); 50 37 : } 51 : 52 : /************************************************************************/ 53 : /* GDALRasterAspectAlgorithm::RunStep() */ 54 : /************************************************************************/ 55 : 56 10 : bool GDALRasterAspectAlgorithm::RunStep(GDALPipelineStepRunContext &) 57 : { 58 10 : const auto poSrcDS = m_inputDataset[0].GetDatasetRef(); 59 10 : CPLAssert(poSrcDS); 60 10 : CPLAssert(m_outputDataset.GetName().empty()); 61 10 : CPLAssert(!m_outputDataset.GetDatasetRef()); 62 : 63 20 : CPLStringList aosOptions; 64 10 : aosOptions.AddString("-of"); 65 10 : aosOptions.AddString("stream"); 66 10 : aosOptions.AddString("-b"); 67 10 : aosOptions.AddString(CPLSPrintf("%d", m_band)); 68 10 : if (m_convention == "trigonometric-angle") 69 1 : aosOptions.AddString("-trigonometric"); 70 10 : aosOptions.AddString("-alg"); 71 10 : aosOptions.AddString(m_gradientAlg.c_str()); 72 10 : if (m_zeroForFlat) 73 1 : aosOptions.AddString("-zero_for_flat"); 74 10 : if (!m_noEdges) 75 8 : aosOptions.AddString("-compute_edges"); 76 : 77 : GDALDEMProcessingOptions *psOptions = 78 10 : GDALDEMProcessingOptionsNew(aosOptions.List(), nullptr); 79 : 80 : auto poOutDS = std::unique_ptr<GDALDataset>(GDALDataset::FromHandle( 81 : GDALDEMProcessing("", GDALDataset::ToHandle(poSrcDS), "aspect", nullptr, 82 10 : psOptions, nullptr))); 83 10 : GDALDEMProcessingOptionsFree(psOptions); 84 10 : const bool bRet = poOutDS != nullptr; 85 10 : if (poOutDS) 86 : { 87 10 : m_outputDataset.Set(std::move(poOutDS)); 88 : } 89 : 90 20 : return bRet; 91 : } 92 : 93 : GDALRasterAspectAlgorithmStandalone::~GDALRasterAspectAlgorithmStandalone() = 94 : default; 95 : 96 : //! @endcond