0

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'>
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 17, 2019 at 19:07
1
  • 2
    Use arcpy mapping and ListLayers. Then loop over the listed layers, for layer in listedlayers:... And use select_analysis instead of select by attributes - copy features - unselect. Commented Jul 17, 2019 at 19:13

1 Answer 1

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)
answered Jul 17, 2019 at 21:38
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.