13#ifndef GDALALG_VECTOR_PIPELINE_INCLUDED
14#define GDALALG_VECTOR_PIPELINE_INCLUDED
16#include "gdalalgorithm.h"
17#include "gdalalg_abstract_pipeline.h"
20#include "ogrlayerwithtranslatefeature.h"
32class GDALRasterAlgorithmStepRegistry;
34class GDALVectorPipelineStepAlgorithm
35 :
public GDALPipelineStepAlgorithm
38 ~GDALVectorPipelineStepAlgorithm()
override;
41 GDALVectorPipelineStepAlgorithm(
const std::string &name,
42 const std::string &description,
43 const std::string &helpURL,
46 GDALVectorPipelineStepAlgorithm(
const std::string &name,
47 const std::string &description,
48 const std::string &helpURL,
49 const ConstructorOptions &options);
51 friend class GDALVectorPipelineAlgorithm;
52 friend class GDALVectorConcatAlgorithm;
54 int GetInputType()
const override
59 int GetOutputType()
const override
72 GDALVectorAlgorithmStepRegistry() =
default;
73 ~GDALVectorAlgorithmStepRegistry()
override;
77 template <
class MyAlgorithm>
78 bool Register(
const std::string &name = std::string())
81 std::is_base_of_v<GDALVectorPipelineStepAlgorithm, MyAlgorithm>,
82 "Algorithm is not a GDALVectorPipelineStepAlgorithm");
85 info.m_name = name.empty() ? MyAlgorithm::NAME : name;
86 info.m_aliases = MyAlgorithm::GetAliasesStatic();
87 info.m_creationFunc = []() -> std::unique_ptr<GDALAlgorithm>
88 {
return std::make_unique<MyAlgorithm>(); };
97class GDALVectorPipelineAlgorithm final :
public GDALAbstractPipelineAlgorithm
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";
106 static std::vector<std::string> GetAliasesStatic()
109#ifdef GDAL_PIPELINE_PROJ_NOSTALGIA
117 GDALVectorPipelineAlgorithm();
119 std::string GetUsageForCLI(
bool shortUsage,
120 const UsageOptions &usageOptions)
const override;
122 static void RegisterAlgorithms(GDALVectorAlgorithmStepRegistry ®istry,
123 bool forMixedPipeline);
125 int GetInputType()
const override
130 int GetOutputType()
const override
136 GDALVectorAlgorithmStepRegistry m_stepRegistry{};
140 return m_stepRegistry;
145 return m_stepRegistry;
149 std::unique_ptr<GDALAbstractPipelineAlgorithm>
150 CreateNestedPipeline()
const override
152 auto pipeline = std::make_unique<GDALVectorPipelineAlgorithm>();
153 pipeline->m_bInnerPipeline =
true;
165class GDALVectorDecoratedDataset
169 ~GDALVectorDecoratedDataset()
override;
173 return m_srcDS.GetMetadata(pszDomain);
177 const char *pszDomain)
override
179 return m_srcDS.GetMetadataItem(pszName, pszDomain);
182 std::vector<std::string>
185 return m_srcDS.GetFieldDomainNames(papszOptions);
190 return m_srcDS.GetFieldDomain(name);
193 std::vector<std::string>
196 return m_srcDS.GetRelationshipNames(papszOptions);
202 return m_srcDS.GetRelationship(name);
206 std::unique_ptr<GDALDataset> m_dummySrcDS{};
209 explicit GDALVectorDecoratedDataset(
GDALDataset *poSrcDS);
218class GDALVectorOutputDataset final :
public GDALVectorDecoratedDataset
222 explicit GDALVectorOutputDataset(
GDALDataset *poSrcDS);
224 int GetLayerCount()
const override
226 return static_cast<int>(m_layers.size());
229 const OGRLayer *GetLayer(
int idx)
const override
231 return m_layers[idx].get();
234 int TestCapability(
const char *)
const override;
236 void AddLayer(std::unique_ptr<OGRLayer> layer)
238 m_layers.emplace_back(std::move(layer));
242 std::vector<std::unique_ptr<OGRLayer>> m_layers{};
253class GDALVectorAlgorithmLayerProgressHelper
257 GDALVectorAlgorithmLayerProgressHelper(GDALProgressFunc pfnProgress,
258 void *pProgressData);
260 explicit GDALVectorAlgorithmLayerProgressHelper(
261 const GDALPipelineStepRunContext &ctxt);
264 void AddProcessedLayer(
OGRLayer &srcLayer);
268 void AddPassThroughLayer(
OGRLayer &srcLayer);
274 explicit iterator(
const GDALVectorAlgorithmLayerProgressHelper &helper,
277 m_nLayerIdx(start ? 0 : m_helper.m_apoSrcLayers.size())
281 inline bool operator==(
const iterator &other)
const
283 return m_nLayerIdx == other.m_nLayerIdx;
286 inline bool operator!=(
const iterator &other)
const
288 return m_nLayerIdx != other.m_nLayerIdx;
291 inline iterator &operator++()
293 if (!m_helper.m_anFeatures.empty())
294 m_nFeatureIdx += m_helper.m_anFeatures[m_nProcessedLayerIdx];
295 if (m_helper.m_apoSrcLayers[m_nLayerIdx].second)
296 ++m_nProcessedLayerIdx;
301 using progress_data_unique_ptr =
302 std::unique_ptr<void,
decltype(&GDALDestroyScaledProgress)>;
303 using value_type = std::tuple<
OGRLayer *, bool, GDALProgressFunc,
304 progress_data_unique_ptr>;
306 value_type operator*()
const;
309 const GDALVectorAlgorithmLayerProgressHelper &m_helper;
310 size_t m_nLayerIdx = 0;
311 size_t m_nProcessedLayerIdx = 0;
319 iterator
begin()
const
321 return iterator(*
this,
true);
328 return iterator(*
this,
false);
332 bool HasProcessedLayers()
const
334 return !m_anFeatures.empty();
338 GDALProgressFunc m_pfnProgress =
nullptr;
339 void *m_pProgressData =
nullptr;
340 int64_t m_nTotalFeatures = 0;
341 std::vector<std::pair<OGRLayer *, bool>> m_apoSrcLayers{};
342 std::vector<int64_t> m_anFeatures{};
355class GDALVectorPipelineOutputLayer
356 :
public OGRLayerWithTranslateFeature,
360 explicit GDALVectorPipelineOutputLayer(
OGRLayer &oSrcLayer);
361 ~GDALVectorPipelineOutputLayer()
override;
367 void FailTranslation()
369 m_translateError =
true;
373 void ResetReading()
override;
377 std::vector<std::unique_ptr<OGRFeature>> m_pendingFeatures{};
378 size_t m_idxInPendingFeatures = 0;
379 bool m_translateError =
false;
389class GDALVectorPipelinePassthroughLayer
390 :
public GDALVectorPipelineOutputLayer
393 explicit GDALVectorPipelinePassthroughLayer(
OGRLayer &oSrcLayer)
394 : GDALVectorPipelineOutputLayer(oSrcLayer)
401 int TestCapability(
const char *pszCap)
const override
405 return m_srcLayer.TestCapability(pszCap);
409 bool bForce)
override
411 return m_srcLayer.GetExtent(iGeomField, psExtent, bForce);
415 bool bForce)
override
417 return m_srcLayer.GetExtent3D(iGeomField, psExtent, bForce);
420 void TranslateFeature(
421 std::unique_ptr<OGRFeature> poSrcFeature,
422 std::vector<std::unique_ptr<OGRFeature>> &apoOutFeatures)
override;
437class GDALVectorNonStreamingAlgorithmLayer
442 GDALVectorNonStreamingAlgorithmLayer(
OGRLayer &srcLayer,
445 ~GDALVectorNonStreamingAlgorithmLayer()
override;
452 virtual bool Process(GDALProgressFunc pfnProgress,
void *pProgressData) = 0;
454 virtual std::unique_ptr<OGRFeature> GetNextProcessedFeature() = 0;
462 int m_geomFieldIndex{0};
476class GDALVectorNonStreamingAlgorithmDataset
477 :
public GDALVectorDecoratedDataset
480 explicit GDALVectorNonStreamingAlgorithmDataset(
GDALDataset &oSrcDS);
481 ~GDALVectorNonStreamingAlgorithmDataset()
override;
484 bool AddProcessedLayer(
485 std::unique_ptr<GDALVectorNonStreamingAlgorithmLayer> srcLayer,
486 GDALProgressFunc progressFunc,
void *progressData);
488 void AddPassThroughLayer(
OGRLayer &oLayer);
490 int GetLayerCount() const final override;
491 OGRLayer *GetLayer(
int idx) const final override;
492 int TestCapability(const
char *pszCap) const override;
495 std::vector<std::unique_ptr<
OGRLayer>> m_owned_layers{};
496 std::vector<OGRLayer *> m_layers{};
506class GDALVectorPipelineOutputDataset
507 :
public GDALVectorDecoratedDataset
509 std::map<OGRLayer *, OGRLayerWithTranslateFeature *>
510 m_mapSrcLayerToNewLayer{};
511 std::vector<std::unique_ptr<OGRLayerWithTranslateFeature>>
513 std::vector<OGRLayerWithTranslateFeature *> m_layers{};
515 OGRLayerWithTranslateFeature *m_belongingLayer =
nullptr;
516 std::vector<std::unique_ptr<OGRFeature>> m_pendingFeatures{};
517 size_t m_idxInPendingFeatures = 0;
522 explicit GDALVectorPipelineOutputDataset(
GDALDataset &oSrcDS);
525 std::unique_ptr<OGRLayerWithTranslateFeature> poNewLayer);
527 int GetLayerCount()
const override;
529 OGRLayer *GetLayer(
int idx)
const override;
531 int TestCapability(
const char *pszCap)
const override;
533 void ResetReading()
override;
536 double *pdfProgressPct,
537 GDALProgressFunc pfnProgress,
538 void *pProgressData)
override;
Registry of GDAL algorithms.
Definition gdalalgorithm_cpp.h:2316
static constexpr const char * HIDDEN_ALIAS_SEPARATOR
Special value to put in m_aliases to separate public alias from hidden aliases.
Definition gdalalgorithm_cpp.h:2320
bool Register()
Register the algorithm of type MyAlgorithm.
Definition gdalalgorithm_cpp.h:2343
A set of associated raster bands, usually from one file.
Definition gdal_dataset.h:77
virtual const GDALRelationship * GetRelationship(const std::string &name) const
Get a relationship from its name.
Definition gdaldataset.cpp:10542
virtual const OGRFieldDomain * GetFieldDomain(const std::string &name) const
Get a field domain from its name.
Definition gdaldataset.cpp:10225
virtual std::vector< std::string > GetRelationshipNames(CSLConstList papszOptions=nullptr) const
Returns a list of the names of all relationships stored in the dataset.
Definition gdaldataset.cpp:10498
void static void CSLConstList GetMetadata(const char *pszDomain="") override
Fetch metadata.
Definition gdaldataset.cpp:5217
virtual std::vector< std::string > GetFieldDomainNames(CSLConstList papszOptions=nullptr) const
Returns a list of the names of all field domains stored in the dataset.
Definition gdaldataset.cpp:10175
virtual const char * GetMetadataItem(const char *pszName, const char *pszDomain="")
Fetch single metadata item.
Definition gdalmajorobject.cpp:322
virtual const char * GetDescription() const
Fetch object description.
Definition gdalmajorobject.cpp:61
Definition of a table relationship.
Definition gdal_relationship.h:38
Simple container for a bounding region in 3D.
Definition ogr_core.h:217
Simple container for a bounding region (rectangle)
Definition ogr_core.h:44
Definition of a feature class or feature layer.
Definition ogr_feature.h:521
A simple feature, including geometry and attributes.
Definition ogr_feature.h:1041
Definition of a field domain.
Definition ogr_feature.h:1780
Template class offering a GetNextFeature() implementation relying on GetNextRawFeature().
Definition ogrsf_frmts.h:509
This class represents a layer of simple features, with access methods.
Definition ogrsf_frmts.h:61
virtual const char * GetName() const
Return the layer name.
Definition ogrlayer.cpp:4807
#define EQUAL(a, b)
Alias for strcasecmp() == 0.
Definition cpl_port.h:532
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition cpl_port.h:1101
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition cpl_port.h:1252
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:195
#define GDAL_OF_VECTOR
Allow vector drivers to be used.
Definition gdal.h:1130
#define OLCFastFeatureCount
Layer capability for fast feature count retrieval .
Definition ogr_core.h:983
int OGRErr
Type for a OGR error.
Definition ogr_core.h:388
Classes related to registration of format support, and opening datasets.
OGRLayer::FeatureIterator begin(OGRLayer *poLayer)
Return begin of feature iterator.
Definition ogrsf_frmts.h:478
OGRLayer::FeatureIterator end(OGRLayer *poLayer)
Return end of feature iterator.
Definition ogrsf_frmts.h:486
#define DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(BaseLayer)
Utility macro to define GetNextFeature() through GetNextRawFeature()
Definition ogrsf_frmts.h:539