1

Can anybody help me to solve a problem with running gdal_calc for many rasters?

I have several multispectral images and I need a batch to perform NDVI calculation.

I started with gdal code for one raster and it works good:

> C:\Python27\python.exe "C:\Program Files\QGIS Wien\bin\gdal_calc.py" -A D:\my_dir\ras0_2.TIF --A_band=3 -B D:\my_dir\ras0_2.TIF --B_band=4 --outfile=D:\my_dir\ndvi.tif --calc=((B-A)/(B+A))

But when I tried to run code for all TiFFs in a folder the program gave an error "nonetype object has no attribute GetRasterBand"

for %i in (*.tif) do C:\Python27\python.exe "C:\Program Files\QGIS Wien\bin\gdal_calc.py" -A $i --A_band=3 -B $i --B_band=4 --calc="((B-A)/(B+A))" %i ndvi/ndvi_%i

3

Nearly there. Couple of small problems:

  • You've used the bash syntax instead of cmd. i.e. $ instead of %.
  • Your --outfile parameter isn't specified explicitly, and looks a bit jumbled.

Try:

for %i in (*.tif) do C:\Python27\python.exe "C:\Program Files\QGIS Wien\bin\gdal_calc.py" -A %i --A_band=3 -B %i --B_band=4 --calc="((B-A)/(B+A))" --outfile=ndvi_%i

If you have osgeo4w installed you can go open the osgeo4w shell, cd to the directory and run:

for %i in (*.tif) do gdal_calc -A %i --A_band=3 -B %i --B_band=4 --calc="((B-A)/(B+A))" --outfile=ndvi_%i

If you want a batch file to do this you'll need to use two percent signs before your variables - i.e. %%i

  • I've tested two scripts in my osgeo4w shell command line. The first one works fine, the operation is done, but I get the following error message : "Python has stopped working" after each processed TIF-file... When I use second script you recomended for osgeo4w (with *comp.tif) it don't work at all. Also I cleated a .bat file and tried to run it, but nothing happend. – MayLadyBeetle Oct 23 '17 at 9:41
  • Glad to help. the comp bit was just from my system. you want *.tif. I've amended my answer – RoperMaps Oct 23 '17 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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