[GIS] Error when using gdal_calc.py

command linegdalpython

I'm trying to perform some raster calculations (ex:A+B, A*(A>0)) using gdal_calc.py, but I'm stuck with an error when I try to make this simple test command:

import subprocess
import sys

subprocess.call([sys.executable, 'C:\\PROGRA~2\\GDAL\\gdal_calc.py', '-A', 'd:\\mnt_5x5.tif', '--outfile=d:\\test.tif', '--calc="A+1" '])

When I run it in CMD, it gives the following error:

0 ..
Traceback (most recent call last):
  File "C:\PROGRA~2\GDAL\gdal_calc.py", line 329, in <module>
    main()
  File "C:\PROGRA~2\GDAL\gdal_calc.py", line 326, in main
    doit(opts, args)
  File "C:\PROGRA~2\GDAL\gdal_calc.py", line 282, in doit
    myResult = ((1*(myNDVs==0))*myResult) + (myOutNDV*myNDVs)
TypeError: ufunc 'multiply' did not contain a loop with signature matching types
 dtype('S11') dtype('S11') dtype('S11')

GDAL is installed properly, since I had no problems running similar scripts with gdalwarp.exe and gdal_translate.exe.
The error mentions something about the numpy class dtype, but I'm not very familiar with numpy syntax.

As seen here it's an easy task, I don't know what I'm missing.

Best Answer

This seems to be a quoting issue. Try running: subprocess.call([sys.executable, 'C:\\PROGRA~2\\GDAL\\gdal_calc.py', '-A', 'd:\\mnt_5x5.tif', '--outfile=d:\\test.tif', '--calc=A+1'])

The --calc=A+1 argument needs to be quoted when run at the CLI, but doesn't need quotes when run in python using subprocess.

Related Question