Skip to main content
5 of 7
added 835 characters in body
spotter
  • 101
  • 1
  • 5
  • 13

Using Loop with Raster Calculator in ArcPy?

I am trying to loop through multiple rasters and extract cells that are above 0.1 The code I am trying to use is:

import arcpy 
arcpy.CheckOutExtension('Spatial')
arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\Indices\Main\NDWI\main_master'
#pathway to all rasters in workspace directory
rasters = arcpy.ListRasters('*.tif*')
for raster in rasters:
     #give new file a name
     outraster = raster.replace('.tif','_water.tif')
     arcpy.gp.RasterCalculator_sa("""raster >= 0.1""",outraster)
print('Done Processing')

For some reason I can't copy and paste the error (python actually shuts down when I run this code) but here is a screen shot of part of it.

enter image description here

EDIT: My parameters have changed and I now need everything >=-0.4 and but !=0.00. I am trying to use:

import arcpy 
from arcpy.sa import *
arcpy.CheckOutExtension('Spatial')
arcpy.env.workspace=r'D:\Sheyenne\Atmospherically Corrected Landsat\Indices\Main\NDWI\main_master'
#pathway to all rasters in workspace directory
rasters = arcpy.ListRasters('*.tif*')
for raster in rasters:
    #Save temp raster to disk with new name
    ras = Raster(raster)
    outraster = Con(ras >= -0.4 & ras !=0.00, ras)
    outraster.save(raster.replace('.tif','_water.tif'))
print('Done Processing')

but this returns:

ValueError: The truth value of a raster is ambiguous. Invalid use of raster with Boolean operator or function. Check the use of parentheses where applicable.
spotter
  • 101
  • 1
  • 5
  • 13