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.

3
  • Why do you even call a python script as a subprocess?
    – user30184
    Commented Jul 8, 2016 at 8:06
  • Because I have several versions of python/GDAL installed and I need to specify which one should run the script/command.
    – Ralu Bur
    Commented Jul 8, 2016 at 8:09
  • And something like c:\python27\python gdal_calc.py will not work in your case, or?
    – user30184
    Commented Jul 8, 2016 at 9:13

2 Answers 2

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.

5
  • Is it possible to search 'gdal_calc.py' from 'path' of environment variable automatically? Commented Mar 11, 2021 at 9: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? Commented Mar 11, 2021 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. Commented Mar 11, 2021 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. Commented Mar 11, 2021 at 15:01
  • I haven't worked much in conda @BruceXiaolongLiu, but glad it's working in WSL/LInux. Commented Mar 11, 2021 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)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.