1

On the second iteration of the loop starting line 7 I get the following error. Everything works as intended on the first iteration. Any ideas on why this is failing the second time around?

Error:

Runtime error Traceback (most recent call last): File "", line 10, in File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 7221, in SelectLayerByAttribute raise e ExecuteError: ERROR 000622: Failed to execute (Select Layer By Attribute). Parameters are not valid. ERROR 000628: Cannot set input into parameter in_layer_or_view.

Code:

mxd = arcpy.mapping.MapDocument("CURRENT")
ObjIDs = []
with arcpy.da.SearchCursor('sLineGravityMain_L', 'OBJECTID') as SCur:
    for SRow in SCur:
        ObjIDs.append(SRow[0])

for ObjID in ObjIDs:
    tmpLyr = 'temp' + str(ObjID)
    arcpy.MakeFeatureLayer_management(PathToFC,tmpLyr,'"OBJECTID"=%d' % ObjID)
    arcpy.SelectLayerByAttribute_management('sLineGravityMain_L',"NEW_SELECTION", '"OBJECTID"=%d' % ObjID)
    arcpy.SelectLayerByLocation_management('sManhole_P', "INTERSECT",tmpLyr,"","NEW_SELECTION","")
    arcpy.Delete_management(tmpLyr, 'GPFeatureLayer')

FYI, PathToFC is a placeholder, I have the full path passed in for the actual code. The in_layer for MakeFeatureLayer_management is the 'sLineGravityMain_L' feature class.

  • 2
    Generally I can see no problems in your code, it may be in one of the lines you have removed to create a minimum code sample, the only possibility I can think of is that you may need to clear the selection before the next iteration, both in 'sLineGravityMain_L' and 'sManhole_P' arcpy.SelectLayerByAttribute_management ('sLineGravityMain_L','CLEAR_SELECTION') in case the selection persists on the layer object. – Michael Stimson May 27 at 3:50
  • 1
    Is your code exactly like you showed, or is anything else happening between the curosr loop and the ObjIDs loop? Like a selection on the sLineGravityMain_L layer or similar? – umbe1987 May 27 at 7:40
  • @MichaelStimson this is the entirety of the code I am running. Tried clearing the selection at the end of each loop but same error is showing up on the 2nd iteration. Basically I am trying to identify and grab attributes from the lines and write to the point's table when a point is on the line. The selection I'm trying to get working would be for the cursors to only run on the selection of points and the line they are on. Loop each line, find and select the points that intersect it and run a search cursor to get line attributes and update cursor to write those to the points. – albutler18 May 27 at 21:14
  • 1
    Fixed the issue by having the in_layer parameter for the SelectLayers come from a list of layers populated by arcpy.mapping.ListLayers and handling it to pass in the layers I want. Getting the selections I want now and it works. That's what I get for trying to explicitly name a single layer for testing purposes... – albutler18 May 27 at 23:04

Your Answer

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

Browse other questions tagged or ask your own question.