LCOV - code coverage report
Current view: top level - apps - gdalalg_abstract_pipeline.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 102 102 100.0 %
Date: 2025-11-11 01:53:33 Functions: 39 39 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  gdal "raster/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             : #ifndef GDALALG_ABSTRACT_PIPELINE_INCLUDED
      14             : #define GDALALG_ABSTRACT_PIPELINE_INCLUDED
      15             : 
      16             : //! @cond Doxygen_Suppress
      17             : 
      18             : #include "cpl_json.h"
      19             : 
      20             : #include "gdalalgorithm.h"
      21             : #include "gdal_priv.h"
      22             : 
      23             : #include <algorithm>
      24             : 
      25             : // This is an easter egg to pay tribute to PROJ pipeline syntax
      26             : // We accept "gdal vector +gdal=pipeline +step +gdal=read +input=in.tif +step +gdal=reproject +dst-crs=EPSG:32632 +step +gdal=write +output=out.tif +overwrite"
      27             : // as an alternative to (recommended):
      28             : // "gdal vector pipeline ! read in.tif ! reproject--dst-crs=EPSG:32632 ! write out.tif --overwrite"
      29             : #ifndef GDAL_PIPELINE_PROJ_NOSTALGIA
      30             : #define GDAL_PIPELINE_PROJ_NOSTALGIA
      31             : #endif
      32             : 
      33             : /************************************************************************/
      34             : /*                      GDALPipelineStepRunContext                      */
      35             : /************************************************************************/
      36             : 
      37             : class GDALPipelineStepAlgorithm;
      38             : 
      39             : class GDALPipelineStepRunContext
      40             : {
      41             :   public:
      42             :     GDALPipelineStepRunContext() = default;
      43             : 
      44             :     // Progress callback to use during execution of the step
      45             :     GDALProgressFunc m_pfnProgress = nullptr;
      46             :     void *m_pProgressData = nullptr;
      47             : 
      48             :     // If there is a step in the pipeline immediately following step to which
      49             :     // this instance of GDALRasterPipelineStepRunContext is passed, and that
      50             :     // this next step is usable by the current step (as determined by
      51             :     // CanHandleNextStep()), then this member will point to this next step.
      52             :     GDALPipelineStepAlgorithm *m_poNextUsableStep = nullptr;
      53             : 
      54             :   private:
      55             :     CPL_DISALLOW_COPY_ASSIGN(GDALPipelineStepRunContext)
      56             : };
      57             : 
      58             : /************************************************************************/
      59             : /*                     GDALPipelineStepAlgorithm                        */
      60             : /************************************************************************/
      61             : 
      62             : class GDALPipelineStepAlgorithm /* non final */ : public GDALAlgorithm
      63             : {
      64             :   public:
      65         103 :     std::vector<GDALArgDatasetValue> &GetInputDatasets()
      66             :     {
      67         103 :         return m_inputDataset;
      68             :     }
      69             : 
      70             :     const std::vector<GDALArgDatasetValue> &GetInputDatasets() const
      71             :     {
      72             :         return m_inputDataset;
      73             :     }
      74             : 
      75          93 :     GDALArgDatasetValue &GetOutputDataset()
      76             :     {
      77          93 :         return m_outputDataset;
      78             :     }
      79             : 
      80             :     const GDALArgDatasetValue &GetOutputDataset() const
      81             :     {
      82             :         return m_outputDataset;
      83             :     }
      84             : 
      85         407 :     const std::string &GetOutputString() const
      86             :     {
      87         407 :         return m_output;
      88             :     }
      89             : 
      90          19 :     const std::string &GetOutputLayerName() const
      91             :     {
      92          19 :         return m_outputLayerName;
      93             :     }
      94             : 
      95         162 :     const std::string &GetOutputFormat() const
      96             :     {
      97         162 :         return m_format;
      98             :     }
      99             : 
     100          63 :     const std::vector<std::string> &GetCreationOptions() const
     101             :     {
     102          63 :         return m_creationOptions;
     103             :     }
     104             : 
     105          15 :     const std::vector<std::string> &GetLayerCreationOptions() const
     106             :     {
     107          15 :         return m_layerCreationOptions;
     108             :     }
     109             : 
     110          18 :     bool GetOverwriteLayer() const
     111             :     {
     112          18 :         return m_overwriteLayer;
     113             :     }
     114             : 
     115          18 :     bool GetAppendLayer() const
     116             :     {
     117          18 :         return m_appendLayer;
     118             :     }
     119             : 
     120             :     virtual int GetInputType() const = 0;
     121             : 
     122             :     virtual int GetOutputType() const = 0;
     123             : 
     124             :     bool Finalize() override;
     125             : 
     126             :     // Used by GDALDispatcherAlgorithm for vector info/convert
     127          32 :     GDALDataset *GetInputDatasetRef()
     128             :     {
     129          32 :         return m_inputDataset.empty() ? nullptr
     130          32 :                                       : m_inputDataset[0].GetDatasetRef();
     131             :     }
     132             : 
     133             :     // Used by GDALDispatcherAlgorithm for vector info/convert
     134             :     void SetInputDataset(GDALDataset *poDS);
     135             : 
     136             :   protected:
     137             :     struct ConstructorOptions
     138             :     {
     139             :         bool standaloneStep = false;
     140             :         bool addDefaultArguments = true;
     141             :         bool autoOpenInputDatasets = true;
     142             :         bool outputDatasetRequired = true;
     143             :         bool addInputLayerNameArgument = true;   // only for vector input
     144             :         bool addUpdateArgument = true;           // only for vector output
     145             :         bool addAppendLayerArgument = true;      // only for vector output
     146             :         bool addOverwriteLayerArgument = true;   // only for vector output
     147             :         bool addUpsertArgument = true;           // only for vector output
     148             :         bool addSkipErrorsArgument = true;       // only for vector output
     149             :         bool addOutputLayerNameArgument = true;  // only for vector output
     150             :         int inputDatasetMaxCount = 1;
     151             :         std::string inputDatasetHelpMsg{};
     152             :         std::string inputDatasetAlias{};
     153             :         std::string inputDatasetMetaVar = "INPUT";
     154             :         std::string outputDatasetHelpMsg{};
     155             :         std::string updateMutualExclusionGroup{};
     156             :         std::string outputDatasetMutualExclusionGroup{};
     157             :         std::string outputFormatCreateCapability = GDAL_DCAP_CREATECOPY;
     158             : 
     159        6384 :         inline ConstructorOptions &SetStandaloneStep(bool b)
     160             :         {
     161        6384 :             standaloneStep = b;
     162        6384 :             return *this;
     163             :         }
     164             : 
     165        2850 :         inline ConstructorOptions &SetAddDefaultArguments(bool b)
     166             :         {
     167        2850 :             addDefaultArguments = b;
     168        2850 :             return *this;
     169             :         }
     170             : 
     171         136 :         inline ConstructorOptions &SetAddInputLayerNameArgument(bool b)
     172             :         {
     173         136 :             addInputLayerNameArgument = b;
     174         136 :             return *this;
     175             :         }
     176             : 
     177        1528 :         inline ConstructorOptions &SetInputDatasetMaxCount(int maxCount)
     178             :         {
     179        1528 :             inputDatasetMaxCount = maxCount;
     180        1528 :             return *this;
     181             :         }
     182             : 
     183         849 :         inline ConstructorOptions &SetInputDatasetHelpMsg(const std::string &s)
     184             :         {
     185         849 :             inputDatasetHelpMsg = s;
     186         849 :             return *this;
     187             :         }
     188             : 
     189         506 :         inline ConstructorOptions &SetInputDatasetAlias(const std::string &s)
     190             :         {
     191         506 :             inputDatasetAlias = s;
     192         506 :             return *this;
     193             :         }
     194             : 
     195         213 :         inline ConstructorOptions &SetInputDatasetMetaVar(const std::string &s)
     196             :         {
     197         213 :             inputDatasetMetaVar = s;
     198         213 :             return *this;
     199             :         }
     200             : 
     201          87 :         inline ConstructorOptions &SetOutputDatasetHelpMsg(const std::string &s)
     202             :         {
     203          87 :             outputDatasetHelpMsg = s;
     204          87 :             return *this;
     205             :         }
     206             : 
     207         772 :         inline ConstructorOptions &SetAutoOpenInputDatasets(bool b)
     208             :         {
     209         772 :             autoOpenInputDatasets = b;
     210         772 :             return *this;
     211             :         }
     212             : 
     213          51 :         inline ConstructorOptions &SetOutputDatasetRequired(bool b)
     214             :         {
     215          51 :             outputDatasetRequired = b;
     216          51 :             return *this;
     217             :         }
     218             : 
     219             :         inline ConstructorOptions &
     220          51 :         SetUpdateMutualExclusionGroup(const std::string &s)
     221             :         {
     222          51 :             updateMutualExclusionGroup = s;
     223          51 :             return *this;
     224             :         }
     225             : 
     226             :         inline ConstructorOptions &
     227          51 :         SetOutputDatasetMutualExclusionGroup(const std::string &s)
     228             :         {
     229          51 :             outputDatasetMutualExclusionGroup = s;
     230          51 :             return *this;
     231             :         }
     232             : 
     233             :         inline ConstructorOptions &
     234         657 :         SetOutputFormatCreateCapability(const std::string &capability)
     235             :         {
     236         657 :             outputFormatCreateCapability = capability;
     237         657 :             return *this;
     238             :         }
     239             : 
     240          40 :         inline ConstructorOptions &SetAddAppendLayerArgument(bool b)
     241             :         {
     242          40 :             addAppendLayerArgument = b;
     243          40 :             return *this;
     244             :         }
     245             : 
     246          40 :         inline ConstructorOptions &SetAddOverwriteLayerArgument(bool b)
     247             :         {
     248          40 :             addOverwriteLayerArgument = b;
     249          40 :             return *this;
     250             :         }
     251             : 
     252          40 :         inline ConstructorOptions &SetAddUpdateArgument(bool b)
     253             :         {
     254          40 :             addUpdateArgument = b;
     255          40 :             return *this;
     256             :         }
     257             : 
     258         106 :         inline ConstructorOptions &SetAddUpsertArgument(bool b)
     259             :         {
     260         106 :             addUpsertArgument = b;
     261         106 :             return *this;
     262             :         }
     263             : 
     264         106 :         inline ConstructorOptions &SetAddSkipErrorsArgument(bool b)
     265             :         {
     266         106 :             addSkipErrorsArgument = b;
     267         106 :             return *this;
     268             :         }
     269             : 
     270         118 :         inline ConstructorOptions &SetAddOutputLayerNameArgument(bool b)
     271             :         {
     272         118 :             addOutputLayerNameArgument = b;
     273         118 :             return *this;
     274             :         }
     275             :     };
     276             : 
     277             :     GDALPipelineStepAlgorithm(const std::string &name,
     278             :                               const std::string &description,
     279             :                               const std::string &helpURL,
     280             :                               const ConstructorOptions &);
     281             : 
     282             :     friend class GDALPipelineAlgorithm;
     283             :     friend class GDALRasterPipelineAlgorithm;
     284             :     friend class GDALVectorPipelineAlgorithm;
     285             :     friend class GDALAbstractPipelineAlgorithm;
     286             : 
     287        1659 :     virtual bool CanBeFirstStep() const
     288             :     {
     289        1659 :         return false;
     290             :     }
     291             : 
     292         433 :     virtual bool CanBeMiddleStep() const
     293             :     {
     294         433 :         return !CanBeFirstStep() && !CanBeLastStep();
     295             :     }
     296             : 
     297         641 :     virtual bool CanBeLastStep() const
     298             :     {
     299         641 :         return false;
     300             :     }
     301             : 
     302             :     //! Whether a user parameter can cause a file to be written at a specified location
     303          43 :     virtual bool GeneratesFilesFromUserInput() const
     304             :     {
     305          43 :         return false;
     306             :     }
     307             : 
     308         456 :     virtual bool IsNativelyStreamingCompatible() const
     309             :     {
     310         456 :         return true;
     311             :     }
     312             : 
     313         160 :     virtual bool SupportsInputMultiThreading() const
     314             :     {
     315         160 :         return false;
     316             :     }
     317             : 
     318         995 :     virtual bool CanHandleNextStep(GDALPipelineStepAlgorithm *) const
     319             :     {
     320         995 :         return false;
     321             :     }
     322             : 
     323         167 :     virtual CPLJSONObject Get_OGR_SCHEMA_OpenOption_Layer() const
     324             :     {
     325         167 :         CPLJSONObject obj;
     326         167 :         obj.Deinit();
     327         167 :         return obj;
     328             :     }
     329             : 
     330             :     virtual bool RunStep(GDALPipelineStepRunContext &ctxt) = 0;
     331             : 
     332             :     bool m_standaloneStep = false;
     333             :     const ConstructorOptions m_constructorOptions;
     334             :     bool m_outputVRTCompatible = true;
     335             :     std::string m_helpDocCategory{};
     336             : 
     337             :     // Input arguments
     338             :     std::vector<GDALArgDatasetValue> m_inputDataset{};
     339             :     std::vector<std::string> m_openOptions{};
     340             :     std::vector<std::string> m_inputFormats{};
     341             :     std::vector<std::string> m_inputLayerNames{};
     342             : 
     343             :     // Output arguments
     344             :     bool m_stdout = false;
     345             :     std::string m_output{};
     346             :     GDALArgDatasetValue m_outputDataset{};
     347             :     std::string m_format{};
     348             :     std::vector<std::string> m_outputOpenOptions{};
     349             :     std::vector<std::string> m_creationOptions{};
     350             :     bool m_overwrite = false;
     351             :     std::string m_outputLayerName{};
     352             :     GDALInConstructionAlgorithmArg *m_outputFormatArg = nullptr;
     353             :     bool m_appendRaster = false;
     354             : 
     355             :     // Output arguments (vector specific)
     356             :     std::vector<std::string> m_layerCreationOptions{};
     357             :     bool m_update = false;
     358             :     bool m_overwriteLayer = false;
     359             :     bool m_appendLayer = false;
     360             :     bool m_upsert = false;
     361             :     bool m_skipErrors = false;
     362             : 
     363             :     void AddRasterInputArgs(bool openForMixedRasterVector, bool hiddenForCLI);
     364             :     void AddRasterOutputArgs(bool hiddenForCLI);
     365             :     void AddRasterHiddenInputDatasetArg();
     366             : 
     367             :     void AddVectorInputArgs(bool hiddenForCLI);
     368             :     void AddVectorOutputArgs(bool hiddenForCLI,
     369             :                              bool shortNameOutputLayerAllowed);
     370             : 
     371             :   private:
     372             :     bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) override;
     373             :     GDALAlgorithm::ProcessGDALGOutputRet ProcessGDALGOutput() override;
     374             :     bool CheckSafeForStreamOutput() override;
     375             : 
     376             :     CPL_DISALLOW_COPY_ASSIGN(GDALPipelineStepAlgorithm)
     377             : };
     378             : 
     379             : /************************************************************************/
     380             : /*                    GDALAbstractPipelineAlgorithm                     */
     381             : /************************************************************************/
     382             : 
     383             : class GDALAbstractPipelineAlgorithm CPL_NON_FINAL
     384             :     : public GDALPipelineStepAlgorithm
     385             : {
     386             :   public:
     387             :     std::vector<std::string> GetAutoComplete(std::vector<std::string> &args,
     388             :                                              bool lastWordIsComplete,
     389             :                                              bool /* showAllOptions*/) override;
     390             : 
     391             :     bool Finalize() override;
     392             : 
     393             :     std::string GetUsageAsJSON() const override;
     394             : 
     395             :     bool
     396             :     ParseCommandLineArguments(const std::vector<std::string> &args) override;
     397             : 
     398           2 :     bool HasSteps() const
     399             :     {
     400           2 :         return !m_steps.empty();
     401             :     }
     402             : 
     403             :     static constexpr const char *OPEN_NESTED_PIPELINE = "[";
     404             :     static constexpr const char *CLOSE_NESTED_PIPELINE = "]";
     405             : 
     406             :     static constexpr const char *RASTER_SUFFIX = "-raster";
     407             :     static constexpr const char *VECTOR_SUFFIX = "-vector";
     408             : 
     409             :   protected:
     410             :     friend class GDALTeeStepAlgorithmAbstract;
     411             : 
     412         435 :     GDALAbstractPipelineAlgorithm(
     413             :         const std::string &name, const std::string &description,
     414             :         const std::string &helpURL,
     415             :         const GDALPipelineStepAlgorithm::ConstructorOptions &options)
     416         435 :         : GDALPipelineStepAlgorithm(
     417             :               name, description, helpURL,
     418         435 :               ConstructorOptions(options).SetAutoOpenInputDatasets(false))
     419             :     {
     420         435 :     }
     421             : 
     422             :     std::string m_pipeline{};
     423             : 
     424             :     virtual GDALAlgorithmRegistry &GetStepRegistry() = 0;
     425             : 
     426             :     virtual const GDALAlgorithmRegistry &GetStepRegistry() const = 0;
     427             : 
     428             :     std::unique_ptr<GDALPipelineStepAlgorithm>
     429             :     GetStepAlg(const std::string &name) const;
     430             : 
     431             :     bool HasOutputString() const override;
     432             : 
     433             :     static bool IsReadSpecificArgument(const char *pszArgName);
     434             :     static bool IsWriteSpecificArgument(const char *pszArgName);
     435             : 
     436             :   private:
     437             :     friend class GDALPipelineAlgorithm;
     438             :     friend class GDALRasterPipelineAlgorithm;
     439             :     friend class GDALVectorPipelineAlgorithm;
     440             : 
     441             :     std::vector<std::unique_ptr<GDALPipelineStepAlgorithm>> m_steps{};
     442             : 
     443             :     std::unique_ptr<GDALPipelineStepAlgorithm> m_stepOnWhichHelpIsRequested{};
     444             : 
     445             :     bool m_bInnerPipeline = false;
     446             :     bool m_bExpectReadStep = true;
     447             : 
     448             :     enum class StepConstraint
     449             :     {
     450             :         MUST_BE,
     451             :         CAN_BE,
     452             :         CAN_NOT_BE
     453             :     };
     454             : 
     455             :     StepConstraint m_eLastStepAsWrite = StepConstraint::CAN_BE;
     456             : 
     457             :     std::vector<std::unique_ptr<GDALAbstractPipelineAlgorithm>>
     458             :         m_apoNestedPipelines{};
     459             : 
     460             :     // More would lead to unreadable pipelines
     461             :     static constexpr int MAX_NESTING_LEVEL = 3;
     462             : 
     463             :     bool
     464             :     CheckFirstAndLastStep(const std::vector<GDALPipelineStepAlgorithm *> &steps,
     465             :                           bool forAutoComplete) const;
     466             : 
     467             :     bool ParseCommandLineArguments(const std::vector<std::string> &args,
     468             :                                    bool forAutoComplete);
     469             : 
     470             :     bool RunStep(GDALPipelineStepRunContext &ctxt) override;
     471             : 
     472             :     std::string
     473             :     BuildNestedPipeline(GDALPipelineStepAlgorithm *curAlg,
     474             :                         std::vector<std::string> &nestedPipelineArgs,
     475             :                         bool forAutoComplete);
     476             : 
     477             :     bool SaveGDALGFile(const std::string &outFilename,
     478             :                        std::string &outString) const;
     479             : 
     480             :     virtual std::unique_ptr<GDALAbstractPipelineAlgorithm>
     481             :     CreateNestedPipeline() const = 0;
     482             : };
     483             : 
     484             : //! @endcond
     485             : 
     486             : #endif

Generated by: LCOV version 1.14