There are several questions about conditional reclassification on the Stack Exchange, but I haven't been able to find exactly what I need...
In my workspace, I have a list of raster layers (8 bit unsigned integer) that have values from 0-255. I am attempting to use conditional statements within the raster calculator to create a binary (0 and 1) layer in which only values between 90-100 are reclassified to 1 and the remaining values are reclassified to 0.
The code snippet I am working with can be seen below
# IMPORT MODULES
import arcpy, os
# SET GEO_PROCESSING ENVIRONMENTS
arcpy.env.workspace = r"C:\Users\BP_Mosaics"
outws = r"C:\Users90円BP_Mosaics"
# CREATE A LIST OF THE MOSAIC RASTER LAYERS
rasterList = arcpy.ListRasters("*", "tif")
# CREATE LOOP TO RECLASSIFY ALL BP MOSAICS INTO 0s and 1s
# CREATE LOOP TO RECLASSIFY ALL BP MOSAICS INTO 0s and 1s
for i in rasterList:
inputRaster = (os.path.join(env.workspace, i))
filename = i[-18:-4] + '_01.tif'
outname = os.path.join(outws, filename)
arcpy.gp.RasterCalculator_sa("Con(((" + inputRaster + ">= 90) & (" + inputRaster + "<= 100)), 1, 0)", outname)
I am not seeing any immediate errors in the code, however something does not seem to be working properly as I receive the following error message...
arcgisscripting.ExecuteError: ERROR 000539: (null)
Failed to execute (RasterCalculator).
Would anyone happen to know what is happening here?
-
Import SA functions at the beginning as shown in help. Do R = Raster (I) and S =Con((R>90)&(R<100),1). R.save(out)FelixIP– FelixIP2020年05月21日 19:46:08 +00:00Commented May 21, 2020 at 19:46
-
Would you mind elaborating on this?Eli Simonson– Eli Simonson2020年05月21日 20:14:00 +00:00Commented May 21, 2020 at 20:14
-
See example 3, desktop.arcgis.com/en/arcmap/10.3/tools/spatial-analyst-toolbox/…FelixIP– FelixIP2020年05月21日 21:15:07 +00:00Commented May 21, 2020 at 21:15
1 Answer 1
I think there are 2 problems: i
coming from your rasterList
is not being constructed correctly and how you are building your expression to use in CON().
Firstly you create rasterList
by searching a workspace and this returns a list of raster names, not the full path to the raster. So your input into your CON() expression should be something like:
inputRaster = os.path.join(outws, i)
arcpy.gp.RasterCalculator_sa("Con(((" + inputRaster +" >= 90) & (" + inputRaster +" <= 100)), 1, 0)", outname)
UPDATE:
I realised that you don't really need to call the CON tool within a Raster calculator, you are boxing a tool within a tool. So you just need to call the CON tool which has much easier syntax to understand. I also show you best practise which you were not doing, checking in/out extensions and encapsulating your code within a try/except to capture errors.
Having reviewed your updated code I have made the following edits so that it works, just change the paths to your requirements:
# IMPORT MODULES
import arcpy, os
arcpy.CheckOutExtension('Spatial')
try:
# SET GEO_PROCESSING ENVIRONMENTS
arcpy.env.workspace = r"D:\GIS\GIS Data"
outws = r"D:\GIS"
# CREATE A LIST OF THE MOSAIC RASTER LAYERS
rasterList = arcpy.ListRasters("*", "tif")
# CREATE LOOP TO RECLASSIFY ALL BP MOSAICS INTO 0s and 1s
for i in rasterList:
inputRaster = (os.path.join(arcpy.env.workspace, i))
filename = i[-18:-4] + '_01.tif'
outname = os.path.join(outws, filename)
arcpy.gp.Con_sa(inputRaster, "1", outname, "0", '"Value" >=90 AND "Value" <=100')
except arcpy.ExecuteError:
# Geoprocessor threw an error
print(arcpy.GetMessages(2))
finally:
arcpy.CheckInExtension('Spatial')
-
I adjusted my for loop as follows
for i in rasterList: inputRaster = os.path.join(env.workspace, i) filename = i[-18:-4] + '_01.tif' outname = os.path.join(outws, filename) arcpy.gp.RasterCalculator_sa(Con(((inputRaster >= 90) & (inputRaster <= 100)), 1, 0), outname)
and obtain the following error message.. "Type Error '>=' not supported between instances of 'str' and 'int'"Eli Simonson– Eli Simonson2020年05月22日 14:47:01 +00:00Commented May 22, 2020 at 14:47 -
Look at my CON expression, its enclosed in quotes, yours is not.Hornbydd– Hornbydd2020年05月22日 15:21:17 +00:00Commented May 22, 2020 at 15:21
-
Can you update your question with the new fully updated code, add it as an amendment.Hornbydd– Hornbydd2020年05月22日 16:03:13 +00:00Commented May 22, 2020 at 16:03
-
See update above..Eli Simonson– Eli Simonson2020年05月22日 17:11:06 +00:00Commented May 22, 2020 at 17:11
-
1Indeed, your correction made the code work as I was hoping it would. Thank you for your assistance!!Eli Simonson– Eli Simonson2020年05月22日 18:52:19 +00:00Commented May 22, 2020 at 18:52