Skip to content

Commit d773b49

Browse files
committed
gdal raster calc: allow to specify input files as nested pipelines
Fixes #13493
1 parent 32edc37 commit d773b49

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

apps/gdalalg_raster_calc.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,17 @@ static bool ParseSourceDescriptors(const std::vector<std::string> &inputs,
649649

650650
std::string dsn =
651651
(pos == std::string::npos) ? input : input.substr(pos + 1);
652+
653+
if (!dsn.empty() && dsn.front() == '[' && dsn.back() == ']')
654+
{
655+
dsn = "{\"type\":\"gdal_streamed_alg\", \"command_line\":\"gdal "
656+
"raster pipeline " +
657+
CPLString(dsn.substr(1, dsn.size() - 2))
658+
.replaceAll('\\', "\\\\")
659+
.replaceAll('"', "\\\"") +
660+
"\"}";
661+
}
662+
652663
if (datasets.find(name) != datasets.end())
653664
{
654665
CPLError(CE_Failure, CPLE_AppDefined,

autotest/utilities/test_gdalalg_raster_calc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,3 +1025,14 @@ def test_gdalalg_raster_calc_sum_float_input_with_nodata(calc, tmp_vsimem):
10251025

10261026
out_ds = calc["output"].GetDataset()
10271027
assert out_ds.GetRasterBand(1).Checksum() == 1
1028+
1029+
1030+
def test_gdalalg_raster_calc_input_pipeline(calc):
1031+
1032+
calc["input"] = "A=[ read ../gcore/data/byte.tif ! aspect ]"
1033+
calc["output-format"] = "MEM"
1034+
calc["calc"] = "A * 10"
1035+
calc.Run()
1036+
1037+
out_ds = calc["output"].GetDataset()
1038+
assert out_ds.GetRasterBand(1).Checksum() == 4692

doc/source/programs/gdal_raster_calc.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,11 @@ Examples
184184
.. code-block:: bash
185185
186186
gdal raster calc -i A=input1.tif -i B=input2.tif -o result.tif --flatten --calc=mean --dialect=builtin
187+
188+
189+
.. example::
190+
:title: Generate a masked aspect layer where the slope angle is greater than 2 degrees, using nested pipelines (since GDAL 3.12.1)
191+
192+
.. code-block:: bash
193+
194+
gdal raster calc -i "SLOPE=[ read dem.tif ! slope ]" -i "ASPECT=[ read dem.tif ! aspect ]" -o result.tif --calc "(SLOPE >= 2) * ASPECT + (SLOPE < 2) * -9999" --nodata -9999

0 commit comments

Comments
 (0)