I have multiple rasters, organized by years. I.e. raster y1 = from year 1 raster y2 = from year 2, etc...).
All of rasters contain values 0-11, representing forest (0-6) and disturbance (7-11). I would like to:
- Identify the forest on scene in y1 (by Reclassify tool)
Use the forest from y1 to Extract by mask raster y2
Identify the forest on scene in y2 (by Reclassify tool)
Use the forest from y2 to Extract by mask raster y3
Identify the forest on scene in y3 (by Reclassify tool)
- Use the forest from y3 to Extract by mask raster y4
etc for every year
However, I can't imagine how can I automate this process? I tried to run for loop
and advance the raster index value by one to move to following raster in a list of rasters. Instead I have created unfinite loop, where the for loop always start with the first raster in a list... Or should I use another type of loop?
My approach:
# Import system modules
import arcpy
from arcpy.sa import *
# Set environment settings
wd = "C:/Users/Projects/2017_deforest"
env.workspace = wd + "/raw/SAO"
arcpy.env.overwriteOutput = True
# Set local variables
rasters = arcpy.ListRasters("sao*", "IMG")
reclassField = "Value"
remap = RemapRange([[0,0, "NODATA"],
[1,6,1], # forest in that year
[7,11, "NODATA"], # disturbance
[12,255, "NODATA"]])
# Check out the ArcGIS Spatial Analyst extension licence
arcpy.CheckOutExtension("Spatial")
# Execute Reclassify
for raster in rasters:
# Identify forest in raster y1
tempForest = Reclassify(raster, reclassField, remap, "NODATA")
# Create list index to move to following raster in a list
rasNumber = 1
inRaster2 = rasters[rasNumber]
# Execute Extract by mask
outName = wd + "/outputSAO/" + inRaster2
outExtractByMask = ExtractByMask(inRaster2, tempForest)
# advance the index by one to move to another element in a list
rasNumber = rasNumber + 1
# Save the output
outExtractByMask.save(outName)
# Check in the ArcGIS Spatial Analyst extension license
arcpy.CheckInExtension("Spatial")