AnsweredAssumed Answered

Invalid parameters on the *second* iteration of a loop; input layer disappears

Question asked by CCStormwater on Jan 13, 2014
Latest reply on Jan 16, 2014 by msayler
I'm trying to sort mapbook pages by township.  I have a layer of mapbook grids (rectangular polygon layer called "MapbookPages") and a layer of townships (irregular polygon layer with polygons named "Boundary_<TwpName>" each covering a couple dozen mapbook rectangles).  I'm trying to create a unique layer of mapbook grids per township.

Boundaries = arcpy.mapping.ListLayers(mxd,"Boundary_*") for Boundary in Boundaries:    print Boundary    arcpy.SelectLayerByLocation_management("MapbookPages","INTERSECT",Boundary)    arcpy.MakeFeatureLayer_management("MapbookPages", + "_MapSet_lyr")

This successfully creates the first layer; however, it fails on the second selection.  Here's the output:

Runtime error  Traceback (most recent call last):  
[INDENT]File "<string>", line 4, in <module>
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\", line 6559, in SelectLayerByLocation     raise e
ExecuteError: ERROR 000622: Failed to execute (Select Layer By Location).
Parameters are not valid. ERROR 000628: Cannot set input into parameter in_layer.[/INDENT]

[/INDENT](Error indentation by me)

An additional, and probably key fact, is that when I modify the code in the Python window I sometimes am not offered the MapbookPages layer by the intellisense.  If I copy the MapbookPages layer, and go back to modify the code then I'll usually see the new layer.  If I reload the project and run again I'll get the first selection layer again with the failure on the second again.  What is causing this layer to disappear?

Win 7, 64bit, 10.1, Python 2.7

PS: I created all ten layers by saving off my MapbookPages layer as an .lyr file, then repeatedly removing the just created boundary and readding the MapbookPages lyr in every time.  Somehow ArcMap or arcpy seems to be losing track of the selected features layer after the first use.