[GIS] SelectLayerByLocation_management gives ERROR 000368: Invalid input data

arcpyerror-000368error-000732select-by-location

I'm trying to use SelectLayerByLocation_managment in a script and keep getting the following error:

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000368: Invalid input data.
ERROR 000732: Selecting Features: Dataset pnrsLyr does not exist or is not supported
Failed to execute (SelectLayerByLocation).

I made both feature classes into layers. When I run the same syntax in the Python command window in ArcMap it seems to work fine.

Here's the script:

import arcpy

cities = r"C:\RS_Data\Python\Geog485\Lesson3\Lesson3PracticeExercises\Lesson3PracticeExerciseA\Washington.gdb\CityBoundaries"
pnrs = r"C:\RS_Data\Python\Geog485\Lesson3\Lesson3PracticeExercises\Lesson3PracticeExerciseA\Washington.gdb\ParkAndRide"

citiesLyr = arcpy.MakeFeatureLayer_management(cities)
pnrsLyr = arcpy.MakeFeatureLayer_management(pnrs)
print "made feature layers"

nameField = "HasParkAndRide"

selection = arcpy.SelectLayerByLocation_management("citiesLyr","INTERSECT","pnrsLyr")

Best Answer

The Make Feature Layer tool needs two parameter values, and you are only supplying one.

MakeFeatureLayer_management (in_features, out_layer, {where_clause}, {workspace}, {field_info})

Try replacing:

citiesLyr = arcpy.MakeFeatureLayer_management(cities)
pnrsLyr = arcpy.MakeFeatureLayer_management(pnrs)
selection = arcpy.SelectLayerByLocation_management("citiesLyr","INTERSECT","pnrsLyr")

with:

arcpy.MakeFeatureLayer_management(cities,"Cities Layer")
arcpy.MakeFeatureLayer_management(pnrs,"PnR Layer")
selection = arcpy.SelectLayerByLocation_management("Cities Layer","INTERSECT","PnR Layer")
Related Question