I have a gdb of 50 polygon fcs named based on HUC # abbreviation - ie: HUC_002, HUC_003, etc.
I have another gdb of rasters also based on the same state abbreviation - ie: Pluvial_002, Pluvial_003.
I want to create a short scripts that loops through the second gdb and extracts each of the rasters by the features as masks and output to a new gdb. For example, I want to take the raster named pluvial_002 and grab the fc named HUC_002 and use it to extract the raster by mask and output it to floodplain.gdb. Attached is my script - it runs for 2 seconds and says it's complete, but outputs nothing.
TIA!
#fgdb of rasters being clipped
source_fgdb = arcpy.GetParameterAsText(0)
#fgdb of features doing the clipping
clipping_fgdb = arcpy.GetParameterAsText(1)
#preferably empy fgdb to put outputs
output_fgdb = arcpy.GetParameterAsText(2)
#list all the rasters
arcpy.env.workspace = source_fgdb
source_rasts = arcpy.ListRasters()
arcpy.env.workspace = clipping_fgdb
clipping_fcs = arcpy.ListFeatureClasses()
#make a dictionary of source fc path
#to clipping fc and source name
#NB relies on names before the '_' being equal
source_clip_fc_dict = {}
for source_rast in source_rasts:
for clipping_fc in clipping_fcs:
if source_rast.split("_")[0] == clipping_fc.split("_")[0]:
source_rast_path = os.path.join(source_fgdb, source_rast)
clipping_fc_path = os.path.join(clipping_fgdb, clipping_fc)
source_clip_fc_dict[source_rast_path] = [clipping_fc_path, source_rast]
#clip each source feature and output to output_fgdb
for source_rast_path in source_clip_fc_dict:
clipping_fc_path = source_clip_fc_dict[source_rast_path][0]
source_rast_name = source_clip_fc_dict[source_rast_path][1]
out_rast_name = source_rast_name + "_Mask"
out_rast_path = os.path.join(output_fgdb, out_rast_name)
OutExtractbyMask = ExtractByMask(source_rast_path, clipping_fc_path)
OutExtractByMask.save(out_rast_path)
Solved! Go to Solution.
You need to define the out raster path within the loop (just below clipping_fc_path)
out_raster_name = source_rast_name + "_Mask"
out_raster_path = os.path.join(output_fgdb, out_raster_name).
I don't have anything to test on, so I would suggest a well speckled interjection of print statements before and within your for and if statements to see what is going on
Thank you. I was able to figure out the issue in defining things that weren't defined! What is the general rule for the matching of things before and after the "_"? If I'm looking to match string before the _ then it's [1] but after it is [-1]? Is there ever a [0]?