I have been using Python to perform gdal_warp commands like so:

options = f"-te {extent} -tr {pixelsize}"
interpolated_tif = gdal.Warp('c:/users/public/warped.tiff', 'c:/users/public/input.tif', options=options)

However, now I need to perform gdal_calc and gdal_edit commands in Python but there doesn't seem to be a gdal.Calc or gdal.Edit - how can I perform these commands then?


1 Answer 1

from osgeo_utils import gdal_calc
ds = gdal_calc.Calc()

The function takes the following arguments:

calc: MaybeSequence[str],
outfile: PathLikeOrStr | None = None,
NoDataValue: Number | None = None,
type: GDALDataType | str | None = None,
format: str | None = None,
creation_options: Sequence[str] | None = None,
allBands: str = "",
overwrite: bool = False,
hideNoData: bool = False,
projectionCheck: bool = False,
color_table: ColorTableLike | None = None,
extent: Extent | None = None,
projwin: Tuple | GeoRectangle | None = None,
user_namespace: Dict | None = None,
debug: bool = False,
quiet: bool = False,
progress_callback: Any = gdal.TermProgress_nocb,
**input_files: Any

Some examples from the docstring:

add two files together:
    Calc("A+B", A="input1.tif", B="input2.tif", outfile="result.tif")

average of two layers:
    Calc(calc="(A+B)/2", A="input1.tif", B="input2.tif", outfile="result.tif")

set values of zero and below to null:
    Calc(calc="A*(A>0)", A="input.tif", A_band=2, outfile="result.tif", NoDataValue=0)

work with two bands:
    Calc(["(A+B)/2", "A*(A>0)"], A="input.tif", A_band=1, B="input.tif", B_band=2, outfile="result.tif", NoDataValue=0)

sum all files with hidden noDataValue
    Calc(calc="sum(a,axis=0)", a=['0.tif','1.tif','2.tif'], outfile="sum.tif", hideNoData=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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