13#ifndef GDALALG_ABSTRACT_PIPELINE_INCLUDED
14#define GDALALG_ABSTRACT_PIPELINE_INCLUDED
20#include "gdalalgorithm.h"
29#ifndef GDAL_PIPELINE_PROJ_NOSTALGIA
30#define GDAL_PIPELINE_PROJ_NOSTALGIA
37class GDALPipelineStepAlgorithm;
39class GDALPipelineStepRunContext
42 GDALPipelineStepRunContext() =
default;
45 GDALProgressFunc m_pfnProgress =
nullptr;
46 void *m_pProgressData =
nullptr;
52 GDALPipelineStepAlgorithm *m_poNextUsableStep =
nullptr;
65 std::vector<GDALArgDatasetValue> &GetInputDatasets()
67 return m_inputDataset;
70 const std::vector<GDALArgDatasetValue> &GetInputDatasets()
const
72 return m_inputDataset;
77 return m_outputDataset;
82 return m_outputDataset;
85 const std::string &GetOutputString()
const
90 const std::string &GetOutputLayerName()
const
92 return m_outputLayerName;
95 const std::string &GetOutputFormat()
const
100 const std::vector<std::string> &GetCreationOptions()
const
102 return m_creationOptions;
105 const std::vector<std::string> &GetLayerCreationOptions()
const
107 return m_layerCreationOptions;
110 bool GetOverwriteLayer()
const
112 return m_overwriteLayer;
115 bool GetAppendLayer()
const
117 return m_appendLayer;
120 virtual int GetInputType()
const = 0;
122 virtual int GetOutputType()
const = 0;
129 return m_inputDataset.empty() ? nullptr
130 : m_inputDataset[0].GetDatasetRef();
137 struct ConstructorOptions
139 bool standaloneStep =
false;
140 bool addDefaultArguments =
true;
141 bool autoOpenInputDatasets =
true;
142 bool inputDatasetRequired =
true;
143 bool inputDatasetPositional =
true;
144 bool outputDatasetRequired =
true;
145 bool addInputLayerNameArgument =
true;
146 bool addUpdateArgument =
true;
147 bool addAppendLayerArgument =
true;
148 bool addNoCreateEmptyLayersArgument =
false;
149 bool addOverwriteLayerArgument =
true;
150 bool addUpsertArgument =
true;
151 bool addSkipErrorsArgument =
true;
152 bool addOutputLayerNameArgument =
true;
153 bool outputLayerNameAvailableInPipelineStep =
false;
154 int inputDatasetMaxCount = 1;
155 int inputDatasetInputFlags = GADV_NAME | GADV_OBJECT;
156 std::string inputDatasetHelpMsg{};
157 std::string inputDatasetAlias{};
158 std::string inputDatasetMetaVar =
"INPUT";
159 std::string outputDatasetHelpMsg{};
162 inline ConstructorOptions &SetStandaloneStep(
bool b)
168 inline ConstructorOptions &SetAddDefaultArguments(
bool b)
170 addDefaultArguments = b;
174 inline ConstructorOptions &SetAddInputLayerNameArgument(
bool b)
176 addInputLayerNameArgument = b;
180 inline ConstructorOptions &SetInputDatasetRequired(
bool b)
182 inputDatasetRequired = b;
186 inline ConstructorOptions &SetInputDatasetPositional(
bool b)
188 inputDatasetPositional = b;
192 inline ConstructorOptions &SetInputDatasetMaxCount(
int maxCount)
194 inputDatasetMaxCount = maxCount;
198 inline ConstructorOptions &SetInputDatasetInputFlags(
int flags)
200 inputDatasetInputFlags = flags;
204 inline ConstructorOptions &SetInputDatasetHelpMsg(
const std::string &s)
206 inputDatasetHelpMsg = s;
210 inline ConstructorOptions &SetInputDatasetAlias(
const std::string &s)
212 inputDatasetAlias = s;
216 inline ConstructorOptions &SetInputDatasetMetaVar(
const std::string &s)
218 inputDatasetMetaVar = s;
222 inline ConstructorOptions &SetOutputDatasetHelpMsg(
const std::string &s)
224 outputDatasetHelpMsg = s;
228 inline ConstructorOptions &SetAutoOpenInputDatasets(
bool b)
230 autoOpenInputDatasets = b;
234 inline ConstructorOptions &SetOutputDatasetRequired(
bool b)
236 outputDatasetRequired = b;
240 inline ConstructorOptions &
241 SetOutputFormatCreateCapability(
const std::string &capability)
243 outputFormatCreateCapability = capability;
247 inline ConstructorOptions &SetAddAppendLayerArgument(
bool b)
249 addAppendLayerArgument = b;
253 inline ConstructorOptions &SetAddOverwriteLayerArgument(
bool b)
255 addOverwriteLayerArgument = b;
259 inline ConstructorOptions &SetAddUpdateArgument(
bool b)
261 addUpdateArgument = b;
265 inline ConstructorOptions &SetAddUpsertArgument(
bool b)
267 addUpsertArgument = b;
271 inline ConstructorOptions &SetNoCreateEmptyLayersArgument(
bool b)
273 addNoCreateEmptyLayersArgument = b;
277 inline ConstructorOptions &SetAddSkipErrorsArgument(
bool b)
279 addSkipErrorsArgument = b;
283 inline ConstructorOptions &SetAddOutputLayerNameArgument(
bool b)
285 addOutputLayerNameArgument = b;
289 inline ConstructorOptions &
290 SetOutputLayerNameAvailableInPipelineStep(
bool b)
292 outputLayerNameAvailableInPipelineStep = b;
297 GDALPipelineStepAlgorithm(
const std::string &name,
298 const std::string &description,
299 const std::string &helpURL,
300 const ConstructorOptions &);
302 friend class GDALPipelineAlgorithm;
303 friend class GDALRasterPipelineAlgorithm;
304 friend class GDALVectorPipelineAlgorithm;
305 friend class GDALAbstractPipelineAlgorithm;
307 virtual bool CanBeFirstStep()
const
312 virtual bool CanBeMiddleStep()
const
314 return !CanBeFirstStep() && !CanBeLastStep();
317 virtual bool CanBeLastStep()
const
323 virtual bool GeneratesFilesFromUserInput()
const
328 virtual bool IsNativelyStreamingCompatible()
const
333 virtual bool SupportsInputMultiThreading()
const
338 virtual bool CanHandleNextStep(GDALPipelineStepAlgorithm *)
const
343 virtual bool OutputDatasetAllowedBeforeRunningStep()
const
348 virtual CPLJSONObject Get_OGR_SCHEMA_OpenOption_Layer()
const
355 virtual bool RunStep(GDALPipelineStepRunContext &ctxt) = 0;
357 bool m_standaloneStep =
false;
358 const ConstructorOptions m_constructorOptions;
359 bool m_outputVRTCompatible =
true;
360 std::string m_helpDocCategory{};
363 std::vector<GDALArgDatasetValue> m_inputDataset{};
364 std::vector<std::string> m_openOptions{};
365 std::vector<std::string> m_inputFormats{};
366 std::vector<std::string> m_inputLayerNames{};
369 bool m_stdout =
false;
370 std::string m_output{};
372 std::string m_format{};
373 std::vector<std::string> m_outputOpenOptions{};
374 std::vector<std::string> m_creationOptions{};
375 bool m_overwrite =
false;
376 std::string m_outputLayerName{};
378 bool m_appendRaster =
false;
381 std::vector<std::string> m_layerCreationOptions{};
382 bool m_update =
false;
383 bool m_overwriteLayer =
false;
384 bool m_appendLayer =
false;
385 bool m_upsert =
false;
386 bool m_skipErrors =
false;
387 bool m_noCreateEmptyLayers =
false;
389 void AddRasterInputArgs(
bool openForMixedRasterVector,
bool hiddenForCLI);
390 void AddRasterOutputArgs(
bool hiddenForCLI);
391 void AddRasterHiddenInputDatasetArg();
393 void AddVectorInputArgs(
bool hiddenForCLI);
394 void AddVectorHiddenInputDatasetArg();
395 void AddVectorOutputArgs(
bool hiddenForCLI,
396 bool shortNameOutputLayerAllowed);
399 bool shortNameOutputLayerAllowed);
402 bool RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
override;
414 :
public GDALPipelineStepAlgorithm
417 std::vector<std::string> GetAutoComplete(std::vector<std::string> &args,
418 bool lastWordIsComplete,
421 bool Finalize()
override;
423 std::string GetUsageAsJSON()
const override;
426 ParseCommandLineArguments(
const std::vector<std::string> &args)
override;
428 bool HasSteps()
const
430 return !m_steps.empty();
433 static constexpr const char *OPEN_NESTED_PIPELINE =
"[";
434 static constexpr const char *CLOSE_NESTED_PIPELINE =
"]";
436 static constexpr const char *RASTER_SUFFIX =
"-raster";
437 static constexpr const char *VECTOR_SUFFIX =
"-vector";
440 friend class GDALTeeStepAlgorithmAbstract;
442 GDALAbstractPipelineAlgorithm(
443 const std::string &name,
const std::string &description,
444 const std::string &helpURL,
445 const GDALPipelineStepAlgorithm::ConstructorOptions &options)
446 : GDALPipelineStepAlgorithm(
447 name, description, helpURL,
448 ConstructorOptions(options).SetAutoOpenInputDatasets(false))
452 std::string m_pipeline{};
458 std::unique_ptr<GDALPipelineStepAlgorithm>
459 GetStepAlg(
const std::string &name)
const;
461 bool HasOutputString()
const override;
463 static bool IsReadSpecificArgument(
const char *pszArgName);
464 static bool IsWriteSpecificArgument(
const char *pszArgName);
467 friend class GDALPipelineAlgorithm;
468 friend class GDALRasterPipelineAlgorithm;
469 friend class GDALVectorPipelineAlgorithm;
471 std::vector<std::unique_ptr<GDALPipelineStepAlgorithm>> m_steps{};
473 std::unique_ptr<GDALPipelineStepAlgorithm> m_stepOnWhichHelpIsRequested{};
475 bool m_bInnerPipeline =
false;
476 bool m_bExpectReadStep =
true;
477 int m_nFirstStepWithUnknownInputType = -1;
479 enum class StepConstraint
486 StepConstraint m_eLastStepAsWrite = StepConstraint::CAN_BE;
488 std::vector<std::unique_ptr<GDALAbstractPipelineAlgorithm>>
489 m_apoNestedPipelines{};
492 static constexpr int MAX_NESTING_LEVEL = 3;
495 CheckFirstAndLastStep(
const std::vector<GDALPipelineStepAlgorithm *> &steps,
496 bool forAutoComplete)
const;
498 static int GetInputDatasetType(
const GDALPipelineStepAlgorithm *alg);
500 bool CopyStepAlgorithmFromAnother(GDALPipelineStepAlgorithm *dst,
501 const GDALPipelineStepAlgorithm *src,
502 bool maybeWriteStep)
const;
504 bool ParseCommandLineArguments(
const std::vector<std::string> &args,
505 bool forAutoComplete);
507 bool RunStep(GDALPipelineStepRunContext &ctxt)
override;
510 BuildNestedPipeline(GDALPipelineStepAlgorithm *curAlg,
511 std::vector<std::string> &nestedPipelineArgs,
512 bool forAutoComplete);
514 bool SaveGDALGIntoFileOrString(
const std::string &outFilename,
515 std::string &outString)
const;
517 virtual std::unique_ptr<GDALAbstractPipelineAlgorithm>
518 CreateNestedPipeline()
const = 0;
The CPLJSONArray class holds JSON object from CPLJSONDocument.
Definition cpl_json.h:60
void Deinit()
Decrement reference counter and make pointer NULL.
Definition cpl_json.cpp:1390
Registry of GDAL algorithms.
Definition gdalalgorithm_cpp.h:2316
GDAL algorithm.
Definition gdalalgorithm_cpp.h:2445
virtual bool Finalize()
Complete any pending actions, and return the final status.
Definition gdalalgorithm.cpp:6307
ProcessGDALGOutputRet
Return value for ProcessGDALGOutput.
Definition gdalalgorithm_cpp.h:3147
GDALInConstructionAlgorithmArg & AddOutputLayerNameArg(std::string *pValue, const char *helpMessage=nullptr)
Add (single) output layer name argument.
Definition gdalalgorithm.cpp:5033
virtual bool CheckSafeForStreamOutput()
Method executed by Run() when m_executionForStreamOutput is set to ensure the command is safe to exec...
Definition gdalalgorithm.cpp:6284
virtual ProcessGDALGOutputRet ProcessGDALGOutput()
Process output to a .gdalg file.
Definition gdalalgorithm.cpp:5726
Value for an argument that points to a GDALDataset.
Definition gdalalgorithm_cpp.h:163
A set of associated raster bands, usually from one file.
Definition gdal_dataset.h:77
Technical class used by GDALAlgorithm when constructing argument declarations.
Definition gdalalgorithm_cpp.h:1918
Interface for read and write JSON documents.
#define CPL_NON_FINAL
Mark that a class is explicitly recognized as non-final.
Definition cpl_port.h:1094
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition cpl_port.h:1101
#define GDAL_DCAP_CREATECOPY
Capability set by a driver that implements the CreateCopy() API.
Definition gdal.h:623
This file is legacy since GDAL 3.12, but will be kept at least in the whole GDAL 3....