2

For a large number of rasters, I need to define a pixel value less than zero as being Null and output the rasters. However, my simple SetNull loop (below) only processes the first raster in my list as Null, before stopping and not looping through the rest.

Triple commented ### is a dummy ExtractByMask function which I used to test the loop. When commented in, the ExtractByMask does loop successfully through all my rasters, indicating the loop itself seems okay. Can someone please advise, is there something specific I am not doing with SetNull please?

import arcpy, os, sys
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

arcpy.env.workspace = "C:\\GIS\\in" # set up a folder containing rasters to clip
outws = "C:\\GIS\\out" # set up a folder to output clipped rasters to
#mask = "C:\\GIS\\dump\\pan.shp" # shapefile that defines clip

rasterlist = arcpy.ListRasters("*", "tif")
print(len(rasterlist))


for i in rasterlist:
    print i
    temp_nulled = SetNull(i,i,"VALUE < 0")
    ####temp_extracted = ExtractByMask(i, mask)
    rastername = str(i) # converts loop(i) raster name into a string
    rastername_crop = rastername[:7] # takes first n characters of raster name
    outname = os.path.join(outws, rastername_crop + '_NULL.tif') # Create the full out path
    ####outname = os.path.join(outws, rastername_crop + '_EXTRACT.tif')
    print outname
    temp_nulled.save(outname)
    ###temp_extracted.save(outname)
  • 1
    print(len(rasterlist)) is printing more than 1? – BERA Jul 4 '18 at 13:28
  • 2
    are the first 7 characters of raster name different for each raster (i.e. the output name is also different for each raster) – JGH Jul 4 '18 at 13:35
  • Hi Bera and JGH, thanks for good points. Yes, print(len(rasterlist)) returns 6, and the input rasters are different names (they represent different daily dates). – J_goss Jul 5 '18 at 14:00

Your Answer

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

Browse other questions tagged or ask your own question.