5

I've searched the web extensively (e.g. here) but I am not able to find any documentation on how to setup gdal_cal.py.

When I try to run gdal_calc.py from my command line e.g.:

gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B"

i simply get prompted with which program i want to open the file. how do i setup up gdal_calc.py so that I can run it from my command line? Or where can i find documentation about it.

INFO:

  • i have newest version of python
  • i have newest version of QGIS (3.0)
  • i have newest version of python (2.7.14)(using spyder IDE(3.2.6))
  • i have newest version of GDAL (2.2.4)
  • running on windows
6
  • 1
    Have you set files of type '.py' opens with python.exe? thewindowsclub.com/change-file-associations-windows how did you install python? Apr 16, 2018 at 5:06
  • good one! I no longer get prompted to choose with what i want to open the gdal_calc.py-file. But...after running the line, nothing seems to happen. No error message either. Should i also setup some internal commands for the cmd.exe?
    – J.A.Cado
    Apr 16, 2018 at 5:11
  • Try a simple script like print("Hello world") and see if your script is running. If not there's some problem with python, reinstalling might help, if not it would be better to ask this question on Stack Overflow as it has no GIS component. If you do get "Hello world" echoed to the CMD window then there's likely a problem with your GDAL install, try reinstalling the python bindings, and make sure your python bindings match your GDAL and python versions.. did you get the installs from GIS Internals? Apr 16, 2018 at 5:16
  • i've made a script with just print("Hello world"). I openend command prompt in folder and typed the name of the file. But nothing happens either. I installed OSGeo4W yesterday...the newest version. So i dont understand what it is that isn't working properly.
    – J.A.Cado
    Apr 16, 2018 at 5:44
  • 2
    Try using OSGEO4WShell and call the program with explicitly naming Python, like this: python gdal_calc.py -A .... Also try to cd into the GDAL bin folder before executing or call it like this: python "C:\Program Files\QGIS 3.0\bin\gdal_calc.py" -A ...
    – s6hebern
    Apr 16, 2018 at 6:24

4 Answers 4

7

If you have OSGeo4W installed (which normally comes with QGIS) You can "directly" run gdal_calc.py with the OSGeo4W shell from the windows command shell by using something like:

C:\Program Files\QGIS 3.6>OSGeo4W.bat gdal_calc -help

Or you can also use the OSGeo4W Shell:

Start first the OSGeo4W Shell from the installed programs menu.

enter image description here

The list that you get with o-help contains gdal_calc

enter image description here

And you can start gdal_calc simply with "gdal_calc"

enter image description here

1
  • 1
    I had to setup enviroment with py3_env.bat in OSGeo shell
    – Albine Pro
    Apr 13, 2021 at 14:45
3

If you are using Anaconda/Miniconda/Miniforge/etc., use the %CONDA_PREFIX% environment variable:

python %CONDA_PREFIX%\Scripts\gdal_calc.py

There are other GDAL-related scripts available too:

dir %CONDA_PREFIX%\Scripts\gdal*
2
  • Hi Mike. I don't have gdal_calc.py or any of the gdal_merge.py, etc. scripts in that folder (C:/Users/<me>/Miniconda3/Scripts). I installed the qgis package via conda install qgis -c conda-forge. Not sure where or how to acquire those scripts? Additionally, I installed qgis software via OSGeo4W Express, as opposed to the standalone. Any ideas? Thanks! Apr 21, 2022 at 18:22
  • @NW_Photo_Laureate you'll need to do: conda install gdal -c conda-forge
    – Mike T
    Apr 21, 2022 at 21:24
2

This isn't strictly a GIS question, but its an issue that can crop up when installing FOSS4G software so its probably worth answering here.

Looks like gdal (or python, or both) either aren't on your system path, or the order in which they appear is wrong. This can happen when installing or updating older software, particularly if there's an old leftover entry in there from, say, another version of Python you may have had previously.

To view what's currently in the PATH variable, at a command prompt just type PATH and hit enter. The paths to the folders where python.exe and gdal_calc.py live should both be visible.

To edit the path, go to Control Panel > System > Advanced System Settings > 'Advanced' Tab > Environment Variables and scroll down in the list of entries under 'System Variables'. Do not delete anything, but you may need to add or reorder entries. Make sure they're separated properly by ;.

You may also want to check some other system variables. From another answer of mine - https://gis.stackexchange.com/a/258480/76240

1

For me worked simply running py3_env.bat after opening OSGeo4W Shell to setup Python environment variables and later executing directly 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.