Actually, it sounds like you want to find the smallest radius necessary to patch the raster. That can be obtained as the maximum of the Euclidean distance to the data cells.
To count the number of NoData cells in any raster, compute an indicator of those cells and sum it. The NoData indicator in ArcGIS is called IsNull (as shown in your sample code): by definition, it returns 1 at NoData cells and 0 elsewhere.
One (slightly hacky) way of doing it is to use an intermediate VRT with the gdalbuildvrt
command and specifying a new NoData value with the srcnodata
and vrtnodata
arguments. This does actually change the underlying values.
First, get your actual nodata value from gdalinfo
:
$ gdalinfo input.tif | grep No
NoData Value=3.39999995214436425e+38
Assign it to a bash variable: na=3.39999995214436425e+38
Or if you have jq
installed (in ubuntu/debian based Linux distros sudo apt install jq
):
na=$(gdalinfo input.tif -json | jq '.bands[0].noDataValue')
And use it in the following examples:
gdalbuildvrt -srcnodata ${na} -vrtnodata 0 output.vrt input.tif
However, it also sets those values to NoData in the output, so you need to specify a different value to be considered as NoData when converting to your final GeoTIFF.
gdal_translate -a_nodata ${na} output.vrt output.tif # original 3.39999995214436425e+38 values from input.tif are now 0
Chaining it together with a pipe so you don't need to clean up any intermediate VRT files:
gdalbuildvrt -srcnodata ${na} -vrtnodata 0 /vsistdout/ input.tif | gdal_translate -a_nodata -9999 /vsistdin/ output.tif
Best Answer
You could try something like this with gdal_translate