Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#5388 closed enhancement (fixed)

[FEATURE] add --allBands option to gdal_calc.py, this allows to apply calculation to all bands in one of the input files

Reported by: etourigny Owned by: warmerdam
Priority: normal Milestone:
Component: Utilities Version: unspecified
Severity: normal Keywords:
Cc: Even Rouault

Description

This feature is useful for applying a calculation to all bands in a given file. Example usage which multiplies all band in A.tif with first band in B.tif:

gdal_calc.py -A A.tif -B B.tif --allBands A --calc A*B --NoDataValue?=999 --overwrite --outfile out.tif

Currently it has to be used in conjunction with --overwrite if the destination file exists.

I have created a test suite for gdal_calc.py to test against present and future regressions.

Can someone look at this?

Attachments (2)

patch_gdal_calc1.txt (9.4 KB) - added by etourigny 3 years ago.
patch
test_gdal_calc.py (8.2 KB) - added by etourigny 3 years ago.
new autotest

Download all attachments as: .zip

Change History (10)

Changed 3 years ago by etourigny

Attachment: patch_gdal_calc1.txt added

patch

Changed 3 years ago by etourigny

Attachment: test_gdal_calc.py added

new autotest

comment:1 Changed 3 years ago by Even Rouault

Just a question. You specify "--allBands letter_of_raster". What happens if all input rasters have not the same number of bands ? Does specifying a letter_of_raster makes sense ?

comment:2 Changed 3 years ago by etourigny

Currently you only get one raster band in output, because if you do not specify a band for a given band the first one is used.

With this patch, you essentially loop for every band in the specified raster, and a single band is used for the other rasters.

Unless I missed something in my understanding of the code?

Also, I think the docs should be updated, because the rule that first band is used, unless specified by --A_band option is used.

comment:3 Changed 3 years ago by etourigny

I've written a first draft of the doc (a .dox file that would be listed along other utilities), I'll commit it after this work is done.

comment:4 Changed 3 years ago by Even Rouault

ok, thanks for explanations. I think you can just go ahead.

comment:5 Changed 3 years ago by etourigny

Resolution: fixed
Status: newclosed

commited code in r26954 , added new test file in r26955 , added docs in r26956

comment:6 Changed 3 years ago by Even Rouault

Resolution: fixed
Status: closedreopened

Etienne,

for robustness, it would be good if there was some testing of availability of gdalnumeric in the new test script, since it causes failure on the Travis mingw target where it is not available. See https://api.travis-ci.org/jobs/19056470/log.txt?deansi=true

Running tests from pyscripts/test_gdal_calc.py
  TEST: test_gdal_calc_py_1 ... fixme:msvcrt:MSVCRT__sopen_s : pmode 0x81b6 ignored
fixme:msvcrt:MSVCRT__sopen_s : pmode 0x81b6 ignored
Traceback (most recent call last):
  File "pymod\gdaltest_python2.py", line 41, in run_func
    result = func()
  File ".\test_gdal_calc.py", line 57, in test_gdal_calc_py_1
    test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --calc=A --overwrite --outfile tmp/test_gdal_calc_py_1_1.tif')
  File "../pymod\test_py_scripts.py", line 79, in run_py_script
    return run_py_script_as_py_module(script_path, script_name, concatenated_argv)
  File "../pymod\test_py_scripts.py", line 133, in run_py_script_as_py_module
    exec('import ' + script_name)
  File "<string>", line 1, in <module>
  File "Z:\home\travis\build\rouault\gdal_coverage\autotest\pyscripts\..\..\gdal\swig\python\scripts\gdal_calc.py", line 49, in <module>
    from gdalnumeric import *
ImportError: No module named gdalnumeric
Traceback (most recent call last):
  File "pymod\gdaltest_python2.py", line 41, in run_func
    result = func()
  File ".\test_gdal_calc.py", line 91, in test_gdal_calc_py_2
    test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --A_band 1 -B tmp/test_gdal_calc_py.tif --B_band 2 --calc=A+B --overwrite --outfile tmp/test_gdal_calc_py_2_1.tif')
  File "../pymod\test_py_scripts.py", line 79, in run_py_script
    return run_py_script_as_py_module(script_path, script_name, concatenated_argv)
  File "../pymod\test_py_scripts.py", line 133, in run_py_script_as_py_module
    exec('import ' + script_name)
  File "<string>", line 1, in <module>
  File "Z:\home\travis\build\rouault\gdal_coverage\autotest\pyscripts\..\..\gdal\swig\python\scripts\gdal_calc.py", line 49, in <module>
    from gdalnumeric import *

and same for following tests.

comment:7 Changed 3 years ago by etourigny

Resolution: fixed
Status: reopenedclosed

fixed in r26959 . let me know if it still fails. I tested with bogus import.

Etienne

comment:8 Changed 3 years ago by Even Rouault

Looks good. Thanks. You can check at https://travis-ci.org/rouault/gdal_coverage/builds/ (but random failures can happen so you have to check the log if that's the case to see if it is likely related to your recent activity or not)

Note: See TracTickets for help on using tickets.