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