1

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.

EDIT:

I think I just needed to change this line:

 outraster = Con((ras >= -0.4) & (ras !=0.00), ras)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Sep 17, 2015 at 20:15
3
  • Take a chance to add try: "CODE" except Exception, e: print e . This will allow You to detect error Commented Sep 17, 2015 at 21:47
  • nah, wasn't anaconda it always works fine. Was the reason listed below. Commented Sep 18, 2015 at 2:35
  • @spotter yes you need brackets around your expressions outraster = Con((ras >= -0.4) & (ras !=0.00), ras) Commented Sep 18, 2015 at 3:26

1 Answer 1

4

From the ArcGIS Help:

The Raster Calculator tool is intended for use in the ArcGIS Desktop application only as a GP tool dialog box or in ModelBuilder. It is not intended for use in scripting and is not available in the ArcPy Spatial Analyst module.

You should use arcpy.sa map algebra instead:

import arcpy 
from arcpy.sa import *
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:
 outraster = Raster(raster) >= 0.1
 #Save temp raster to disk with new name
 outraster.save(raster.replace('.tif','_water.tif'))
print('Done Processing')

Note: both your original expression and the map algebra version above will not extract the values of the cells, but output a 1 where the condition is met and 0 otherwise. If you want the actual cell values, you need to use the Con function:

ras = Raster(raster)
outraster = Con(ras >= 0.1, ras)

You may be having issues with the spaces in your path, it would be better to remove the spaces, but you could also try setting the scratch workspace to a folder without spaces, i.e arcpy.env.scratchWorkspace = r'C:\Temp'

answered Sep 17, 2015 at 23:19
4
  • I see, was not aware of that, this seems to work though thanks! Commented Sep 18, 2015 at 2:34
  • also, how would I edit it this to say everything >= 0.1, but !=0? would it just be >= 0.1, !=0 Commented Sep 18, 2015 at 2:40
  • well, 0.0 for me is actually no data, and my parameters have changes since initial post so I want everything >=-0.4, but != 0.00 but seems like it will just be raster >=-0.4 & raster !=0.00 Commented Sep 18, 2015 at 2:46
  • forgive me but new to all this, I added an edit to show you what I'm attempting Commented Sep 18, 2015 at 2:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.