From e2989901c21010d86f531354b1b39f932dbf2772 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 5 Feb 2025 22:22:09 +0100 Subject: [PATCH] gdal vector pipeline steps in standalone mode: allow --of=stream --- apps/gdalalg_vector_pipeline.cpp | 18 +++++++++++++----- gcore/gdalalgorithm.cpp | 27 ++++++++++++++++++--------- gcore/gdalalgorithm.h | 5 +++-- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/apps/gdalalg_vector_pipeline.cpp b/apps/gdalalg_vector_pipeline.cpp index eba4c0ae10ab..dc26d831f9d9 100644 --- a/apps/gdalalg_vector_pipeline.cpp +++ b/apps/gdalalg_vector_pipeline.cpp @@ -72,7 +72,7 @@ void GDALVectorPipelineStepAlgorithm::AddInputArgs(bool hiddenForCLI) void GDALVectorPipelineStepAlgorithm::AddOutputArgs( bool hiddenForCLI, bool shortNameOutputLayerAllowed) { - AddOutputFormatArg(&m_format) + AddOutputFormatArg(&m_format, true) .AddMetadataItem(GAAMDI_REQUIRED_CAPABILITIES, {GDAL_DCAP_VECTOR, GDAL_DCAP_CREATE}) .SetHiddenForCLI(hiddenForCLI); @@ -138,13 +138,21 @@ bool GDALVectorPipelineStepAlgorithm::RunImpl(GDALProgressFunc pfnProgress, m_outputDataset.Set(nullptr); if (RunStep(nullptr, nullptr)) { - writeAlg.m_inputDataset.Set(m_outputDataset.GetDatasetRef()); - if (writeAlg.Run(pfnProgress, pProgressData)) + if (m_format == "stream") { - m_outputDataset.Set( - writeAlg.m_outputDataset.GetDatasetRef()); ret = true; } + else + { + writeAlg.m_inputDataset.Set( + m_outputDataset.GetDatasetRef()); + if (writeAlg.Run(pfnProgress, pProgressData)) + { + m_outputDataset.Set( + writeAlg.m_outputDataset.GetDatasetRef()); + ret = true; + } + } } } diff --git a/gcore/gdalalgorithm.cpp b/gcore/gdalalgorithm.cpp index 0ddf208f13f8..5d47ad0f14fc 100644 --- a/gcore/gdalalgorithm.cpp +++ b/gcore/gdalalgorithm.cpp @@ -2328,12 +2328,17 @@ GDALAlgorithm::AddOpenOptionsArg(std::vector *pValue) /* ValidateFormat() */ /************************************************************************/ -bool GDALAlgorithm::ValidateFormat(const GDALAlgorithmArg &arg) const +bool GDALAlgorithm::ValidateFormat(const GDALAlgorithmArg &arg, + bool bStreamAllowed) const { if (arg.GetChoices().empty()) { - const auto Validate = [this, &arg](const std::string &val) + const auto Validate = + [this, &arg, bStreamAllowed](const std::string &val) { + if (bStreamAllowed && val == "stream") + return true; + auto hDriver = GDALGetDriverByName(val.c_str()); if (!hDriver) { @@ -2451,7 +2456,8 @@ GDALAlgorithm::AddInputFormatsArg(std::vector *pValue) AddArg(GDAL_ARG_NAME_INPUT_FORMAT, 0, _("Input formats"), pValue) .AddAlias("if") .SetCategory(GAAC_ADVANCED); - arg.AddValidationAction([this, &arg]() { return ValidateFormat(arg); }); + arg.AddValidationAction([this, &arg]() + { return ValidateFormat(arg, false); }); arg.SetAutoCompleteFunction([&arg](const std::string &) { return FormatAutoCompleteFunction(arg); }); return arg; @@ -2462,13 +2468,16 @@ GDALAlgorithm::AddInputFormatsArg(std::vector *pValue) /************************************************************************/ GDALInConstructionAlgorithmArg & -GDALAlgorithm::AddOutputFormatArg(std::string *pValue) +GDALAlgorithm::AddOutputFormatArg(std::string *pValue, bool bStreamAllowed) { - auto &arg = - AddArg(GDAL_ARG_NAME_OUTPUT_FORMAT, 'f', _("Output format"), pValue) - .AddAlias("of") - .AddAlias("format"); - arg.AddValidationAction([this, &arg]() { return ValidateFormat(arg); }); + auto &arg = AddArg(GDAL_ARG_NAME_OUTPUT_FORMAT, 'f', + bStreamAllowed ? _("Output format (\"stream\" allowed)") + : _("Output format"), + pValue) + .AddAlias("of") + .AddAlias("format"); + arg.AddValidationAction([this, &arg, bStreamAllowed]() + { return ValidateFormat(arg, bStreamAllowed); }); arg.SetAutoCompleteFunction([&arg](const std::string &) { return FormatAutoCompleteFunction(arg); }); return arg; diff --git a/gcore/gdalalgorithm.h b/gcore/gdalalgorithm.h index be7019bbf297..0399e8e9f409 100644 --- a/gcore/gdalalgorithm.h +++ b/gcore/gdalalgorithm.h @@ -2153,7 +2153,8 @@ class CPL_DLL GDALAlgorithmRegistry GDALInConstructionAlgorithmArg &AddOutputStringArg(std::string *pValue); /** Add output format argument. */ - GDALInConstructionAlgorithmArg &AddOutputFormatArg(std::string *pValue); + GDALInConstructionAlgorithmArg & + AddOutputFormatArg(std::string *pValue, bool bStreamAllowed = false); /** Add creation option(s) argument. */ GDALInConstructionAlgorithmArg & @@ -2241,7 +2242,7 @@ class CPL_DLL GDALAlgorithmRegistry std::vector, std::vector>> &inConstructionValues); - bool ValidateFormat(const GDALAlgorithmArg &arg) const; + bool ValidateFormat(const GDALAlgorithmArg &arg, bool bStreamAllowed) const; virtual bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) = 0;