Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/gdalalg_vector_pipeline.h
Line
Count
Source
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
#ifndef GDALALG_VECTOR_PIPELINE_INCLUDED
14
#define GDALALG_VECTOR_PIPELINE_INCLUDED
15
16
#include "gdalalgorithm.h"
17
#include "gdalalg_abstract_pipeline.h"
18
19
#include "ogrsf_frmts.h"
20
#include "ogrlayerwithtranslatefeature.h"
21
22
#include <map>
23
#include <tuple>
24
#include <vector>
25
26
//! @cond Doxygen_Suppress
27
28
/************************************************************************/
29
/*                   GDALVectorPipelineStepAlgorithm                    */
30
/************************************************************************/
31
32
class GDALRasterAlgorithmStepRegistry;
33
34
class GDALVectorPipelineStepAlgorithm /* non final */
35
    : public GDALPipelineStepAlgorithm
36
{
37
  public:
38
    ~GDALVectorPipelineStepAlgorithm() override;
39
40
  protected:
41
    GDALVectorPipelineStepAlgorithm(const std::string &name,
42
                                    const std::string &description,
43
                                    const std::string &helpURL,
44
                                    bool standaloneStep);
45
46
    GDALVectorPipelineStepAlgorithm(const std::string &name,
47
                                    const std::string &description,
48
                                    const std::string &helpURL,
49
                                    const ConstructorOptions &options);
50
51
    friend class GDALVectorPipelineAlgorithm;
52
    friend class GDALVectorConcatAlgorithm;
53
54
    int GetInputType() const override
55
0
    {
56
0
        return GDAL_OF_VECTOR;
57
0
    }
58
59
    int GetOutputType() const override
60
0
    {
61
0
        return GDAL_OF_VECTOR;
62
0
    }
63
};
64
65
/************************************************************************/
66
/*                   GDALVectorAlgorithmStepRegistry                    */
67
/************************************************************************/
68
69
class GDALVectorAlgorithmStepRegistry : public virtual GDALAlgorithmRegistry
70
{
71
  public:
72
0
    GDALVectorAlgorithmStepRegistry() = default;
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::GDALVectorAlgorithmStepRegistry()
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::GDALVectorAlgorithmStepRegistry()
73
    ~GDALVectorAlgorithmStepRegistry() override;
74
75
    /** Register the algorithm of type MyAlgorithm.
76
     */
77
    template <class MyAlgorithm>
78
    bool Register(const std::string &name = std::string())
79
0
    {
80
0
        static_assert(
81
0
            std::is_base_of_v<GDALVectorPipelineStepAlgorithm, MyAlgorithm>,
82
0
            "Algorithm is not a GDALVectorPipelineStepAlgorithm");
83
84
0
        AlgInfo info;
85
0
        info.m_name = name.empty() ? MyAlgorithm::NAME : name;
86
0
        info.m_aliases = MyAlgorithm::GetAliasesStatic();
87
0
        info.m_creationFunc = []() -> std::unique_ptr<GDALAlgorithm>
88
0
        { return std::make_unique<MyAlgorithm>(); };
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorReadAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorWriteAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorInfoAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorBufferAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorCheckCoverageAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorCheckGeometryAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorConcatAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorCleanCoverageAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorClipAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorEditAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorExplodeCollectionsAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALMaterializeVectorAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorReprojectAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorFilterAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorLimitAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorMakePointAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorMakeValidAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorPartitionAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorSegmentizeAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorSelectAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorSetFieldTypeAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorSetGeomTypeAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorSimplifyAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorSimplifyCoverageAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorSortAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorSQLAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorUpdateAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALVectorSwapXYAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
Unexecuted instantiation: GDALVectorAlgorithmStepRegistry::Register<GDALTeeVectorAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::{lambda()#1}::operator()() const
89
0
        return GDALAlgorithmRegistry::Register(info);
90
0
    }
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorReadAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorWriteAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorInfoAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorBufferAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorCheckCoverageAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorCheckGeometryAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorConcatAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorCleanCoverageAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorClipAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorEditAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorExplodeCollectionsAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALMaterializeVectorAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorReprojectAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorFilterAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorLimitAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorMakePointAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorMakeValidAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorPartitionAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorSegmentizeAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorSelectAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorSetFieldTypeAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorSetGeomTypeAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorSimplifyAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorSimplifyCoverageAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorSortAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorSQLAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorUpdateAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALVectorSwapXYAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: bool GDALVectorAlgorithmStepRegistry::Register<GDALTeeVectorAlgorithm>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
91
};
92
93
/************************************************************************/
94
/*                     GDALVectorPipelineAlgorithm                      */
95
/************************************************************************/
96
97
class GDALVectorPipelineAlgorithm final : public GDALAbstractPipelineAlgorithm
98
{
99
  public:
100
    static constexpr const char *NAME = "pipeline";
101
    static constexpr const char *DESCRIPTION =
102
        "Process a vector dataset applying several steps.";
103
    static constexpr const char *HELP_URL =
104
        "/programs/gdal_vector_pipeline.html";
105
106
    static std::vector<std::string> GetAliasesStatic()
107
0
    {
108
0
        return {
109
0
#ifdef GDAL_PIPELINE_PROJ_NOSTALGIA
110
0
            GDALAlgorithmRegistry::HIDDEN_ALIAS_SEPARATOR,
111
0
            "+pipeline",
112
0
            "+gdal=pipeline",
113
0
#endif
114
0
        };
115
0
    }
116
117
    GDALVectorPipelineAlgorithm();
118
119
    std::string GetUsageForCLI(bool shortUsage,
120
                               const UsageOptions &usageOptions) const override;
121
122
    static void RegisterAlgorithms(GDALVectorAlgorithmStepRegistry &registry,
123
                                   bool forMixedPipeline);
124
125
    int GetInputType() const override
126
0
    {
127
0
        return GDAL_OF_VECTOR;
128
0
    }
129
130
    int GetOutputType() const override
131
0
    {
132
0
        return GDAL_OF_VECTOR;
133
0
    }
134
135
  protected:
136
    GDALVectorAlgorithmStepRegistry m_stepRegistry{};
137
138
    GDALAlgorithmRegistry &GetStepRegistry() override
139
0
    {
140
0
        return m_stepRegistry;
141
0
    }
142
143
    const GDALAlgorithmRegistry &GetStepRegistry() const override
144
0
    {
145
0
        return m_stepRegistry;
146
0
    }
147
148
  private:
149
    std::unique_ptr<GDALAbstractPipelineAlgorithm>
150
    CreateNestedPipeline() const override
151
0
    {
152
0
        auto pipeline = std::make_unique<GDALVectorPipelineAlgorithm>();
153
0
        pipeline->m_bInnerPipeline = true;
154
0
        return pipeline;
155
0
    }
156
};
157
158
/************************************************************************/
159
/*                       GDALVectorOutputDataset                        */
160
/************************************************************************/
161
162
class GDALVectorOutputDataset final : public GDALDataset
163
{
164
165
  public:
166
    int GetLayerCount() const override
167
0
    {
168
0
        return static_cast<int>(m_layers.size());
169
0
    }
170
171
    const OGRLayer *GetLayer(int idx) const override
172
0
    {
173
0
        return m_layers[idx].get();
174
0
    }
175
176
    int TestCapability(const char *) const override;
177
178
    void AddLayer(std::unique_ptr<OGRLayer> layer)
179
0
    {
180
0
        m_layers.emplace_back(std::move(layer));
181
0
    }
182
183
  private:
184
    std::vector<std::unique_ptr<OGRLayer>> m_layers{};
185
};
186
187
/************************************************************************/
188
/*                GDALVectorAlgorithmLayerProgressHelper                */
189
/************************************************************************/
190
191
/**
192
 * This class helps doing progress report for algorithm iterating over layers
193
 * of the source dataset.
194
 */
195
class GDALVectorAlgorithmLayerProgressHelper
196
{
197
  public:
198
    /** Constructor */
199
    GDALVectorAlgorithmLayerProgressHelper(GDALProgressFunc pfnProgress,
200
                                           void *pProgressData);
201
    /** Constructor */
202
    explicit GDALVectorAlgorithmLayerProgressHelper(
203
        const GDALPipelineStepRunContext &ctxt);
204
205
    /** Register the passed layer as a layer that will be processed. */
206
    void AddProcessedLayer(OGRLayer &srcLayer);
207
208
    /** Register the passed layer as a layer that will be forwarded without
209
     * processing. */
210
    void AddPassThroughLayer(OGRLayer &srcLayer);
211
212
    //! @cond Doxygen_Suppress
213
    class iterator
214
    {
215
      public:
216
        explicit iterator(const GDALVectorAlgorithmLayerProgressHelper &helper,
217
                          bool start)
218
0
            : m_helper(helper),
219
0
              m_nLayerIdx(start ? 0 : m_helper.m_apoSrcLayers.size())
220
0
        {
221
0
        }
222
223
        inline bool operator==(const iterator &other) const
224
0
        {
225
0
            return m_nLayerIdx == other.m_nLayerIdx;
226
0
        }
227
228
        inline bool operator!=(const iterator &other) const
229
0
        {
230
0
            return m_nLayerIdx != other.m_nLayerIdx;
231
0
        }
232
233
        inline iterator &operator++()
234
0
        {
235
0
            if (!m_helper.m_anFeatures.empty())
236
0
                m_nFeatureIdx += m_helper.m_anFeatures[m_nProcessedLayerIdx];
237
0
            if (m_helper.m_apoSrcLayers[m_nLayerIdx].second)
238
0
                ++m_nProcessedLayerIdx;
239
0
            ++m_nLayerIdx;
240
0
            return *this;
241
0
        }
242
243
        using progress_data_unique_ptr =
244
            std::unique_ptr<void, decltype(&GDALDestroyScaledProgress)>;
245
        using value_type = std::tuple<OGRLayer *, bool, GDALProgressFunc,
246
                                      progress_data_unique_ptr>;
247
248
        value_type operator*() const;
249
250
      private:
251
        const GDALVectorAlgorithmLayerProgressHelper &m_helper;
252
        size_t m_nLayerIdx = 0;
253
        size_t m_nProcessedLayerIdx = 0;
254
        GIntBig m_nFeatureIdx = 0;
255
    };
256
257
    //! @endcond
258
259
    /** Start of an iterator over layers registered with AddProcessedLayer()
260
     * and AddUnprocessedLayer() */
261
    iterator begin() const
262
0
    {
263
0
        return iterator(*this, true);
264
0
    }
265
266
    /** End of an iterator over layers registered with AddProcessedLayer()
267
     * and AddUnprocessedLayer() */
268
    iterator end() const
269
0
    {
270
0
        return iterator(*this, false);
271
0
    }
272
273
    /** Return if AddProcessedLayer() has been called at least once. */
274
    bool HasProcessedLayers() const
275
0
    {
276
0
        return !m_anFeatures.empty();
277
0
    }
278
279
  private:
280
    GDALProgressFunc m_pfnProgress = nullptr;
281
    void *m_pProgressData = nullptr;
282
    int64_t m_nTotalFeatures = 0;
283
    std::vector<std::pair<OGRLayer *, bool>> m_apoSrcLayers{};
284
    std::vector<int64_t> m_anFeatures{};
285
286
    CPL_DISALLOW_COPY_ASSIGN(GDALVectorAlgorithmLayerProgressHelper)
287
};
288
289
/************************************************************************/
290
/*                    GDALVectorPipelineOutputLayer                     */
291
/************************************************************************/
292
293
/** Class that implements GetNextFeature() by forwarding to
294
 * OGRLayerWithTranslateFeature::TranslateFeature() implementation, which
295
 * might return several features.
296
 */
297
class GDALVectorPipelineOutputLayer /* non final */
298
    : public OGRLayerWithTranslateFeature,
299
      public OGRGetNextFeatureThroughRaw<GDALVectorPipelineOutputLayer>
300
{
301
  protected:
302
    explicit GDALVectorPipelineOutputLayer(OGRLayer &oSrcLayer);
303
    ~GDALVectorPipelineOutputLayer() override;
304
305
    DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(GDALVectorPipelineOutputLayer)
306
307
    OGRLayer &m_srcLayer;
308
309
    void FailTranslation()
310
0
    {
311
0
        m_translateError = true;
312
0
    }
313
314
  public:
315
    void ResetReading() override;
316
    OGRFeature *GetNextRawFeature();
317
318
  private:
319
    std::vector<std::unique_ptr<OGRFeature>> m_pendingFeatures{};
320
    size_t m_idxInPendingFeatures = 0;
321
    bool m_translateError = false;
322
};
323
324
/************************************************************************/
325
/*                  GDALVectorPipelinePassthroughLayer                  */
326
/************************************************************************/
327
328
/** Class that forwards GetNextFeature() calls to the source layer and
329
 * can be added to GDALVectorPipelineOutputDataset::AddLayer()
330
 */
331
class GDALVectorPipelinePassthroughLayer /* non final */
332
    : public GDALVectorPipelineOutputLayer
333
{
334
  public:
335
    explicit GDALVectorPipelinePassthroughLayer(OGRLayer &oSrcLayer)
336
0
        : GDALVectorPipelineOutputLayer(oSrcLayer)
337
0
    {
338
0
    }
Unexecuted instantiation: GDALVectorPipelinePassthroughLayer::GDALVectorPipelinePassthroughLayer(OGRLayer&)
Unexecuted instantiation: GDALVectorPipelinePassthroughLayer::GDALVectorPipelinePassthroughLayer(OGRLayer&)
339
340
    const OGRFeatureDefn *GetLayerDefn() const override;
341
342
    int TestCapability(const char *pszCap) const override
343
0
    {
344
0
        return m_srcLayer.TestCapability(pszCap);
345
0
    }
346
347
    OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent,
348
                      bool bForce) override
349
0
    {
350
0
        return m_srcLayer.GetExtent(iGeomField, psExtent, bForce);
351
0
    }
352
353
    OGRErr IGetExtent3D(int iGeomField, OGREnvelope3D *psExtent,
354
                        bool bForce) override
355
0
    {
356
0
        return m_srcLayer.GetExtent3D(iGeomField, psExtent, bForce);
357
0
    }
358
359
    void TranslateFeature(
360
        std::unique_ptr<OGRFeature> poSrcFeature,
361
        std::vector<std::unique_ptr<OGRFeature>> &apoOutFeatures) override
362
0
    {
363
0
        apoOutFeatures.push_back(std::move(poSrcFeature));
364
0
    }
365
};
366
367
/************************************************************************/
368
/*                 GDALVectorNonStreamingAlgorithmLayer                 */
369
/************************************************************************/
370
371
/**
372
 * This class represents a layer for algorithms that process vector data
373
 * in a non-streaming manner.
374
 *
375
 * Implementations must override the following methods:
376
 * - Process(), which is called when the first feature is read
377
 * - GetNextProcessedFeature() method which provides features in sequential order
378
 */
379
class GDALVectorNonStreamingAlgorithmLayer
380
    : public OGRLayer,
381
      public OGRGetNextFeatureThroughRaw<GDALVectorNonStreamingAlgorithmLayer>
382
{
383
  public:
384
    GDALVectorNonStreamingAlgorithmLayer(OGRLayer &srcLayer,
385
                                         int geomFieldIndex);
386
387
    ~GDALVectorNonStreamingAlgorithmLayer() override;
388
389
    const char *GetDescription() const override
390
0
    {
391
0
        return GetName();
392
0
    }
393
394
    virtual bool Process(GDALProgressFunc pfnProgress, void *pProgressData) = 0;
395
396
    virtual std::unique_ptr<OGRFeature> GetNextProcessedFeature() = 0;
397
398
    OGRFeature *GetNextRawFeature();
399
400
    DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(GDALVectorNonStreamingAlgorithmLayer)
401
402
  protected:
403
    OGRLayer &m_srcLayer;
404
    int m_geomFieldIndex{0};
405
406
  private:
407
    CPL_DISALLOW_COPY_ASSIGN(GDALVectorNonStreamingAlgorithmLayer)
408
};
409
410
/************************************************************************/
411
/*                GDALVectorNonStreamingAlgorithmDataset                */
412
/************************************************************************/
413
414
/**
415
 * Dataset used to read all input features into memory and perform some
416
 * processing.
417
 */
418
class GDALVectorNonStreamingAlgorithmDataset /* non final */
419
    : public GDALDataset
420
{
421
  public:
422
    GDALVectorNonStreamingAlgorithmDataset();
423
    ~GDALVectorNonStreamingAlgorithmDataset() override;
424
425
    /** Add a layer to the dataset and perform the associated processing. */
426
    bool AddProcessedLayer(
427
        std::unique_ptr<GDALVectorNonStreamingAlgorithmLayer> srcLayer,
428
        GDALProgressFunc progressFunc, void *progressData);
429
430
    void AddPassThroughLayer(OGRLayer &oLayer);
431
432
    int GetLayerCount() const final override;
433
    OGRLayer *GetLayer(int idx) const final override;
434
    int TestCapability(const char *pszCap) const override;
435
436
  private:
437
    std::vector<std::unique_ptr<OGRLayer>> m_owned_layers{};
438
    std::vector<OGRLayer *> m_layers{};
439
};
440
441
/************************************************************************/
442
/*                   GDALVectorPipelineOutputDataset                    */
443
/************************************************************************/
444
445
/** Class used by vector pipeline steps to create an output on-the-fly
446
 * dataset where they can store on-the-fly layers.
447
 */
448
class GDALVectorPipelineOutputDataset final : public GDALDataset
449
{
450
    GDALDataset &m_srcDS;
451
    std::map<OGRLayer *, OGRLayerWithTranslateFeature *>
452
        m_mapSrcLayerToNewLayer{};
453
    std::vector<std::unique_ptr<OGRLayerWithTranslateFeature>>
454
        m_layersToDestroy{};
455
    std::vector<OGRLayerWithTranslateFeature *> m_layers{};
456
457
    OGRLayerWithTranslateFeature *m_belongingLayer = nullptr;
458
    std::vector<std::unique_ptr<OGRFeature>> m_pendingFeatures{};
459
    size_t m_idxInPendingFeatures = 0;
460
461
    CPL_DISALLOW_COPY_ASSIGN(GDALVectorPipelineOutputDataset)
462
463
  public:
464
    explicit GDALVectorPipelineOutputDataset(GDALDataset &oSrcDS);
465
466
    void AddLayer(OGRLayer &oSrcLayer,
467
                  std::unique_ptr<OGRLayerWithTranslateFeature> poNewLayer);
468
469
    int GetLayerCount() const override;
470
471
    OGRLayer *GetLayer(int idx) const override;
472
473
    int TestCapability(const char *pszCap) const override;
474
475
    void ResetReading() override;
476
477
    OGRFeature *GetNextFeature(OGRLayer **ppoBelongingLayer,
478
                               double *pdfProgressPct,
479
                               GDALProgressFunc pfnProgress,
480
                               void *pProgressData) override;
481
};
482
483
//! @endcond
484
485
#endif