Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL
4 : * Purpose: gdal "vector pipeline" subcommand
5 : * Author: Even Rouault <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "gdalalg_vector_pipeline.h"
14 : #include "gdalalg_external.h"
15 : #include "gdalalg_materialize.h"
16 : #include "gdalalg_vector_read.h"
17 : #include "gdalalg_vector_buffer.h"
18 : #include "gdalalg_vector_check_coverage.h"
19 : #include "gdalalg_vector_check_geometry.h"
20 : #include "gdalalg_vector_clean_coverage.h"
21 : #include "gdalalg_vector_clip.h"
22 : #include "gdalalg_vector_combine.h"
23 : #include "gdalalg_vector_concat.h"
24 : #include "gdalalg_vector_concave_hull.h"
25 : #include "gdalalg_vector_convex_hull.h"
26 : #include "gdalalg_vector_create.h"
27 : #include "gdalalg_vector_dissolve.h"
28 : #include "gdalalg_vector_edit.h"
29 : #include "gdalalg_vector_explode.h"
30 : #include "gdalalg_vector_explode_collections.h"
31 : #include "gdalalg_vector_export_schema.h"
32 : #include "gdalalg_vector_filter.h"
33 : #include "gdalalg_vector_info.h"
34 : #include "gdalalg_vector_layer_algebra.h"
35 : #include "gdalalg_vector_limit.h"
36 : #include "gdalalg_vector_make_point.h"
37 : #include "gdalalg_vector_make_valid.h"
38 : #include "gdalalg_vector_partition.h"
39 : #include "gdalalg_vector_rename_layer.h"
40 : #include "gdalalg_vector_reproject.h"
41 : #include "gdalalg_vector_segmentize.h"
42 : #include "gdalalg_vector_select.h"
43 : #include "gdalalg_vector_set_field_type.h"
44 : #include "gdalalg_vector_set_geom_type.h"
45 : #include "gdalalg_vector_simplify.h"
46 : #include "gdalalg_vector_simplify_coverage.h"
47 : #include "gdalalg_vector_sort.h"
48 : #include "gdalalg_vector_sql.h"
49 : #include "gdalalg_vector_swap_xy.h"
50 : #include "gdalalg_vector_update.h"
51 : #include "gdalalg_vector_write.h"
52 : #include "gdalalg_tee.h"
53 :
54 : #include "../frmts/mem/memdataset.h"
55 :
56 : #include "cpl_conv.h"
57 : #include "cpl_string.h"
58 :
59 : #include <algorithm>
60 : #include <cassert>
61 :
62 : //! @cond Doxygen_Suppress
63 :
64 : #ifndef _
65 : #define _(x) (x)
66 : #endif
67 :
68 : GDALVectorAlgorithmStepRegistry::~GDALVectorAlgorithmStepRegistry() = default;
69 :
70 : /************************************************************************/
71 : /* GDALVectorPipelineStepAlgorithm::GDALVectorPipelineStepAlgorithm() */
72 : /************************************************************************/
73 :
74 1983 : GDALVectorPipelineStepAlgorithm::GDALVectorPipelineStepAlgorithm(
75 : const std::string &name, const std::string &description,
76 1983 : const std::string &helpURL, bool standaloneStep)
77 : : GDALVectorPipelineStepAlgorithm(
78 : name, description, helpURL,
79 1983 : ConstructorOptions().SetStandaloneStep(standaloneStep))
80 : {
81 1983 : }
82 :
83 : /************************************************************************/
84 : /* GDALVectorPipelineStepAlgorithm::GDALVectorPipelineStepAlgorithm() */
85 : /************************************************************************/
86 :
87 5200 : GDALVectorPipelineStepAlgorithm::GDALVectorPipelineStepAlgorithm(
88 : const std::string &name, const std::string &description,
89 5200 : const std::string &helpURL, const ConstructorOptions &options)
90 5200 : : GDALPipelineStepAlgorithm(name, description, helpURL, options)
91 : {
92 5200 : if (m_standaloneStep)
93 : {
94 2285 : m_supportsStreamedOutput = true;
95 :
96 2285 : if (m_constructorOptions.addDefaultArguments)
97 : {
98 1729 : AddVectorInputArgs(false);
99 1729 : AddProgressArg();
100 1729 : AddVectorOutputArgs(false, false);
101 : }
102 : }
103 : else
104 : {
105 2915 : if (m_constructorOptions.addDefaultArguments)
106 : {
107 1901 : AddVectorHiddenInputDatasetArg();
108 : }
109 : }
110 5200 : }
111 :
112 : GDALVectorPipelineStepAlgorithm::~GDALVectorPipelineStepAlgorithm() = default;
113 :
114 : /************************************************************************/
115 : /* GDALVectorPipelineAlgorithm::GDALVectorPipelineAlgorithm() */
116 : /************************************************************************/
117 :
118 170 : GDALVectorPipelineAlgorithm::GDALVectorPipelineAlgorithm()
119 : : GDALAbstractPipelineAlgorithm(
120 : NAME, DESCRIPTION, HELP_URL,
121 170 : ConstructorOptions().SetInputDatasetMaxCount(INT_MAX))
122 : {
123 170 : m_supportsStreamedOutput = true;
124 :
125 170 : AddVectorInputArgs(/* hiddenForCLI = */ true);
126 170 : AddProgressArg();
127 340 : AddArg("pipeline", 0, _("Pipeline string"), &m_pipeline)
128 170 : .SetHiddenForCLI()
129 170 : .SetPositional();
130 170 : AddVectorOutputArgs(/* hiddenForCLI = */ true,
131 : /* shortNameOutputLayerAllowed=*/false);
132 :
133 170 : AddOutputStringArg(&m_output).SetHiddenForCLI();
134 170 : AddStdoutArg(&m_stdout);
135 :
136 170 : RegisterAlgorithms(m_stepRegistry, false);
137 170 : }
138 :
139 : /************************************************************************/
140 : /* GDALVectorPipelineAlgorithm::RegisterAlgorithms() */
141 : /************************************************************************/
142 :
143 : /* static */
144 504 : void GDALVectorPipelineAlgorithm::RegisterAlgorithms(
145 : GDALVectorAlgorithmStepRegistry ®istry, bool forMixedPipeline)
146 : {
147 1008 : GDALAlgorithmRegistry::AlgInfo algInfo;
148 :
149 : const auto addSuffixIfNeeded =
150 5544 : [forMixedPipeline](const char *name) -> std::string
151 : {
152 9218 : return forMixedPipeline ? std::string(name).append(VECTOR_SUFFIX)
153 14762 : : std::string(name);
154 504 : };
155 :
156 504 : registry.Register<GDALVectorReadAlgorithm>(
157 1008 : addSuffixIfNeeded(GDALVectorReadAlgorithm::NAME));
158 :
159 504 : registry.Register<GDALVectorWriteAlgorithm>(
160 1008 : addSuffixIfNeeded(GDALVectorWriteAlgorithm::NAME));
161 :
162 504 : registry.Register<GDALVectorInfoAlgorithm>(
163 1008 : addSuffixIfNeeded(GDALVectorInfoAlgorithm::NAME));
164 :
165 504 : registry.Register<GDALVectorBufferAlgorithm>();
166 504 : registry.Register<GDALVectorCheckCoverageAlgorithm>();
167 504 : registry.Register<GDALVectorCheckGeometryAlgorithm>();
168 504 : registry.Register<GDALVectorCombineAlgorithm>();
169 504 : registry.Register<GDALVectorConcatAlgorithm>();
170 504 : registry.Register<GDALVectorConcaveHullAlgorithm>();
171 504 : registry.Register<GDALVectorConvexHullAlgorithm>();
172 504 : registry.Register<GDALVectorCleanCoverageAlgorithm>();
173 :
174 504 : registry.Register<GDALVectorClipAlgorithm>(
175 1008 : addSuffixIfNeeded(GDALVectorClipAlgorithm::NAME));
176 504 : registry.Register<GDALVectorDissolveAlgorithm>();
177 :
178 504 : registry.Register<GDALVectorCreateAlgorithm>(
179 1008 : addSuffixIfNeeded(GDALVectorCreateAlgorithm::NAME));
180 :
181 504 : registry.Register<GDALVectorEditAlgorithm>(
182 1008 : addSuffixIfNeeded(GDALVectorEditAlgorithm::NAME));
183 :
184 504 : registry.Register<GDALVectorExplodeAlgorithm>();
185 504 : registry.Register<GDALVectorExplodeCollectionsAlgorithm>();
186 504 : registry.Register<GDALVectorExportSchemaAlgorithm>();
187 :
188 504 : registry.Register<GDALMaterializeVectorAlgorithm>(
189 1008 : addSuffixIfNeeded(GDALMaterializeVectorAlgorithm::NAME));
190 :
191 504 : registry.Register<GDALVectorReprojectAlgorithm>(
192 1008 : addSuffixIfNeeded(GDALVectorReprojectAlgorithm::NAME));
193 :
194 504 : registry.Register<GDALVectorFilterAlgorithm>();
195 504 : registry.Register<GDALVectorLayerAlgebraAlgorithm>();
196 504 : registry.Register<GDALVectorLimitAlgorithm>();
197 504 : registry.Register<GDALVectorMakePointAlgorithm>();
198 504 : registry.Register<GDALVectorMakeValidAlgorithm>();
199 504 : registry.Register<GDALVectorPartitionAlgorithm>();
200 504 : registry.Register<GDALVectorRenameLayerAlgorithm>();
201 504 : registry.Register<GDALVectorSegmentizeAlgorithm>();
202 :
203 504 : registry.Register<GDALVectorSelectAlgorithm>(
204 1008 : addSuffixIfNeeded(GDALVectorSelectAlgorithm::NAME));
205 :
206 504 : registry.Register<GDALVectorSetFieldTypeAlgorithm>();
207 504 : registry.Register<GDALVectorSetGeomTypeAlgorithm>();
208 504 : registry.Register<GDALVectorSimplifyAlgorithm>();
209 504 : registry.Register<GDALVectorSimplifyCoverageAlgorithm>();
210 504 : registry.Register<GDALVectorSortAlgorithm>();
211 504 : registry.Register<GDALVectorSQLAlgorithm>();
212 504 : registry.Register<GDALVectorUpdateAlgorithm>(
213 1008 : addSuffixIfNeeded(GDALVectorUpdateAlgorithm::NAME));
214 504 : registry.Register<GDALVectorSwapXYAlgorithm>();
215 :
216 504 : registry.Register<GDALTeeVectorAlgorithm>(
217 1008 : addSuffixIfNeeded(GDALTeeVectorAlgorithm::NAME));
218 :
219 504 : if (!forMixedPipeline)
220 : {
221 170 : registry.Register<GDALExternalVectorAlgorithm>();
222 : }
223 504 : }
224 :
225 : /************************************************************************/
226 : /* GDALVectorPipelineAlgorithm::GetUsageForCLI() */
227 : /************************************************************************/
228 :
229 8 : std::string GDALVectorPipelineAlgorithm::GetUsageForCLI(
230 : bool shortUsage, const UsageOptions &usageOptions) const
231 : {
232 8 : UsageOptions stepUsageOptions;
233 8 : stepUsageOptions.isPipelineStep = true;
234 :
235 8 : if (!m_helpDocCategory.empty() && m_helpDocCategory != "main")
236 : {
237 4 : auto alg = GetStepAlg(m_helpDocCategory);
238 2 : if (alg)
239 : {
240 2 : alg->SetCallPath({m_helpDocCategory});
241 1 : alg->GetArg("help-doc")->Set(true);
242 1 : return alg->GetUsageForCLI(shortUsage, stepUsageOptions);
243 : }
244 : else
245 : {
246 1 : fprintf(stderr, "ERROR: unknown pipeline step '%s'\n",
247 : m_helpDocCategory.c_str());
248 : return CPLSPrintf("ERROR: unknown pipeline step '%s'\n",
249 1 : m_helpDocCategory.c_str());
250 : }
251 : }
252 :
253 6 : UsageOptions usageOptionsMain(usageOptions);
254 6 : usageOptionsMain.isPipelineMain = true;
255 : std::string ret =
256 12 : GDALAlgorithm::GetUsageForCLI(shortUsage, usageOptionsMain);
257 6 : if (shortUsage)
258 2 : return ret;
259 :
260 : ret += "\n<PIPELINE> is of the form: read|concat [READ-OPTIONS] "
261 4 : "( ! <STEP-NAME> [STEP-OPTIONS] )* ! write|info [WRITE-OPTIONS]\n";
262 :
263 4 : if (m_helpDocCategory == "main")
264 : {
265 1 : return ret;
266 : }
267 :
268 3 : ret += '\n';
269 3 : ret += "Example: 'gdal vector pipeline --progress ! read in.gpkg ! \\\n";
270 3 : ret += " reproject --output-crs=EPSG:32632 ! ";
271 3 : ret += "write out.gpkg --overwrite'\n";
272 3 : ret += '\n';
273 3 : ret += "Potential steps are:\n";
274 :
275 120 : for (const std::string &name : m_stepRegistry.GetNames())
276 : {
277 234 : auto alg = GetStepAlg(name);
278 117 : assert(alg);
279 117 : auto [options, maxOptLen] = alg->GetArgNamesForCLI();
280 117 : stepUsageOptions.maxOptLen =
281 117 : std::max(stepUsageOptions.maxOptLen, maxOptLen);
282 : }
283 :
284 : {
285 3 : const auto name = GDALVectorReadAlgorithm::NAME;
286 3 : ret += '\n';
287 6 : auto alg = GetStepAlg(name);
288 6 : alg->SetCallPath({name});
289 3 : ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
290 : }
291 120 : for (const std::string &name : m_stepRegistry.GetNames())
292 : {
293 234 : auto alg = GetStepAlg(name);
294 117 : assert(alg);
295 129 : if (alg->CanBeFirstStep() && !alg->CanBeMiddleStep() &&
296 129 : !alg->IsHidden() && name != GDALVectorReadAlgorithm::NAME)
297 : {
298 3 : ret += '\n';
299 6 : alg->SetCallPath({name});
300 3 : ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
301 : }
302 : }
303 120 : for (const std::string &name : m_stepRegistry.GetNames())
304 : {
305 234 : auto alg = GetStepAlg(name);
306 117 : assert(alg);
307 117 : if (alg->CanBeMiddleStep() && !alg->IsHidden())
308 : {
309 99 : ret += '\n';
310 198 : alg->SetCallPath({name});
311 99 : ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
312 : }
313 : }
314 120 : for (const std::string &name : m_stepRegistry.GetNames())
315 : {
316 234 : auto alg = GetStepAlg(name);
317 117 : assert(alg);
318 138 : if (alg->CanBeLastStep() && !alg->CanBeMiddleStep() &&
319 138 : !alg->IsHidden() && name != GDALVectorWriteAlgorithm::NAME)
320 : {
321 9 : ret += '\n';
322 18 : alg->SetCallPath({name});
323 9 : ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
324 : }
325 : }
326 : {
327 3 : const auto name = GDALVectorWriteAlgorithm::NAME;
328 3 : ret += '\n';
329 6 : auto alg = GetStepAlg(name);
330 6 : alg->SetCallPath({name});
331 3 : ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
332 : }
333 :
334 3 : ret += GetUsageForCLIEnd();
335 :
336 3 : return ret;
337 : }
338 :
339 : /************************************************************************/
340 : /* GDALVectorDecoratedDataset */
341 : /************************************************************************/
342 :
343 : namespace
344 : {
345 : class DummyDataset final : public GDALDataset
346 : {
347 : public:
348 16 : DummyDataset() = default;
349 : };
350 : } // namespace
351 :
352 : /************************************************************************/
353 : /* GDALVectorDecoratedDataset() */
354 : /************************************************************************/
355 :
356 402 : GDALVectorDecoratedDataset::GDALVectorDecoratedDataset(GDALDataset *poSrcDS)
357 804 : : m_dummySrcDS(poSrcDS ? nullptr : std::make_unique<DummyDataset>()),
358 1206 : m_srcDS(poSrcDS ? *poSrcDS : *(m_dummySrcDS.get()))
359 : {
360 402 : m_srcDS.Reference();
361 402 : SetDescription(m_srcDS.GetDescription());
362 402 : }
363 :
364 : /************************************************************************/
365 : /* ~GDALVectorDecoratedDataset() */
366 : /************************************************************************/
367 :
368 402 : GDALVectorDecoratedDataset::~GDALVectorDecoratedDataset()
369 : {
370 402 : m_srcDS.ReleaseRef();
371 402 : }
372 :
373 : /************************************************************************/
374 : /* GDALVectorPipelineOutputLayer */
375 : /************************************************************************/
376 :
377 : /************************************************************************/
378 : /* GDALVectorPipelineOutputLayer() */
379 : /************************************************************************/
380 :
381 332 : GDALVectorPipelineOutputLayer::GDALVectorPipelineOutputLayer(OGRLayer &srcLayer)
382 332 : : m_srcLayer(srcLayer)
383 : {
384 332 : }
385 :
386 : /************************************************************************/
387 : /* ~GDALVectorPipelineOutputLayer() */
388 : /************************************************************************/
389 :
390 : GDALVectorPipelineOutputLayer::~GDALVectorPipelineOutputLayer() = default;
391 :
392 : /************************************************************************/
393 : /* GDALVectorPipelineOutputLayer::ResetReading() */
394 : /************************************************************************/
395 :
396 1753 : void GDALVectorPipelineOutputLayer::ResetReading()
397 : {
398 1753 : m_srcLayer.ResetReading();
399 1753 : m_pendingFeatures.clear();
400 1753 : m_idxInPendingFeatures = 0;
401 1753 : }
402 :
403 : /************************************************************************/
404 : /* GDALVectorPipelineOutputLayer::GetNextRawFeature() */
405 : /************************************************************************/
406 :
407 5695 : OGRFeature *GDALVectorPipelineOutputLayer::GetNextRawFeature()
408 : {
409 5695 : if (m_idxInPendingFeatures < m_pendingFeatures.size())
410 : {
411 : OGRFeature *poFeature =
412 282 : m_pendingFeatures[m_idxInPendingFeatures].release();
413 282 : ++m_idxInPendingFeatures;
414 282 : return poFeature;
415 : }
416 5413 : m_pendingFeatures.clear();
417 5413 : m_idxInPendingFeatures = 0;
418 : while (true)
419 : {
420 : auto poSrcFeature =
421 5743 : std::unique_ptr<OGRFeature>(m_srcLayer.GetNextFeature());
422 5743 : if (!poSrcFeature)
423 764 : return nullptr;
424 4979 : if (!TranslateFeature(std::move(poSrcFeature), m_pendingFeatures))
425 : {
426 14 : return nullptr;
427 : }
428 4965 : if (!m_pendingFeatures.empty())
429 4635 : break;
430 330 : }
431 4635 : OGRFeature *poFeature = m_pendingFeatures[0].release();
432 4635 : m_idxInPendingFeatures = 1;
433 4635 : return poFeature;
434 : }
435 :
436 : /************************************************************************/
437 : /* GDALVectorOutputDataset */
438 : /************************************************************************/
439 :
440 : /************************************************************************/
441 : /* GDALVectorOutputDataset() */
442 : /************************************************************************/
443 :
444 18 : GDALVectorOutputDataset::GDALVectorOutputDataset(GDALDataset *poSrcDS)
445 18 : : GDALVectorDecoratedDataset(poSrcDS)
446 : {
447 18 : }
448 :
449 : /************************************************************************/
450 : /* TestCapability() */
451 : /************************************************************************/
452 :
453 15 : int GDALVectorOutputDataset::TestCapability(const char *) const
454 : {
455 15 : return 0;
456 : }
457 :
458 : /************************************************************************/
459 : /* GDALVectorPipelineOutputDataset */
460 : /************************************************************************/
461 :
462 : /************************************************************************/
463 : /* GDALVectorPipelineOutputDataset() */
464 : /************************************************************************/
465 :
466 287 : GDALVectorPipelineOutputDataset::GDALVectorPipelineOutputDataset(
467 287 : GDALDataset &srcDS)
468 287 : : GDALVectorDecoratedDataset(&srcDS)
469 : {
470 287 : }
471 :
472 : /************************************************************************/
473 : /* GDALVectorPipelineOutputDataset::AddLayer() */
474 : /************************************************************************/
475 :
476 336 : void GDALVectorPipelineOutputDataset::AddLayer(
477 : OGRLayer &oSrcLayer,
478 : std::unique_ptr<OGRLayerWithTranslateFeature> poNewLayer)
479 : {
480 336 : m_layersToDestroy.push_back(std::move(poNewLayer));
481 : OGRLayerWithTranslateFeature *poNewLayerRaw =
482 336 : m_layersToDestroy.back().get();
483 336 : m_layers.push_back(poNewLayerRaw);
484 336 : m_mapSrcLayerToNewLayer[&oSrcLayer] = poNewLayerRaw;
485 336 : }
486 :
487 : /************************************************************************/
488 : /* GDALVectorPipelineOutputDataset::GetLayerCount() */
489 : /************************************************************************/
490 :
491 1767 : int GDALVectorPipelineOutputDataset::GetLayerCount() const
492 : {
493 1767 : return static_cast<int>(m_layers.size());
494 : }
495 :
496 : /************************************************************************/
497 : /* GDALVectorPipelineOutputDataset::GetLayer() */
498 : /************************************************************************/
499 :
500 794 : OGRLayer *GDALVectorPipelineOutputDataset::GetLayer(int idx) const
501 : {
502 794 : return idx >= 0 && idx < GetLayerCount() ? m_layers[idx] : nullptr;
503 : }
504 :
505 : /************************************************************************/
506 : /* GDALVectorPipelineOutputDataset::TestCapability() */
507 : /************************************************************************/
508 :
509 227 : int GDALVectorPipelineOutputDataset::TestCapability(const char *pszCap) const
510 : {
511 227 : if (EQUAL(pszCap, ODsCRandomLayerRead) ||
512 80 : EQUAL(pszCap, ODsCMeasuredGeometries) || EQUAL(pszCap, ODsCZGeometries))
513 : {
514 187 : return m_srcDS.TestCapability(pszCap);
515 : }
516 40 : return false;
517 : }
518 :
519 : /************************************************************************/
520 : /* GDALVectorPipelineOutputDataset::ResetReading() */
521 : /************************************************************************/
522 :
523 3 : void GDALVectorPipelineOutputDataset::ResetReading()
524 : {
525 3 : m_srcDS.ResetReading();
526 3 : m_pendingFeatures.clear();
527 3 : m_idxInPendingFeatures = 0;
528 3 : }
529 :
530 : /************************************************************************/
531 : /* GDALVectorPipelineOutputDataset::GetNextFeature() */
532 : /************************************************************************/
533 :
534 23 : OGRFeature *GDALVectorPipelineOutputDataset::GetNextFeature(
535 : OGRLayer **ppoBelongingLayer, double *pdfProgressPct,
536 : GDALProgressFunc pfnProgress, void *pProgressData)
537 : {
538 23 : if (m_idxInPendingFeatures < m_pendingFeatures.size())
539 : {
540 : OGRFeature *poFeature =
541 3 : m_pendingFeatures[m_idxInPendingFeatures].release();
542 3 : if (ppoBelongingLayer)
543 3 : *ppoBelongingLayer = m_belongingLayer;
544 3 : ++m_idxInPendingFeatures;
545 3 : return poFeature;
546 : }
547 :
548 20 : m_pendingFeatures.clear();
549 20 : m_idxInPendingFeatures = 0;
550 :
551 : while (true)
552 : {
553 20 : OGRLayer *poSrcBelongingLayer = nullptr;
554 20 : auto poSrcFeature = std::unique_ptr<OGRFeature>(m_srcDS.GetNextFeature(
555 20 : &poSrcBelongingLayer, pdfProgressPct, pfnProgress, pProgressData));
556 20 : if (!poSrcFeature)
557 3 : return nullptr;
558 17 : auto iterToDstLayer = m_mapSrcLayerToNewLayer.find(poSrcBelongingLayer);
559 17 : if (iterToDstLayer != m_mapSrcLayerToNewLayer.end())
560 : {
561 17 : m_belongingLayer = iterToDstLayer->second;
562 :
563 17 : if (!m_belongingLayer->TranslateFeature(std::move(poSrcFeature),
564 17 : m_pendingFeatures))
565 : {
566 0 : return nullptr;
567 : }
568 :
569 17 : if (!m_pendingFeatures.empty())
570 17 : break;
571 : }
572 0 : }
573 17 : OGRFeature *poFeature = m_pendingFeatures[0].release();
574 17 : if (ppoBelongingLayer)
575 17 : *ppoBelongingLayer = m_belongingLayer;
576 17 : m_idxInPendingFeatures = 1;
577 17 : return poFeature;
578 : }
579 :
580 : /************************************************************************/
581 : /* GDALVectorPipelinePassthroughLayer::GetLayerDefn() */
582 : /************************************************************************/
583 :
584 2485 : const OGRFeatureDefn *GDALVectorPipelinePassthroughLayer::GetLayerDefn() const
585 : {
586 2485 : return m_srcLayer.GetLayerDefn();
587 : }
588 :
589 : /************************************************************************/
590 : /* GDALVectorPipelinePassthroughLayer::GetLayerDefn() */
591 : /************************************************************************/
592 :
593 1091 : bool GDALVectorPipelinePassthroughLayer::TranslateFeature(
594 : std::unique_ptr<OGRFeature> poSrcFeature,
595 : std::vector<std::unique_ptr<OGRFeature>> &apoOutFeatures)
596 : {
597 2115 : if ((!m_poFilterGeom || FilterGeometry(poSrcFeature->GetGeometryRef())) &&
598 1024 : (!m_poAttrQuery || m_poAttrQuery->Evaluate(poSrcFeature.get())))
599 : {
600 911 : apoOutFeatures.push_back(std::move(poSrcFeature));
601 : }
602 :
603 1091 : return true;
604 : }
605 :
606 : /************************************************************************/
607 : /* GDALVectorNonStreamingAlgorithmDataset() */
608 : /************************************************************************/
609 :
610 79 : GDALVectorNonStreamingAlgorithmDataset::GDALVectorNonStreamingAlgorithmDataset(
611 79 : GDALDataset &oSrcDS)
612 79 : : GDALVectorDecoratedDataset(&oSrcDS)
613 : {
614 79 : }
615 :
616 : /************************************************************************/
617 : /* ~GDALVectorNonStreamingAlgorithmDataset() */
618 : /************************************************************************/
619 :
620 : GDALVectorNonStreamingAlgorithmDataset::
621 : ~GDALVectorNonStreamingAlgorithmDataset() = default;
622 :
623 : /************************************************************************/
624 : /* GDALVectorNonStreamingAlgorithmDataset::AddProcessedLayer() */
625 : /************************************************************************/
626 :
627 71 : bool GDALVectorNonStreamingAlgorithmDataset::AddProcessedLayer(
628 : std::unique_ptr<GDALVectorNonStreamingAlgorithmLayer> layer,
629 : GDALProgressFunc progressFn, void *progressData)
630 : {
631 71 : if (!layer->Process(progressFn, progressData))
632 : {
633 6 : return false;
634 : }
635 :
636 65 : m_owned_layers.emplace_back(std::move(layer));
637 65 : m_layers.push_back(m_owned_layers.back().get());
638 :
639 65 : return true;
640 : }
641 :
642 : /************************************************************************/
643 : /* GDALVectorNonStreamingAlgorithmDataset::AddPassThroughLayer() */
644 : /************************************************************************/
645 :
646 8 : void GDALVectorNonStreamingAlgorithmDataset::AddPassThroughLayer(
647 : OGRLayer &oLayer)
648 : {
649 8 : m_owned_layers.push_back(
650 16 : std::make_unique<GDALVectorPipelinePassthroughLayer>(oLayer));
651 8 : m_layers.push_back(m_owned_layers.back().get());
652 8 : }
653 :
654 : /************************************************************************/
655 : /* GDALVectorNonStreamingAlgorithmDataset::GetLayerCount() */
656 : /************************************************************************/
657 :
658 248 : int GDALVectorNonStreamingAlgorithmDataset::GetLayerCount() const
659 : {
660 248 : return static_cast<int>(m_layers.size());
661 : }
662 :
663 : /************************************************************************/
664 : /* GDALVectorNonStreamingAlgorithmDataset::GetLayer() */
665 : /************************************************************************/
666 :
667 171 : OGRLayer *GDALVectorNonStreamingAlgorithmDataset::GetLayer(int idx) const
668 : {
669 171 : if (idx < 0 || idx >= static_cast<int>(m_layers.size()))
670 : {
671 8 : return nullptr;
672 : }
673 163 : return m_layers[idx];
674 : }
675 :
676 : /************************************************************************/
677 : /* GDALVectorNonStreamingAlgorithmDataset::TestCapability() */
678 : /************************************************************************/
679 :
680 35 : int GDALVectorNonStreamingAlgorithmDataset::TestCapability(
681 : const char *pszCap) const
682 : {
683 35 : if (EQUAL(pszCap, ODsCCurveGeometries) ||
684 31 : EQUAL(pszCap, ODsCMeasuredGeometries) || EQUAL(pszCap, ODsCZGeometries))
685 : {
686 16 : return true;
687 : }
688 :
689 19 : return false;
690 : }
691 :
692 : /************************************************************************/
693 : /* GDALVectorAlgorithmLayerProgressHelper() */
694 : /************************************************************************/
695 :
696 79 : GDALVectorAlgorithmLayerProgressHelper::GDALVectorAlgorithmLayerProgressHelper(
697 79 : GDALProgressFunc pfnProgress, void *pProgressData)
698 79 : : m_pfnProgress(pfnProgress), m_pProgressData(pProgressData)
699 : {
700 79 : }
701 :
702 : /************************************************************************/
703 : /* GDALVectorAlgorithmLayerProgressHelper() */
704 : /************************************************************************/
705 :
706 79 : GDALVectorAlgorithmLayerProgressHelper::GDALVectorAlgorithmLayerProgressHelper(
707 79 : const GDALPipelineStepRunContext &ctxt)
708 79 : : GDALVectorAlgorithmLayerProgressHelper(ctxt.m_pfnProgress,
709 79 : ctxt.m_pProgressData)
710 : {
711 79 : }
712 :
713 : /************************************************************************/
714 : /* GDALVectorAlgorithmLayerProgressHelper::AddProcessedLayer() */
715 : /************************************************************************/
716 :
717 73 : void GDALVectorAlgorithmLayerProgressHelper::AddProcessedLayer(
718 : OGRLayer &srcLayer)
719 : {
720 73 : m_apoSrcLayers.emplace_back(&srcLayer, true);
721 81 : if (m_pfnProgress && m_nTotalFeatures >= 0 &&
722 8 : srcLayer.TestCapability(OLCFastFeatureCount))
723 : {
724 8 : const auto nLayerFeatures = srcLayer.GetFeatureCount(false);
725 8 : if (nLayerFeatures < 0)
726 0 : m_nTotalFeatures = -1;
727 : else
728 8 : m_nTotalFeatures += nLayerFeatures;
729 8 : m_anFeatures.push_back(nLayerFeatures);
730 : }
731 : else
732 : {
733 65 : m_anFeatures.push_back(-1);
734 65 : m_nTotalFeatures = -1;
735 : }
736 73 : }
737 :
738 : /************************************************************************/
739 : /* GDALVectorAlgorithmLayerProgressHelper::AddPassThroughLayer() */
740 : /************************************************************************/
741 :
742 12 : void GDALVectorAlgorithmLayerProgressHelper::AddPassThroughLayer(
743 : OGRLayer &srcLayer)
744 : {
745 12 : m_apoSrcLayers.emplace_back(&srcLayer, false);
746 12 : }
747 :
748 : /************************************************************************/
749 : /* GDALVectorNonStreamingAlgorithmLayer() */
750 : /************************************************************************/
751 :
752 71 : GDALVectorNonStreamingAlgorithmLayer::GDALVectorNonStreamingAlgorithmLayer(
753 71 : OGRLayer &srcLayer, int geomFieldIndex)
754 71 : : m_srcLayer(srcLayer), m_geomFieldIndex(geomFieldIndex)
755 : {
756 71 : }
757 :
758 : /************************************************************************/
759 : /* ~GDALVectorNonStreamingAlgorithmLayer() */
760 : /************************************************************************/
761 :
762 : GDALVectorNonStreamingAlgorithmLayer::~GDALVectorNonStreamingAlgorithmLayer() =
763 : default;
764 :
765 : /************************************************************************/
766 : /* GDALVectorNonStreamingAlgorithmLayer::GetNextRawFeature() */
767 : /************************************************************************/
768 :
769 2492 : OGRFeature *GDALVectorNonStreamingAlgorithmLayer::GetNextRawFeature()
770 : {
771 2492 : return GetNextProcessedFeature().release();
772 : }
773 :
774 : /************************************************************************/
775 : /* GDALVectorAlgorithmLayerProgressHelper::iterator::operator*() */
776 : /************************************************************************/
777 :
778 : GDALVectorAlgorithmLayerProgressHelper::iterator::value_type
779 81 : GDALVectorAlgorithmLayerProgressHelper::iterator::operator*() const
780 : {
781 : const double dfProgressStart =
782 156 : m_helper.m_anFeatures.empty() ? 0
783 75 : : m_helper.m_nTotalFeatures > 0
784 75 : ? static_cast<double>(m_nFeatureIdx) /
785 8 : static_cast<double>(m_helper.m_nTotalFeatures)
786 67 : : static_cast<double>(m_nProcessedLayerIdx) /
787 67 : std::max(1.0,
788 67 : static_cast<double>(m_helper.m_anFeatures.size()));
789 : const double dfProgressEnd =
790 156 : m_helper.m_anFeatures.empty() ? 0
791 75 : : m_helper.m_nTotalFeatures > 0
792 75 : ? static_cast<double>(m_nFeatureIdx +
793 8 : m_helper.m_anFeatures[m_nProcessedLayerIdx]) /
794 8 : static_cast<double>(m_helper.m_nTotalFeatures)
795 67 : : static_cast<double>(m_nProcessedLayerIdx + 1) /
796 67 : std::max(1.0,
797 67 : static_cast<double>(m_helper.m_anFeatures.size()));
798 :
799 : progress_data_unique_ptr pScaledProgressData(nullptr,
800 81 : GDALDestroyScaledProgress);
801 81 : if (m_helper.m_pfnProgress && m_helper.m_apoSrcLayers[m_nLayerIdx].second)
802 : {
803 8 : pScaledProgressData.reset(GDALCreateScaledProgress(
804 8 : dfProgressStart, dfProgressEnd, m_helper.m_pfnProgress,
805 8 : m_helper.m_pProgressData));
806 : }
807 :
808 162 : return value_type(m_helper.m_apoSrcLayers[m_nLayerIdx].first,
809 81 : m_helper.m_apoSrcLayers[m_nLayerIdx].second,
810 162 : m_helper.m_pfnProgress ? GDALScaledProgress : nullptr,
811 243 : std::move(pScaledProgressData));
812 : }
813 :
814 : //! @endcond
|