LCOV - code coverage report
Current view: top level - apps - gdalalg_abstract_pipeline.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 101 101 100.0 %
Date: 2025-12-17 16:19:51 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          95 :     GDALArgDatasetValue &GetOutputDataset()
      76             :     {
      77          95 :         return m_outputDataset;
      78             :     }
      79             : 
      80             :     const GDALArgDatasetValue &GetOutputDataset() const
      81             :     {
      82             :         return m_outputDataset;
      83             :     }
      84             : 
      85         434 :     const std::string &GetOutputString() const
      86             :     {
      87         434 :         return m_output;
      88             :     }
      89             : 
      90          19 :     const std::string &GetOutputLayerName() const
      91             :     {
      92          19 :         return m_outputLayerName;
      93             :     }
      94             : 
      95         168 :     const std::string &GetOutputFormat() const
      96             :     {
      97         168 :         return m_format;
      98             :     }
      99             : 
     100          65 :     const std::vector<std::string> &GetCreationOptions() const
     101             :     {
     102          65 :         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 inputDatasetRequired = true;
     143             :         bool outputDatasetRequired = true;
     144             :         bool addInputLayerNameArgument = true;   // only for vector input
     145             :         bool addUpdateArgument = true;           // only for vector output
     146             :         bool addAppendLayerArgument = true;      // only for vector output
     147             :         bool addOverwriteLayerArgument = true;   // only for vector output
     148             :         bool addUpsertArgument = true;           // only for vector output
     149             :         bool addSkipErrorsArgument = true;       // only for vector output
     150             :         bool addOutputLayerNameArgument = true;  // only for vector output
     151             :         int inputDatasetMaxCount = 1;
     152             :         std::string inputDatasetHelpMsg{};
     153             :         std::string inputDatasetAlias{};
     154             :         std::string inputDatasetMetaVar = "INPUT";
     155             :         std::string outputDatasetHelpMsg{};
     156             :         std::string outputFormatCreateCapability = GDAL_DCAP_CREATECOPY;
     157             : 
     158        6747 :         inline ConstructorOptions &SetStandaloneStep(bool b)
     159             :         {
     160        6747 :             standaloneStep = b;
     161        6747 :             return *this;
     162             :         }
     163             : 
     164        3112 :         inline ConstructorOptions &SetAddDefaultArguments(bool b)
     165             :         {
     166        3112 :             addDefaultArguments = b;
     167        3112 :             return *this;
     168             :         }
     169             : 
     170         197 :         inline ConstructorOptions &SetAddInputLayerNameArgument(bool b)
     171             :         {
     172         197 :             addInputLayerNameArgument = b;
     173         197 :             return *this;
     174             :         }
     175             : 
     176          92 :         inline ConstructorOptions &SetInputDatasetRequired(bool b)
     177             :         {
     178          92 :             inputDatasetRequired = b;
     179          92 :             return *this;
     180             :         }
     181             : 
     182        1773 :         inline ConstructorOptions &SetInputDatasetMaxCount(int maxCount)
     183             :         {
     184        1773 :             inputDatasetMaxCount = maxCount;
     185        1773 :             return *this;
     186             :         }
     187             : 
     188         859 :         inline ConstructorOptions &SetInputDatasetHelpMsg(const std::string &s)
     189             :         {
     190         859 :             inputDatasetHelpMsg = s;
     191         859 :             return *this;
     192             :         }
     193             : 
     194         672 :         inline ConstructorOptions &SetInputDatasetAlias(const std::string &s)
     195             :         {
     196         672 :             inputDatasetAlias = s;
     197         672 :             return *this;
     198             :         }
     199             : 
     200         217 :         inline ConstructorOptions &SetInputDatasetMetaVar(const std::string &s)
     201             :         {
     202         217 :             inputDatasetMetaVar = s;
     203         217 :             return *this;
     204             :         }
     205             : 
     206          88 :         inline ConstructorOptions &SetOutputDatasetHelpMsg(const std::string &s)
     207             :         {
     208          88 :             outputDatasetHelpMsg = s;
     209          88 :             return *this;
     210             :         }
     211             : 
     212         884 :         inline ConstructorOptions &SetAutoOpenInputDatasets(bool b)
     213             :         {
     214         884 :             autoOpenInputDatasets = b;
     215         884 :             return *this;
     216             :         }
     217             : 
     218          54 :         inline ConstructorOptions &SetOutputDatasetRequired(bool b)
     219             :         {
     220          54 :             outputDatasetRequired = b;
     221          54 :             return *this;
     222             :         }
     223             : 
     224             :         inline ConstructorOptions &
     225         699 :         SetOutputFormatCreateCapability(const std::string &capability)
     226             :         {
     227         699 :             outputFormatCreateCapability = capability;
     228         699 :             return *this;
     229             :         }
     230             : 
     231          41 :         inline ConstructorOptions &SetAddAppendLayerArgument(bool b)
     232             :         {
     233          41 :             addAppendLayerArgument = b;
     234          41 :             return *this;
     235             :         }
     236             : 
     237          41 :         inline ConstructorOptions &SetAddOverwriteLayerArgument(bool b)
     238             :         {
     239          41 :             addOverwriteLayerArgument = b;
     240          41 :             return *this;
     241             :         }
     242             : 
     243          41 :         inline ConstructorOptions &SetAddUpdateArgument(bool b)
     244             :         {
     245          41 :             addUpdateArgument = b;
     246          41 :             return *this;
     247             :         }
     248             : 
     249         122 :         inline ConstructorOptions &SetAddUpsertArgument(bool b)
     250             :         {
     251         122 :             addUpsertArgument = b;
     252         122 :             return *this;
     253             :         }
     254             : 
     255         122 :         inline ConstructorOptions &SetAddSkipErrorsArgument(bool b)
     256             :         {
     257         122 :             addSkipErrorsArgument = b;
     258         122 :             return *this;
     259             :         }
     260             : 
     261         122 :         inline ConstructorOptions &SetAddOutputLayerNameArgument(bool b)
     262             :         {
     263         122 :             addOutputLayerNameArgument = b;
     264         122 :             return *this;
     265             :         }
     266             :     };
     267             : 
     268             :     GDALPipelineStepAlgorithm(const std::string &name,
     269             :                               const std::string &description,
     270             :                               const std::string &helpURL,
     271             :                               const ConstructorOptions &);
     272             : 
     273             :     friend class GDALPipelineAlgorithm;
     274             :     friend class GDALRasterPipelineAlgorithm;
     275             :     friend class GDALVectorPipelineAlgorithm;
     276             :     friend class GDALAbstractPipelineAlgorithm;
     277             : 
     278        1715 :     virtual bool CanBeFirstStep() const
     279             :     {
     280        1715 :         return false;
     281             :     }
     282             : 
     283         439 :     virtual bool CanBeMiddleStep() const
     284             :     {
     285         439 :         return !CanBeFirstStep() && !CanBeLastStep();
     286             :     }
     287             : 
     288         659 :     virtual bool CanBeLastStep() const
     289             :     {
     290         659 :         return false;
     291             :     }
     292             : 
     293             :     //! Whether a user parameter can cause a file to be written at a specified location
     294          49 :     virtual bool GeneratesFilesFromUserInput() const
     295             :     {
     296          49 :         return false;
     297             :     }
     298             : 
     299         480 :     virtual bool IsNativelyStreamingCompatible() const
     300             :     {
     301         480 :         return true;
     302             :     }
     303             : 
     304         171 :     virtual bool SupportsInputMultiThreading() const
     305             :     {
     306         171 :         return false;
     307             :     }
     308             : 
     309        1023 :     virtual bool CanHandleNextStep(GDALPipelineStepAlgorithm *) const
     310             :     {
     311        1023 :         return false;
     312             :     }
     313             : 
     314           2 :     virtual bool OutputDatasetAllowedBeforeRunningStep() const
     315             :     {
     316           2 :         return false;
     317             :     }
     318             : 
     319         175 :     virtual CPLJSONObject Get_OGR_SCHEMA_OpenOption_Layer() const
     320             :     {
     321         175 :         CPLJSONObject obj;
     322         175 :         obj.Deinit();
     323         175 :         return obj;
     324             :     }
     325             : 
     326             :     virtual bool RunStep(GDALPipelineStepRunContext &ctxt) = 0;
     327             : 
     328             :     bool m_standaloneStep = false;
     329             :     const ConstructorOptions m_constructorOptions;
     330             :     bool m_outputVRTCompatible = true;
     331             :     std::string m_helpDocCategory{};
     332             : 
     333             :     // Input arguments
     334             :     std::vector<GDALArgDatasetValue> m_inputDataset{};
     335             :     std::vector<std::string> m_openOptions{};
     336             :     std::vector<std::string> m_inputFormats{};
     337             :     std::vector<std::string> m_inputLayerNames{};
     338             : 
     339             :     // Output arguments
     340             :     bool m_stdout = false;
     341             :     std::string m_output{};
     342             :     GDALArgDatasetValue m_outputDataset{};
     343             :     std::string m_format{};
     344             :     std::vector<std::string> m_outputOpenOptions{};
     345             :     std::vector<std::string> m_creationOptions{};
     346             :     bool m_overwrite = false;
     347             :     std::string m_outputLayerName{};
     348             :     GDALInConstructionAlgorithmArg *m_outputFormatArg = nullptr;
     349             :     bool m_appendRaster = false;
     350             : 
     351             :     // Output arguments (vector specific)
     352             :     std::vector<std::string> m_layerCreationOptions{};
     353             :     bool m_update = false;
     354             :     bool m_overwriteLayer = false;
     355             :     bool m_appendLayer = false;
     356             :     bool m_upsert = false;
     357             :     bool m_skipErrors = false;
     358             : 
     359             :     void AddRasterInputArgs(bool openForMixedRasterVector, bool hiddenForCLI);
     360             :     void AddRasterOutputArgs(bool hiddenForCLI);
     361             :     void AddRasterHiddenInputDatasetArg();
     362             : 
     363             :     void AddVectorInputArgs(bool hiddenForCLI);
     364             :     void AddVectorOutputArgs(bool hiddenForCLI,
     365             :                              bool shortNameOutputLayerAllowed);
     366             : 
     367             :   private:
     368             :     bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) override;
     369             :     GDALAlgorithm::ProcessGDALGOutputRet ProcessGDALGOutput() override;
     370             :     bool CheckSafeForStreamOutput() override;
     371             : 
     372             :     CPL_DISALLOW_COPY_ASSIGN(GDALPipelineStepAlgorithm)
     373             : };
     374             : 
     375             : /************************************************************************/
     376             : /*                    GDALAbstractPipelineAlgorithm                     */
     377             : /************************************************************************/
     378             : 
     379             : class GDALAbstractPipelineAlgorithm CPL_NON_FINAL
     380             :     : public GDALPipelineStepAlgorithm
     381             : {
     382             :   public:
     383             :     std::vector<std::string> GetAutoComplete(std::vector<std::string> &args,
     384             :                                              bool lastWordIsComplete,
     385             :                                              bool /* showAllOptions*/) override;
     386             : 
     387             :     bool Finalize() override;
     388             : 
     389             :     std::string GetUsageAsJSON() const override;
     390             : 
     391             :     bool
     392             :     ParseCommandLineArguments(const std::vector<std::string> &args) override;
     393             : 
     394           2 :     bool HasSteps() const
     395             :     {
     396           2 :         return !m_steps.empty();
     397             :     }
     398             : 
     399             :     static constexpr const char *OPEN_NESTED_PIPELINE = "[";
     400             :     static constexpr const char *CLOSE_NESTED_PIPELINE = "]";
     401             : 
     402             :     static constexpr const char *RASTER_SUFFIX = "-raster";
     403             :     static constexpr const char *VECTOR_SUFFIX = "-vector";
     404             : 
     405             :   protected:
     406             :     friend class GDALTeeStepAlgorithmAbstract;
     407             : 
     408         449 :     GDALAbstractPipelineAlgorithm(
     409             :         const std::string &name, const std::string &description,
     410             :         const std::string &helpURL,
     411             :         const GDALPipelineStepAlgorithm::ConstructorOptions &options)
     412         449 :         : GDALPipelineStepAlgorithm(
     413             :               name, description, helpURL,
     414         449 :               ConstructorOptions(options).SetAutoOpenInputDatasets(false))
     415             :     {
     416         449 :     }
     417             : 
     418             :     std::string m_pipeline{};
     419             : 
     420             :     virtual GDALAlgorithmRegistry &GetStepRegistry() = 0;
     421             : 
     422             :     virtual const GDALAlgorithmRegistry &GetStepRegistry() const = 0;
     423             : 
     424             :     std::unique_ptr<GDALPipelineStepAlgorithm>
     425             :     GetStepAlg(const std::string &name) const;
     426             : 
     427             :     bool HasOutputString() const override;
     428             : 
     429             :     static bool IsReadSpecificArgument(const char *pszArgName);
     430             :     static bool IsWriteSpecificArgument(const char *pszArgName);
     431             : 
     432             :   private:
     433             :     friend class GDALPipelineAlgorithm;
     434             :     friend class GDALRasterPipelineAlgorithm;
     435             :     friend class GDALVectorPipelineAlgorithm;
     436             : 
     437             :     std::vector<std::unique_ptr<GDALPipelineStepAlgorithm>> m_steps{};
     438             : 
     439             :     std::unique_ptr<GDALPipelineStepAlgorithm> m_stepOnWhichHelpIsRequested{};
     440             : 
     441             :     bool m_bInnerPipeline = false;
     442             :     bool m_bExpectReadStep = true;
     443             : 
     444             :     enum class StepConstraint
     445             :     {
     446             :         MUST_BE,
     447             :         CAN_BE,
     448             :         CAN_NOT_BE
     449             :     };
     450             : 
     451             :     StepConstraint m_eLastStepAsWrite = StepConstraint::CAN_BE;
     452             : 
     453             :     std::vector<std::unique_ptr<GDALAbstractPipelineAlgorithm>>
     454             :         m_apoNestedPipelines{};
     455             : 
     456             :     // More would lead to unreadable pipelines
     457             :     static constexpr int MAX_NESTING_LEVEL = 3;
     458             : 
     459             :     bool
     460             :     CheckFirstAndLastStep(const std::vector<GDALPipelineStepAlgorithm *> &steps,
     461             :                           bool forAutoComplete) const;
     462             : 
     463             :     bool ParseCommandLineArguments(const std::vector<std::string> &args,
     464             :                                    bool forAutoComplete);
     465             : 
     466             :     bool RunStep(GDALPipelineStepRunContext &ctxt) override;
     467             : 
     468             :     std::string
     469             :     BuildNestedPipeline(GDALPipelineStepAlgorithm *curAlg,
     470             :                         std::vector<std::string> &nestedPipelineArgs,
     471             :                         bool forAutoComplete);
     472             : 
     473             :     bool SaveGDALGFile(const std::string &outFilename,
     474             :                        std::string &outString) const;
     475             : 
     476             :     virtual std::unique_ptr<GDALAbstractPipelineAlgorithm>
     477             :     CreateNestedPipeline() const = 0;
     478             : };
     479             : 
     480             : //! @endcond
     481             : 
     482             : #endif

Generated by: LCOV version 1.14