From 85e0ee04e21af442cc0a1d90c82072da8c603443 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Thu, 6 Feb 2025 16:01:18 +0100 Subject: [PATCH] Factor out a GDALVectorPipelineOutputDataset class --- apps/gdalalg_vector_clip.cpp | 26 +-------------------- apps/gdalalg_vector_filter.cpp | 30 +----------------------- apps/gdalalg_vector_pipeline.h | 39 +++++++++++++++++++++++++++++++ apps/gdalalg_vector_read.cpp | 32 +------------------------ apps/gdalalg_vector_reproject.cpp | 33 +------------------------- 5 files changed, 43 insertions(+), 117 deletions(-) diff --git a/apps/gdalalg_vector_clip.cpp b/apps/gdalalg_vector_clip.cpp index 2963ea620c1c..ed5e9e714426 100644 --- a/apps/gdalalg_vector_clip.cpp +++ b/apps/gdalalg_vector_clip.cpp @@ -65,30 +65,6 @@ GDALVectorClipAlgorithm::GDALVectorClipAlgorithm(bool standaloneStep) namespace { -class GDALVectorClipAlgorithmDataset final : public GDALDataset -{ - std::vector> m_layers{}; - - public: - GDALVectorClipAlgorithmDataset() = default; - - void AddLayer(std::unique_ptr poLayer) - { - m_layers.push_back(std::move(poLayer)); - } - - int GetLayerCount() override - { - return static_cast(m_layers.size()); - } - - OGRLayer *GetLayer(int idx) override - { - return idx >= 0 && idx < GetLayerCount() ? m_layers[idx].get() - : nullptr; - } -}; - class GDALVectorClipAlgorithmLayer final : public OGRLayer, public OGRGetNextFeatureThroughRaw @@ -452,7 +428,7 @@ bool GDALVectorClipAlgorithm::RunStep(GDALProgressFunc, void *) return false; } - auto outDS = std::make_unique(); + auto outDS = std::make_unique(); outDS->SetDescription(poSrcDS->GetDescription()); bool ret = true; diff --git a/apps/gdalalg_vector_filter.cpp b/apps/gdalalg_vector_filter.cpp index 769e5e53108d..65620475c060 100644 --- a/apps/gdalalg_vector_filter.cpp +++ b/apps/gdalalg_vector_filter.cpp @@ -47,34 +47,6 @@ GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm(bool standaloneStep) namespace { -/************************************************************************/ -/* GDALVectorFilterAlgorithmDataset */ -/************************************************************************/ - -class GDALVectorFilterAlgorithmDataset final : public GDALDataset -{ - std::vector> m_layers{}; - - public: - GDALVectorFilterAlgorithmDataset() = default; - - void AddLayer(std::unique_ptr poLayer) - { - m_layers.push_back(std::move(poLayer)); - } - - int GetLayerCount() override - { - return static_cast(m_layers.size()); - } - - OGRLayer *GetLayer(int idx) override - { - return idx >= 0 && idx < GetLayerCount() ? m_layers[idx].get() - : nullptr; - } -}; - /************************************************************************/ /* GDALVectorFilterAlgorithmLayer */ /************************************************************************/ @@ -316,7 +288,7 @@ bool GDALVectorFilterAlgorithm::RunStep(GDALProgressFunc, void *) if (ret && !m_selectedFields.empty()) { - auto outDS = std::make_unique(); + auto outDS = std::make_unique(); outDS->SetDescription(poSrcDS->GetDescription()); for (int i = 0; i < nLayerCount; ++i) diff --git a/apps/gdalalg_vector_pipeline.h b/apps/gdalalg_vector_pipeline.h index 97564af4e04e..4bf66820e0f0 100644 --- a/apps/gdalalg_vector_pipeline.h +++ b/apps/gdalalg_vector_pipeline.h @@ -16,6 +16,8 @@ #include "gdalalgorithm.h" #include "gdalalg_abstract_pipeline.h" +#include "ogrsf_frmts.h" + //! @cond Doxygen_Suppress /************************************************************************/ @@ -106,6 +108,43 @@ class GDALVectorPipelineAlgorithm final } }; +/************************************************************************/ +/* GDALVectorPipelineOutputDataset */ +/************************************************************************/ + +/** Class used by vector pipeline steps to create an output on-the-fly + * dataset where they can store on-the-fly layers. + */ +class GDALVectorPipelineOutputDataset final : public GDALDataset +{ + std::vector> m_layersToDestroy{}; + std::vector m_layers{}; + + public: + GDALVectorPipelineOutputDataset() = default; + + void AddLayer(std::unique_ptr poLayer) + { + m_layersToDestroy.push_back(std::move(poLayer)); + m_layers.push_back(m_layersToDestroy.back().get()); + } + + void AddLayer(OGRLayer *poLayer) + { + m_layers.push_back(poLayer); + } + + int GetLayerCount() override + { + return static_cast(m_layers.size()); + } + + OGRLayer *GetLayer(int idx) override + { + return idx >= 0 && idx < GetLayerCount() ? m_layers[idx] : nullptr; + } +}; + //! @endcond #endif diff --git a/apps/gdalalg_vector_read.cpp b/apps/gdalalg_vector_read.cpp index ed8a4947d7bd..4af6c5e15d6e 100644 --- a/apps/gdalalg_vector_read.cpp +++ b/apps/gdalalg_vector_read.cpp @@ -32,36 +32,6 @@ GDALVectorReadAlgorithm::GDALVectorReadAlgorithm() AddInputArgs(/* hiddenForCLI = */ false); } -/************************************************************************/ -/* GDALVectorReadAlgorithmDataset */ -/************************************************************************/ - -namespace -{ -class GDALVectorReadAlgorithmDataset final : public GDALDataset -{ - std::vector m_srcLayers{}; - - public: - GDALVectorReadAlgorithmDataset() = default; - - void AddLayer(OGRLayer *poSrcLayer) - { - m_srcLayers.push_back(poSrcLayer); - } - - int GetLayerCount() override - { - return static_cast(m_srcLayers.size()); - } - - OGRLayer *GetLayer(int idx) override - { - return idx >= 0 && idx < GetLayerCount() ? m_srcLayers[idx] : nullptr; - } -}; -} // namespace - /************************************************************************/ /* GDALVectorReadAlgorithm::RunStep() */ /************************************************************************/ @@ -79,7 +49,7 @@ bool GDALVectorReadAlgorithm::RunStep(GDALProgressFunc, void *) else { auto poSrcDS = m_inputDataset.GetDatasetRef(); - auto poOutDS = std::make_unique(); + auto poOutDS = std::make_unique(); poOutDS->SetDescription(poSrcDS->GetDescription()); for (const auto &srcLayerName : m_inputLayerNames) { diff --git a/apps/gdalalg_vector_reproject.cpp b/apps/gdalalg_vector_reproject.cpp index 8d3fa8ef11c7..fccacbbe1f4b 100644 --- a/apps/gdalalg_vector_reproject.cpp +++ b/apps/gdalalg_vector_reproject.cpp @@ -40,37 +40,6 @@ GDALVectorReprojectAlgorithm::GDALVectorReprojectAlgorithm(bool standaloneStep) .AddHiddenAlias("t_srs"); } -/************************************************************************/ -/* GDALVectorReprojectAlgorithmDataset */ -/************************************************************************/ - -namespace -{ -class GDALVectorReprojectAlgorithmDataset final : public GDALDataset -{ - std::vector> m_layers{}; - - public: - GDALVectorReprojectAlgorithmDataset() = default; - - void AddLayer(std::unique_ptr poLayer) - { - m_layers.push_back(std::move(poLayer)); - } - - int GetLayerCount() override - { - return static_cast(m_layers.size()); - } - - OGRLayer *GetLayer(int idx) override - { - return idx >= 0 && idx < GetLayerCount() ? m_layers[idx].get() - : nullptr; - } -}; -} // namespace - /************************************************************************/ /* GDALVectorReprojectAlgorithm::RunStep() */ /************************************************************************/ @@ -96,7 +65,7 @@ bool GDALVectorReprojectAlgorithm::RunStep(GDALProgressFunc, void *) auto poSrcDS = m_inputDataset.GetDatasetRef(); auto reprojectedDataset = - std::make_unique(); + std::make_unique(); reprojectedDataset->SetDescription(poSrcDS->GetDescription()); const int nLayerCount = poSrcDS->GetLayerCount();