[GIS] Gdal_calc NDVI for many rasters

batchgdalgdal-calcmap-algebra

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

Best Answer

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

Related Question