I have a gdb of 50 polygon fcs named based on state abbreviation - ie: AR_HUC12, AL_HUC12, etc.
I have another gdb of polygon fcs also based on the same state abbreviation - ie: AR_wetlands, AL_wetlands.
I want to create a short scripts that loops through the second gdb and clips each of these fcs by the fc in the first gdb that has the same name. For example, I want to take the fc named AR_wetlands and clip it by the fc named AR_HUC12.
Is there a quick script that will do this? I am very much a beginner!
Solved! Go to Solution.
it's not going to win any beauty contests, but it'll work (underlying premise is that everything before the _ in your fc names matches in each fgdb).
import arcpy
import os
#not a fan of stating the obvious, but change the paths below to your fgdbs
#also keep the r then surround the path in quotes
#fgdb of features being clipped
source_fgdb = r'D:\a.gdb'
#fgdb of features doing the clipping
clipping_fgdb = r'D:\b.gdb'
#preferably empy fgdb to put outputs
output_fgdb = r'D:\c.gdb'
#list all the feature classes
arcpy.env.workspace = source_fgdb
source_fcs = arcpy.ListFeatureClasses()
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_fc in source_fcs:
for clipping_fc in clipping_fcs:
if source_fc.split("_")[0] == clipping_fc.split("_")[0]:
source_fc_path = os.path.join(source_fgdb, source_fc)
clipping_fc_path = os.path.join(clipping_fgdb, clipping_fc)
source_clip_fc_dict[source_fc_path] = [clipping_fc_path, source_fc]
#clip each source feature and output to output_fgdb
for source_fc_path in source_clip_fc_dict:
clipping_fc_path = source_clip_fc_dict[source_fc_path][0]
source_fc_name = source_clip_fc_dict[source_fc_path][1]
out_fc_name = source_fc_name + "_Clip"
out_fc_path = os.path.join(output_fgdb, out_fc_name)
arcpy.Clip_analysis(source_fc_path, clipping_fc_path, out_fc_path)
it's not going to win any beauty contests, but it'll work (underlying premise is that everything before the _ in your fc names matches in each fgdb).
import arcpy
import os
#not a fan of stating the obvious, but change the paths below to your fgdbs
#also keep the r then surround the path in quotes
#fgdb of features being clipped
source_fgdb = r'D:\a.gdb'
#fgdb of features doing the clipping
clipping_fgdb = r'D:\b.gdb'
#preferably empy fgdb to put outputs
output_fgdb = r'D:\c.gdb'
#list all the feature classes
arcpy.env.workspace = source_fgdb
source_fcs = arcpy.ListFeatureClasses()
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_fc in source_fcs:
for clipping_fc in clipping_fcs:
if source_fc.split("_")[0] == clipping_fc.split("_")[0]:
source_fc_path = os.path.join(source_fgdb, source_fc)
clipping_fc_path = os.path.join(clipping_fgdb, clipping_fc)
source_clip_fc_dict[source_fc_path] = [clipping_fc_path, source_fc]
#clip each source feature and output to output_fgdb
for source_fc_path in source_clip_fc_dict:
clipping_fc_path = source_clip_fc_dict[source_fc_path][0]
source_fc_name = source_clip_fc_dict[source_fc_path][1]
out_fc_name = source_fc_name + "_Clip"
out_fc_path = os.path.join(output_fgdb, out_fc_name)
arcpy.Clip_analysis(source_fc_path, clipping_fc_path, out_fc_path)
No problem, also if you think that's the correct answer please mark it so.
I'd recommend stating this as a new problem in the forum, stating what you intend the script to do exactly, and then actually post the code as text (properly formatted) as a screenshot is maybe ok for capturing what an error message looks like, but not blocks of code someone would have to type out to correct/modify.