3

I'm trying to run a loop through a number of rasters while using SetNull but keep receiving an error message. I have worked on these rasters individually using SetNull and it it works, but would need to do this multiple times so a loop would be so much better.

I am new to Python.

Update - sorted it out! The script below shows the changes in comments

# basic mask example: SetNull according to criteria and output set to 1
# Description: Returns NoData to any input cell with a value greater than 120 in the output raster, and the remaining output cells will be set to 1.
# Requirements: Spatial Analyst extension

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = r'C:\Users\Documents\ArcGIS\pythontest\acc'
arcpy.env.scratchWorkspace = r'C:\Users\Documents\ArcGIS\pythontest\acc'

#Set local variables
rasters = arcpy.ListRasters()
#inRaster = rasters
inFalseRaster = 1
whereClause = "value > 120"
#output folder is now different bc don't want to overwrite
outfolder = r'C:\Users\Documents\ArcGIS\pythontest\test'

#Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Loop through list of rasters
for raster in rasters:
    #try/except has been dropped
    # Execute SetNull outSetNull = SetNull(inRaster, inFalseRaster, whereClause)
    #outSetNull = arcpy.gp.SetNull_sa(inRaster, inFalseRaster, "value > 120")
    # updated with correct variable from the for loop 
    # rephrased the SetNull function. arcpy.gp.SetNull_sa is not correct
    outSetNull = SetNull(raster, inFalseRaster, "value > 120")
    #print the output before attempting to save
    print(outSetNull)
    #import os module
    import os
    #Set output name for each output to be the same as the input
    # updated with correct variable from the for loop, attempting os module
    outSetNull = os.path.join(outfolder, raster)
    outSetNull.save(outras)

Old error message - refers to the line where outSetNull = arcpy.gp.SetNull_sa(raster, inFalseRaster, "value > 120") :

Runtime error Traceback (most recent call last): File "", line 31, in File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\geoprocessing_base.py", line 510, in return lambda *args: val(*gp_fixargs(args, True)) ExecuteError: ERROR 000877: Output raster: The grid or stack base name in C:\Users\Documents\ArcGIS\pythontest\acc\value > 120 has space.

  • 1
    Temporarily at least take off the try/except. We/you need to see the error produced, which is being suppressed by the try/except. – smiller Nov 7 '19 at 13:40
  • May need to capitalize - Value instead of value. – smiller Nov 7 '19 at 13:43
  • I've tried capitalizing Value instead and it runs the same either way. If I take out the try/except, this is the error message that I receive: Runtime error Traceback (most recent call last): File "<string>", line 27, in <module> File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\geoprocessing_base.py", line 510, in <lambda> return lambda *args: val(*gp_fixargs(args, True)) RuntimeError: Object: Error in executing tool – tsm Nov 7 '19 at 17:02
  • For formatting and for ease of access when people are reviewing your question, please edit your question to include details requested in the comments. – smiller Nov 7 '19 at 17:04
  • Is anything returned by arcpy.GetMessages()? – smiller Nov 7 '19 at 17:06
2

Earlier in the code inRaster is assigned to the output from ListRasters(), which makes it a list of rasters. This variable is called in the for loop, instead of the variable raster, and doesn't appear to be used elsewhere in the code. The code below just comments out inRaster and changes the variable from inRaster to raster within the loop.

Similarly, the output line of outSetNull = outfolder + "\\" + str(rasters) is inputting the entire list of rasters rather than the current raster.

Removing the try/except can help troubleshoot these errors because it doesn't suppress the error messages.

# basic mask example: SetNull according to criteria and output set to 1
# Description: Returns NoData to any input cell with a value greater than 120 in the output raster, and the remaining output cells will be set to 1.
# Requirements: Spatial Analyst extension

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = r'C:\Users\Documents\ArcGIS\pythontest\acc'
arcpy.env.scratchWorkspace = r'C:\Users\Documents\ArcGIS\pythontest\acc'

#Set local variables
rasters = arcpy.ListRasters()
#inRaster = rasters  # commented this out
inFalseRaster = 1
outfolder = r'C:\Users\Documents\ArcGIS\pythontest\acc'

#Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Loop through list of rasters
for raster in rasters:
    # Execute SetNull outSetNull = SetNull(inRaster, inFalseRaster, whereClause)
    #outSetNull = arcpy.gp.SetNull_sa(inRaster, inFalseRaster, "value > 120")
    # update with correct variable from the for loop
    outSetNull = arcpy.gp.SetNull_sa(raster, inFalseRaster, "value > 120")
    #Set output name for each output to be the same as the input
    # update with correct variable from the for loop
    outras = os.path.join(outfolder, raster)
    #(or back to your original: outras = outfolder + "\\" + raster)
    outSetNull.save(outras)

You may also want to look into the a) os module or b) test whether this tool writes to the workspace by default. For a) you could write the output as outSetNull = os.path.join(outfielder, raster). For b) you would just write the output as outSetNull = raster. (In either case I'm not including str because the output from ListRasters() is a list of strings - the names of the rasters.) If you want to modify the output name, look into string formatting. e.g. outSetNull = os.path.join(outfielder, "ras2{}".format(raster))

Finally, are you trying to overwrite the existing rasters? If the list of rasters is coming from the same outfolder with the same output name, I anticipate it trying to overwrite, and likely failing. I don't know if this tool obeys overwriteOutput environment setting. To be safe, either output to a different directory or use the string formatting method to change your output raster name.

  • As mentioned above in my edited question, the error I get when I run it without the try/except says that my grid or stack base name has a space. I have check and it doesn't. It also doesn't surpass the character limit for the name or for the path. – tsm Nov 8 '19 at 10:39
  • The latest error is caused by the same type of issue as before -- outSetNull uses a list rasters instead of the current raster within the list. I didn't look at this line earlier since it was commented out. You may also want to use string formatting and the os module rather than string concatenation when building the output path. I'll update my answer. – smiller Nov 8 '19 at 12:52
  • Ah good catch. I made some changes and will update my question. I"ll delete some of that old script and comment in where the changes have been made (following your example). Still, however, the error message I get refers to the 'outSetNull = arcpy.gp.SetNull_sa(raster, inFalseRaster, "value > 120")' line so I suspect this means my changes to the output path haven't been read yet. – tsm Nov 8 '19 at 15:22
  • Print the output before attempting the save. What are you getting? Also note you are currently not trying to save. At what point does the error appear? – smiller Nov 8 '19 at 18:07
  • Updated code, had missed the .save method on the raster. – smiller Nov 9 '19 at 23:43

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.