0

I have dozens of feature classes in a project geodatabase. I have the code that will identify all the misplaced wells in each dataset, but for the life of me I can't figure out how to get the lines of code into a for loop. I need to run each one through this process to get the output I need:

 1. arcpy.management.SelectLayerByAttribute("Counties", "NEW_SELECTION", "NAME = 'CountyName'", "NON_INVERT")
 2. arcpy.management.SelectLayerByLocation("CountyName_WaterWells", "INTERSECT", "Counties", None, "NEW_SELECTION", "INVERT")
 3. arcpy.management.CopyFeatures("CountyName_WaterWells", r"C:\Users\UserName\Documents\ArcGIS\Projects\Water Well Outliers Script\Water Well Outliers Script.gdb\CountyName_WaterWells_Outliers", '', None, None, None)

"Counties" is my polygon feature class, "CountyName_WaterWells" are my water well feature classes, "NAME" is the field name for "CountyNames" in the "Counties" feature class.

I can just keep using these lines of code and replace the county names, but then I'd have over 250 lines of code.

4
  • 1
    If you are not familiar with basic looping in python then I suggest you explore model builder and the feature iterator to loop over values as what you are asking is quite trivial in modelbuilder. It's all in the help file.
    – Hornbydd
    Jan 25 at 17:25
  • 2
    I suggest reading up on Walk - ArcGIS Pro | Documentation, "Returns data names in directory and database structures by moving through the tree from the top down or the bottom up."
    – bixb0012
    Jan 25 at 17:38
  • 1
    Basic Python looping is pretty basic. You could learn it in 5 minutes (the only prerequisites are variables and lists).
    – Vince
    Jan 25 at 18:28
  • Yeah, these comments really aren't helpful. I understand how loops work. I understand what walk is. What I don't understand is how to combine three geoprocessing tools within the loop and how to configure the parameters so I don't have to hard code them.
    – redleg_64
    Jan 25 at 18:34

1 Answer 1

2

One option is to use ListFeatureClasses() which will provide a list of the feature classes in the workspace. Assuming the feature classes are all in the same geodatabase, you can set the workspace to your geodatabase. Something else that might help is to chain the selections and capture the output of the previous selection so you can use it later.

Something along these lines:

arcpy.env.workspace = r"C:\Users\UserName\Documents\ArcGIS\Projects\Water Well Outliers Script\Water Well Outliers Script.gdb"
fcs = arcpy.ListFeatureClasses("*_WaterWells")
for fc in fcs:
    countyName = fc.split("_")[0] #assuming that the feature classes all have the same pattern
    sel_counties = arcpy.management.SelectLayerByAttribute("Counties", "NEW_SELECTION", "NAME = '{}'".format(countyName), "NON_INVERT").getOutput(0)
    sel_wells = arcpy.management.SelectLayerByLocation(fc, "INTERSECT", sel_counties, None, "NEW_SELECTION", "INVERT").getOutput(0)
    arcpy.management.CopyFeatures(sel_wells, "{}_WaterWells_Outliers".format(countyName), '', None, None, None)
2
  • This helps a bit. I had to put if fcs is not None: in front of the loop to get rid of a "NoneType is not iterable" error. I run the loop and it outputs every well from the last county it iterates through, including the ones that are appropriately placed within the county. When I do this task manually I clear the selected features after copying them to a new feature class. Do I need to somehow incorporate that into the loop as well?
    – redleg_64
    Jan 25 at 19:29
  • Using NEW_SELECTION should clear out the previous selections. If you want to incorporate it, it would go within the loop after the CopyFeatures.
    – dslamb
    Jan 25 at 19:50

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