Skip to content

Commit

Permalink
Factor out a GDALVectorPipelineOutputDataset class
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Feb 6, 2025
1 parent 4b09e61 commit 85e0ee0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 117 deletions.
26 changes: 1 addition & 25 deletions apps/gdalalg_vector_clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,6 @@ GDALVectorClipAlgorithm::GDALVectorClipAlgorithm(bool standaloneStep)

namespace
{
class GDALVectorClipAlgorithmDataset final : public GDALDataset
{
std::vector<std::unique_ptr<OGRLayer>> m_layers{};

public:
GDALVectorClipAlgorithmDataset() = default;

void AddLayer(std::unique_ptr<OGRLayer> poLayer)
{
m_layers.push_back(std::move(poLayer));
}

int GetLayerCount() override
{
return static_cast<int>(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<GDALVectorClipAlgorithmLayer>
Expand Down Expand Up @@ -452,7 +428,7 @@ bool GDALVectorClipAlgorithm::RunStep(GDALProgressFunc, void *)
return false;
}

auto outDS = std::make_unique<GDALVectorClipAlgorithmDataset>();
auto outDS = std::make_unique<GDALVectorPipelineOutputDataset>();
outDS->SetDescription(poSrcDS->GetDescription());

bool ret = true;
Expand Down
30 changes: 1 addition & 29 deletions apps/gdalalg_vector_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,6 @@ GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm(bool standaloneStep)
namespace
{

/************************************************************************/
/* GDALVectorFilterAlgorithmDataset */
/************************************************************************/

class GDALVectorFilterAlgorithmDataset final : public GDALDataset
{
std::vector<std::unique_ptr<OGRLayer>> m_layers{};

public:
GDALVectorFilterAlgorithmDataset() = default;

void AddLayer(std::unique_ptr<OGRLayer> poLayer)
{
m_layers.push_back(std::move(poLayer));
}

int GetLayerCount() override
{
return static_cast<int>(m_layers.size());
}

OGRLayer *GetLayer(int idx) override
{
return idx >= 0 && idx < GetLayerCount() ? m_layers[idx].get()
: nullptr;
}
};

/************************************************************************/
/* GDALVectorFilterAlgorithmLayer */
/************************************************************************/
Expand Down Expand Up @@ -316,7 +288,7 @@ bool GDALVectorFilterAlgorithm::RunStep(GDALProgressFunc, void *)

if (ret && !m_selectedFields.empty())
{
auto outDS = std::make_unique<GDALVectorFilterAlgorithmDataset>();
auto outDS = std::make_unique<GDALVectorPipelineOutputDataset>();
outDS->SetDescription(poSrcDS->GetDescription());

for (int i = 0; i < nLayerCount; ++i)
Expand Down
39 changes: 39 additions & 0 deletions apps/gdalalg_vector_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "gdalalgorithm.h"
#include "gdalalg_abstract_pipeline.h"

#include "ogrsf_frmts.h"

//! @cond Doxygen_Suppress

/************************************************************************/
Expand Down Expand Up @@ -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<std::unique_ptr<OGRLayer>> m_layersToDestroy{};
std::vector<OGRLayer *> m_layers{};

public:
GDALVectorPipelineOutputDataset() = default;

void AddLayer(std::unique_ptr<OGRLayer> 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<int>(m_layers.size());
}

OGRLayer *GetLayer(int idx) override
{
return idx >= 0 && idx < GetLayerCount() ? m_layers[idx] : nullptr;
}
};

//! @endcond

#endif
32 changes: 1 addition & 31 deletions apps/gdalalg_vector_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,6 @@ GDALVectorReadAlgorithm::GDALVectorReadAlgorithm()
AddInputArgs(/* hiddenForCLI = */ false);
}

/************************************************************************/
/* GDALVectorReadAlgorithmDataset */
/************************************************************************/

namespace
{
class GDALVectorReadAlgorithmDataset final : public GDALDataset
{
std::vector<OGRLayer *> m_srcLayers{};

public:
GDALVectorReadAlgorithmDataset() = default;

void AddLayer(OGRLayer *poSrcLayer)
{
m_srcLayers.push_back(poSrcLayer);
}

int GetLayerCount() override
{
return static_cast<int>(m_srcLayers.size());
}

OGRLayer *GetLayer(int idx) override
{
return idx >= 0 && idx < GetLayerCount() ? m_srcLayers[idx] : nullptr;
}
};
} // namespace

/************************************************************************/
/* GDALVectorReadAlgorithm::RunStep() */
/************************************************************************/
Expand All @@ -79,7 +49,7 @@ bool GDALVectorReadAlgorithm::RunStep(GDALProgressFunc, void *)
else
{
auto poSrcDS = m_inputDataset.GetDatasetRef();
auto poOutDS = std::make_unique<GDALVectorReadAlgorithmDataset>();
auto poOutDS = std::make_unique<GDALVectorPipelineOutputDataset>();
poOutDS->SetDescription(poSrcDS->GetDescription());
for (const auto &srcLayerName : m_inputLayerNames)
{
Expand Down
33 changes: 1 addition & 32 deletions apps/gdalalg_vector_reproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,6 @@ GDALVectorReprojectAlgorithm::GDALVectorReprojectAlgorithm(bool standaloneStep)
.AddHiddenAlias("t_srs");
}

/************************************************************************/
/* GDALVectorReprojectAlgorithmDataset */
/************************************************************************/

namespace
{
class GDALVectorReprojectAlgorithmDataset final : public GDALDataset
{
std::vector<std::unique_ptr<OGRLayer>> m_layers{};

public:
GDALVectorReprojectAlgorithmDataset() = default;

void AddLayer(std::unique_ptr<OGRLayer> poLayer)
{
m_layers.push_back(std::move(poLayer));
}

int GetLayerCount() override
{
return static_cast<int>(m_layers.size());
}

OGRLayer *GetLayer(int idx) override
{
return idx >= 0 && idx < GetLayerCount() ? m_layers[idx].get()
: nullptr;
}
};
} // namespace

/************************************************************************/
/* GDALVectorReprojectAlgorithm::RunStep() */
/************************************************************************/
Expand All @@ -96,7 +65,7 @@ bool GDALVectorReprojectAlgorithm::RunStep(GDALProgressFunc, void *)
auto poSrcDS = m_inputDataset.GetDatasetRef();

auto reprojectedDataset =
std::make_unique<GDALVectorReprojectAlgorithmDataset>();
std::make_unique<GDALVectorPipelineOutputDataset>();
reprojectedDataset->SetDescription(poSrcDS->GetDescription());

const int nLayerCount = poSrcDS->GetLayerCount();
Expand Down

0 comments on commit 85e0ee0

Please sign in to comment.