1

I am trying to find a way of looping through Rasters in a directory, (a collection of individual tiff images in a folder) and selecting by name only those rasters that contain noData values and passing those TIFF file names into a list. By inspection most of the TIFF files do not have noData values in them. However a number do and I would like to be able to achieve this programmatically. I am trying to do this using arcpy / ArcGIS tools and modules

My code so far is below, but it is not picking out the rasters where I know there are no data values

What is wrong?

import arcpy
arcpy.env.workspace = r'C:\myRasters\...'

rasterList = arcpy.ListRasters()

incompleteRasters = []

for raster in rasterList:
    rasterObj = arcpy.Raster(raster)
    print(rasterObj.name)

    if rasterObj.noDataValue == True:
        incompleteRasters.append(rasterObj)           
        print("NoData Value: %s \n" %noData, "in ", rasterObj.name)
    else:
        print("all ok")

print('All', len(rasterList), 'tifs have been checked')

2

What you script is doing at the moment is just testing if the raster's noDataValue is set to True, it's not actually checking if there are noData values, but what value has been specified to represent noData.

I think you need to use the GetRasterProperties function and call out the ANYNODATA property.

Try this edited version of your script:

import arcpy
arcpy.env.workspace = r'C:\myRasters\...'

rasterList = arcpy.ListRasters()

incompleteRasters = []

for raster in rasterList:
    rasterObj = arcpy.Raster(raster)
    print(rasterObj.name)

    # Get the raster properties (this returns a string, not an integer)
    noDataFound = arcpy.GetRasterProperties_management(rasterObj, "ANYNODATA").getOutput(0)

    if noDataFound == "1":
        incompleteRasters.append(rasterObj)           
        print("NoData Value: %s \n" %noData, "in ", rasterObj.name)
    else:
        print("all ok")

print('All', len(rasterList), 'tifs have been checked')
1
  • Thanks Dan. This is doing exactly what I wanted. I was heading down the path of trying something with arcpy.Describe(raster) .. but your suggestion is much slicker.. Cheers
    – TrevP
    Dec 5 '21 at 11:43
0

If you are on Windows and you have GDAL installed and you just need to get the job done fast you could try this one-liner from command line:

for /f  %i in ('dir /b *.tif') do gdalinfo %i |find "NoData"

The output looks like this:

>gdalinfo File1.tif   | find "NoData"
>gdalinfo File2.tif   | find "NoData"
  NoData Value=255
>gdalinfo File3.tif   | find "NoData"

File2.tif has nodata set to value 255, files File1.tif and File3.tif do not have nodata.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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