1

I am using a Python script for running an iteration of raster calculator. The operation is a conditional clause for changing no-data values to "0". The script is the following

import arcpy 
from arcpy.sa import *
 
arcpy.env.workspace = r'F:\GIS\SourceData\Model\EnergyWet'
for r in arcpy.ListRasters("*"): # list all rasters in workspace
    outRaster = Con(IsNull(Raster(r)),0,Raster(r)) #perfoms Conditional
    outRaster.save(r['F:\GIS\SourceData\Model\EnergyWet\zeroed\-4'] + "out.tif") #save output

It appears to work, depending on the route in the last line, But it only saves the last iteration.

What is wrong with my script?

1 Answer 1

1

Where you have an output file name of:

r['F:\GIS\SourceData\Model\EnergyWet\zeroed\-4'] + "out.tif"

you have a random variable called r next to a list, so that's bad syntax and apparently you are concatenating a string to a list which makes no sense.

Ignoring all the syntax issues nothing about the output name changes so the logic of your script is to constantly overwrite its output.

A better script would be:

import arcpy 
from arcpy.sa import *
 
arcpy.env.workspace = r'F:\GIS\SourceData\Model\EnergyWet'
for r in arcpy.ListRasters("*"): # list all rasters in workspace
    outRaster = Con(IsNull(Raster(r)),0,Raster(r)) #perfoms Conditional
    outRaster.save(r"F:\GIS\SourceData\Model\EnergyWet\zeroed\" + r + ".tif")
2
  • Thanks a lot for your help! The syntax seems to work now, but I still have the same issue, The script seems to run all the operations but it only returns the last one as output. How can I make the script save every single output in the iteration? I appreciate ny mre guidance since I'm new to python scripting
    – Josh
    Commented Jun 26, 2022 at 21:13
  • Edit your question show whats in the folder EnergyWet. Put a print statement within the loop to see what r is.
    – Hornbydd
    Commented Jun 26, 2022 at 22:03

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.