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