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
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
-
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 argumentsbrentiemapper– brentiemapper2020年07月23日 05:50:02 +00:00Commented 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.Zoltan– Zoltan2020年07月23日 05:56:12 +00:00Commented 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.brentiemapper– brentiemapper2020年07月23日 07:26:45 +00:00Commented Jul 23, 2020 at 7:26
-
Try to start Python and import numpy manually. Do you use Windows or Linux?Zoltan– Zoltan2020年07月23日 09:31:21 +00:00Commented Jul 23, 2020 at 9:31
-
I use Linux. Yes I'll try doing it manually.brentiemapper– brentiemapper2020年07月24日 10:53:41 +00:00Commented Jul 24, 2020 at 10:53
default