Coverage Report

Created: 2026-07-25 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/gdalalg_raster_as_features.cpp
Line
Count
Source
1
/******************************************************************************
2
*
3
 * Project:  GDAL
4
 * Purpose:  "as-features" step of "gdal pipeline"
5
 * Author:   Daniel Baston
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2025, ISciences, LLC
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12
13
#include "gdalalg_raster_as_features.h"
14
#include "gdalalg_vector_pipeline.h"
15
16
#include "cpl_conv.h"
17
#include "gdal_priv.h"
18
#include "gdal_alg.h"
19
#include "ogrsf_frmts.h"
20
21
#include <cmath>
22
#include <limits>
23
#include <optional>
24
25
//! @cond Doxygen_Suppress
26
27
#ifndef _
28
0
#define _(x) (x)
29
#endif
30
31
GDALRasterAsFeaturesAlgorithm::GDALRasterAsFeaturesAlgorithm(
32
    bool standaloneStep)
33
0
    : GDALPipelineStepAlgorithm(
34
0
          NAME, DESCRIPTION, HELP_URL,
35
0
          ConstructorOptions()
36
0
              .SetStandaloneStep(standaloneStep)
37
0
              .SetAddUpsertArgument(false)
38
0
              .SetAddSkipErrorsArgument(false)
39
0
              .SetOutputFormatCreateCapability(GDAL_DCAP_CREATE))
40
0
{
41
0
    m_outputLayerName = "pixels";
42
43
0
    if (standaloneStep)
44
0
    {
45
0
        AddProgressArg(/* hidden = */ true);
46
0
        AddRasterInputArgs(false, false);
47
0
        AddVectorOutputArgs(false, false);
48
0
    }
49
0
    else
50
0
    {
51
0
        AddRasterHiddenInputDatasetArg();
52
0
        AddOutputLayerNameArg(/* hiddenForCLI = */ false,
53
0
                              /* shortNameOutputLayerAllowed = */ false);
54
0
    }
55
56
0
    AddBandArg(&m_bands);
57
0
    AddArg("geometry-type", 0, _("Geometry type"), &m_geomTypeName)
58
0
        .SetChoices("none", "point", "polygon")
59
0
        .SetDefault(m_geomTypeName);
60
0
    AddArg("skip-nodata", 0, _("Omit NoData pixels from the result"),
61
0
           &m_skipNoData);
62
0
    AddArg("include-xy", 0, _("Include fields for cell center coordinates"),
63
0
           &m_includeXY);
64
0
    AddArg("include-row-col", 0, _("Include columns for row and column"),
65
0
           &m_includeRowCol);
66
0
}
67
68
0
GDALRasterAsFeaturesAlgorithm::~GDALRasterAsFeaturesAlgorithm() = default;
69
70
GDALRasterAsFeaturesAlgorithmStandalone::
71
    ~GDALRasterAsFeaturesAlgorithmStandalone() = default;
72
73
namespace
74
{
75
struct RasterAsFeaturesOptions
76
{
77
    OGRwkbGeometryType geomType{wkbNone};
78
    bool includeXY{false};
79
    bool includeRowCol{false};
80
    bool skipNoData{false};
81
    std::vector<int> bands{};
82
    std::string outputLayerName{};
83
};
84
85
class GDALRasterAsFeaturesLayer final
86
    : public OGRLayer,
87
      public OGRGetNextFeatureThroughRaw<GDALRasterAsFeaturesLayer>
88
{
89
  public:
90
    static constexpr const char *ROW_FIELD = "ROW";
91
    static constexpr const char *COL_FIELD = "COL";
92
    static constexpr const char *X_FIELD = "CENTER_X";
93
    static constexpr const char *Y_FIELD = "CENTER_Y";
94
95
    DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(GDALRasterAsFeaturesLayer)
96
97
    GDALRasterAsFeaturesLayer(GDALDataset &ds, RasterAsFeaturesOptions options)
98
0
        : m_ds(ds), m_it(GDALRasterBand::WindowIterator(
99
0
                        m_ds.GetRasterXSize(), m_ds.GetRasterYSize(),
100
0
                        m_ds.GetRasterXSize(), m_ds.GetRasterYSize(), 0, 0)),
101
0
          m_end(GDALRasterBand::WindowIterator(
102
0
              m_ds.GetRasterXSize(), m_ds.GetRasterYSize(),
103
0
              m_ds.GetRasterXSize(), m_ds.GetRasterYSize(), 1, 0)),
104
0
          m_defn(OGRFeatureDefnRefCountedPtr::makeInstance(
105
0
              options.outputLayerName.c_str())),
106
0
          m_includeXY(options.includeXY),
107
0
          m_includeRowCol(options.includeRowCol),
108
0
          m_excludeNoDataPixels(options.skipNoData)
109
0
    {
110
        // TODO: Handle Int64, UInt64
111
0
        m_ds.GetGeoTransform(m_gt);
112
113
0
        int nBands = m_ds.GetRasterCount();
114
0
        m_bands.resize(nBands);
115
0
        for (int i = 1; i <= nBands; i++)
116
0
        {
117
0
            m_bands[i - 1] = i;
118
0
        }
119
120
        // TODO: Handle per-band NoData values
121
0
        if (nBands > 0)
122
0
        {
123
0
            int hasNoData;
124
0
            double noData = m_ds.GetRasterBand(1)->GetNoDataValue(&hasNoData);
125
0
            if (hasNoData)
126
0
            {
127
0
                m_noData = noData;
128
0
            }
129
0
        }
130
131
0
        SetDescription(options.outputLayerName.c_str());
132
0
        if (options.geomType == wkbNone)
133
0
        {
134
0
            m_defn->SetGeomType(wkbNone);
135
0
        }
136
0
        else
137
0
        {
138
0
            m_defn->GetGeomFieldDefn(0)->SetType(options.geomType);
139
0
            m_defn->GetGeomFieldDefn(0)->SetSpatialRef(ds.GetSpatialRef());
140
0
        }
141
142
0
        if (m_includeXY)
143
0
        {
144
0
            auto xField = std::make_unique<OGRFieldDefn>(X_FIELD, OFTReal);
145
0
            auto yField = std::make_unique<OGRFieldDefn>(Y_FIELD, OFTReal);
146
0
            m_defn->AddFieldDefn(std::move(xField));
147
0
            m_defn->AddFieldDefn(std::move(yField));
148
0
        }
149
0
        if (m_includeRowCol)
150
0
        {
151
0
            auto rowField =
152
0
                std::make_unique<OGRFieldDefn>(ROW_FIELD, OFTInteger);
153
0
            auto colField =
154
0
                std::make_unique<OGRFieldDefn>(COL_FIELD, OFTInteger);
155
0
            m_defn->AddFieldDefn(std::move(rowField));
156
0
            m_defn->AddFieldDefn(std::move(colField));
157
0
        }
158
0
        for (int band : m_bands)
159
0
        {
160
0
            CPLString fieldName = CPLSPrintf("BAND_%d", band);
161
0
            auto bandField =
162
0
                std::make_unique<OGRFieldDefn>(fieldName.c_str(), OFTReal);
163
0
            m_defn->AddFieldDefn(std::move(bandField));
164
0
            m_bandFields.push_back(m_defn->GetFieldIndex(fieldName));
165
0
        }
166
167
0
        GDALRasterAsFeaturesLayer::ResetReading();
168
0
    }
169
170
    void ResetReading() override
171
0
    {
172
0
        if (m_ds.GetRasterCount() > 0)
173
0
        {
174
0
            GDALRasterBand *poFirstBand = m_ds.GetRasterBand(1);
175
0
            CPLAssert(poFirstBand);  // appease clang scan-build
176
0
            m_it = poFirstBand->IterateWindows().begin();
177
0
            m_end = poFirstBand->IterateWindows().end();
178
0
        }
179
0
    }
180
181
    int TestCapability(const char *pszCap) const override
182
0
    {
183
0
        return EQUAL(pszCap, OLCFastFeatureCount) &&
184
0
               m_poFilterGeom == nullptr && m_poAttrQuery == nullptr &&
185
0
               !m_excludeNoDataPixels;
186
0
    }
187
188
    GIntBig GetFeatureCount(int bForce) override
189
0
    {
190
0
        if (m_poFilterGeom == nullptr && m_poAttrQuery == nullptr &&
191
0
            !m_excludeNoDataPixels)
192
0
        {
193
0
            return static_cast<GIntBig>(m_ds.GetRasterXSize()) *
194
0
                   m_ds.GetRasterYSize();
195
0
        }
196
0
        return OGRLayer::GetFeatureCount(bForce);
197
0
    }
198
199
    OGRFeatureDefn *GetLayerDefn() const override
200
0
    {
201
0
        return m_defn.get();
202
0
    }
203
204
    OGRFeature *GetNextRawFeature()
205
0
    {
206
0
        if (m_row >= m_window.nYSize && !NextWindow())
207
0
        {
208
0
            return nullptr;
209
0
        }
210
211
0
        std::unique_ptr<OGRFeature> feature;
212
213
0
        while (m_row < m_window.nYSize)
214
0
        {
215
0
            const double *pSrcVal = reinterpret_cast<double *>(m_buf.data()) +
216
0
                                    (m_bands.size() * m_row * m_window.nXSize +
217
0
                                     m_col * m_bands.size());
218
219
0
            const bool emitFeature =
220
0
                !m_excludeNoDataPixels || !IsNoData(*pSrcVal);
221
222
0
            if (emitFeature)
223
0
            {
224
0
                feature.reset(OGRFeature::CreateFeature(m_defn.get()));
225
226
0
                for (int fieldPos : m_bandFields)
227
0
                {
228
0
                    feature->SetField(fieldPos, *pSrcVal);
229
0
                    pSrcVal++;
230
0
                }
231
232
0
                const double line = m_window.nYOff + m_row;
233
0
                const double pixel = m_window.nXOff + m_col;
234
235
0
                if (m_includeRowCol)
236
0
                {
237
0
                    feature->SetField(ROW_FIELD, static_cast<GIntBig>(line));
238
0
                    feature->SetField(COL_FIELD, static_cast<GIntBig>(pixel));
239
0
                }
240
0
                if (m_includeXY)
241
0
                {
242
0
                    double x, y;
243
0
                    m_gt.Apply(pixel + 0.5, line + 0.5, &x, &y);
244
0
                    feature->SetField(X_FIELD, x);
245
0
                    feature->SetField(Y_FIELD, y);
246
0
                }
247
248
0
                std::unique_ptr<OGRGeometry> geom;
249
0
                const auto geomType = m_defn->GetGeomType();
250
0
                if (geomType == wkbPoint)
251
0
                {
252
0
                    double x, y;
253
0
                    m_gt.Apply(pixel + 0.5, line + 0.5, &x, &y);
254
255
0
                    geom = std::make_unique<OGRPoint>(x, y);
256
0
                    geom->assignSpatialReference(
257
0
                        m_defn->GetGeomFieldDefn(0)->GetSpatialRef());
258
0
                }
259
0
                else if (geomType == wkbPolygon)
260
0
                {
261
0
                    double x, y;
262
263
0
                    auto lr = std::make_unique<OGRLinearRing>();
264
265
0
                    m_gt.Apply(pixel, line, &x, &y);
266
0
                    lr->addPoint(x, y);
267
0
                    m_gt.Apply(pixel, line + 1, &x, &y);
268
0
                    lr->addPoint(x, y);
269
0
                    m_gt.Apply(pixel + 1, line + 1, &x, &y);
270
0
                    lr->addPoint(x, y);
271
0
                    m_gt.Apply(pixel + 1, line, &x, &y);
272
0
                    lr->addPoint(x, y);
273
0
                    m_gt.Apply(pixel, line, &x, &y);
274
0
                    lr->addPoint(x, y);
275
276
0
                    auto poly = std::make_unique<OGRPolygon>();
277
0
                    poly->addRing(std::move(lr));
278
0
                    geom = std::move(poly);
279
0
                    geom->assignSpatialReference(
280
0
                        m_defn->GetGeomFieldDefn(0)->GetSpatialRef());
281
0
                }
282
283
0
                feature->SetGeometry(std::move(geom));
284
0
            }
285
286
0
            m_col += 1;
287
0
            if (m_col >= m_window.nXSize)
288
0
            {
289
0
                m_col = 0;
290
0
                m_row++;
291
0
            }
292
293
0
            if (m_row >= m_window.nYSize)
294
0
            {
295
0
                NextWindow();
296
0
            }
297
298
0
            if (feature)
299
0
            {
300
0
                return feature.release();
301
0
            }
302
0
        }
303
304
0
        return nullptr;
305
0
    }
306
307
    CPL_DISALLOW_COPY_ASSIGN(GDALRasterAsFeaturesLayer)
308
309
  private:
310
    bool IsNoData(double x) const
311
0
    {
312
0
        if (!m_noData.has_value())
313
0
        {
314
0
            return false;
315
0
        }
316
317
0
        return m_noData.value() == x ||
318
0
               (std::isnan(m_noData.value()) && std::isnan(x));
319
0
    }
320
321
    bool NextWindow()
322
0
    {
323
0
        int nBandCount = static_cast<int>(m_bands.size());
324
325
0
        if (m_it == m_end)
326
0
        {
327
0
            return false;
328
0
        }
329
330
0
        if (m_ds.GetRasterXSize() == 0 || m_ds.GetRasterYSize() == 0)
331
0
        {
332
0
            return false;
333
0
        }
334
335
0
        m_window = *m_it;
336
0
        ++m_it;
337
338
0
        if (!m_bands.empty())
339
0
        {
340
0
            const auto nBufTypeSize = GDALGetDataTypeSizeBytes(m_bufType);
341
            if constexpr (sizeof(int) < sizeof(size_t))
342
0
            {
343
0
                if (m_window.nYSize > 0 &&
344
0
                    static_cast<size_t>(m_window.nXSize) >
345
0
                        std::numeric_limits<size_t>::max() / m_window.nYSize)
346
0
                {
347
0
                    CPLError(CE_Failure, CPLE_OutOfMemory,
348
0
                             "Failed to allocate buffer");
349
0
                    return false;
350
0
                }
351
0
            }
352
0
            const size_t nPixelCount =
353
0
                static_cast<size_t>(m_window.nXSize) * m_window.nYSize;
354
0
            if (static_cast<size_t>(nBandCount) * nBufTypeSize >
355
0
                std::numeric_limits<size_t>::max() / nPixelCount)
356
0
            {
357
0
                CPLError(CE_Failure, CPLE_OutOfMemory,
358
0
                         "Failed to allocate buffer");
359
0
                return false;
360
0
            }
361
0
            const size_t nBufSize = nPixelCount * nBandCount * nBufTypeSize;
362
0
            if (m_buf.size() < nBufSize)
363
0
            {
364
0
                try
365
0
                {
366
0
                    m_buf.resize(nBufSize);
367
0
                }
368
0
                catch (const std::exception &)
369
0
                {
370
0
                    CPLError(CE_Failure, CPLE_OutOfMemory,
371
0
                             "Failed to allocate buffer");
372
0
                    return false;
373
0
                }
374
0
            }
375
376
0
            const auto nPixelSpace =
377
0
                static_cast<GSpacing>(nBandCount) * nBufTypeSize;
378
0
            const auto eErr = m_ds.RasterIO(
379
0
                GF_Read, m_window.nXOff, m_window.nYOff, m_window.nXSize,
380
0
                m_window.nYSize, m_buf.data(), m_window.nXSize, m_window.nYSize,
381
0
                m_bufType, static_cast<int>(m_bands.size()), m_bands.data(),
382
0
                nPixelSpace, nPixelSpace * m_window.nXSize, nBufTypeSize,
383
0
                nullptr);
384
385
0
            if (eErr != CE_None)
386
0
            {
387
0
                CPLError(CE_Failure, CPLE_AppDefined,
388
0
                         "Failed to read raster data");
389
0
                return false;
390
0
            }
391
0
        }
392
393
0
        m_row = 0;
394
0
        m_col = 0;
395
396
0
        return true;
397
0
    }
398
399
    GDALDataset &m_ds;
400
    std::vector<GByte> m_buf{};
401
    const GDALDataType m_bufType = GDT_Float64;
402
    GDALGeoTransform m_gt{};
403
    std::optional<double> m_noData{std::nullopt};
404
405
    std::vector<int> m_bands{};
406
    std::vector<int> m_bandFields{};
407
408
    GDALRasterBand::WindowIterator m_it;
409
    GDALRasterBand::WindowIterator m_end;
410
    GDALRasterWindow m_window{};
411
412
    int m_row{0};
413
    int m_col{0};
414
415
    const OGRFeatureDefnRefCountedPtr m_defn;
416
    bool m_includeXY;
417
    bool m_includeRowCol;
418
    bool m_excludeNoDataPixels;
419
};
420
421
}  // namespace
422
423
bool GDALRasterAsFeaturesAlgorithm::RunStep(GDALPipelineStepRunContext &)
424
0
{
425
0
    auto poSrcDS = m_inputDataset[0].GetDatasetRef();
426
427
0
    RasterAsFeaturesOptions options;
428
0
    options.geomType = m_geomTypeName == "point"     ? wkbPoint
429
0
                       : m_geomTypeName == "polygon" ? wkbPolygon
430
0
                                                     : wkbNone;
431
0
    options.includeRowCol = m_includeRowCol;
432
0
    options.includeXY = m_includeXY;
433
0
    options.skipNoData = m_skipNoData;
434
0
    options.outputLayerName = m_outputLayerName;
435
436
0
    if (!m_bands.empty())
437
0
    {
438
0
        options.bands = std::move(m_bands);
439
0
    }
440
441
0
    auto poLayer =
442
0
        std::make_unique<GDALRasterAsFeaturesLayer>(*poSrcDS, options);
443
0
    auto poRetDS = std::make_unique<GDALVectorOutputDataset>(nullptr);
444
0
    poRetDS->AddLayer(std::move(poLayer));
445
446
0
    m_outputDataset.Set(std::move(poRetDS));
447
448
0
    return true;
449
0
}
450
451
//! @endcond