diff --git a/autotest/utilities/test_gdalalg_info.py b/autotest/utilities/test_gdalalg_info.py index 94d493a67e7d..1f3d157ac4b0 100755 --- a/autotest/utilities/test_gdalalg_info.py +++ b/autotest/utilities/test_gdalalg_info.py @@ -17,8 +17,7 @@ def get_info_alg(): - reg = gdal.GetGlobalAlgorithmRegistry() - return reg.InstantiateAlg("info") + return gdal.GetGlobalAlgorithmRegistry()["info"] @pytest.mark.parametrize( @@ -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") @@ -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") @@ -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() @@ -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() diff --git a/autotest/utilities/test_gdalalg_pipeline.py b/autotest/utilities/test_gdalalg_pipeline.py index 6e3f80b898b4..5f0468b421ee 100755 --- a/autotest/utilities/test_gdalalg_pipeline.py +++ b/autotest/utilities/test_gdalalg_pipeline.py @@ -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): @@ -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() diff --git a/autotest/utilities/test_gdalalg_raster_convert.py b/autotest/utilities/test_gdalalg_raster_convert.py index a774972f1645..739ad35eebd0 100755 --- a/autotest/utilities/test_gdalalg_raster_convert.py +++ b/autotest/utilities/test_gdalalg_raster_convert.py @@ -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 @@ -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, diff --git a/autotest/utilities/test_gdalalg_raster_edit.py b/autotest/utilities/test_gdalalg_raster_edit.py index d85556a8f36a..0f24976fd4f9 100755 --- a/autotest/utilities/test_gdalalg_raster_edit.py +++ b/autotest/utilities/test_gdalalg_raster_edit.py @@ -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): @@ -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" ): diff --git a/autotest/utilities/test_gdalalg_raster_info.py b/autotest/utilities/test_gdalalg_raster_info.py index 18a8857c9cd7..f4a10b88333c 100755 --- a/autotest/utilities/test_gdalalg_raster_info.py +++ b/autotest/utilities/test_gdalalg_raster_info.py @@ -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(): @@ -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") @@ -50,7 +48,7 @@ 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 @@ -58,9 +56,9 @@ def test_gdalalg_raster_info_mm_checksum(): 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] @@ -68,9 +66,9 @@ def test_gdalalg_raster_info_stats(): 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] @@ -78,9 +76,9 @@ def test_gdalalg_raster_info_approx_stats(): 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] @@ -88,7 +86,7 @@ def test_gdalalg_raster_info_hist(): 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"] ) @@ -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"] @@ -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"}, @@ -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" @@ -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", diff --git a/autotest/utilities/test_gdalalg_raster_mosaic.py b/autotest/utilities/test_gdalalg_raster_mosaic.py index 414d871a9f07..4100b53aef3c 100755 --- a/autotest/utilities/test_gdalalg_raster_mosaic.py +++ b/autotest/utilities/test_gdalalg_raster_mosaic.py @@ -17,27 +17,23 @@ def get_mosaic_alg(): - reg = gdal.GetGlobalAlgorithmRegistry() - raster = reg.InstantiateAlg("raster") - return raster.InstantiateSubAlgorithm("mosaic") + return gdal.GetGlobalAlgorithmRegistry()["raster"]["mosaic"] def test_gdalalg_raster_mosaic_from_dataset_handle(): alg = get_mosaic_alg() - alg.GetArg("input").Set( - [ - gdal.Translate( - "", "../gcore/data/byte.tif", options="-f MEM -srcwin 0 0 10 20" - ), - gdal.Translate( - "", "../gcore/data/byte.tif", options="-f MEM -srcwin 10 0 10 20" - ), - ] - ) - alg.GetArg("output").Set("") + alg["input"] = [ + gdal.Translate( + "", "../gcore/data/byte.tif", options="-f MEM -srcwin 0 0 10 20" + ), + gdal.Translate( + "", "../gcore/data/byte.tif", options="-f MEM -srcwin 10 0 10 20" + ), + ] + alg["output"] = "" assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterXSize == 20 assert ds.RasterYSize == 20 assert ds.GetGeoTransform() == pytest.approx( @@ -49,10 +45,10 @@ def test_gdalalg_raster_mosaic_from_dataset_handle(): def test_gdalalg_raster_mosaic_from_dataset_name(): alg = get_mosaic_alg() - alg.GetArg("input").Set(["../gcore/data/byte.tif"]) - alg.GetArg("output").Set("") + alg["input"] = ["../gcore/data/byte.tif"] + alg["output"] = "" assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterXSize == 20 assert ds.RasterYSize == 20 assert ds.GetGeoTransform() == pytest.approx( @@ -97,7 +93,7 @@ def test_gdalalg_raster_mosaic_bbox(): ] ) assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterXSize == 18 assert ds.RasterYSize == 18 assert ds.GetGeoTransform() == pytest.approx( @@ -114,11 +110,11 @@ def test_gdalalg_raster_mosaic_resolution_average(): src2_ds.SetGeoTransform([3, 0.5, 0, 49, 0, -0.5]) alg = get_mosaic_alg() - alg.GetArg("input").Set([src1_ds, src2_ds]) - alg.GetArg("resolution").Set("average") - alg.GetArg("output").Set("") + alg["input"] = [src1_ds, src2_ds] + alg["resolution"] = "average" + alg["output"] = "" assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterXSize == 3 assert ds.RasterYSize == 1 assert ds.GetGeoTransform() == pytest.approx((2.0, 0.75, 0.0, 49.0, 0.0, -0.75)) @@ -132,11 +128,11 @@ def test_gdalalg_raster_mosaic_resolution_highest(): src2_ds.SetGeoTransform([3, 0.5, 0, 49, 0, -0.5]) alg = get_mosaic_alg() - alg.GetArg("input").Set([src1_ds, src2_ds]) - alg.GetArg("output").Set("") - alg.GetArg("resolution").Set("highest") + alg["input"] = [src1_ds, src2_ds] + alg["output"] = "" + alg["resolution"] = "highest" assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterXSize == 4 assert ds.RasterYSize == 2 assert ds.GetGeoTransform() == pytest.approx((2.0, 0.5, 0.0, 49.0, 0.0, -0.5)) @@ -150,11 +146,11 @@ def test_gdalalg_raster_mosaic_resolution_lowest(): src2_ds.SetGeoTransform([3, 0.5, 0, 49, 0, -0.5]) alg = get_mosaic_alg() - alg.GetArg("input").Set([src1_ds, src2_ds]) - alg.GetArg("output").Set("") - alg.GetArg("resolution").Set("lowest") + alg["input"] = [src1_ds, src2_ds] + alg["output"] = "" + alg["resolution"] = "lowest" assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterXSize == 2 assert ds.RasterYSize == 1 assert ds.GetGeoTransform() == pytest.approx((2.0, 1.0, 0.0, 49.0, 0.0, -1.0)) @@ -168,11 +164,11 @@ def test_gdalalg_raster_mosaic_resolution_custom(): src2_ds.SetGeoTransform([3, 0.5, 0, 49, 0, -0.5]) alg = get_mosaic_alg() - alg.GetArg("input").Set([src1_ds, src2_ds]) - alg.GetArg("output").Set("") - alg.GetArg("resolution").Set("0.5,1") + alg["input"] = [src1_ds, src2_ds] + alg["output"] = "" + alg["resolution"] = "0.5,1" assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterXSize == 4 assert ds.RasterYSize == 1 assert ds.GetGeoTransform() == pytest.approx((2.0, 0.5, 0.0, 49.0, 0.0, -1.0)) @@ -186,12 +182,12 @@ def test_gdalalg_raster_mosaic_target_aligned_pixels(): src2_ds.SetGeoTransform([3, 0.5, 0, 49, 0, -0.5]) alg = get_mosaic_alg() - alg.GetArg("input").Set([src1_ds, src2_ds]) - alg.GetArg("output").Set("") - alg.GetArg("resolution").Set("0.3,0.6") - alg.GetArg("target-aligned-pixels").Set(True) + alg["input"] = [src1_ds, src2_ds] + alg["output"] = "" + alg["resolution"] = "0.3,0.6" + alg["target-aligned-pixels"] = True assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterXSize == 8 assert ds.RasterYSize == 2 assert ds.GetGeoTransform() == pytest.approx((1.8, 0.3, 0.0, 49.2, 0.0, -0.6)) @@ -205,8 +201,8 @@ def test_gdalalg_raster_mosaic_resolution_same_default(): src2_ds.SetGeoTransform([3, 0.5, 0, 49, 0, -0.5]) alg = get_mosaic_alg() - alg.GetArg("input").Set([src1_ds, src2_ds]) - alg.GetArg("output").Set("") + alg["input"] = [src1_ds, src2_ds] + alg["output"] = "" with pytest.raises( Exception, match="whereas previous sources have resolution", @@ -221,19 +217,19 @@ def test_gdalalg_raster_mosaic_resolution_invalid(): Exception, match="resolution: two comma separated positive values should be provided, or 'same', 'average', 'highest' or 'lowest'", ): - alg.GetArg("resolution").Set("invalid") + alg["resolution"] = "invalid" with pytest.raises( Exception, match="resolution: two comma separated positive values should be provided, or 'same', 'average', 'highest' or 'lowest'", ): - alg.GetArg("resolution").Set("0.5") + alg["resolution"] = "0.5" with pytest.raises( Exception, match="resolution: two comma separated positive values should be provided, or 'same', 'average', 'highest' or 'lowest'", ): - alg.GetArg("resolution").Set("-0.5,-0.5") + alg["resolution"] = "-0.5,-0.5" def test_gdalalg_raster_mosaic_srcnodata_dstnodata(): @@ -243,12 +239,12 @@ def test_gdalalg_raster_mosaic_srcnodata_dstnodata(): src1_ds.GetRasterBand(1).Fill(1) alg = get_mosaic_alg() - alg.GetArg("input").Set([src1_ds]) - alg.GetArg("output").Set("") - alg.GetArg("srcnodata").Set([1]) - alg.GetArg("dstnodata").Set([2]) + alg["input"] = [src1_ds] + alg["output"] = "" + alg["srcnodata"] = [1] + alg["dstnodata"] = [2] assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.GetRasterBand(1).Checksum() == 2 assert ds.GetRasterBand(1).GetNoDataValue() == 2 @@ -260,13 +256,13 @@ def test_gdalalg_raster_mosaic_hidenodata(): src1_ds.GetRasterBand(1).Fill(1) alg = get_mosaic_alg() - alg.GetArg("input").Set([src1_ds]) - alg.GetArg("output").Set("") - alg.GetArg("srcnodata").Set([1]) - alg.GetArg("dstnodata").Set([2]) - alg.GetArg("hidenodata").Set(True) + alg["input"] = [src1_ds] + alg["output"] = "" + alg["srcnodata"] = [1] + alg["dstnodata"] = [2] + alg["hidenodata"] = True assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.GetRasterBand(1).Checksum() == 2 assert ds.GetRasterBand(1).GetNoDataValue() is None @@ -274,25 +270,11 @@ def test_gdalalg_raster_mosaic_hidenodata(): def test_gdalalg_raster_mosaic_addalpha(): alg = get_mosaic_alg() - alg.GetArg("input").Set(["../gcore/data/byte.tif"]) - alg.GetArg("output").Set("") - alg.GetArg("addalpha").Set(True) - assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() - assert ds.RasterCount == 2 - assert ds.GetRasterBand(1).Checksum() == 4672 - assert ds.GetRasterBand(2).GetColorInterpretation() == gdal.GCI_AlphaBand - assert ds.GetRasterBand(2).Checksum() == 4873 - - -def test_gdalalg_raster_mosaic_band(): - - alg = get_mosaic_alg() - alg.GetArg("input").Set(["../gcore/data/rgbsmall.tif"]) - alg.GetArg("output").Set("") - alg.GetArg("band").Set([3, 2]) + alg["input"] = ["../gcore/data/rgbsmall.tif"] + alg["output"] = "" + alg["band"] = [3, 2] assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterCount == 2 assert ds.GetRasterBand(1).Checksum() == 21349 assert ds.GetRasterBand(2).Checksum() == 21053 @@ -301,10 +283,10 @@ def test_gdalalg_raster_mosaic_band(): def test_gdalalg_raster_mosaic_glob(): alg = get_mosaic_alg() - alg.GetArg("input").Set(["../gcore/data/rgbsm?ll.tif"]) - alg.GetArg("output").Set("") + alg["input"] = ["../gcore/data/rgbsm?ll.tif"] + alg["output"] = "" assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.RasterCount == 3 @@ -314,18 +296,18 @@ def test_gdalalg_raster_mosaic_at_filename(tmp_vsimem): gdal.FileFromMemBuffer(input_file_list, "../gcore/data/byte.tif") alg = get_mosaic_alg() - alg.GetArg("input").Set([f"@{input_file_list}"]) - alg.GetArg("output").Set("") + alg["input"] = [f"@{input_file_list}"] + alg["output"] = "" assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.GetRasterBand(1).Checksum() == 4672 def test_gdalalg_raster_mosaic_at_filename_error(): alg = get_mosaic_alg() - alg.GetArg("input").Set(["@i_do_not_exist"]) - alg.GetArg("output").Set("") + alg["input"] = ["@i_do_not_exist"] + alg["output"] = "" with pytest.raises(Exception, match="mosaic: Cannot open i_do_not_exist"): alg.Run() @@ -335,8 +317,8 @@ def test_gdalalg_raster_mosaic_output_ds_alread_set(): out_ds = gdal.GetDriverByName("MEM").Create("", 1, 1) alg = get_mosaic_alg() - alg.GetArg("input").Set(["../gcore/data/byte.tif"]) - alg.GetArg("output").Set(out_ds) + alg["input"] = ["../gcore/data/byte.tif"] + alg["output"] = out_ds with pytest.raises( Exception, match="mosaic: gdal raster mosaic does not support outputting to an already opened output dataset", @@ -347,11 +329,11 @@ def test_gdalalg_raster_mosaic_output_ds_alread_set(): def test_gdalalg_raster_mosaic_co(): alg = get_mosaic_alg() - alg.GetArg("input").Set(["../gcore/data/byte.tif"]) - alg.GetArg("output").Set("") - alg.GetArg("creation-option").Set(["BLOCKXSIZE=10", "BLOCKYSIZE=15"]) + alg["input"] = ["../gcore/data/byte.tif"] + alg["output"] = "" + alg["creation-option"] = ["BLOCKXSIZE=10", "BLOCKYSIZE=15"] assert alg.Run() - ds = alg.GetArg("output").Get().GetDataset() + ds = alg["output"].GetDataset() assert ds.GetRasterBand(1).GetBlockSize() == [10, 15] @@ -405,8 +387,8 @@ def test_gdalalg_raster_mosaic_inconsistent_characteristics(): src2_ds.SetSpatialRef(srs) alg = get_mosaic_alg() - alg.GetArg("input").Set([src1_ds, src2_ds]) - alg.GetArg("output").Set("") + alg["input"] = [src1_ds, src2_ds] + alg["output"] = "" with pytest.raises( Exception, match="gdal raster mosaic does not support heterogeneous projection" ): diff --git a/autotest/utilities/test_gdalalg_raster_overview.py b/autotest/utilities/test_gdalalg_raster_overview.py index e193469d5154..d911b480e661 100755 --- a/autotest/utilities/test_gdalalg_raster_overview.py +++ b/autotest/utilities/test_gdalalg_raster_overview.py @@ -17,28 +17,26 @@ def get_overview_alg(): - reg = gdal.GetGlobalAlgorithmRegistry() - raster = reg.InstantiateAlg("raster") - return raster.InstantiateSubAlgorithm("overview") + return gdal.GetGlobalAlgorithmRegistry()["raster"]["overview"] def get_overview_add_alg(): - return get_overview_alg().InstantiateSubAlgorithm("add") + return get_overview_alg()["add"] def get_overview_delete_alg(): - return get_overview_alg().InstantiateSubAlgorithm("delete") + return get_overview_alg()["delete"] def test_gdalalg_overview_invalid_arguments(): add = get_overview_add_alg() with pytest.raises(Exception): - add.GetArg("levels").Set([1]) + add["levels"] = [1] add = get_overview_add_alg() with pytest.raises(Exception): - add.GetArg("min-size").Set(0) + add["min-size"] = 0 def test_gdalalg_overview_explicit_level(): @@ -46,8 +44,8 @@ def test_gdalalg_overview_explicit_level(): ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM") add = get_overview_add_alg() - add.GetArg("dataset").Get().SetDataset(ds) - add.GetArg("levels").Set([2]) + add["dataset"] = ds + add["levels"] = [2] assert add.Run() assert ds.GetRasterBand(1).Checksum() == 4672 @@ -60,9 +58,9 @@ def test_gdalalg_overview_minsize_and_resampling(): ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM") add = get_overview_add_alg() - add.GetArg("dataset").Get().SetDataset(ds) - add.GetArg("resampling").Set("average") - add.GetArg("min-size").Set(10) + add["dataset"] = ds + add["resampling"] = "average" + add["min-size"] = 10 assert add.Run() assert ds.GetRasterBand(1).Checksum() == 4672 @@ -76,9 +74,9 @@ def test_gdalalg_overview_reuse_resampling_and_levels(tmp_vsimem): ds = gdal.Translate(tmp_filename, "../gcore/data/byte.tif") add = get_overview_add_alg() - add.GetArg("dataset").Get().SetDataset(ds) - add.GetArg("resampling").Set("average") - add.GetArg("min-size").Set(10) + add["dataset"] = ds + add["resampling"] = "average" + add["min-size"] = 10 assert add.Run() assert ds.GetRasterBand(1).Checksum() == 4672 @@ -89,7 +87,7 @@ def test_gdalalg_overview_reuse_resampling_and_levels(tmp_vsimem): ds.GetRasterBand(1).GetOverview(0).Fill(0) add = get_overview_add_alg() - add.GetArg("dataset").Get().SetDataset(ds) + add["dataset"] = ds assert add.Run() assert ds.GetRasterBand(1).Checksum() == 4672 @@ -136,15 +134,15 @@ def test_gdalalg_overview_delete(): ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM") add = get_overview_add_alg() - add.GetArg("dataset").Get().SetDataset(ds) - add.GetArg("resampling").Set("average") - add.GetArg("min-size").Set(10) + add["dataset"] = ds + add["resampling"] = "average" + add["min-size"] = 10 assert add.Run() assert ds.GetRasterBand(1).GetOverviewCount() == 1 delete = get_overview_delete_alg() - delete.GetArg("dataset").Get().SetDataset(ds) + delete["dataset"] = ds assert delete.Run() assert ds.GetRasterBand(1).GetOverviewCount() == 0 diff --git a/autotest/utilities/test_gdalalg_raster_pipeline.py b/autotest/utilities/test_gdalalg_raster_pipeline.py index 55ede9b0fa23..570ac85b8fd5 100755 --- a/autotest/utilities/test_gdalalg_raster_pipeline.py +++ b/autotest/utilities/test_gdalalg_raster_pipeline.py @@ -19,9 +19,7 @@ def get_pipeline_alg(): - reg = gdal.GetGlobalAlgorithmRegistry() - raster = reg.InstantiateAlg("raster") - return raster.InstantiateSubAlgorithm("pipeline") + return gdal.GetGlobalAlgorithmRegistry()["raster"]["pipeline"] def test_gdalalg_raster_pipeline_read_and_write(tmp_vsimem): @@ -68,11 +66,9 @@ def test_gdalalg_raster_pipeline_as_api(tmp_vsimem): out_filename = str(tmp_vsimem / "out.tif") pipeline = get_pipeline_alg() - pipeline.GetArg("pipeline").Set( - f"read ../gcore/data/byte.tif ! write {out_filename}" - ) + pipeline["pipeline"] = f"read ../gcore/data/byte.tif ! write {out_filename}" assert pipeline.Run() - ds = pipeline.GetArg("output").Get().GetDataset() + ds = pipeline["output"].GetDataset() assert ds.GetRasterBand(1).Checksum() == 4672 assert pipeline.Finalize() ds = None @@ -86,8 +82,8 @@ def test_gdalalg_raster_pipeline_input_through_api(tmp_vsimem): out_filename = str(tmp_vsimem / "out.tif") pipeline = get_pipeline_alg() - pipeline.GetArg("input").Get().SetDataset(gdal.OpenEx("../gcore/data/byte.tif")) - pipeline.GetArg("pipeline").Set(f"read ! write {out_filename}") + pipeline["input"] = "../gcore/data/byte.tif" + pipeline["pipeline"] = f"read ! write {out_filename}" assert pipeline.Run() assert pipeline.Finalize() @@ -100,8 +96,8 @@ def test_gdalalg_raster_pipeline_input_through_api_run_twice(tmp_vsimem): out_filename = str(tmp_vsimem / "out.tif") pipeline = get_pipeline_alg() - pipeline.GetArg("input").Get().SetDataset(gdal.OpenEx("../gcore/data/byte.tif")) - pipeline.GetArg("pipeline").Set(f"read ! write {out_filename}") + pipeline["input"] = "../gcore/data/byte.tif" + pipeline["pipeline"] = f"read ! write {out_filename}" assert pipeline.Run() with pytest.raises( Exception, match=r"pipeline: Step nr 0 \(read\) has already an output dataset" @@ -114,8 +110,8 @@ def test_gdalalg_raster_pipeline_output_through_api(tmp_vsimem): out_filename = str(tmp_vsimem / "out.tif") pipeline = get_pipeline_alg() - pipeline.GetArg("output").Get().SetName(out_filename) - pipeline.GetArg("pipeline").Set("read ../gcore/data/byte.tif ! write") + pipeline["output"] = out_filename + pipeline["pipeline"] = "read ../gcore/data/byte.tif ! write" assert pipeline.Run() assert pipeline.Finalize() @@ -126,7 +122,7 @@ def test_gdalalg_raster_pipeline_output_through_api(tmp_vsimem): def test_gdalalg_raster_pipeline_as_api_error(): pipeline = get_pipeline_alg() - pipeline.GetArg("pipeline").Set("read") + pipeline["pipeline"] = "read" with pytest.raises(Exception, match="pipeline: At least 2 steps must be provided"): pipeline.Run() diff --git a/autotest/utilities/test_gdalalg_vector_clip.py b/autotest/utilities/test_gdalalg_vector_clip.py index 9c5afbe0379a..0ec6a9fea798 100755 --- a/autotest/utilities/test_gdalalg_vector_clip.py +++ b/autotest/utilities/test_gdalalg_vector_clip.py @@ -20,9 +20,7 @@ def get_clip_alg(): - reg = gdal.GetGlobalAlgorithmRegistry() - vector = reg.InstantiateAlg("vector") - return vector.InstantiateSubAlgorithm("clip") + return gdal.GetGlobalAlgorithmRegistry()["vector"]["clip"] @pytest.mark.require_driver("GPKG") @@ -136,14 +134,14 @@ def test_gdalalg_vector_clip_bbox(): src_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) + clip["input"] = src_ds assert clip.ParseCommandLineArguments( ["--bbox", "0.2,0.3,0.7,0.8", "--of", "Memory", "--output", "memory_ds"] ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -169,7 +167,7 @@ def test_gdalalg_vector_clip_bbox_srs(): src_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) + clip["input"] = src_ds assert clip.ParseCommandLineArguments( [ @@ -185,7 +183,7 @@ def test_gdalalg_vector_clip_bbox_srs(): ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) assert out_lyr.GetSpatialRef().GetAuthorityCode(None) == "4326" out_f = out_lyr.GetNextFeature() @@ -220,14 +218,14 @@ def test_gdalalg_vector_clip_split_multipart(): src_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) + clip["input"] = src_ds assert clip.ParseCommandLineArguments( ["--bbox", "-0.1,0.3,1.1,0.7", "--of", "Memory", "--output", "memory_ds"] ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) ref1_geom = ogr.CreateGeometryFromWkt( @@ -284,14 +282,14 @@ def test_gdalalg_vector_clip_geom(clip_geom): src_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) + clip["input"] = src_ds assert clip.ParseCommandLineArguments( ["--geometry", clip_geom, "--of", "Memory", "--output", "memory_ds"] ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -324,7 +322,7 @@ def test_gdalalg_vector_clip_geom_srs(clip_geom): src_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) + clip["input"] = src_ds clip_geom = clip_geom.replace("0.2", "22263.8981586547") clip_geom = clip_geom.replace("0.3", "33395.9998333802") clip_geom = clip_geom.replace("0.7", "77923.6435552915") @@ -343,7 +341,7 @@ def test_gdalalg_vector_clip_geom_srs(clip_geom): ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -376,7 +374,7 @@ def test_gdalalg_vector_clip_geom_not_rectangle(): src_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) + clip["input"] = src_ds assert clip.ParseCommandLineArguments( [ @@ -390,7 +388,7 @@ def test_gdalalg_vector_clip_geom_not_rectangle(): ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -411,7 +409,7 @@ def test_gdalalg_vector_clip_intersection_incompatible_geometry_type(): src_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) + clip["input"] = src_ds assert clip.ParseCommandLineArguments( [ @@ -425,7 +423,7 @@ def test_gdalalg_vector_clip_intersection_incompatible_geometry_type(): ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) assert out_lyr.GetNextFeature() is None @@ -444,7 +442,7 @@ def test_gdalalg_vector_clip_intersection_promote_simple_type_to_multi(): src_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) + clip["input"] = src_ds assert clip.ParseCommandLineArguments( [ @@ -458,7 +456,7 @@ def test_gdalalg_vector_clip_intersection_promote_simple_type_to_multi(): ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() ogrtest.check_feature_geometry( @@ -487,13 +485,13 @@ def test_gdalalg_vector_clip_like_vector(): like_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments(["--of", "Memory", "--output", "memory_ds"]) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -525,8 +523,8 @@ def test_gdalalg_vector_clip_like_vector_invalid_geom(): like_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments(["--of", "Memory", "--output", "memory_ds"]) with gdal.quiet_errors(), pytest.raises( @@ -556,15 +554,15 @@ def test_gdalalg_vector_clip_like_vector_like_layer(): like_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments( ["--like-layer", "my_layer", "--of", "Memory", "--output", "memory_ds"] ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -585,8 +583,8 @@ def test_gdalalg_vector_clip_like_vector_like_layer_invalid(): like_ds.CreateLayer("test") clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments( ["--like-layer", "invalid", "--of", "Memory", "--output", "memory_ds"] @@ -618,8 +616,8 @@ def test_gdalalg_vector_clip_like_vector_like_sql(): like_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments( [ @@ -633,7 +631,7 @@ def test_gdalalg_vector_clip_like_vector_like_sql(): ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -672,15 +670,15 @@ def test_gdalalg_vector_clip_like_vector_like_where(): like_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments( ["--like-where", "id=1", "--of", "Memory", "--output", "memory_ds"] ) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -708,8 +706,8 @@ def test_gdalalg_vector_clip_like_vector_like_where_empty(): like_lyr.CreateField(ogr.FieldDefn("id", ogr.OFTInteger)) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments( ["--like-where", "id=1", "--of", "Memory", "--output", "memory_ds"] @@ -745,13 +743,13 @@ def test_gdalalg_vector_clip_like_vector_srs(): like_lyr.CreateFeature(f) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments(["--of", "Memory", "--output", "memory_ds"]) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() ogrtest.check_feature_geometry( @@ -777,13 +775,13 @@ def test_gdalalg_vector_clip_like_raster(): like_ds.SetGeoTransform([0.2, 0.5, 0, 0.3, 0, 0.5]) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments(["--of", "Memory", "--output", "memory_ds"]) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -818,13 +816,13 @@ def test_gdalalg_vector_clip_like_raster_srs(): like_ds.SetSpatialRef(srs_3857) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments(["--of", "Memory", "--output", "memory_ds"]) assert clip.Run() - out_ds = clip.GetArg("output").Get().GetDataset() + out_ds = clip["output"].GetDataset() out_lyr = out_ds.GetLayer(0) out_f = out_lyr.GetNextFeature() assert out_f["foo"] == "bar" @@ -852,7 +850,7 @@ def test_gdalalg_vector_clip_geometry_invalid(): src_ds.CreateLayer("test") clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) + clip["input"] = src_ds assert clip.ParseCommandLineArguments( ["--geometry", "invalid", "--of", "Memory", "--output", "memory_ds"] @@ -873,8 +871,8 @@ def test_gdalalg_vector_clip_like_vector_too_many_layers(): like_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments(["--of", "Memory", "--output", "memory_ds"]) with pytest.raises( @@ -895,8 +893,8 @@ def test_gdalalg_vector_clip_like_raster_no_geotransform(): like_ds.CreateLayer("test2") clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments(["--of", "Memory", "--output", "memory_ds"]) with pytest.raises( @@ -913,8 +911,8 @@ def test_gdalalg_vector_clip_like_neither_raster_no_vector(): like_ds = gdal.GetDriverByName("Memory").Create("clip", 0, 0, 0, gdal.GDT_Unknown) clip = get_clip_alg() - clip.GetArg("input").Get().SetDataset(src_ds) - clip.GetArg("like").Get().SetDataset(like_ds) + clip["input"] = src_ds + clip["like"] = like_ds assert clip.ParseCommandLineArguments(["--of", "Memory", "--output", "memory_ds"]) with pytest.raises( diff --git a/autotest/utilities/test_gdalalg_vector_convert.py b/autotest/utilities/test_gdalalg_vector_convert.py index b38ffc7a1241..b42be87d39b7 100755 --- a/autotest/utilities/test_gdalalg_vector_convert.py +++ b/autotest/utilities/test_gdalalg_vector_convert.py @@ -17,9 +17,7 @@ def get_convert_alg(): - reg = gdal.GetGlobalAlgorithmRegistry() - vector = reg.InstantiateAlg("vector") - return vector.InstantiateSubAlgorithm("convert") + return gdal.GetGlobalAlgorithmRegistry()["vector"]["convert"] @pytest.mark.require_driver("GPKG") @@ -156,8 +154,11 @@ def test_gdalalg_vector_wrong_layer_name(tmp_vsimem): def test_gdalalg_vector_convert_error_output_not_set(): convert = get_convert_alg() - convert.GetArg("input").Get().SetName("../ogr/data/poly.shp") - convert.GetArg("output").Set(convert.GetArg("output").Get()) + convert["input"] = "../ogr/data/poly.shp" + + # Make it such that the "output" argment is set, but to a unset GDALArgDatasetValue + convert["output"] = convert["output"] + with pytest.raises( Exception, match="convert: Argument 'output' has no dataset object or dataset name", diff --git a/autotest/utilities/test_gdalalg_vector_filter.py b/autotest/utilities/test_gdalalg_vector_filter.py index f0a62a97c65c..33b11a4239a3 100755 --- a/autotest/utilities/test_gdalalg_vector_filter.py +++ b/autotest/utilities/test_gdalalg_vector_filter.py @@ -15,9 +15,7 @@ def get_filter_alg(): - reg = gdal.GetGlobalAlgorithmRegistry() - vector = reg.InstantiateAlg("vector") - return vector.InstantiateSubAlgorithm("filter") + return gdal.GetGlobalAlgorithmRegistry()["vector"]["filter"] def test_gdalalg_vector_filter_no_filter(tmp_vsimem): @@ -27,7 +25,7 @@ def test_gdalalg_vector_filter_no_filter(tmp_vsimem): filter_alg = get_filter_alg() assert filter_alg.ParseCommandLineArguments(["../ogr/data/poly.shp", out_filename]) assert filter_alg.Run() - ds = filter_alg.GetArg("output").Get().GetDataset() + ds = filter_alg["output"].GetDataset() assert ds.GetLayer(0).GetFeatureCount() == 10 assert filter_alg.Finalize() ds = None diff --git a/autotest/utilities/test_gdalalg_vector_info.py b/autotest/utilities/test_gdalalg_vector_info.py index 9fbaace7eee7..524dcf104a14 100755 --- a/autotest/utilities/test_gdalalg_vector_info.py +++ b/autotest/utilities/test_gdalalg_vector_info.py @@ -19,9 +19,7 @@ def get_info_alg(): - reg = gdal.GetGlobalAlgorithmRegistry() - vector = reg.InstantiateAlg("vector") - return vector.InstantiateSubAlgorithm("info") + return gdal.GetGlobalAlgorithmRegistry()["vector"]["info"] def test_gdalalg_vector_info_stdout(): @@ -42,14 +40,14 @@ def test_gdalalg_vector_info_stdout(): def test_gdalalg_vector_info_text(): info = get_info_alg() assert info.ParseRunAndFinalize(["--format=text", "data/path.shp"]) - output_string = info.GetArg("output-string").Get() + output_string = info["output-string"] assert output_string.startswith("INFO: Open of") def test_gdalalg_vector_info_json(): info = get_info_alg() assert info.ParseRunAndFinalize(["data/path.shp"]) - output_string = info.GetArg("output-string").Get() + output_string = info["output-string"] j = json.loads(output_string) assert j["layers"][0]["name"] == "path" assert "features" not in j["layers"][0] @@ -58,7 +56,7 @@ def test_gdalalg_vector_info_json(): def test_gdalalg_vector_info_features(): info = get_info_alg() assert info.ParseRunAndFinalize(["--features", "data/path.shp"]) - output_string = info.GetArg("output-string").Get() + output_string = info["output-string"] j = json.loads(output_string) assert "features" in j["layers"][0] @@ -68,7 +66,7 @@ def test_gdalalg_vector_info_sql(): assert info.ParseRunAndFinalize( ["--sql", "SELECT 1 AS foo FROM path", "data/path.shp"] ) - output_string = info.GetArg("output-string").Get() + output_string = info["output-string"] j = json.loads(output_string) assert len(j["layers"][0]["fields"]) == 1 assert j["layers"][0]["fields"][0]["name"] == "foo" @@ -77,7 +75,7 @@ def test_gdalalg_vector_info_sql(): def test_gdalalg_vector_info_layer(): info = get_info_alg() assert info.ParseRunAndFinalize(["-l", "path", "data/path.shp"]) - output_string = info.GetArg("output-string").Get() + output_string = info["output-string"] j = json.loads(output_string) assert j["layers"][0]["name"] == "path" @@ -92,7 +90,7 @@ def test_gdalalg_vector_info_wrong_layer(): def test_gdalalg_vector_info_where(cond, featureCount): info = get_info_alg() assert info.ParseRunAndFinalize(["--where", cond, "data/path.shp"]) - output_string = info.GetArg("output-string").Get() + output_string = info["output-string"] j = json.loads(output_string) assert j["layers"][0]["featureCount"] == featureCount @@ -110,6 +108,6 @@ def test_gdalalg_vector_info_dialect(): "data/path.shp", ] ) - output_string = info.GetArg("output-string").Get() + output_string = info["output-string"] j = json.loads(output_string) assert j["layers"][0]["features"][0]["properties"]["version"].startswith("3.") diff --git a/autotest/utilities/test_gdalalg_vector_pipeline.py b/autotest/utilities/test_gdalalg_vector_pipeline.py index d16968344b56..db93707735f7 100755 --- a/autotest/utilities/test_gdalalg_vector_pipeline.py +++ b/autotest/utilities/test_gdalalg_vector_pipeline.py @@ -19,9 +19,7 @@ def get_pipeline_alg(): - reg = gdal.GetGlobalAlgorithmRegistry() - vector = reg.InstantiateAlg("vector") - return vector.InstantiateSubAlgorithm("pipeline") + return gdal.GetGlobalAlgorithmRegistry()["vector"]["pipeline"] def test_gdalalg_vector_pipeline_read_and_write(tmp_vsimem): @@ -68,9 +66,9 @@ def test_gdalalg_vector_pipeline_as_api(tmp_vsimem): out_filename = str(tmp_vsimem / "out.shp") pipeline = get_pipeline_alg() - pipeline.GetArg("pipeline").Set(f"read ../ogr/data/poly.shp ! write {out_filename}") + pipeline["pipeline"] = f"read ../ogr/data/poly.shp ! write {out_filename}" assert pipeline.Run() - ds = pipeline.GetArg("output").Get().GetDataset() + ds = pipeline["output"].GetDataset() assert ds.GetLayer(0).GetFeatureCount() == 10 assert pipeline.Finalize() ds = None @@ -84,8 +82,8 @@ def test_gdalalg_vector_pipeline_input_through_api(tmp_vsimem): out_filename = str(tmp_vsimem / "out.shp") pipeline = get_pipeline_alg() - pipeline.GetArg("input").Get().SetDataset(gdal.OpenEx("../ogr/data/poly.shp")) - pipeline.GetArg("pipeline").Set(f"read ! write {out_filename}") + pipeline["input"] = gdal.OpenEx("../ogr/data/poly.shp") + pipeline["pipeline"] = f"read ! write {out_filename}" assert pipeline.Run() assert pipeline.Finalize() @@ -98,8 +96,8 @@ def test_gdalalg_vector_pipeline_input_through_api_run_twice(tmp_vsimem): out_filename = str(tmp_vsimem / "out.shp") pipeline = get_pipeline_alg() - pipeline.GetArg("input").Get().SetDataset(gdal.OpenEx("../ogr/data/poly.shp")) - pipeline.GetArg("pipeline").Set(f"read ! write {out_filename}") + pipeline["input"] = gdal.OpenEx("../ogr/data/poly.shp") + pipeline["pipeline"] = f"read ! write {out_filename}" assert pipeline.Run() with pytest.raises( Exception, match=r"pipeline: Step nr 0 \(read\) has already an output dataset" @@ -112,8 +110,8 @@ def test_gdalalg_vector_pipeline_output_through_api(tmp_vsimem): out_filename = str(tmp_vsimem / "out.shp") pipeline = get_pipeline_alg() - pipeline.GetArg("output").Get().SetName(out_filename) - pipeline.GetArg("pipeline").Set("read ../ogr/data/poly.shp ! write") + pipeline["output"] = out_filename + pipeline["pipeline"] = "read ../ogr/data/poly.shp ! write" assert pipeline.Run() assert pipeline.Finalize() @@ -124,7 +122,7 @@ def test_gdalalg_vector_pipeline_output_through_api(tmp_vsimem): def test_gdalalg_vector_pipeline_as_api_error(): pipeline = get_pipeline_alg() - pipeline.GetArg("pipeline").Set("read") + pipeline["pipeline"] = "read" with pytest.raises(Exception, match="pipeline: At least 2 steps must be provided"): pipeline.Run() @@ -624,10 +622,10 @@ def test_gdalalg_vector_pipeline_reproject_missing_layer_crs(tmp_vsimem): pipeline = get_pipeline_alg() mem_ds = gdal.GetDriverByName("Memory").Create("", 0, 0, 0, gdal.GDT_Unknown) mem_ds.CreateLayer("layer") - pipeline.GetArg("input").Get().SetDataset(mem_ds) - pipeline.GetArg("pipeline").Set( - f"read ! reproject --dst-crs=EPSG:4326 ! write {out_filename}" - ) + pipeline["input"] = mem_ds + pipeline[ + "pipeline" + ] = f"read ! reproject --dst-crs=EPSG:4326 ! write {out_filename}" with pytest.raises( Exception, match="reproject: Layer 'layer' has no spatial reference system" ): diff --git a/swig/include/python/gdal_python.i b/swig/include/python/gdal_python.i index 3421377200fb..db9a0ac37dcb 100644 --- a/swig/include/python/gdal_python.i +++ b/swig/include/python/gdal_python.i @@ -5251,6 +5251,92 @@ class VSIFile(BytesIO): return VSIFTellL(self._fp) %} + +/* -------------------------------------------------------------------- */ +/* GDALAlgorithmRegistryHS */ +/* -------------------------------------------------------------------- */ + +%extend GDALAlgorithmRegistryHS { +%pythoncode %{ + + def __getitem__(self, key): + """Instantiate an algorithm + + Shortcut for self.InstantiateAlg(key) + + Example + ------- + >>> gdal.GetGlobalAlgorithmRegistry()["raster"] + """ + + return self.InstantiateAlg(key) +%} +} + +/* -------------------------------------------------------------------- */ +/* GDALAlgorithmHS */ +/* -------------------------------------------------------------------- */ + +%extend GDALAlgorithmHS { +%pythoncode %{ + + def __getitem__(self, key): + """Get the value of an argument. + + Shortcut for self.GetActualAlgorithm().GetArg(key).Get() + or self.InstantiateSubAlgorithm(key) for a non-leaf algorithm + + Parameters + ----------- + key: str + Name of a known argument of the algorithm + value: + Value of the argument + + Example + ------- + >>> alg["output-string"] + >>> alg["output"].GetName() + >>> alg["output"].GetDataset() + >>> gdal.GetGlobalAlgorithmRegistry()["raster"]["info"] + """ + + if self.HasSubAlgorithms(): + return self.InstantiateSubAlgorithm(key) + else: + return self.GetActualAlgorithm().GetArg(key).Get() + + def __setitem__(self, key, value): + """Set the value of an argment. + + Shortcut for self.GetArg(key).Set(value) + + Parameters + ----------- + key: str + Name of a known argument of the algorithm + value: + Value of the argument + + Examples + -------- + >>> alg["bbox"] = [2, 49, 3, 50] + >>> alg["where"] = "country = 'France'" + >>> alg["input"] = "byte.tif" + >>> alg["input"] = gdal.Open("byte.tif") + >>> alg["target-aligned-pixels"] = True + + >>> # Multiple input datasets + >>> alg["input"] = ["one.tif", "two.tif"] + >>> alg["input"] = [one_ds, two_ds] + """ + + if not self.GetArg(key).Set(value): + raise Exception(f"Cannot set argument {key} to {value}") +%} +} + + /* -------------------------------------------------------------------- */ /* GDALAlgorithmArgHS */ /* -------------------------------------------------------------------- */ @@ -5290,9 +5376,11 @@ class VSIFile(BytesIO): return self.SetAsDouble(value) if type == GAAT_DATASET: if isinstance(value, str): - return self.GetAsDatasetValue().SetName(value) + self.GetAsDatasetValue().SetName(value) + return True elif isinstance(value, Dataset): - return self.GetAsDatasetValue().SetDataset(value) + self.GetAsDatasetValue().SetDataset(value) + return True else: return self.SetAsDatasetValue(value) if type == GAAT_STRING_LIST: