gdal_calc raster calculator syntax for logical operators and other functions

In the source for gdal_calc.py, the calculation is made directly using eval:

myResult = eval(opts.calc, global_namespace, local_namespace)

That would suggest that any well-formed expression that also evaluates on the command line will work. According to the documentation, you may use gdalnumeric syntax with +-/*, and/or numpy functions. You can test your functions using small dummy arrays in the interactive shell, then use the same calls in gdal_calc.

Keep in mind that chaining together multiple numpy functions is likely to produce temporary in-memory arrays that can substantially increase memory usage, especially when dealing with large images.

You can look at the numpy documentation for a list of all the functions: routines. The ones you are after are likely here: math or here: routines.logic.

This is where functions like minimum are coming from, it's just that the namespace is already imported. Really, it's numpy.minimum, etc


Following on from Benjamin's answer, you can use logical_or() or logical_and(). See http://docs.scipy.org/doc/numpy/reference/routines.logic.html. The following example worked nicely for me. This sets all values between 177 and 185 (inclusive) to 0, which is then treated as nodata.

gdal_calc.py -A input.tif --outfile=output.tif --calc="A*logical_or(A<=177,A>=185)" --NoDataValue=0