GDALAlgorithmArgDecl: Make SetChoices override previously-set values

This commit is contained in:
Dan Baston
2026-09-08 21:37:40 -04:00
parent 981de5eda7
commit 0e1a65c525
3 changed files with 28 additions and 21 deletions
+2 -13
View File
@@ -35,8 +35,9 @@ GDALRasterProximityAlgorithm::GDALRasterProximityAlgorithm(bool standaloneStep)
HELP_URL, standaloneStep)
{
AddOutputDataTypeArg(&m_outputDataType)
.SetChoices("Byte", "UInt16", "Int16", "UInt32", "Int32", "Float32",
.SetChoices("Int8", "UInt16", "Int16", "UInt32", "Int32", "Float32",
"Float64")
.SetHiddenChoices("Byte")
.SetDefault(m_outputDataType);
AddBandArg(&m_inputBand);
AddArg("target-values", 0, _("Target pixel values"), &m_targetPixelValues);
@@ -79,13 +80,6 @@ bool GDALRasterProximityAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
outputType = GDALGetDataTypeByName(m_outputDataType.c_str());
}
if (GDALDataTypeIsComplex(outputType))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Complex output types not supported");
return false;
}
auto poTmpDS = CreateTemporaryDataset(
poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize(), 1, outputType,
/* bTiledIfPossible = */ true, poSrcDS, /* bCopyMetadata = */ false);
@@ -145,11 +139,6 @@ bool GDALRasterProximityAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
{
dstBand->SetNoDataValue(std::numeric_limits<double>::quiet_NaN());
}
else if (outputType == GDT_Int64)
{
constexpr auto nMax = std::numeric_limits<int64_t>::max();
dstBand->SetNoDataValueAsInt64(nMax);
}
else
{
double dfNoData;
@@ -325,8 +325,6 @@ def test_gdalalg_raster_proximity_overwrite(tmp_vsimem):
gdal.GDT_UInt16,
gdal.GDT_Int32,
gdal.GDT_UInt32,
gdal.GDT_Int64,
gdal.GDT_Float16,
gdal.GDT_Float32,
gdal.GDT_Float64,
),
@@ -391,17 +389,22 @@ def test_gdalalg_raster_proximity_nodata_not_representable_as_float32(dt, nodata
@pytest.mark.parametrize(
"dt", (gdal.GDT_CFloat32, gdal.GDT_CFloat64), ids=gdal.GetDataTypeName
"dt",
(
gdal.GDT_Float16,
gdal.GDT_Int64,
gdal.GDT_UInt64,
gdal.GDT_CFloat32,
gdal.GDT_CFloat64,
),
ids=gdal.GetDataTypeName,
)
def test_gdalalg_raster_proximity_invalid_output_type(dt):
alg = get_alg()
alg["input"] = "../gcore/data/byte.tif"
alg["output-format"] = "MEM"
alg["output-data-type"] = gdal.GetDataTypeName(dt)
with pytest.raises(Exception, match="Complex output types not supported"):
assert alg.Run()
with pytest.raises(Exception, match="Invalid value .* for .* 'output-data-type"):
alg["output-data-type"] = gdal.GetDataTypeName(dt)
@pytest.mark.require_driver("GTiff")
+15
View File
@@ -565,6 +565,7 @@ class CPL_DLL GDALAlgorithmArgDecl final
//! @cond Doxygen_Suppress
GDALAlgorithmArgDecl &SetChoices()
{
m_choicesSet = true;
return *this;
}
@@ -579,6 +580,11 @@ class CPL_DLL GDALAlgorithmArgDecl final
bool>::type = true>
GDALAlgorithmArgDecl &SetChoices(T &&first, U &&...rest)
{
if (m_choicesSet)
{
m_choices.clear();
m_choicesSet = false;
}
m_choices.push_back(std::forward<T>(first));
SetChoices(std::forward<U>(rest)...);
return *this;
@@ -590,6 +596,7 @@ class CPL_DLL GDALAlgorithmArgDecl final
GDALAlgorithmArgDecl &SetChoices(const std::vector<std::string> &choices)
{
m_choices = choices;
m_choicesSet = true;
return *this;
}
@@ -654,6 +661,7 @@ class CPL_DLL GDALAlgorithmArgDecl final
//! @cond Doxygen_Suppress
GDALAlgorithmArgDecl &SetHiddenChoices()
{
m_hiddenChoicesSet = true;
return *this;
}
@@ -665,6 +673,11 @@ class CPL_DLL GDALAlgorithmArgDecl final
template <typename T, typename... U>
GDALAlgorithmArgDecl &SetHiddenChoices(T &&first, U &&...rest)
{
if (m_hiddenChoicesSet)
{
m_hiddenChoices.clear();
m_hiddenChoicesSet = false;
}
m_hiddenChoices.push_back(std::forward<T>(first));
SetHiddenChoices(std::forward<U>(rest)...);
return *this;
@@ -1247,6 +1260,8 @@ class CPL_DLL GDALAlgorithmArgDecl final
double m_maxVal = std::numeric_limits<double>::quiet_NaN();
bool m_minValIsIncluded = false;
bool m_maxValIsIncluded = false;
bool m_choicesSet = false;
bool m_hiddenChoicesSet = false;
int m_minCharCount = 0;
int m_maxCharCount = std::numeric_limits<int>::max();
GDALArgDatasetType m_datasetType =