Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/gdalalg_raster_contour.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL
4
 * Purpose:  gdal "raster contour" subcommand
5
 * Author:   Alessandro Pasotti <elpaso at itopen dot it>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2025, Alessandro Pasotti <elpaso at itopen dot it>
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12
13
#include <cmath>
14
15
#include "gdalalg_raster_contour.h"
16
17
#include "cpl_conv.h"
18
#include "gdal_priv.h"
19
#include "gdal_utils.h"
20
#include "gdal_alg.h"
21
#include "gdal_utils_priv.h"
22
23
//! @cond Doxygen_Suppress
24
25
#ifndef _
26
0
#define _(x) (x)
27
#endif
28
29
/************************************************************************/
30
/*       GDALRasterContourAlgorithm::GDALRasterContourAlgorithm()       */
31
/************************************************************************/
32
33
GDALRasterContourAlgorithm::GDALRasterContourAlgorithm(bool standaloneStep)
34
0
    : GDALPipelineStepAlgorithm(
35
0
          NAME, DESCRIPTION, HELP_URL,
36
0
          ConstructorOptions()
37
0
              .SetStandaloneStep(standaloneStep)
38
0
              .SetAddAppendLayerArgument(false)
39
0
              .SetAddOverwriteLayerArgument(false)
40
0
              .SetAddUpdateArgument(false)
41
0
              .SetAddUpsertArgument(false)
42
0
              .SetAddSkipErrorsArgument(false)
43
0
              .SetOutputFormatCreateCapability(GDAL_DCAP_CREATE))
44
0
{
45
0
    m_outputLayerName = "contour";
46
47
0
    AddProgressArg();
48
0
    if (standaloneStep)
49
0
    {
50
0
        AddRasterInputArgs(false, false);
51
0
        AddVectorOutputArgs(false, false);
52
0
    }
53
0
    else
54
0
    {
55
0
        AddRasterHiddenInputDatasetArg();
56
0
        AddOutputLayerNameArg(/* hiddenForCLI = */ false,
57
0
                              /* shortNameOutputLayerAllowed = */ false);
58
0
    }
59
60
    // gdal_contour specific options
61
0
    AddBandArg(&m_band).SetDefault(1);
62
63
0
    AddArg("elevation-name", 0, _("Name of the elevation field"),
64
0
           &m_elevAttributeName);
65
0
    AddArg("min-name", 0, _("Name of the minimum elevation field"), &m_amin);
66
0
    AddArg("max-name", 0, _("Name of the maximum elevation field"), &m_amax);
67
0
    AddArg("3d", 0, _("Force production of 3D vectors instead of 2D"), &m_3d);
68
69
0
    AddArg("src-nodata", 0, _("Input pixel value to treat as 'nodata'"),
70
0
           &m_sNodata);
71
0
    AddArg("interval", 0, _("Elevation interval between contours"), &m_interval)
72
0
        .SetMutualExclusionGroup("levels")
73
0
        .SetMinValueExcluded(0);
74
0
    AddArg("levels", 0, _("List of contour levels"), &m_levels)
75
0
        .SetMutualExclusionGroup("levels");
76
0
    AddArg("exp-base", 'e', _("Base for exponential contour level generation"),
77
0
           &m_expBase)
78
0
        .SetMutualExclusionGroup("levels");
79
0
    AddArg("offset", 0, _("Offset to apply to contour levels"), &m_offset)
80
0
        .AddAlias("off");
81
0
    AddArg("polygonize", 'p', _("Create polygons instead of lines"),
82
0
           &m_polygonize);
83
0
    AddArg("group-transactions", 0,
84
0
           _("Group n features per transaction (default 100 000)"),
85
0
           &m_groupTransactions)
86
0
        .SetMinValueIncluded(0);
87
0
}
88
89
/************************************************************************/
90
/*                GDALRasterContourAlgorithm::RunImpl()                 */
91
/************************************************************************/
92
93
bool GDALRasterContourAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
94
                                         void *pProgressData)
95
0
{
96
0
    GDALPipelineStepRunContext stepCtxt;
97
0
    stepCtxt.m_pfnProgress = pfnProgress;
98
0
    stepCtxt.m_pProgressData = pProgressData;
99
0
    return RunPreStepPipelineValidations() && RunStep(stepCtxt);
100
0
}
101
102
/************************************************************************/
103
/*                GDALRasterContourAlgorithm::RunStep()                 */
104
/************************************************************************/
105
106
bool GDALRasterContourAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
107
0
{
108
0
    CPLErrorReset();
109
110
0
    auto poSrcDS = m_inputDataset[0].GetDatasetRef();
111
0
    CPLAssert(poSrcDS);
112
113
0
    CPLAssert(!m_outputDataset.GetDatasetRef());
114
115
0
    CPLStringList aosOptions;
116
117
0
    std::string outputFilename;
118
0
    if (m_standaloneStep)
119
0
    {
120
0
        outputFilename = m_outputDataset.GetName();
121
0
        if (!m_format.empty())
122
0
        {
123
0
            aosOptions.AddString("-of");
124
0
            aosOptions.AddString(m_format.c_str());
125
0
        }
126
127
0
        for (const auto &co : m_creationOptions)
128
0
        {
129
0
            aosOptions.push_back("-co");
130
0
            aosOptions.push_back(co.c_str());
131
0
        }
132
133
0
        for (const auto &co : m_layerCreationOptions)
134
0
        {
135
0
            aosOptions.push_back("-lco");
136
0
            aosOptions.push_back(co.c_str());
137
0
        }
138
0
    }
139
0
    else
140
0
    {
141
0
        if (GetGDALDriverManager()->GetDriverByName("GPKG"))
142
0
        {
143
0
            aosOptions.AddString("-of");
144
0
            aosOptions.AddString("GPKG");
145
146
0
            outputFilename = CPLGenerateTempFilenameSafe("_contour") + ".gpkg";
147
0
        }
148
0
        else
149
0
        {
150
0
            aosOptions.AddString("-of");
151
0
            aosOptions.AddString("MEM");
152
0
        }
153
0
    }
154
155
0
    if (m_band > 0)
156
0
    {
157
0
        aosOptions.AddString("-b");
158
0
        aosOptions.AddString(CPLSPrintf("%d", m_band));
159
0
    }
160
0
    if (!m_elevAttributeName.empty())
161
0
    {
162
0
        aosOptions.AddString("-a");
163
0
        aosOptions.AddString(m_elevAttributeName);
164
0
    }
165
0
    if (!m_amin.empty())
166
0
    {
167
0
        aosOptions.AddString("-amin");
168
0
        aosOptions.AddString(m_amin);
169
0
    }
170
0
    if (!m_amax.empty())
171
0
    {
172
0
        aosOptions.AddString("-amax");
173
0
        aosOptions.AddString(m_amax);
174
0
    }
175
0
    if (m_3d)
176
0
    {
177
0
        aosOptions.AddString("-3d");
178
0
    }
179
0
    if (!std::isnan(m_sNodata))
180
0
    {
181
0
        aosOptions.AddString("-snodata");
182
0
        aosOptions.AddString(CPLSPrintf("%.16g", m_sNodata));
183
0
    }
184
0
    if (m_levels.size() > 0)
185
0
    {
186
0
        for (const auto &level : m_levels)
187
0
        {
188
0
            aosOptions.AddString("-fl");
189
0
            aosOptions.AddString(level);
190
0
        }
191
0
    }
192
0
    if (!std::isnan(m_interval))
193
0
    {
194
0
        aosOptions.AddString("-i");
195
0
        aosOptions.AddString(CPLSPrintf("%.16g", m_interval));
196
0
    }
197
0
    if (m_expBase > 0)
198
0
    {
199
0
        aosOptions.AddString("-e");
200
0
        aosOptions.AddString(CPLSPrintf("%d", m_expBase));
201
0
    }
202
0
    if (!std::isnan(m_offset))
203
0
    {
204
0
        aosOptions.AddString("-off");
205
0
        aosOptions.AddString(CPLSPrintf("%.16g", m_offset));
206
0
    }
207
0
    if (m_polygonize)
208
0
    {
209
0
        aosOptions.AddString("-p");
210
0
    }
211
0
    if (!m_outputLayerName.empty())
212
0
    {
213
0
        aosOptions.AddString("-nln");
214
0
        aosOptions.AddString(m_outputLayerName);
215
0
    }
216
217
    // Check that one of --interval, --levels, --exp-base is specified
218
0
    if (m_levels.size() == 0 && std::isnan(m_interval) && m_expBase == 0)
219
0
    {
220
0
        ReportError(
221
0
            CE_Failure, CPLE_AppDefined,
222
0
            "One of 'interval', 'levels', 'exp-base' must be specified.");
223
0
        return false;
224
0
    }
225
226
    // Check that interval is not negative
227
0
    if (!std::isnan(m_interval) && m_interval < 0)
228
0
    {
229
0
        ReportError(CE_Failure, CPLE_AppDefined,
230
0
                    "Interval must be a positive number.");
231
0
        return false;
232
0
    }
233
234
0
    aosOptions.AddString(m_inputDataset[0].GetName());
235
0
    aosOptions.AddString(outputFilename);
236
237
0
    bool bRet = false;
238
0
    GDALContourOptionsForBinary optionsForBinary;
239
0
    std::unique_ptr<GDALContourOptions, decltype(&GDALContourOptionsFree)>
240
0
        psOptions{GDALContourOptionsNew(aosOptions.List(), &optionsForBinary),
241
0
                  GDALContourOptionsFree};
242
0
    if (psOptions)
243
0
    {
244
0
        GDALDatasetH hSrcDS{poSrcDS};
245
0
        GDALRasterBandH hBand{nullptr};
246
0
        GDALDatasetH hDstDS{m_outputDataset.GetDatasetRef()};
247
0
        OGRLayerH hLayer{nullptr};
248
0
        char **papszStringOptions = nullptr;
249
250
0
        bRet = GDALContourProcessOptions(psOptions.get(), &papszStringOptions,
251
0
                                         &hSrcDS, &hBand, &hDstDS,
252
0
                                         &hLayer) == CE_None;
253
254
0
        if (bRet)
255
0
        {
256
0
            bRet = GDALContourGenerateEx(hBand, hLayer, papszStringOptions,
257
0
                                         ctxt.m_pfnProgress,
258
0
                                         ctxt.m_pProgressData) == CE_None;
259
0
        }
260
261
0
        CSLDestroy(papszStringOptions);
262
263
0
        auto poDstDS = GDALDataset::FromHandle(hDstDS);
264
0
        if (bRet)
265
0
        {
266
0
            bRet = poDstDS != nullptr;
267
0
        }
268
0
        if (poDstDS && !m_standaloneStep && !outputFilename.empty())
269
0
        {
270
0
            poDstDS->MarkSuppressOnClose();
271
0
            if (bRet)
272
0
                bRet = poDstDS->FlushCache() == CE_None;
273
0
#if !defined(__APPLE__)
274
            // For some unknown reason, unlinking the file on MacOSX
275
            // leads to later "disk I/O error". See https://github.com/OSGeo/gdal/issues/13794
276
0
            VSIUnlink(outputFilename.c_str());
277
0
#endif
278
0
        }
279
0
        m_outputDataset.Set(std::unique_ptr<GDALDataset>(poDstDS));
280
0
    }
281
282
0
    return bRet;
283
0
}
284
285
0
GDALRasterContourAlgorithmStandalone::~GDALRasterContourAlgorithmStandalone() =
286
    default;
287
288
//! @endcond