LCOV - code coverage report
Current view: top level - apps - gdalalg_pipeline.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 383 386 99.2 %
Date: 2026-05-29 08:57:34 Functions: 16 16 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  gdal "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             : //! @cond Doxygen_Suppress
      14             : 
      15             : #include "gdalalg_pipeline.h"
      16             : #include "cpl_error.h"
      17             : #include "gdal_priv.h"
      18             : 
      19             : #include "gdalalg_external.h"
      20             : 
      21             : #include "gdalalg_raster_read.h"
      22             : #include "gdalalg_raster_mosaic.h"
      23             : #include "gdalalg_raster_stack.h"
      24             : #include "gdalalg_raster_write.h"
      25             : #include "gdalalg_raster_zonal_stats.h"
      26             : 
      27             : #include "gdalalg_vector_read.h"
      28             : #include "gdalalg_vector_write.h"
      29             : 
      30             : #include "gdalalg_raster_as_features.h"
      31             : #include "gdalalg_raster_compare.h"
      32             : #include "gdalalg_raster_contour.h"
      33             : #include "gdalalg_raster_footprint.h"
      34             : #include "gdalalg_raster_polygonize.h"
      35             : #include "gdalalg_raster_info.h"
      36             : #include "gdalalg_raster_pixel_info.h"
      37             : #include "gdalalg_raster_tile.h"
      38             : #include "gdalalg_vector_grid.h"
      39             : #include "gdalalg_vector_info.h"
      40             : #include "gdalalg_vector_rasterize.h"
      41             : 
      42             : #include <algorithm>
      43             : #include <cassert>
      44             : 
      45             : #ifndef _
      46             : #define _(x) (x)
      47             : #endif
      48             : 
      49             : /************************************************************************/
      50             : /*                     GDALPipelineStepAlgorithm()                      */
      51             : /************************************************************************/
      52             : 
      53       13789 : GDALPipelineStepAlgorithm::GDALPipelineStepAlgorithm(
      54             :     const std::string &name, const std::string &description,
      55       13789 :     const std::string &helpURL, const ConstructorOptions &options)
      56             :     : GDALAlgorithm(name, description, helpURL),
      57       13789 :       m_standaloneStep(options.standaloneStep), m_constructorOptions(options)
      58             : {
      59       13789 : }
      60             : 
      61             : /************************************************************************/
      62             : /*     GDALPipelineStepAlgorithm::AddRasterHiddenInputDatasetArg()      */
      63             : /************************************************************************/
      64             : 
      65        2141 : void GDALPipelineStepAlgorithm::AddRasterHiddenInputDatasetArg()
      66             : {
      67        2141 :     AddInputDatasetArg(&m_inputDataset, GDAL_OF_RASTER, false)
      68        2141 :         .SetMinCount(0)
      69        2141 :         .SetMaxCount(m_constructorOptions.inputDatasetMaxCount)
      70        2141 :         .SetAutoOpenDataset(m_constructorOptions.autoOpenInputDatasets)
      71        2141 :         .SetMetaVar(m_constructorOptions.inputDatasetMetaVar)
      72        2141 :         .SetHidden();
      73        2141 : }
      74             : 
      75             : /************************************************************************/
      76             : /*           GDALPipelineStepAlgorithm::AddRasterInputArgs()            */
      77             : /************************************************************************/
      78             : 
      79        4708 : void GDALPipelineStepAlgorithm::AddRasterInputArgs(
      80             :     bool openForMixedRasterVector, bool hiddenForCLI)
      81             : {
      82        4708 :     AddInputFormatsArg(&m_inputFormats)
      83             :         .AddMetadataItem(
      84             :             GAAMDI_REQUIRED_CAPABILITIES,
      85             :             openForMixedRasterVector
      86       14454 :                 ? std::vector<std::string>{GDAL_DCAP_RASTER, GDAL_DCAP_VECTOR}
      87       13794 :                 : std::vector<std::string>{GDAL_DCAP_RASTER})
      88        4708 :         .SetHiddenForCLI(hiddenForCLI)
      89        4708 :         .SetAvailableInPipelineStep(false);
      90        4708 :     AddOpenOptionsArg(&m_openOptions)
      91        4708 :         .SetHiddenForCLI(hiddenForCLI)
      92        4708 :         .SetAvailableInPipelineStep(false);
      93             :     auto &arg =
      94             :         AddInputDatasetArg(
      95             :             &m_inputDataset,
      96             :             openForMixedRasterVector ? (GDAL_OF_RASTER | GDAL_OF_VECTOR)
      97             :                                      : GDAL_OF_RASTER,
      98        4708 :             false, m_constructorOptions.inputDatasetHelpMsg.c_str())
      99        4708 :             .SetDatasetInputFlags(m_constructorOptions.inputDatasetInputFlags)
     100        4708 :             .SetMinCount(m_constructorOptions.inputDatasetRequired ? 1 : 0)
     101        4708 :             .SetMaxCount(m_constructorOptions.inputDatasetMaxCount)
     102        4708 :             .SetAutoOpenDataset(m_constructorOptions.autoOpenInputDatasets)
     103        4708 :             .SetMetaVar(m_constructorOptions.inputDatasetMetaVar)
     104        4708 :             .SetHiddenForCLI(hiddenForCLI)
     105        4708 :             .SetAvailableInPipelineStep(false);
     106        4708 :     if (m_constructorOptions.inputDatasetPositional && !hiddenForCLI)
     107        4377 :         arg.SetPositional();
     108        4708 :     if (m_constructorOptions.inputDatasetRequired && !hiddenForCLI)
     109        4377 :         arg.SetRequired();
     110        4708 :     if (!m_constructorOptions.inputDatasetAlias.empty())
     111         841 :         arg.AddAlias(m_constructorOptions.inputDatasetAlias);
     112        4708 : }
     113             : 
     114             : /************************************************************************/
     115             : /*           GDALPipelineStepAlgorithm::AddRasterOutputArgs()           */
     116             : /************************************************************************/
     117             : 
     118        3060 : void GDALPipelineStepAlgorithm::AddRasterOutputArgs(bool hiddenForCLI)
     119             : {
     120        3060 :     m_outputFormatArg =
     121             :         &(AddOutputFormatArg(&m_format, /* bStreamAllowed = */ true,
     122        3060 :                              /* bGDALGAllowed = */ true)
     123             :               .AddMetadataItem(
     124             :                   GAAMDI_REQUIRED_CAPABILITIES,
     125             :                   {GDAL_DCAP_RASTER,
     126       12240 :                    m_constructorOptions.outputFormatCreateCapability.c_str()})
     127        3060 :               .SetHiddenForCLI(hiddenForCLI))
     128        3060 :              .SetAvailableInPipelineStep(false);
     129             :     AddOutputDatasetArg(&m_outputDataset, GDAL_OF_RASTER,
     130        3060 :                         /* positionalAndRequired = */ !hiddenForCLI,
     131        3060 :                         m_constructorOptions.outputDatasetHelpMsg.c_str())
     132        3060 :         .SetHiddenForCLI(hiddenForCLI)
     133        3060 :         .SetDatasetInputFlags(GADV_NAME | GADV_OBJECT)
     134        3060 :         .SetAvailableInPipelineStep(false);
     135        3060 :     AddCreationOptionsArg(&m_creationOptions)
     136        3060 :         .SetHiddenForCLI(hiddenForCLI)
     137        3060 :         .SetAvailableInPipelineStep(false);
     138        3060 :     constexpr const char *MUTUAL_EXCLUSION_GROUP_OVERWRITE_APPEND =
     139             :         "overwrite-append";
     140        3060 :     AddOverwriteArg(&m_overwrite)
     141        3060 :         .SetHiddenForCLI(hiddenForCLI)
     142        6120 :         .SetMutualExclusionGroup(MUTUAL_EXCLUSION_GROUP_OVERWRITE_APPEND)
     143        3060 :         .SetAvailableInPipelineStep(false);
     144             :     AddArg(GDAL_ARG_NAME_APPEND, 0,
     145        6120 :            _("Append as a subdataset to existing output"), &m_appendRaster)
     146        3060 :         .SetDefault(false)
     147        3060 :         .SetHiddenForCLI(hiddenForCLI)
     148        6120 :         .SetMutualExclusionGroup(MUTUAL_EXCLUSION_GROUP_OVERWRITE_APPEND)
     149        3060 :         .SetAvailableInPipelineStep(false);
     150        3060 : }
     151             : 
     152             : /************************************************************************/
     153             : /*     GDALPipelineStepAlgorithm::AddVectorHiddenInputDatasetArg()      */
     154             : /************************************************************************/
     155             : 
     156        2003 : void GDALPipelineStepAlgorithm::AddVectorHiddenInputDatasetArg()
     157             : {
     158        2003 :     AddInputDatasetArg(&m_inputDataset, GDAL_OF_VECTOR, false)
     159        2003 :         .SetMinCount(0)
     160        2003 :         .SetMaxCount(m_constructorOptions.inputDatasetMaxCount)
     161        2003 :         .SetAutoOpenDataset(m_constructorOptions.autoOpenInputDatasets)
     162        2003 :         .SetMetaVar(m_constructorOptions.inputDatasetMetaVar)
     163        2003 :         .SetHidden();
     164        2003 : }
     165             : 
     166             : /************************************************************************/
     167             : /*           GDALPipelineStepAlgorithm::AddVectorInputArgs()            */
     168             : /************************************************************************/
     169             : 
     170        2738 : void GDALPipelineStepAlgorithm::AddVectorInputArgs(bool hiddenForCLI)
     171             : {
     172        2738 :     AddInputFormatsArg(&m_inputFormats)
     173        8214 :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR})
     174        2738 :         .SetHiddenForCLI(hiddenForCLI)
     175        2738 :         .SetAvailableInPipelineStep(false);
     176        2738 :     AddOpenOptionsArg(&m_openOptions)
     177        2738 :         .SetHiddenForCLI(hiddenForCLI)
     178        2738 :         .SetAvailableInPipelineStep(false);
     179             :     auto &datasetArg =
     180        2738 :         AddInputDatasetArg(&m_inputDataset, GDAL_OF_VECTOR, false)
     181        2738 :             .SetMinCount(m_constructorOptions.inputDatasetRequired ? 1 : 0)
     182        2738 :             .SetMaxCount(m_constructorOptions.inputDatasetMaxCount)
     183        2738 :             .SetDatasetInputFlags(m_constructorOptions.inputDatasetInputFlags)
     184        2738 :             .SetAutoOpenDataset(m_constructorOptions.autoOpenInputDatasets)
     185        2738 :             .SetHiddenForCLI(hiddenForCLI)
     186        2738 :             .SetAvailableInPipelineStep(false);
     187        2738 :     if (!m_constructorOptions.inputDatasetAlias.empty())
     188         190 :         datasetArg.AddAlias(m_constructorOptions.inputDatasetAlias);
     189        2738 :     if (!m_constructorOptions.inputDatasetMetaVar.empty())
     190        2738 :         datasetArg.SetMetaVar(m_constructorOptions.inputDatasetMetaVar);
     191        2738 :     if (m_constructorOptions.inputDatasetPositional && !hiddenForCLI)
     192        2439 :         datasetArg.SetPositional();
     193        2738 :     if (m_constructorOptions.inputDatasetRequired && !hiddenForCLI)
     194        2439 :         datasetArg.SetRequired();
     195        2738 :     if (m_constructorOptions.addInputLayerNameArgument)
     196             :     {
     197             :         auto &layerArg = AddArg(GDAL_ARG_NAME_INPUT_LAYER, 'l',
     198        4936 :                                 _("Input layer name(s)"), &m_inputLayerNames)
     199        4936 :                              .AddAlias("layer")
     200        2468 :                              .SetHiddenForCLI(hiddenForCLI)
     201        2468 :                              .SetAvailableInPipelineStep(false);
     202        2468 :         SetAutoCompleteFunctionForLayerName(layerArg, datasetArg);
     203             :     }
     204        2738 : }
     205             : 
     206             : /************************************************************************/
     207             : /*           GDALPipelineStepAlgorithm::AddVectorOutputArgs()           */
     208             : /************************************************************************/
     209             : 
     210        3154 : void GDALPipelineStepAlgorithm::AddVectorOutputArgs(
     211             :     bool hiddenForCLI, bool shortNameOutputLayerAllowed)
     212             : {
     213             :     AddOutputFormatArg(&m_format, /* bStreamAllowed = */ true,
     214        3154 :                        /* bGDALGAllowed = */ true)
     215             :         .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES,
     216       12616 :                          {GDAL_DCAP_VECTOR, GDAL_DCAP_CREATE})
     217        3154 :         .SetHiddenForCLI(hiddenForCLI)
     218        3154 :         .SetAvailableInPipelineStep(false);
     219        3154 :     AddOutputOpenOptionsArg(&m_outputOpenOptions)
     220        3154 :         .SetHiddenForCLI(hiddenForCLI)
     221        3154 :         .SetAvailableInPipelineStep(false);
     222             :     auto &outputDatasetArg =
     223             :         AddOutputDatasetArg(&m_outputDataset, GDAL_OF_VECTOR,
     224        3154 :                             /* positionalAndRequired = */ false)
     225        3154 :             .SetHiddenForCLI(hiddenForCLI)
     226        3154 :             .SetDatasetInputFlags(GADV_NAME | GADV_OBJECT)
     227        3154 :             .SetAvailableInPipelineStep(false);
     228        3154 :     if (!hiddenForCLI)
     229        2986 :         outputDatasetArg.SetPositional();
     230        3154 :     if (!hiddenForCLI && m_constructorOptions.outputDatasetRequired)
     231        2927 :         outputDatasetArg.SetRequired();
     232             : 
     233        3154 :     AddCreationOptionsArg(&m_creationOptions)
     234        3154 :         .SetHiddenForCLI(hiddenForCLI)
     235        3154 :         .SetAvailableInPipelineStep(false);
     236        3154 :     AddLayerCreationOptionsArg(&m_layerCreationOptions)
     237        3154 :         .SetHiddenForCLI(hiddenForCLI)
     238        3154 :         .SetAvailableInPipelineStep(false);
     239        3154 :     AddOverwriteArg(&m_overwrite)
     240        3154 :         .SetHiddenForCLI(hiddenForCLI)
     241        3154 :         .SetAvailableInPipelineStep(false);
     242        3154 :     GDALInConstructionAlgorithmArg *updateArg = nullptr;
     243        3154 :     if (m_constructorOptions.addUpdateArgument)
     244             :     {
     245        3092 :         updateArg = &AddUpdateArg(&m_update)
     246        3092 :                          .SetHiddenForCLI(hiddenForCLI)
     247        3092 :                          .SetAvailableInPipelineStep(false);
     248             :     }
     249        3154 :     if (m_constructorOptions.addOverwriteLayerArgument)
     250             :     {
     251        3092 :         AddOverwriteLayerArg(&m_overwriteLayer)
     252        3092 :             .SetHiddenForCLI(hiddenForCLI)
     253        3092 :             .SetAvailableInPipelineStep(false);
     254             :     }
     255        3154 :     constexpr const char *MUTUAL_EXCLUSION_GROUP_APPEND_UPSERT =
     256             :         "append-upsert";
     257        3154 :     if (m_constructorOptions.addAppendLayerArgument)
     258             :     {
     259        2961 :         AddAppendLayerArg(&m_appendLayer)
     260        2961 :             .SetHiddenForCLI(hiddenForCLI)
     261        5922 :             .SetMutualExclusionGroup(MUTUAL_EXCLUSION_GROUP_APPEND_UPSERT)
     262        2961 :             .SetAvailableInPipelineStep(false);
     263             :     }
     264        3154 :     if (m_constructorOptions.addUpsertArgument)
     265             :     {
     266        5664 :         AddArg("upsert", 0, _("Upsert features (implies 'append')"), &m_upsert)
     267        2832 :             .SetHiddenForCLI(hiddenForCLI)
     268        5664 :             .SetMutualExclusionGroup(MUTUAL_EXCLUSION_GROUP_APPEND_UPSERT)
     269             : 
     270        2832 :             .SetAvailableInPipelineStep(false)
     271             :             .AddAction(
     272          12 :                 [updateArg, this]()
     273             :                 {
     274           4 :                     if (m_upsert && updateArg)
     275           4 :                         updateArg->Set(true);
     276        5664 :                 })
     277        2832 :             .SetCategory(GAAC_ADVANCED);
     278             :     }
     279        3154 :     if (m_constructorOptions.addOutputLayerNameArgument)
     280             :     {
     281        3026 :         AddOutputLayerNameArg(hiddenForCLI, shortNameOutputLayerAllowed);
     282             :     }
     283        3154 :     if (m_constructorOptions.addSkipErrorsArgument)
     284             :     {
     285             :         AddArg("skip-errors", 0, _("Skip errors when writing features"),
     286        5664 :                &m_skipErrors)
     287        5664 :             .AddHiddenAlias("skip-failures")  // For ogr2ogr nostalgic people
     288        2832 :             .SetAvailableInPipelineStep(false);
     289             :     }
     290        3154 :     if (m_constructorOptions.addNoCreateEmptyLayersArgument)
     291             :     {
     292             :         AddArg("no-create-empty-layers", 0,
     293             :                _("Avoid creating layers to which no features will be written"),
     294        2150 :                &m_noCreateEmptyLayers)
     295        1075 :             .SetAvailableInPipelineStep(false);
     296             :     }
     297        3154 : }
     298             : 
     299             : /************************************************************************/
     300             : /*          GDALPipelineStepAlgorithm::AddOutputLayerNameArg()          */
     301             : /************************************************************************/
     302             : 
     303        3221 : void GDALPipelineStepAlgorithm::AddOutputLayerNameArg(
     304             :     bool hiddenForCLI, bool shortNameOutputLayerAllowed)
     305             : {
     306             :     AddArg(GDAL_ARG_NAME_OUTPUT_LAYER, shortNameOutputLayerAllowed ? 'l' : 0,
     307             :            _("Output layer name"),
     308        6442 :            &m_outputLayerName)
     309        6442 :         .AddHiddenAlias("nln")  // For ogr2ogr nostalgic people
     310        3221 :         .SetHiddenForCLI(hiddenForCLI)
     311             :         .SetAvailableInPipelineStep(
     312        3221 :             m_constructorOptions.outputLayerNameAvailableInPipelineStep);
     313        3221 : }
     314             : 
     315             : /************************************************************************/
     316             : /*                 GDALPipelineStepAlgorithm::RunImpl()                 */
     317             : /************************************************************************/
     318             : 
     319        2772 : bool GDALPipelineStepAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
     320             :                                         void *pProgressData)
     321             : {
     322        2772 :     if (m_standaloneStep)
     323             :     {
     324        1149 :         std::unique_ptr<GDALPipelineStepAlgorithm> readAlg;
     325        1149 :         if (GetInputType() == GDAL_OF_RASTER)
     326         732 :             readAlg = std::make_unique<GDALRasterReadAlgorithm>();
     327             :         else
     328         417 :             readAlg = std::make_unique<GDALVectorReadAlgorithm>();
     329        9609 :         for (auto &arg : readAlg->GetArgs())
     330             :         {
     331        8460 :             auto stepArg = GetArg(arg->GetName());
     332        9626 :             if (stepArg && stepArg->IsExplicitlySet() &&
     333        1166 :                 stepArg->GetType() == arg->GetType())
     334             :             {
     335        1163 :                 arg->SetSkipIfAlreadySet(true);
     336        1163 :                 arg->SetFrom(*stepArg);
     337             :             }
     338             :         }
     339             : 
     340           0 :         std::unique_ptr<GDALPipelineStepAlgorithm> writeAlg;
     341        1149 :         if (GetOutputType() == GDAL_OF_RASTER)
     342             :         {
     343         516 :             if (GetName() == GDALRasterInfoAlgorithm::NAME)
     344          25 :                 writeAlg = std::make_unique<GDALRasterInfoAlgorithm>();
     345         491 :             else if (GetName() == GDALRasterCompareAlgorithm::NAME)
     346           0 :                 writeAlg = std::make_unique<GDALRasterCompareAlgorithm>();
     347             :             else
     348         491 :                 writeAlg = std::make_unique<GDALRasterWriteAlgorithm>();
     349             :         }
     350             :         else
     351             :         {
     352         633 :             if (GetName() == GDALVectorInfoAlgorithm::NAME)
     353          27 :                 writeAlg = std::make_unique<GDALVectorInfoAlgorithm>();
     354             :             else
     355         606 :                 writeAlg = std::make_unique<GDALVectorWriteAlgorithm>();
     356             :         }
     357       18661 :         for (auto &arg : writeAlg->GetArgs())
     358             :         {
     359       17512 :             auto stepArg = GetArg(arg->GetName());
     360       20420 :             if (stepArg && stepArg->IsExplicitlySet() &&
     361        2908 :                 stepArg->GetType() == arg->GetType())
     362             :             {
     363        2902 :                 arg->SetSkipIfAlreadySet(true);
     364        2902 :                 arg->SetFrom(*stepArg);
     365             :             }
     366             :         }
     367             : 
     368        1149 :         const bool bIsStreaming = m_format == "stream";
     369             : 
     370             :         // Already checked by GDALAlgorithm::Run()
     371        1149 :         CPLAssert(!m_executionForStreamOutput || bIsStreaming);
     372             : 
     373        1149 :         bool ret = false;
     374        1205 :         if (!m_outputVRTCompatible &&
     375         112 :             (EQUAL(m_format.c_str(), "VRT") ||
     376          56 :              (m_format.empty() &&
     377        1150 :               EQUAL(CPLGetExtensionSafe(m_outputDataset.GetName().c_str())
     378             :                         .c_str(),
     379             :                     "VRT"))))
     380             :         {
     381           0 :             ReportError(CE_Failure, CPLE_NotSupported,
     382             :                         "VRT output is not supported. Consider using the "
     383             :                         "GDALG driver instead (files with .gdalg.json "
     384             :                         "extension)");
     385             :         }
     386        1149 :         else if (readAlg->Run())
     387             :         {
     388        1145 :             auto outputArg = GetArg(GDAL_ARG_NAME_OUTPUT);
     389             :             const bool bOutputSpecified =
     390        2035 :                 outputArg && outputArg->IsExplicitlySet() &&
     391         890 :                 (outputArg->GetType() == GAAT_DATASET ||
     392           3 :                  outputArg->GetType() == GAAT_DATASET_LIST);
     393             : 
     394        1145 :             m_inputDataset.clear();
     395        1145 :             m_inputDataset.resize(1);
     396        1145 :             m_inputDataset[0].Set(readAlg->m_outputDataset.GetDatasetRef());
     397        1145 :             if (bOutputSpecified)
     398         884 :                 m_outputDataset.Set(nullptr);
     399             : 
     400             :             std::unique_ptr<void, decltype(&GDALDestroyScaledProgress)>
     401        2290 :                 pScaledData(nullptr, GDALDestroyScaledProgress);
     402             : 
     403             :             const bool bCanHandleNextStep =
     404        1145 :                 !bIsStreaming && CanHandleNextStep(writeAlg.get());
     405             : 
     406        1145 :             GDALPipelineStepRunContext stepCtxt;
     407        1145 :             if (pfnProgress && GetName() == GDALRasterCompareAlgorithm::NAME)
     408             :             {
     409           5 :                 stepCtxt.m_pfnProgress = pfnProgress;
     410           5 :                 stepCtxt.m_pProgressData = pProgressData;
     411             :             }
     412        1190 :             else if (pfnProgress &&
     413          50 :                      (bCanHandleNextStep || !IsNativelyStreamingCompatible()))
     414             :             {
     415          52 :                 pScaledData.reset(GDALCreateScaledProgress(
     416          22 :                     0.0, bIsStreaming || bCanHandleNextStep ? 1.0 : 0.5,
     417             :                     pfnProgress, pProgressData));
     418          30 :                 stepCtxt.m_pfnProgress =
     419          30 :                     pScaledData ? GDALScaledProgress : nullptr;
     420          30 :                 stepCtxt.m_pProgressData = pScaledData.get();
     421             :             }
     422             : 
     423        1145 :             if (bCanHandleNextStep)
     424          52 :                 stepCtxt.m_poNextUsableStep = writeAlg.get();
     425        1145 :             if (RunPreStepPipelineValidations() && RunStep(stepCtxt))
     426             :             {
     427        1060 :                 if (bCanHandleNextStep || !bOutputSpecified)
     428             :                 {
     429         301 :                     ret = true;
     430             :                 }
     431             :                 else
     432             :                 {
     433         759 :                     writeAlg->m_outputVRTCompatible = m_outputVRTCompatible;
     434         759 :                     writeAlg->m_quiet = m_quiet;
     435             : 
     436        1518 :                     std::vector<GDALArgDatasetValue> inputDataset(1);
     437         759 :                     inputDataset[0].Set(m_outputDataset.GetDatasetRef());
     438         759 :                     auto inputArg = writeAlg->GetArg(GDAL_ARG_NAME_INPUT);
     439         759 :                     CPLAssert(inputArg);
     440         759 :                     inputArg->Set(std::move(inputDataset));
     441             : 
     442         759 :                     if (pfnProgress)
     443             :                     {
     444          41 :                         pScaledData.reset(GDALCreateScaledProgress(
     445          41 :                             IsNativelyStreamingCompatible() ? 0.0 : 0.5, 1.0,
     446             :                             pfnProgress, pProgressData));
     447             :                     }
     448         759 :                     stepCtxt.m_pfnProgress =
     449         759 :                         pScaledData ? GDALScaledProgress : nullptr;
     450         759 :                     stepCtxt.m_pProgressData = pScaledData.get();
     451        1518 :                     if (writeAlg->ValidateArguments() &&
     452         759 :                         writeAlg->RunStep(stepCtxt))
     453             :                     {
     454         741 :                         if (pfnProgress)
     455          40 :                             pfnProgress(1.0, "", pProgressData);
     456             : 
     457         741 :                         m_outputDataset.Set(
     458         741 :                             writeAlg->m_outputDataset.GetDatasetRef());
     459         741 :                         ret = true;
     460             :                     }
     461             :                 }
     462             :             }
     463             :         }
     464             : 
     465        1149 :         return ret;
     466             :     }
     467             :     else
     468             :     {
     469        1623 :         GDALPipelineStepRunContext stepCtxt;
     470        1623 :         stepCtxt.m_pfnProgress = pfnProgress;
     471        1623 :         stepCtxt.m_pProgressData = pProgressData;
     472        1623 :         return RunPreStepPipelineValidations() && RunStep(stepCtxt);
     473             :     }
     474             : }
     475             : 
     476             : /************************************************************************/
     477             : /*                          SetInputDataset()                           */
     478             : /************************************************************************/
     479             : 
     480          27 : void GDALPipelineStepAlgorithm::SetInputDataset(GDALDataset *poDS)
     481             : {
     482          27 :     auto arg = GetArg(GDAL_ARG_NAME_INPUT);
     483          27 :     if (arg)
     484             :     {
     485          27 :         auto &val = arg->Get<std::vector<GDALArgDatasetValue>>();
     486          27 :         val.resize(1);
     487          27 :         val[0].Set(poDS);
     488          27 :         arg->NotifyValueSet();
     489          27 :         arg->SetSkipIfAlreadySet();
     490             :     }
     491          27 : }
     492             : 
     493             : /************************************************************************/
     494             : /*                         ProcessGDALGOutput()                         */
     495             : /************************************************************************/
     496             : 
     497             : GDALAlgorithm::ProcessGDALGOutputRet
     498        3801 : GDALPipelineStepAlgorithm::ProcessGDALGOutput()
     499             : {
     500        3801 :     if (m_standaloneStep)
     501             :     {
     502        2096 :         return GDALAlgorithm::ProcessGDALGOutput();
     503             :     }
     504             :     else
     505             :     {
     506             :         // GDALAbstractPipelineAlgorithm<StepAlgorithm>::RunStep() might
     507             :         // actually detect a GDALG output request and process it.
     508        1705 :         return GDALAlgorithm::ProcessGDALGOutputRet::NOT_GDALG;
     509             :     }
     510             : }
     511             : 
     512             : /************************************************************************/
     513             : /*        GDALPipelineStepAlgorithm::CheckSafeForStreamOutput()         */
     514             : /************************************************************************/
     515             : 
     516          93 : bool GDALPipelineStepAlgorithm::CheckSafeForStreamOutput()
     517             : {
     518          93 :     if (m_standaloneStep)
     519             :     {
     520          48 :         return GDALAlgorithm::CheckSafeForStreamOutput();
     521             :     }
     522             :     else
     523             :     {
     524             :         // The check is actually done in
     525             :         // GDALAbstractPipelineAlgorithm<StepAlgorithm>::RunStep()
     526             :         // so return true for now.
     527          45 :         return true;
     528             :     }
     529             : }
     530             : 
     531             : /************************************************************************/
     532             : /*                GDALPipelineStepAlgorithm::Finalize()                 */
     533             : /************************************************************************/
     534             : 
     535        1423 : bool GDALPipelineStepAlgorithm::Finalize()
     536             : {
     537        1423 :     bool ret = GDALAlgorithm::Finalize();
     538        2655 :     for (auto &argValue : m_inputDataset)
     539        1232 :         ret = argValue.Close() && ret;
     540        1423 :     ret = m_outputDataset.Close() && ret;
     541        1423 :     return ret;
     542             : }
     543             : 
     544             : GDALAlgorithmStepRegistry::~GDALAlgorithmStepRegistry() = default;
     545             : 
     546             : /************************************************************************/
     547             : /*                        GDALPipelineAlgorithm                         */
     548             : /************************************************************************/
     549             : 
     550         324 : GDALPipelineAlgorithm::GDALPipelineAlgorithm()
     551             :     : GDALAbstractPipelineAlgorithm(
     552             :           NAME, DESCRIPTION, HELP_URL,
     553         324 :           ConstructorOptions().SetStandaloneStep(false))
     554             : {
     555         324 :     m_supportsStreamedOutput = true;
     556             : 
     557         324 :     AddProgressArg();
     558             :     AddInputDatasetArg(&m_inputDataset, GDAL_OF_RASTER | GDAL_OF_VECTOR,
     559         324 :                        /* positionalAndRequired = */ false)
     560         324 :         .SetMinCount(1)
     561         324 :         .SetMaxCount(INT_MAX)
     562         324 :         .SetHiddenForCLI();
     563             :     AddOutputDatasetArg(&m_outputDataset, GDAL_OF_RASTER | GDAL_OF_VECTOR,
     564         324 :                         /* positionalAndRequired = */ false)
     565         324 :         .SetHiddenForCLI()
     566         324 :         .SetDatasetInputFlags(GADV_NAME | GADV_OBJECT);
     567             :     AddOutputFormatArg(&m_format, /* bStreamAllowed = */ true,
     568         324 :                        /* bGDALGAllowed = */ true)
     569         324 :         .SetHiddenForCLI();
     570         648 :     AddArg("pipeline", 0, _("Pipeline string or filename"), &m_pipeline)
     571         324 :         .SetHiddenForCLI()
     572         324 :         .SetPositional();
     573             : 
     574         324 :     AddOutputStringArg(&m_output).SetHiddenForCLI();
     575         324 :     AddStdoutArg(&m_stdout);
     576             : 
     577         324 :     AllowArbitraryLongNameArgs();
     578             : 
     579         324 :     GDALRasterPipelineAlgorithm::RegisterAlgorithms(m_stepRegistry, true);
     580         324 :     GDALVectorPipelineAlgorithm::RegisterAlgorithms(m_stepRegistry, true);
     581         324 :     m_stepRegistry.Register<GDALExternalRasterOrVectorAlgorithm>();
     582         324 :     m_stepRegistry.Register<GDALRasterAsFeaturesAlgorithm>();
     583         324 :     m_stepRegistry.Register<GDALRasterContourAlgorithm>();
     584         324 :     m_stepRegistry.Register<GDALRasterFootprintAlgorithm>();
     585         324 :     m_stepRegistry.Register<GDALRasterPixelInfoAlgorithm>();
     586         324 :     m_stepRegistry.Register<GDALRasterPolygonizeAlgorithm>();
     587         324 :     m_stepRegistry.Register<GDALRasterZonalStatsAlgorithm>();
     588         324 :     m_stepRegistry.Register<GDALVectorGridAlgorithm>();
     589         324 :     m_stepRegistry.Register<GDALVectorRasterizeAlgorithm>();
     590         324 : }
     591             : 
     592             : /************************************************************************/
     593             : /*               GDALPipelineAlgorithm::GetUsageForCLI()                */
     594             : /************************************************************************/
     595             : 
     596             : std::string
     597          19 : GDALPipelineAlgorithm::GetUsageForCLI(bool shortUsage,
     598             :                                       const UsageOptions &usageOptions) const
     599             : {
     600          19 :     UsageOptions stepUsageOptions;
     601          19 :     stepUsageOptions.isPipelineStep = true;
     602             : 
     603          19 :     if (!m_helpDocCategory.empty() && m_helpDocCategory != "main")
     604             :     {
     605           4 :         auto alg = GetStepAlg(m_helpDocCategory);
     606           2 :         if (alg)
     607             :         {
     608           3 :             alg->SetCallPath({CPLString(m_helpDocCategory)
     609           2 :                                   .replaceAll(RASTER_SUFFIX, "")
     610           3 :                                   .replaceAll(VECTOR_SUFFIX, "")});
     611           1 :             alg->GetArg("help-doc")->Set(true);
     612           1 :             return alg->GetUsageForCLI(shortUsage, stepUsageOptions);
     613             :         }
     614             :         else
     615             :         {
     616           1 :             fprintf(stderr, "ERROR: unknown pipeline step '%s'\n",
     617             :                     m_helpDocCategory.c_str());
     618             :             return CPLSPrintf("ERROR: unknown pipeline step '%s'\n",
     619           1 :                               m_helpDocCategory.c_str());
     620             :         }
     621             :     }
     622             : 
     623          17 :     UsageOptions usageOptionsMain(usageOptions);
     624          17 :     usageOptionsMain.isPipelineMain = true;
     625             :     std::string ret =
     626          34 :         GDALAlgorithm::GetUsageForCLI(shortUsage, usageOptionsMain);
     627          17 :     if (shortUsage)
     628          15 :         return ret;
     629             : 
     630             :     ret +=
     631             :         "\n<PIPELINE> is of the form: read|calc|concat|create|mosaic|stack "
     632             :         "[READ-OPTIONS] "
     633           2 :         "( ! <STEP-NAME> [STEP-OPTIONS] )* ! write!info!tile [WRITE-OPTIONS]\n";
     634             : 
     635           2 :     if (m_helpDocCategory == "main")
     636             :     {
     637           1 :         return ret;
     638             :     }
     639             : 
     640           1 :     ret += '\n';
     641           1 :     ret += "Example: 'gdal pipeline --progress ! read in.tif ! \\\n";
     642           1 :     ret += "               rasterize --size 256 256 ! buffer 20 ! ";
     643           1 :     ret += "write out.gpkg --overwrite'\n";
     644           1 :     ret += '\n';
     645           1 :     ret += "Potential steps are:\n";
     646             : 
     647          84 :     for (const std::string &name : m_stepRegistry.GetNames())
     648             :     {
     649         166 :         auto alg = GetStepAlg(name);
     650          83 :         assert(alg);
     651          83 :         auto [options, maxOptLen] = alg->GetArgNamesForCLI();
     652          83 :         stepUsageOptions.maxOptLen =
     653          83 :             std::max(stepUsageOptions.maxOptLen, maxOptLen);
     654             :     }
     655             : 
     656             :     {
     657           1 :         ret += '\n';
     658           1 :         auto alg = std::make_unique<GDALRasterReadAlgorithm>();
     659           2 :         alg->SetCallPath({alg->GetName()});
     660           1 :         ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
     661             :     }
     662             :     {
     663           1 :         ret += '\n';
     664           1 :         auto alg = std::make_unique<GDALVectorReadAlgorithm>();
     665           2 :         alg->SetCallPath({alg->GetName()});
     666           1 :         ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
     667             :     }
     668          84 :     for (const std::string &name : m_stepRegistry.GetNames())
     669             :     {
     670         166 :         auto alg = GetStepAlg(name);
     671          83 :         assert(alg);
     672          92 :         if (alg->CanBeFirstStep() && !alg->CanBeMiddleStep() &&
     673          98 :             !alg->IsHidden() &&
     674           6 :             !STARTS_WITH(name.c_str(), GDALRasterReadAlgorithm::NAME))
     675             :         {
     676           4 :             ret += '\n';
     677           8 :             alg->SetCallPath({name});
     678           4 :             ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
     679             :         }
     680             :     }
     681          84 :     for (const std::string &name : m_stepRegistry.GetNames())
     682             :     {
     683         166 :         auto alg = GetStepAlg(name);
     684          83 :         assert(alg);
     685          83 :         if (alg->CanBeMiddleStep() && !alg->IsHidden())
     686             :         {
     687          69 :             ret += '\n';
     688         207 :             alg->SetCallPath({CPLString(alg->GetName())
     689         138 :                                   .replaceAll(RASTER_SUFFIX, "")
     690         207 :                                   .replaceAll(VECTOR_SUFFIX, "")});
     691          69 :             ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
     692             :         }
     693             :     }
     694          84 :     for (const std::string &name : m_stepRegistry.GetNames())
     695             :     {
     696         166 :         auto alg = GetStepAlg(name);
     697          83 :         assert(alg);
     698          96 :         if (alg->CanBeLastStep() && !alg->CanBeMiddleStep() &&
     699         104 :             !alg->IsHidden() &&
     700           8 :             !STARTS_WITH(name.c_str(), GDALRasterWriteAlgorithm::NAME))
     701             :         {
     702           6 :             ret += '\n';
     703          12 :             alg->SetCallPath({name});
     704           6 :             ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
     705             :         }
     706             :     }
     707             :     {
     708           1 :         ret += '\n';
     709           1 :         auto alg = std::make_unique<GDALRasterWriteAlgorithm>();
     710           2 :         alg->SetCallPath({alg->GetName()});
     711           1 :         ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
     712             :     }
     713             :     {
     714           1 :         ret += '\n';
     715           1 :         auto alg = std::make_unique<GDALVectorWriteAlgorithm>();
     716           2 :         alg->SetCallPath({alg->GetName()});
     717           1 :         ret += alg->GetUsageForCLI(shortUsage, stepUsageOptions);
     718             :     }
     719             : 
     720           1 :     ret += GetUsageForCLIEnd();
     721             : 
     722           1 :     return ret;
     723             : }
     724             : 
     725             : //! @endcond

Generated by: LCOV version 1.14