0

I tried to use gdal_calc.py to avoid using the manual GUI raster calculator in QGIS. However, I'm not yet familiar with it and having errors with my code:

gdal_calc.py -A perennials.tif -B annuals.tif --outfile=annuals_only.tif --calc="(A<=85 or B=2001)*(A)" --format=GTiff 0 .. evaluation of calculation (A<=85 or B=2001)*(A) 
evaluation of calculation (A<=85 or B=2001)*(A) failed Traceback (most recent call last):
 File "/usr/bin/gdal_calc.py", line 401, in <module>
 main()
 File "/usr/bin/gdal_calc.py", line 394, in main
 doit(opts, args)
 File "/usr/bin/gdal_calc.py", line 288, in doit
 myResult = eval(opts.calc, global_namespace, local_namespace)
 File "<string>", line 1
 (A<=85 or B=2001)*(A)
 ^
SyntaxError: invalid syntax

Is there some thing I can improve in my --calc formula?

asked Jul 23, 2020 at 4:02

1 Answer 1

1

Your syntax is wrong for logical operator (or). Try this:

gdal_calc.py -A perennials.tif -B annuals.tif --outfile=annuals_only.tif --calc="A*logical_or(A<=85, B=2001)"

I would also add --NoDataValue=0 to avoid large black areas in the output.

answered Jul 23, 2020 at 4:31
5
  • Hello, I'm having these errors: 0 .. evaluation of calculation A*logical_or(A<=85, B=2001) failed Traceback (most recent call last): File "/usr/bin/gdal_calc.py", line 401, in <module> main() File "/usr/bin/gdal_calc.py", line 394, in main doit(opts, args) File "/usr/bin/gdal_calc.py", line 288, in doit myResult = eval(opts.calc, global_namespace, local_namespace) File "<string>", line 1, in <module> ValueError: invalid number of arguments Commented Jul 23, 2020 at 5:50
  • Please remove the extra characters at the end of your original command or copy/paste the command from my answer. Commented Jul 23, 2020 at 5:56
  • Hello, I did your suggestion. I'm thinking, is it possible that I have problems with numpy? Installed it recently and nothing happens. Commented Jul 23, 2020 at 7:26
  • Try to start Python and import numpy manually. Do you use Windows or Linux? Commented Jul 23, 2020 at 9:31
  • I use Linux. Yes I'll try doing it manually. Commented Jul 24, 2020 at 10:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.