Skip to content

Commit

Permalink
RFC104: add capability of 'filter' and 'reproject' vector pipeline st…
Browse files Browse the repository at this point in the history
…eps to be standalone too
  • Loading branch information
rouault committed Nov 25, 2024
1 parent 379978f commit f442483
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 32 deletions.
4 changes: 4 additions & 0 deletions apps/gdalalg_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "gdalalg_vector_info.h"
#include "gdalalg_vector_convert.h"
#include "gdalalg_vector_pipeline.h"
#include "gdalalg_vector_filter.h"
#include "gdalalg_vector_reproject.h"

/************************************************************************/
/* GDALVectorAlgorithm */
Expand All @@ -37,6 +39,8 @@ class GDALVectorAlgorithm final : public GDALAlgorithm
RegisterSubAlgorithm<GDALVectorInfoAlgorithm>();
RegisterSubAlgorithm<GDALVectorConvertAlgorithm>();
RegisterSubAlgorithm<GDALVectorPipelineAlgorithm>();
RegisterSubAlgorithm<GDALVectorFilterAlgorithmStandalone>();
RegisterSubAlgorithm<GDALVectorReprojectAlgorithmStandalone>();
}

private:
Expand Down
9 changes: 5 additions & 4 deletions apps/gdalalg_vector_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
/* GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm() */
/************************************************************************/

GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm()
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL)
GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm(bool standaloneStep)
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
standaloneStep)
{
auto &arg =
AddArg("bbox", 0, _("Bounding box as xmin,ymin,xmax,ymax"), &m_bbox)
Expand All @@ -51,10 +52,10 @@ GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm()
}

/************************************************************************/
/* GDALVectorFilterAlgorithm::RunImpl() */
/* GDALVectorFilterAlgorithm::RunStep() */
/************************************************************************/

bool GDALVectorFilterAlgorithm::RunImpl(GDALProgressFunc, void *)
bool GDALVectorFilterAlgorithm::RunStep(GDALProgressFunc, void *)
{
CPLAssert(m_inputDataset.GetDatasetRef());
CPLAssert(m_outputDataset.GetName().empty());
Expand Down
23 changes: 19 additions & 4 deletions apps/gdalalg_vector_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,41 @@
/* GDALVectorFilterAlgorithm */
/************************************************************************/

class GDALVectorFilterAlgorithm final : public GDALVectorPipelineStepAlgorithm
class GDALVectorFilterAlgorithm /* non final */
: public GDALVectorPipelineStepAlgorithm
{
public:
static constexpr const char *NAME = "filter";
static constexpr const char *DESCRIPTION = "Filter.";
static constexpr const char *DESCRIPTION = "Filter a vector dataset.";
static constexpr const char *HELP_URL = ""; // TODO

static std::vector<std::string> GetAliases()
{
return {};
}

GDALVectorFilterAlgorithm();
explicit GDALVectorFilterAlgorithm(bool standaloneStep = false);

private:
bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) override;
bool RunStep(GDALProgressFunc pfnProgress, void *pProgressData) override;

std::vector<double> m_bbox{};
};

/************************************************************************/
/* GDALVectorFilterAlgorithmStandalone */
/************************************************************************/

class GDALVectorFilterAlgorithmStandalone final
: public GDALVectorFilterAlgorithm
{
public:
GDALVectorFilterAlgorithmStandalone()
: GDALVectorFilterAlgorithm(/* standaloneStep = */ true)
{
}
};

//! @endcond

#endif /* GDALALG_VECTOR_FILTER_INCLUDED */
81 changes: 78 additions & 3 deletions apps/gdalalg_vector_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@
#define _(x) (x)
#endif

/************************************************************************/
/* GDALVectorPipelineStepAlgorithm::GDALVectorPipelineStepAlgorithm() */
/************************************************************************/

GDALVectorPipelineStepAlgorithm::GDALVectorPipelineStepAlgorithm(
const std::string &name, const std::string &description,
const std::string &helpURL, bool standaloneStep)
: GDALAlgorithm(name, description, helpURL),
m_standaloneStep(standaloneStep)
{
if (m_standaloneStep)
{
AddInputArgs(false);
AddProgressArg();
AddOutputArgs(false, false);
}
}

/************************************************************************/
/* GDALVectorPipelineStepAlgorithm::AddInputArgs() */
/************************************************************************/
Expand Down Expand Up @@ -81,12 +99,69 @@ void GDALVectorPipelineStepAlgorithm::AddOutputArgs(
.SetHiddenForCLI(hiddenForCLI);
}

/************************************************************************/
/* GDALVectorPipelineStepAlgorithm::RunImpl() */
/************************************************************************/

bool GDALVectorPipelineStepAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
{
if (m_standaloneStep)
{
GDALVectorReadAlgorithm readAlg;
for (auto &arg : readAlg.GetArgs())
{
auto stepArg = GetArg(arg->GetName());
if (stepArg && stepArg->IsExplicitlySet())
{
arg->SetSkipIfAlreadySet(true);
arg->SetFrom(*stepArg);
}
}

GDALVectorWriteAlgorithm writeAlg;
for (auto &arg : writeAlg.GetArgs())
{
auto stepArg = GetArg(arg->GetName());
if (stepArg && stepArg->IsExplicitlySet())
{
arg->SetSkipIfAlreadySet(true);
arg->SetFrom(*stepArg);
}
}

bool ret = false;
if (readAlg.Run())
{
m_inputDataset.Set(readAlg.m_outputDataset.GetDatasetRef());
m_outputDataset.Set(nullptr);
if (RunStep(nullptr, nullptr))
{
writeAlg.m_inputDataset.Set(m_outputDataset.GetDatasetRef());
if (writeAlg.Run(pfnProgress, pProgressData))
{
m_outputDataset.Set(
writeAlg.m_outputDataset.GetDatasetRef());
ret = true;
}
}
}

return ret;
}
else
{
return RunStep(pfnProgress, pProgressData);
}
}

/************************************************************************/
/* GDALVectorPipelineAlgorithm::GDALVectorPipelineAlgorithm() */
/************************************************************************/

GDALVectorPipelineAlgorithm::GDALVectorPipelineAlgorithm()
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL)
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
/*standaloneStep=*/false)
{
AddInputArgs(/* hiddenForCLI = */ true);
AddProgressArg();
Expand Down Expand Up @@ -369,10 +444,10 @@ bool GDALVectorPipelineAlgorithm::ParseCommandLineArguments(
}

/************************************************************************/
/* GDALVectorPipelineAlgorithm::RunImpl() */
/* GDALVectorPipelineAlgorithm::RunStep() */
/************************************************************************/

bool GDALVectorPipelineAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
bool GDALVectorPipelineAlgorithm::RunStep(GDALProgressFunc pfnProgress,
void *pProgressData)
{
if (m_steps.empty())
Expand Down
15 changes: 10 additions & 5 deletions apps/gdalalg_vector_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ class GDALVectorPipelineStepAlgorithm /* non final */ : public GDALAlgorithm
protected:
GDALVectorPipelineStepAlgorithm(const std::string &name,
const std::string &description,
const std::string &helpURL)
: GDALAlgorithm(name, description, helpURL)
{
}
const std::string &helpURL,
bool standaloneStep);

friend class GDALVectorPipelineAlgorithm;

virtual bool RunStep(GDALProgressFunc pfnProgress, void *pProgressData) = 0;

void AddInputArgs(bool hiddenForCLI);
void AddOutputArgs(bool hiddenForCLI, bool shortNameOutputLayerAllowed);

bool m_standaloneStep = false;

// Input arguments
GDALArgDatasetValue m_inputDataset{};
std::vector<std::string> m_openOptions{};
Expand All @@ -52,6 +54,9 @@ class GDALVectorPipelineStepAlgorithm /* non final */ : public GDALAlgorithm
bool m_overwriteLayer = false;
bool m_appendLayer = false;
std::string m_outputLayerName{};

private:
bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) override;
};

/************************************************************************/
Expand Down Expand Up @@ -102,7 +107,7 @@ class GDALVectorPipelineAlgorithm final : public GDALVectorPipelineStepAlgorithm
private:
std::string m_pipeline{};

bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) override;
bool RunStep(GDALProgressFunc pfnProgress, void *pProgressData) override;

std::unique_ptr<GDALVectorPipelineStepAlgorithm>
GetStepAlg(const std::string &name) const;
Expand Down
7 changes: 4 additions & 3 deletions apps/gdalalg_vector_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
/************************************************************************/

GDALVectorReadAlgorithm::GDALVectorReadAlgorithm()
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL)
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
/* standaloneStep =*/false)
{
AddInputArgs(/* hiddenForCLI = */ false);
}
Expand Down Expand Up @@ -62,10 +63,10 @@ class GDALVectorReadAlgorithmDataset final : public GDALDataset
} // namespace

/************************************************************************/
/* GDALVectorReadAlgorithm::RunImpl() */
/* GDALVectorReadAlgorithm::RunStep() */
/************************************************************************/

bool GDALVectorReadAlgorithm::RunImpl(GDALProgressFunc, void *)
bool GDALVectorReadAlgorithm::RunStep(GDALProgressFunc, void *)
{
CPLAssert(m_inputDataset.GetDatasetRef());
CPLAssert(m_outputDataset.GetName().empty());
Expand Down
2 changes: 1 addition & 1 deletion apps/gdalalg_vector_read.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GDALVectorReadAlgorithm final : public GDALVectorPipelineStepAlgorithm
GDALVectorReadAlgorithm();

private:
bool RunImpl(GDALProgressFunc, void *) override;
bool RunStep(GDALProgressFunc, void *) override;
};

//! @endcond
Expand Down
9 changes: 5 additions & 4 deletions apps/gdalalg_vector_reproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
/* GDALVectorReprojectAlgorithm::GDALVectorReprojectAlgorithm() */
/************************************************************************/

GDALVectorReprojectAlgorithm::GDALVectorReprojectAlgorithm()
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL)
GDALVectorReprojectAlgorithm::GDALVectorReprojectAlgorithm(bool standaloneStep)
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
standaloneStep)
{
AddArg("src-crs", 's', _("Source CRS"), &m_srsCrs).AddHiddenAlias("s_srs");
AddArg("dst-crs", 'd', _("Destination CRS"), &m_dstCrs)
Expand Down Expand Up @@ -68,10 +69,10 @@ class GDALVectorReprojectAlgorithmDataset final : public GDALDataset
} // namespace

/************************************************************************/
/* GDALVectorReprojectAlgorithm::RunImpl() */
/* GDALVectorReprojectAlgorithm::RunStep() */
/************************************************************************/

bool GDALVectorReprojectAlgorithm::RunImpl(GDALProgressFunc, void *)
bool GDALVectorReprojectAlgorithm::RunStep(GDALProgressFunc, void *)
{
CPLAssert(m_inputDataset.GetDatasetRef());
CPLAssert(m_outputDataset.GetName().empty());
Expand Down
22 changes: 18 additions & 4 deletions apps/gdalalg_vector_reproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,42 @@
/* GDALVectorReprojectAlgorithm */
/************************************************************************/

class GDALVectorReprojectAlgorithm final
class GDALVectorReprojectAlgorithm /* non final */
: public GDALVectorPipelineStepAlgorithm
{
public:
static constexpr const char *NAME = "reproject";
static constexpr const char *DESCRIPTION = "Reproject.";
static constexpr const char *DESCRIPTION = "Reproject a vector dataset.";
static constexpr const char *HELP_URL = ""; // TODO

static std::vector<std::string> GetAliases()
{
return {};
}

GDALVectorReprojectAlgorithm();
explicit GDALVectorReprojectAlgorithm(bool standaloneStep = false);

private:
bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) override;
bool RunStep(GDALProgressFunc pfnProgress, void *pProgressData) override;

std::string m_srsCrs{};
std::string m_dstCrs{};
};

/************************************************************************/
/* GDALVectorReprojectAlgorithmStandalone */
/************************************************************************/

class GDALVectorReprojectAlgorithmStandalone final
: public GDALVectorReprojectAlgorithm
{
public:
GDALVectorReprojectAlgorithmStandalone()
: GDALVectorReprojectAlgorithm(/* standaloneStep = */ true)
{
}
};

//! @endcond

#endif /* GDALALG_VECTOR_REPROJECT_INCLUDED */
7 changes: 4 additions & 3 deletions apps/gdalalg_vector_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@
/************************************************************************/

GDALVectorWriteAlgorithm::GDALVectorWriteAlgorithm()
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL)
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
/* standaloneStep =*/false)
{
AddOutputArgs(/* hiddenForCLI = */ false,
/* shortNameOutputLayerAllowed=*/true);
}

/************************************************************************/
/* GDALVectorWriteAlgorithm::RunImpl() */
/* GDALVectorWriteAlgorithm::RunStep() */
/************************************************************************/

bool GDALVectorWriteAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
bool GDALVectorWriteAlgorithm::RunStep(GDALProgressFunc pfnProgress,
void *pProgressData)
{
CPLAssert(m_inputDataset.GetDatasetRef());
Expand Down
2 changes: 1 addition & 1 deletion apps/gdalalg_vector_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GDALVectorWriteAlgorithm final : public GDALVectorPipelineStepAlgorithm
GDALVectorWriteAlgorithm();

private:
bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) override;
bool RunStep(GDALProgressFunc pfnProgress, void *pProgressData) override;
};

//! @endcond
Expand Down
Loading

0 comments on commit f442483

Please sign in to comment.