Coverage Report

Created: 2026-04-01 06:20

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
        .SetDuplicateValuesAllowed(false)
76
0
        .SetMutualExclusionGroup("levels");
77
0
    AddArg("exp-base", 'e', _("Base for exponential contour level generation"),
78
0
           &m_expBase)
79
0
        .SetMutualExclusionGroup("levels");
80
0
    AddArg("offset", 0, _("Offset to apply to contour levels"), &m_offset)
81
0
        .AddAlias("off");
82
0
    AddArg("polygonize", 'p', _("Create polygons instead of lines"),
83
0
           &m_polygonize);
84
0
    AddArg("group-transactions", 0,
85
0
           _("Group n features per transaction (default 100 000)"),
86
0
           &m_groupTransactions)
87
0
        .SetMinValueIncluded(0);
88
0
}
89
90
/************************************************************************/
91
/*                GDALRasterContourAlgorithm::RunImpl()                 */
92
/************************************************************************/
93
94
bool GDALRasterContourAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
95
                                         void *pProgressData)
96
0
{
97
0
    GDALPipelineStepRunContext stepCtxt;
98
0
    stepCtxt.m_pfnProgress = pfnProgress;
99
0
    stepCtxt.m_pProgressData = pProgressData;
100
0
    return RunPreStepPipelineValidations() && RunStep(stepCtxt);
101
0
}
102
103
/************************************************************************/
104
/*                GDALRasterContourAlgorithm::RunStep()                 */
105
/************************************************************************/
106
107
bool GDALRasterContourAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
108
0
{
109
0
    CPLErrorReset();
110
111
0
    auto poSrcDS = m_inputDataset[0].GetDatasetRef();
112
0
    CPLAssert(poSrcDS);
113
114
0
    CPLAssert(!m_outputDataset.GetDatasetRef());
115
116
0
    CPLStringList aosOptions;
117
118
0
    std::string outputFilename;
119
0
    if (m_standaloneStep)
120
0
    {
121
0
        outputFilename = m_outputDataset.GetName();
122
0
        if (!m_format.empty())
123
0
        {
124
0
            aosOptions.AddString("-of");
125
0
            aosOptions.AddString(m_format.c_str());
126
0
        }
127
128
0
        for (const auto &co : m_creationOptions)
129
0
        {
130
0
            aosOptions.push_back("-co");
131
0
            aosOptions.push_back(co.c_str());
132
0
        }
133
134
0
        for (const auto &co : m_layerCreationOptions)
135
0
        {
136
0
            aosOptions.push_back("-lco");
137
0
            aosOptions.push_back(co.c_str());
138
0
        }
139
0
    }
140
0
    else
141
0
    {
142
0
        if (GetGDALDriverManager()->GetDriverByName("GPKG"))
143
0
        {
144
0
            aosOptions.AddString("-of");
145
0
            aosOptions.AddString("GPKG");
146
147
0
            outputFilename = CPLGenerateTempFilenameSafe("_contour") + ".gpkg";
148
0
        }
149
0
        else
150
0
        {
151
0
            aosOptions.AddString("-of");
152
0
            aosOptions.AddString("MEM");
153
0
        }
154
0
    }
155
156
0
    if (m_band > 0)
157
0
    {
158
0
        aosOptions.AddString("-b");
159
0
        aosOptions.AddString(CPLSPrintf("%d", m_band));
160
0
    }
161
0
    if (!m_elevAttributeName.empty())
162
0
    {
163
0
        aosOptions.AddString("-a");
164
0
        aosOptions.AddString(m_elevAttributeName);
165
0
    }
166
0
    if (!m_amin.empty())
167
0
    {
168
0
        aosOptions.AddString("-amin");
169
0
        aosOptions.AddString(m_amin);
170
0
    }
171
0
    if (!m_amax.empty())
172
0
    {
173
0
        aosOptions.AddString("-amax");
174
0
        aosOptions.AddString(m_amax);
175
0
    }
176
0
    if (m_3d)
177
0
    {
178
0
        aosOptions.AddString("-3d");
179
0
    }
180
0
    if (!std::isnan(m_sNodata))
181
0
    {
182
0
        aosOptions.AddString("-snodata");
183
0
        aosOptions.AddString(CPLSPrintf("%.16g", m_sNodata));
184
0
    }
185
0
    if (m_levels.size() > 0)
186
0
    {
187
0
        for (const auto &level : m_levels)
188
0
        {
189
0
            aosOptions.AddString("-fl");
190
0
            aosOptions.AddString(level);
191
0
        }
192
0
    }
193
0
    if (!std::isnan(m_interval))
194
0
    {
195
0
        aosOptions.AddString("-i");
196
0
        aosOptions.AddString(CPLSPrintf("%.16g", m_interval));
197
0
    }
198
0
    if (m_expBase > 0)
199
0
    {
200
0
        aosOptions.AddString("-e");
201
0
        aosOptions.AddString(CPLSPrintf("%d", m_expBase));
202
0
    }
203
0
    if (!std::isnan(m_offset))
204
0
    {
205
0
        aosOptions.AddString("-off");
206
0
        aosOptions.AddString(CPLSPrintf("%.16g", m_offset));
207
0
    }
208
0
    if (m_polygonize)
209
0
    {
210
0
        aosOptions.AddString("-p");
211
0
    }
212
0
    if (!m_outputLayerName.empty())
213
0
    {
214
0
        aosOptions.AddString("-nln");
215
0
        aosOptions.AddString(m_outputLayerName);
216
0
    }
217
218
    // Check that one of --interval, --levels, --exp-base is specified
219
0
    if (m_levels.size() == 0 && std::isnan(m_interval) && m_expBase == 0)
220
0
    {
221
0
        ReportError(
222
0
            CE_Failure, CPLE_AppDefined,
223
0
            "One of 'interval', 'levels', 'exp-base' must be specified.");
224
0
        return false;
225
0
    }
226
227
    // Check that interval is not negative
228
0
    if (!std::isnan(m_interval) && m_interval < 0)
229
0
    {
230
0
        ReportError(CE_Failure, CPLE_AppDefined,
231
0
                    "Interval must be a positive number.");
232
0
        return false;
233
0
    }
234
235
0
    aosOptions.AddString(m_inputDataset[0].GetName());
236
0
    aosOptions.AddString(outputFilename);
237
238
0
    bool bRet = false;
239
0
    GDALContourOptionsForBinary optionsForBinary;
240
0
    std::unique_ptr<GDALContourOptions, decltype(&GDALContourOptionsFree)>
241
0
        psOptions{GDALContourOptionsNew(aosOptions.List(), &optionsForBinary),
242
0
                  GDALContourOptionsFree};
243
0
    if (psOptions)
244
0
    {
245
0
        GDALDatasetH hSrcDS{poSrcDS};
246
0
        GDALRasterBandH hBand{nullptr};
247
0
        GDALDatasetH hDstDS{m_outputDataset.GetDatasetRef()};
248
0
        OGRLayerH hLayer{nullptr};
249
0
        char **papszStringOptions = nullptr;
250
251
0
        bRet = GDALContourProcessOptions(psOptions.get(), &papszStringOptions,
252
0
                                         &hSrcDS, &hBand, &hDstDS,
253
0
                                         &hLayer) == CE_None;
254
255
0
        if (bRet)
256
0
        {
257
0
            bRet = GDALContourGenerateEx(hBand, hLayer, papszStringOptions,
258
0
                                         ctxt.m_pfnProgress,
259
0
                                         ctxt.m_pProgressData) == CE_None;
260
0
        }
261
262
0
        CSLDestroy(papszStringOptions);
263
264
0
        auto poDstDS = GDALDataset::FromHandle(hDstDS);
265
0
        if (bRet)
266
0
        {
267
0
            bRet = poDstDS != nullptr;
268
0
        }
269
0
        if (poDstDS && !m_standaloneStep && !outputFilename.empty())
270
0
        {
271
0
            poDstDS->MarkSuppressOnClose();
272
0
            if (bRet)
273
0
                bRet = poDstDS->FlushCache() == CE_None;
274
0
#if !defined(__APPLE__)
275
            // For some unknown reason, unlinking the file on MacOSX
276
            // leads to later "disk I/O error". See https://github.com/OSGeo/gdal/issues/13794
277
0
            VSIUnlink(outputFilename.c_str());
278
0
#endif
279
0
        }
280
0
        m_outputDataset.Set(std::unique_ptr<GDALDataset>(poDstDS));
281
0
    }
282
283
0
    return bRet;
284
0
}
285
286
0
GDALRasterContourAlgorithmStandalone::~GDALRasterContourAlgorithmStandalone() =
287
    default;
288
289
//! @endcond