Search code examples
pythonarcpy

unable to select layer for a given polygon input in arcpy


I have created python script tool and able to select layer by providing feature classes as below.

import arcpy

arcpy.env.workspace = "C:/project/pmms.gdb"

arcpy.SelectLayerByLocation_management('stops', 'intersect', 'adminarea')

But when I used the following code to take user input polygon (FeatureSet), it fails and an error message is given. I have created a parameter of FeatureSet type to allow user to provide interactive polygon input. Please provide your suggestions.

import arcpy

fc = "C:/project/pmms.gdb/stops"

infeat = arcpy.GetParameterAsText(0)

arcpy.SelectLayerByLocation_management(fc, 'intersect', infeat)

Error message:

Traceback (most recent call last):
  File "C:\project\scripts\select.py", line 7, in <module>
    arcpy.SelectLayerByLocation_management(fc, 'intersect', infeat)
  File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\management.py", line 6585, in SelectLayerByLocation
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000368: Invalid input data.
Failed to execute (SelectLayerByLocation).

Solution

  • From the ArcGIS help page on the Select Layer By Location function:

    The input must be a feature layer; it cannot be a feature class.

    Include a Make Feature Layer operation before attempting to select, and it should work as expected.

    fc = "C:/project/pmms.gdb/stops"
    arcpy.MakeFeatureLayer_management(fc, 'stops')
    arcpy.SelectLayerByLocation_management('stops', 'intersect', infeat)
    

    Just make sure you don't already have a layer in your ArcMap table of contents that is called stops (which is presumably why the previous version of your code was working correctly).