How to reclassify multiple raster files based on band values in Python

771
0
09-25-2020 05:10 AM
KK2
by
New Contributor III

I would like to reclassify several raster files (having four bands) into 0,1 binary raster files based on raster values of each band. I would like to assign 1 into all pixels in the image that fulfil the following Raster Calculator assumption:

("_21_cropped.tif - Band_1" == 170) & ("_21_cropped.tif - Band_2" == 255) & ("_21_cropped.tif - Band_3" == 170)

I have found that I can do this automatically with ArcPy with this code (also attached below). I should add the assumption on the penultimate line here: <desired_value> However I don't know how to implement the assumption mentioned above so that it works. I would be grateful for any tips in this matter.

# Import the necessary module
import arcpy
from arcpy import env
from arcpy.sa import *

# Specify the desired workspace
arcpy.env.workspace = r"L:\processing\forest.gdb"
arcpy.env.scratchWorkspace = r"L:\processing\forest.gdb"

# Print the list of available raster files in the folder
raster_list = arcpy.ListRasters("*")
print (raster_list)

# Check out the Spatial Analyst extension.
try:
if arcpy.CheckExtension("Spatial") == "Available":
arcpy.CheckOutExtension("Spatial")
print ("Checked out \"Spatial\" Extension")
else:
raise LicenseError
except LicenseError:
print "Spatial Analyst license is unavailable"
except:
print arcpy.GetMessages(2)

# Loop through all the raster files, perform the calculation, and specify the desired save folder.
for raster in raster_list:
ras = raster_file(raster)
outraster = ras * <desired_value>
outraster.save(r"L:\processing\forest.gdb" + "\\" + raster)

0 Kudos
0 Replies