gdal_calc.py– Raster Calculator using Numby Functions
Posted by smathermather on December 1, 2011
gdal_calc is a python script that makes it easy to do band math and logical operations with gdal/numby. This ranges from basic arithemtic operations to logical functions. And while gdal_calc.py has been around since 2008, it is but is a recent and revelatory discovery for me. I had just today resigned myself to properly learning python so as to use the gdal bindings. But, my laziness may continue unabated. Not learning Python. Now that is lazy.
Anyway, want to do band math? It’s as simple as follows (straight from the python script itself):
# example 1 - add two files together gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B" # example 2 - average of two layers gdal_calc.py -A input.tif -B input2.tif --outfile=result.tif --calc="(A+B)/2" # example 3 - set values of zero and below to null gdal_calc.py -A input.tif --outfile=result.tif --calc="A*(A>0)" --NoDataValue=0
So, I had little modification to average my set of images from PovRay:
gdal_calc.py -A input.tif -B input2.tif -C input3.tif -D input4.tif -E input5.tif --outfile=result.tif --calc="(A+B+C+D+E)/5"
I might write some bash wrappers to create built in functions for basic things I’ll do repeatedly, like average a set of images based on a directory listing. Of course, a little voice inside says “You should just do that in Python.” We’ll see.