2

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.

Ralu Bur
  • 573
  • 5
  • 24

2 Answers2

11

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.

Charlie Lefrak
  • 1,206
  • 12
  • 28
  • Is it possible to search 'gdal_calc.py' from 'path' of environment variable automatically? – Bruce Xiaolong Liu Mar 11 '21 at 09:14
  • Good question @BruceXiaolongLiu. You should be able to add the directory of gdal_calc.py to your PATH, and then call it from your command line. What operating system are you on, and where is the gdal_calc.py code now? – Charlie Lefrak Mar 11 '21 at 13:03
  • Actually I expect my code to be cross-platform. And I used it in the conda environment. I found out the location of gdal_calc.py in the conda prefix directory. So I solved it in my case with: import os, pathlib, subprocess; gdal_calc_script = pathlib.Path(os.getenv('CONDA_EXE')).parent / 'gdal_calc.py'; command = [ sys.executable, gdal_calc_script, .......]; subprocess.run(command);. It works in both Windows and WSL. A little trick. It won't work without conda environment. – Bruce Xiaolong Liu Mar 11 '21 at 14:46
  • It only works in WSL/Linux by adding the directory of gdal_calc.py to the PATH. NOT in Windows. @Charlie. I can just use subprocess.run(['gdal_calc.py', ....]) in linux. While, it is not a win32 application in Windows. So we have to add sys.executable and its absolute path. It was my case. – Bruce Xiaolong Liu Mar 11 '21 at 15:01
  • I haven't worked much in conda @BruceXiaolongLiu, but glad it's working in WSL/LInux. – Charlie Lefrak Mar 11 '21 at 18:09
4

Try to use os instead of subprocess. This worked for me:

import os
gdal_calc =  'python C:/PROGRA~2/GDAL/gdal_calc.py -A d:/mnt_5x5.tif --outfile=d:/test.tif --calc="A+1"'
os.system(gdal_calc)