Error on Make Feature Layer for looping over selected attributes

6688
11
Jump to solution
05-29-2014 02:06 PM
IanMurray
Frequent Contributor
I've ran into a bit of a unique problem, which for the life of me I cannot get debugged.

I have a two geodatabase feature classes, one with customer regions, and one with zip codes.  I'm iterating through each region and selecting each zip code within and writing the region with zip codes to a csv.  This works flawlessly the first loop through, but I run into an error on the Make Featuer Layer on subsequent iterations.  I am entering in the code through the iterative python window, and both fcs are in the TOC, and neither Feature Layer exists prior to running the code. 

 import arcpy from arcpy import env env.overwriteOutput = 1  arcpy.env.overwriteOutput = True #env.workspace = r"C:\Users\iamurray\Desktop\Ray\Zip Code\ZipCodes.gdb" output = open(r"C:\Users\iamurray\Desktop\Output.csv", "w")  count = 1  for item in range(1,163,1): # there are 162 features to be selected,      input1 = "TVA_Distributors"     input2 = "TVA_PSA_Zips"     outlayer1 = "TVA_Distributors_Layer"     outlayer2 = "TVA_PSA_Zips_Layer"     print count     #arcpy.SelectLayerByAttribute_management(input1, "NEW_SELECTION", [OBJECTID] =  + str(count))     arcpy.MakeFeatureLayer_management(input1, outlayer1, 'OBJECTID = ' + str(count)) #works on count = 1 but not on count = 2 and subsequent           cursor = arcpy.da.SearchCursor(outlayer1, ["DISTRIBU_1"])     for row in cursor:         output.write(str(row[0])+ "\n")         print row[0]     del cursor       # Process: Select Layer By Location     arcpy.SelectLayerByLocation_management(input2, "INTERSECT", outlayer1, "", "NEW_SELECTION")  # Process: Make Feature Layer (2)     arcpy.MakeFeatureLayer_management(input2, outlayer2)     field = "ZIP"     cursor = arcpy.SearchCursor(outlayer2)     for row in cursor:         output.write(str(row.getValue(field))+ "\n")     count += 1     print "The Current number of Distributors Processed is " + str(count - 1)     arcpy.Delete_management(outlayer1)     arcpy.Delete_management(outlayer2)     del row     del cursor output.close() 


These are the results I am getting
1
Warren RECC
The Current number of Distributors Processed is 1
2
Runtime error  Traceback (most recent call last):   File "<string>", line 20, in <module>   File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 5748, in MakeFeatureLayer     raise e ExecuteError: ERROR 000622: Failed to execute (Make Feature Layer). Parameters are not valid. ERROR 000628: Cannot set input into parameter in_features.

It works fine the first time, because it reaches the print count on the second run through successfully.  At first I thought it was an issue with the overwrite, but I've set both of those to allow overwrite and deleted the intermediate layers prior to the next MakeFeatureLayer.  The input and outputs should not change just the expression, which should still be valid each time, since each has a unique OBJECTID number.  I simply can't see what is wrong with the tool parameters that would cause this to crash on the subsequent loops. 

If anyone has some insight into this, I would be most appreciative, I'm using ArcGIS 10.1 SP1.

EDIT: There are no syntax errors if you find any, its a result of me copying the code over.
Tags (2)
0 Kudos
11 Replies
IanMurray
Frequent Contributor
Wayne I appreciate the further feedback, but I felt you had explained that as best you could before, and that was more of a question to any possible ESRI staff to comment about it if they had some insight into that issue. 

Also I appreciate the feedback on some good practices for coding the file locations with workspace and the os module.  I'm a grad student with one class in python under his belt, with no prior programming experience so I always appreciate feedback on how I can improve my code.  I've known of the os module for a while, but still getting use to the proper ways to use it.  As with most things I went with what I knew first and ran into some errors, now I'm a bit more comfortable with that useful module and can apply it towards my work in the future.
0 Kudos
T__WayneWhitley
Frequent Contributor
I appreciate the fact you are a grad student and searching for answers; I'll be listening for any further response ESRI may have.
Meanwhile you may appreciate this little trick...briefly demonstrated below--

Consider that you seem to have in your current session a one-time live reference by the name of your layer in the map, for example, I use "erase".  By using 'getOutput(0)' I can essentially get a 'handle' on the layer for later reference...note also (as shown) this result object is of the Layer class.
>>> lyr = arcpy.MakeFeatureLayer_management("erase", "test").getOutput(0)
 
>>> type(lyr)
<class 'arcpy._mapping.Layer'>



...so if I were to do something like the below, using that handle datasource, I can produce multiple feature layer outputs at will:
>>> myLayerHandles = ['lyr1','lyr2','lyr3','lyr4','lyr5']
>>> for i in range(len(myLayerHandles)):
...     arcpy.MakeFeatureLayer_management(lyr.dataSource, myLayerHandles)
...    
>>>



To drive this home, if I were to 'get' those layers from my simple map, I could do the below -- note that I'm retrieving the layers I just loaded (and the original "erase" layer along with the 1st MFL execution output layer, "test").  Also note that they are all the equivalent type as the retrieved result object from the MFL getoutput execution above.
>>> currentMXD = arcpy.mapping.MapDocument('current')
>>> layersInCurrentMap = arcpy.mapping.ListLayers(currentMXD)
 
>>> for i in range(len(layersInCurrentMap)):
...     print layersInCurrentMap.name, type(layersInCurrentMap)
...    
lyr5 <class 'arcpy._mapping.Layer'>
lyr4 <class 'arcpy._mapping.Layer'>
lyr3 <class 'arcpy._mapping.Layer'>
lyr2 <class 'arcpy._mapping.Layer'>
lyr1 <class 'arcpy._mapping.Layer'>
test <class 'arcpy._mapping.Layer'>
erase <class 'arcpy._mapping.Layer'>
>>>  


Admittedly not all of the ESRI documentation is as polished as it should be; however, the above very interesting 'behavior' is documented here:

Result (arcpy)
Desktop » Geoprocessing » ArcPy » ArcPy classes
http://resources.arcgis.com/en/help/main/10.2/index.html#//018z00000046000000
0 Kudos