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'>
  • 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. – BERA Jul 17 at 19:13
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.