Skip to content

Commit fdc4a41

Browse files
authored
Python bindings: make alg['datatype'] accept a GDT_ constant (#12134)
1 parent f16911b commit fdc4a41

File tree

5 files changed

+64
-18
lines changed

5 files changed

+64
-18
lines changed

autotest/utilities/test_gdalalg_raster_astype.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,32 @@
1515

1616

1717
def get_astype_alg():
18-
reg = gdal.GetGlobalAlgorithmRegistry()
19-
raster = reg.InstantiateAlg("raster")
20-
return raster.InstantiateSubAlgorithm("astype")
18+
return gdal.GetGlobalAlgorithmRegistry()["raster"]["astype"]
2119

2220

2321
def test_gdalalg_raster_astype(tmp_vsimem):
2422

2523
out_filename = str(tmp_vsimem / "out.tif")
2624

2725
alg = get_astype_alg()
28-
assert alg.ParseRunAndFinalize(
29-
[
30-
"--datatype=UInt16",
31-
"../gcore/data/rgbsmall.tif",
32-
out_filename,
33-
],
34-
)
26+
alg["datatype"] = "UInt16"
27+
alg["input"] = "../gcore/data/rgbsmall.tif"
28+
alg["output"] = out_filename
29+
assert alg.Run() and alg.Finalize()
30+
31+
with gdal.OpenEx(out_filename) as ds:
32+
assert ds.GetRasterBand(1).DataType == gdal.GDT_UInt16
33+
34+
35+
def test_gdalalg_raster_astype_as_gdt(tmp_vsimem):
36+
37+
out_filename = str(tmp_vsimem / "out.tif")
38+
39+
alg = get_astype_alg()
40+
alg["datatype"] = gdal.GDT_UInt16
41+
alg["input"] = "../gcore/data/rgbsmall.tif"
42+
alg["output"] = out_filename
43+
assert alg.Run() and alg.Finalize()
3544

3645
with gdal.OpenEx(out_filename) as ds:
3746
assert ds.GetRasterBand(1).DataType == gdal.GDT_UInt16

gcore/gdalalgorithm.cpp

+31-7
Original file line numberDiff line numberDiff line change
@@ -2914,13 +2914,15 @@ GDALInConstructionAlgorithmArg &
29142914
GDALAlgorithm::AddOutputDataTypeArg(std::string *pValue,
29152915
const char *helpMessage)
29162916
{
2917-
auto &arg = AddArg(GDAL_ARG_NAME_OUTPUT_DATA_TYPE, 0,
2918-
MsgOrDefault(helpMessage, _("Output data type")), pValue)
2919-
.AddAlias("ot")
2920-
.AddAlias("datatype")
2921-
.SetChoices("Byte", "Int8", "UInt16", "Int16", "UInt32",
2922-
"Int32", "UInt64", "Int64", "CInt16", "CInt32",
2923-
"Float32", "Float64", "CFloat32", "CFloat64");
2917+
auto &arg =
2918+
AddArg(GDAL_ARG_NAME_OUTPUT_DATA_TYPE, 0,
2919+
MsgOrDefault(helpMessage, _("Output data type")), pValue)
2920+
.AddAlias("ot")
2921+
.AddAlias("datatype")
2922+
.AddMetadataItem("type", {"GDALDataType"})
2923+
.SetChoices("Byte", "Int8", "UInt16", "Int16", "UInt32", "Int32",
2924+
"UInt64", "Int64", "CInt16", "CInt32", "Float16",
2925+
"Float32", "Float64", "CFloat32", "CFloat64");
29242926
return arg;
29252927
}
29262928

@@ -4911,6 +4913,28 @@ char **GDALAlgorithmArgGetChoices(GDALAlgorithmArgH hArg)
49114913
return CPLStringList(hArg->ptr->GetChoices()).StealList();
49124914
}
49134915

4916+
/************************************************************************/
4917+
/* GDALAlgorithmArgGetMetadataItem() */
4918+
/************************************************************************/
4919+
4920+
/** Return the values of the metadata item of an argument.
4921+
*
4922+
* @param hArg Handle to an argument. Must NOT be null.
4923+
* @param pszItem Name of the item. Must NOT be null.
4924+
* @return a NULL terminated list of values, which must be destroyed with
4925+
* CSLDestroy()
4926+
4927+
* @since 3.11
4928+
*/
4929+
char **GDALAlgorithmArgGetMetadataItem(GDALAlgorithmArgH hArg,
4930+
const char *pszItem)
4931+
{
4932+
VALIDATE_POINTER1(hArg, __func__, nullptr);
4933+
VALIDATE_POINTER1(pszItem, __func__, nullptr);
4934+
const auto pVecOfStrings = hArg->ptr->GetMetadataItem(pszItem);
4935+
return pVecOfStrings ? CPLStringList(*pVecOfStrings).StealList() : nullptr;
4936+
}
4937+
49144938
/************************************************************************/
49154939
/* GDALAlgorithmArgIsExplicitlySet() */
49164940
/************************************************************************/

gcore/gdalalgorithm.h

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ bool CPL_DLL GDALAlgorithmArgGetRepeatedArgAllowed(GDALAlgorithmArgH);
152152

153153
char CPL_DLL **GDALAlgorithmArgGetChoices(GDALAlgorithmArgH);
154154

155+
char CPL_DLL **GDALAlgorithmArgGetMetadataItem(GDALAlgorithmArgH, const char *);
156+
155157
bool CPL_DLL GDALAlgorithmArgIsExplicitlySet(GDALAlgorithmArgH);
156158

157159
bool CPL_DLL GDALAlgorithmArgHasDefaultValue(GDALAlgorithmArgH);

swig/include/Algorithm.i

+6
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ public:
118118
}
119119
%clear char **;
120120

121+
%apply (char **CSL) {char **};
122+
char **GetMetadataItem(const char* item) {
123+
return GDALAlgorithmArgGetMetadataItem( self, item );
124+
}
125+
%clear char **;
126+
121127
bool IsExplicitlySet() {
122128
return GDALAlgorithmArgIsExplicitlySet(self);
123129
}

swig/include/python/gdal_python.i

+6-1
Original file line numberDiff line numberDiff line change
@@ -5557,7 +5557,12 @@ class VSIFile(BytesIO):
55575557
if type == GAAT_BOOLEAN:
55585558
return self.SetAsBoolean(value)
55595559
if type == GAAT_STRING:
5560-
if isinstance(value, str) or isinstance(value, int) or isinstance(value, float):
5560+
if isinstance(value, int):
5561+
if "GDALDataType" in self.GetMetadataItem("type") and value >= GDT_Byte and value < GDT_TypeCount:
5562+
return self.SetAsString(GetDataTypeName(value))
5563+
else:
5564+
return self.SetAsString(str(value))
5565+
elif isinstance(value, str) or isinstance(value, float):
55615566
return self.SetAsString(str(value))
55625567
else:
55635568
raise "Unexpected value type %s for an argument of type String" % str(type(value))

0 commit comments

Comments
 (0)