LCOV - code coverage report
Current view: top level - apps - gdalalg_vector_pipeline.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 321 323 99.4 %
Date: 2026-04-07 13:24:38 Functions: 34 35 97.1 %

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

Generated by: LCOV version 1.14