Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python bindings: add syntaxic sugar to get/set algorithm arguments + doc #11822

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions autotest/utilities/test_gdalalg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@


def get_info_alg():
reg = gdal.GetGlobalAlgorithmRegistry()
return reg.InstantiateAlg("info")
return gdal.GetGlobalAlgorithmRegistry()["info"]


@pytest.mark.parametrize(
Expand All @@ -31,7 +30,7 @@ def get_info_alg():
def test_gdalalg_info_on_raster(args):
info = get_info_alg()
assert info.ParseRunAndFinalize(args)
output_string = info.GetActualAlgorithm().GetArg("output-string").Get()
output_string = info["output-string"]
assert output_string.startswith("Driver: GTiff/GeoTIFF")


Expand All @@ -57,7 +56,7 @@ def test_gdalalg_info_on_raster_invalid_arg():
def test_gdalalg_info_on_vector(args):
info = get_info_alg()
assert info.ParseRunAndFinalize(args)
output_string = info.GetActualAlgorithm().GetArg("output-string").Get()
output_string = info["output-string"]
assert output_string.startswith("INFO: Open of")


Expand All @@ -75,8 +74,7 @@ def test_gdalalg_info_invalid_arg():

def test_gdalalg_info_run_cannot_be_run():
info = get_info_alg()
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
info.GetArg("input").SetDataset(ds)
info["input"] = gdal.GetDriverByName("MEM").Create("", 1, 1)
with pytest.raises(Exception, match="method should not be called directly"):
info.Run()

Expand Down Expand Up @@ -108,6 +106,6 @@ def test_gdalalg_info_mixed_raster_vector_with_invalid_arg(tmp_vsimem):
def test_gdalalg_info_mixed_run_without_arg(tmp_vsimem):

info = get_info_alg()
info.GetArg("input").Get().SetName("data/utmsmall.tif")
info["input"] = "data/utmsmall.tif"
with pytest.raises(Exception, match="should not be called directly"):
assert info.Run()
5 changes: 2 additions & 3 deletions autotest/utilities/test_gdalalg_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@


def get_pipeline_alg():
reg = gdal.GetGlobalAlgorithmRegistry()
return reg.InstantiateAlg("pipeline")
return gdal.GetGlobalAlgorithmRegistry()["pipeline"]


def test_gdalalg_pipeline_read_and_write(tmp_vsimem):
Expand All @@ -44,6 +43,6 @@ def my_progress(pct, msg, user_data):
def test_gdalalg_pipeline_mixed_run_without_arg(tmp_vsimem):

pipeline = get_pipeline_alg()
pipeline.GetArg("input").Get().SetDataset(gdal.OpenEx("../ogr/data/poly.shp"))
pipeline["input"] = gdal.OpenEx("../ogr/data/poly.shp")
with pytest.raises(Exception, match="should not be called directly"):
assert pipeline.Run()
5 changes: 2 additions & 3 deletions autotest/utilities/test_gdalalg_raster_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_gdalalg_raster_convert_to_mem():
convert = get_convert_alg()
assert convert.ParseCommandLineArguments(["--of=MEM", "data/utmsmall.tif", ""])
assert convert.Run()
out_ds = convert.GetArg("output").Get().GetDataset()
out_ds = convert["output"].GetDataset()
assert out_ds.GetRasterBand(1).Checksum() == 50054


Expand Down Expand Up @@ -82,8 +82,7 @@ def test_gdalalg_raster_convert_append(tmp_vsimem):

def test_gdalalg_raster_convert_error_output_already_set():
convert = get_convert_alg()
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
convert.GetArg("output").Get().SetDataset(ds)
convert["output"] = gdal.GetDriverByName("MEM").Create("", 1, 1)
assert convert.ParseCommandLineArguments(["data/utmsmall.tif"])
with pytest.raises(
Exception,
Expand Down
6 changes: 2 additions & 4 deletions autotest/utilities/test_gdalalg_raster_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@


def get_edit_alg():
reg = gdal.GetGlobalAlgorithmRegistry()
raster = reg.InstantiateAlg("raster")
return raster.InstantiateSubAlgorithm("edit")
return gdal.GetGlobalAlgorithmRegistry()["raster"]["edit"]


def test_gdalalg_raster_edit_read_only(tmp_vsimem):
Expand All @@ -28,7 +26,7 @@ def test_gdalalg_raster_edit_read_only(tmp_vsimem):
gdal.FileFromMemBuffer(tmp_filename, open("../gcore/data/byte.tif", "rb").read())

pipeline = get_edit_alg()
pipeline.GetArg("dataset").Set(gdal.OpenEx(tmp_filename))
pipeline["dataset"] = gdal.OpenEx(tmp_filename)
with pytest.raises(
Exception, match="edit: Dataset should be opened in update mode"
):
Expand Down
34 changes: 16 additions & 18 deletions autotest/utilities/test_gdalalg_raster_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@


def get_info_alg():
reg = gdal.GetGlobalAlgorithmRegistry()
raster = reg.InstantiateAlg("raster")
return raster.InstantiateSubAlgorithm("info")
return gdal.GetGlobalAlgorithmRegistry()["raster"]["info"]


def test_gdalalg_raster_info_stdout():
Expand All @@ -41,7 +39,7 @@ def test_gdalalg_raster_info_stdout():
def test_gdalalg_raster_info():
info = get_info_alg()
assert info.ParseRunAndFinalize(["--format=text", "data/utmsmall.tif"])
output_string = info.GetArg("output-string").Get()
output_string = info["output-string"]
assert output_string.startswith("Driver: GTiff/GeoTIFF")


Expand All @@ -50,45 +48,45 @@ def test_gdalalg_raster_info_mm_checksum():
assert info.ParseRunAndFinalize(
["--format=text", "--mm", "--checksum", "data/utmsmall.tif"]
)
output_string = info.GetArg("output-string").Get()
output_string = info["output-string"]
assert " Computed Min/Max=0.000,255.000" in output_string
assert "Checksum=" in output_string


def test_gdalalg_raster_info_stats():
info = get_info_alg()
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
info.GetArg("input").SetDataset(ds)
info["input"] = ds
assert info.ParseRunAndFinalize(["--stats"])
output_string = info.GetArg("output-string").Get()
output_string = info["output-string"]
j = json.loads(output_string)
assert "stdDev" in j["bands"][0]


def test_gdalalg_raster_info_approx_stats():
info = get_info_alg()
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
info.GetArg("input").SetDataset(ds)
info["input"] = ds
assert info.ParseRunAndFinalize(["--approx-stats"])
output_string = info.GetArg("output-string").Get()
output_string = info["output-string"]
j = json.loads(output_string)
assert "stdDev" in j["bands"][0]


def test_gdalalg_raster_info_hist():
info = get_info_alg()
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
info.GetArg("input").SetDataset(ds)
info["input"] = ds
assert info.ParseRunAndFinalize(["--hist"])
output_string = info.GetArg("output-string").Get()
output_string = info["output-string"]
j = json.loads(output_string)
assert "histogram" in j["bands"][0]


def test_gdalalg_raster_info_no_options():
info = get_info_alg()
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
info.GetArg("input").SetDataset(ds)
info["input"] = ds
assert info.ParseRunAndFinalize(
["--no-gcp", "--no-md", "--no-ct", "--no-fl", "--no-nodata", "--no-mask"]
)
Expand All @@ -98,9 +96,9 @@ def test_gdalalg_raster_info_list_mdd():
info = get_info_alg()
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
ds.SetMetadataItem("foo", "bar", "MY_DOMAIN")
info.GetArg("input").SetDataset(ds)
info["input"] = ds
assert info.ParseRunAndFinalize(["--list-mdd"])
output_string = info.GetArg("output-string").Get()
output_string = info["output-string"]
j = json.loads(output_string)
assert "MY_DOMAIN" in j["metadata"]["metadataDomains"]

Expand All @@ -109,9 +107,9 @@ def test_gdalalg_raster_info_mdd_all():
info = get_info_alg()
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
ds.SetMetadataItem("foo", "bar", "MY_DOMAIN")
info.GetArg("input").SetDataset(ds)
info["input"] = ds
assert info.ParseRunAndFinalize(["--mdd=all"])
output_string = info.GetArg("output-string").Get()
output_string = info["output-string"]
j = json.loads(output_string)
assert j["metadata"] == {
"": {"AREA_OR_POINT": "Area"},
Expand All @@ -128,7 +126,7 @@ def test_gdalalg_raster_info_list_subdataset():
assert info.ParseRunAndFinalize(
["--input=../gcore/data/tiff_with_subifds.tif", "--subdataset=2"]
)
output_string = info.GetArg("output-string").Get()
output_string = info["output-string"]
j = json.loads(output_string)
assert j["description"] == "GTIFF_DIR:2:../gcore/data/tiff_with_subifds.tif"

Expand All @@ -149,7 +147,7 @@ def test_gdalalg_raster_info_list_subdataset_error_cannot_open_subdataset():
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
ds.SetMetadataItem("SUBDATASET_1_DESC", "desc", "SUBDATASETS")
ds.SetMetadataItem("SUBDATASET_1_NAME", "i_do_not_exist", "SUBDATASETS")
info.GetArg("input").SetDataset(ds)
info["input"] = ds
with pytest.raises(
Exception,
match="i_do_not_exist",
Expand Down
Loading
Loading