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
4 Answers 4
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.
The list that you get with o-help contains gdal_calc
And you can start gdal_calc simply with "gdal_calc"
-
1I had to setup enviroment with
py3_env.bat
in OSGeo shellAlbine Pro– Albine Pro2021年04月13日 14:45:35 +00:00Commented Apr 13, 2021 at 14:45
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*
-
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!NW_Photo_Laureate– NW_Photo_Laureate2022年04月21日 18:22:30 +00:00Commented Apr 21, 2022 at 18:22 -
@NW_Photo_Laureate you'll need to do:
conda install gdal -c conda-forge
Mike T– Mike T2022年04月21日 21:24:01 +00:00Commented Apr 21, 2022 at 21:24
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
For me worked simply running py3_env.bat
after opening OSGeo4W Shell to setup Python environment variables and later executing directly gdal_calc
python gdal_calc.py -A ...
. Also try tocd
into the GDAL bin folder before executing or call it like this:python "C:\Program Files\QGIS 3.0\bin\gdal_calc.py" -A ...