I am trying to loop through several rasters and use them as the mask to extract from one large source raster. This script works well but only outputs one output for the first raster in the workspace. I've tried multiple things, but nothing seems to work. Thank you!
import arcpy
import os
import sys
import arcpy
from arcpy import env
from arcpy.sa import Con
from arcpy.sa import *
#Now clipping the land cover grids to each MR FATHOM boundary by HUC2
NLCD19 = r'E:\FATHOM\2022 Update\NLCD2019.gdb\NCLD2019_PrjAlbers'
cropscape = r'E:\FATHOM\2022 Update\Cropscape2021.gdb\Cropscape21_prj'
HR_FloodplainBounds_fgdb = r'E:\FATHOM\2022 Update\Final_Floodplain_Bounds_HR_expand.gdb'
NLCDoutput_fgdb = r'E:\FATHOM\2022 Update\HR_NLCD19_Clip_Floodplains_HUC02.gdb'
Cropscapeoutput_fgdb = r'E:\FATHOM\2022 Update\HR_Cropscape19_Clip_Floodplains_HUC02.gdb'
arcpy.env.workspace = HR_FloodplainBounds_fgdb
HR_FloodplainBounds_rasters = arcpy.ListRasters()
print (HR_FloodplainBounds_rasters)
# First for NLCD 2019
for HR_FloodplainBounds_raster in HR_FloodplainBounds_rasters:
arcpy.env.cellSize = 30
NLCD19 = r'E:\FATHOM\2022 Update\NLCD2019.gdb\NCLD2019_PrjAlbers'
NLCDoutput_fgdb = r'E:\FATHOM\2022
Update\HR_NLCD19_Clip_Floodplains_HUC02.gdb'
source_raster_path = NLCD19
clipping_fc_path = os.path.join(HR_FloodplainBounds_fgdb,
HR_FloodplainBounds_raster)
out_raster_name = "NLCD19" + HR_FloodplainBounds_raster
arcpy.env.mask = clipping_fc_path
arcpy.env.snapRaster = clipping_fc_path
arcpy.env.extent = clipping_fc_path
out_raster_path = os.path.join(NLCDoutput_fgdb, out_raster_name)
arcpy.env.workspace = NLCDoutput_fgdb
OutExtractByMask = arcpy.sa.ExtractByMask(source_raster_path,
clipping_fc_path)
OutExtractByMask.save(out_raster_path)
Solved! Go to Solution.
I figured it out .. ExtractByMask.save needs to be ExtractByMask.Save, with a capital S. Ulgh!
Reviewing your code, it looks like it should work, although you have unnecessary code within the loop. What does the line print (HR_FloodplainBounds_rasters) return?
I figured it out .. ExtractByMask.save needs to be ExtractByMask.Save, with a capital S. Ulgh!