I am new to Python and ArcPy, but I would like to automate this process and/or make my code more efficient here. I am basically selecting a layer by attribute, then copying features from the (selected) layer, and then clearing the selection and starting over again with another layer. I need to do this for many layers in a row in my map document. I have attached my Python workflow for two layers below as an example.
>>> arcpy.SelectLayerByAttribute_management("Outlet_A", "NEW_SELECTION", "Struc_Rec = 'Major Repair'")
<Result 'Quad A\\OM_Rec\\Outlet_A'>
>>> arcpy.CopyFeatures_management("Outlet_A", "MajorRepairOutlet_A")
<Result 'X:/data.gdb\\MajorRepairOutlet_A'>
>>> arcpy.SelectLayerByAttribute_management("Outlet_A", "CLEAR_SELECTION")
<Result 'Quad A\\OM_Rec\\Outlet_A'>
>>> arcpy.SelectLayerByAttribute_management("Inlet_A", "NEW_SELECTION", "Struc_Rec = 'Major Repair'")
<Result 'Quad A\\OM_Rec\\Inlet_A'>
>>> arcpy.CopyFeatures_management("Inlet_A", "MajorRepairInlet_A")
<Result 'X:/data.gdb\\MajorRepairInlet_A'>
>>> arcpy.SelectLayerByAttribute_management("Inlet_A", "CLEAR_SELECTION")
<Result 'Quad A\\OM_Rec\\Inlet_A'>
1 Answer 1
I'd create a list of tuples. The tuples would contain feature classes, their related query, and the output feature class.
values = [("Outlet_A", "Struc_Rec = 'Major Repair'", "MajorRepairOutlet_A"),
("Inlet_A", "Struc_Rec = 'Major Repair'", "MajorRepairInlet_A")] #add all inputs
#iterate
for inFc, sql, outFc in values:
arcpy.Select_analysis (inFc, outFc, sql)
Explore related questions
See similar questions with these tags.
for layer in listedlayers:
... And use select_analysis instead of select by attributes - copy features - unselect.