In documentation for gdal_calc it is stated Command line raster calculator with numpy syntax. Later on there are few examples where in one of them:
gdal_calc.py -A input.tif --outfile=result.tif --calc="A*(A>0)" --NoDataValue=0 - means set values of zero and below to null
Unfortunately there is no example on logical operators like:
--calc="A*(A>0 and A>B)"- means keep A if A bigger zero and bigger B and set the rest to null
Based on Numpy/Scipy logic functions I would expect to write logical operators as:
--calc="A*logical_and(A>0,A>B)"
I tried this and it seems to work but I would like to be assured that is correct.
In the similar way if you want minimum of A and B:
--calc="A*(A<=B)+B*(A>B)"
You can just write:
--calc="minimum(A,B)"
My issue is I can't find any cookbook to make sure I get this right. Is there some good cookbook with advanced examples of what is and is not possible with gdal_calc?
Answer
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
No comments:
Post a Comment