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