ArcPy – Resolve Invalid Input Data Error 000368 in For Loop

arcpyerror-000368layersloopselect-by-location

I am attempting to iterate over all the feature classes in a folder and I'm getting the error 000368. The other solutions in on SE were not helpful to me unfortunately, so I'm posting a separate question. search_grids is a list of point feature classes and searches is a another point feature class.

The points never actually intersect with each other, but I'm using the Select by location tool to select points within a specified distance of each other. This isn't the reason for the error though as I was able to get this to work in the python window in ArcMap.

My code is the following

arcpy.env.workspace = r"C:\workspace"
search_grids = arcpy.ListFeatureClasses()
searches = r"C:\workspace\search"
for grid in search_grids:
    arcpy.management.SelectLayerByLocation(grid,"INTERSECT",searches,"97.5 meters","NEW_SELECTION","INVERT")
    arcpy.management.SelectLayerByLocation(grid,"INTERSECT",searches,"2.5 meters","ADD_TO_SELECTION","NOT_INVERT")

Why am I getting this error?

arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000368: Invalid input data.
Failed to execute (SelectLayerByLocation)

Best Answer

SelectLayerByLocation requires a Layer to select features, you can't select features directly on a feature class, which is what your script is trying to do.

The reason it worked in ArcMap is because in ArcMap you were referencing the map layers rather than feature classes.

Add a MakeFeatureLayer and set your SelectLayerByLocation to select that.

for grid in search_grids:
    arcpy.MakeFeatureLayer_management(grid, 'grid_lyr')
    arcpy.management.SelectLayerByLocation('grid_lyr',"INTERSECT",searches,"97.5 meters","NEW_SELECTION","INVERT")
    arcpy.management.SelectLayerByLocation('grid_lyr',"INTERSECT",searches,"2.5 meters","ADD_TO_SELECTION","NOT_INVERT")

As you are looping, you will probably need to remove that feature layer before recreating it in the next loop. You can do this by adding an arcpy.Delete_management('grid_lyr') after whatever code you are adding after your select statements.

You may also be having trouble with searches as you don't appear to be referencing a feature class here - I would think this would either be a shapefile (ending with .shp) or a geodatabase feature class (inside a .mdb or .gdb). It looks like you are referencing just a folder C:\workspace\search

Related Question