MakeFeatureLayer fails with "where" clause

325
2
Jump to solution
08-16-2012 03:23 PM
RobertMartin2
Occasional Contributor II
I posted earlier about UpdateCursor not working, but since then I have narrowed the problem down to MakeFeatureLayer. What I am trying to do is loop over features in a point class and isolate each one into its own layer using a where statement, and then delete the layer before the next iteration. (This is just for diagnostic purposes - not meant to do anything productive.) The problem is that when I give MakeFeatureLayer a where statement, it will only run twice and then stop without any errors or messages.

This is my code :

# Make feature layer of all points arcpy.MakeFeatureLayer_management("pyServiceConnection", "servLyr")  # Make search cursor with layer rows = arcpy.SearchCursor("servLyr")  # Loop over points for row in rows:          # Make feature layer with the current point only  where = "\"OBJECTID\" = " + str(row.OBJECTID)         arcpy.MakeFeatureLayer_management("servLyr", "curServLyr", where)           # Delete feature layer  arcpy.Delete_management("curServLyr")


Interestingly, if I leave out the where statement the code will run to completion. I have tried using other where statements such as "1=1" but all of them caused the same fail after the second run. I am totally stumped by this - can anyone think of what could be going on here?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
You can't make a layer from the layer you already have open with a cursor. A two-step process may work better for you to get the cursor out of the picture.

# Make feature layer of all points arcpy.MakeFeatureLayer_management("pyServiceConnection", "servLyr")  # Create list of unique object ids rows = arcpy.SearchCursor("servLyr") # Loop over points objIDs = list() for row in rows:     objIDs.append(row.OBJECTID) # make list unique objIDs = list(set(objIDs))  # Make and delete feature layer for each point one at a time for objID in objIDs:     where = "OBJECTID = {0}".format(objID)     arcpy.MakeFeatureLayer_management("servLyr", "lyr1", where)     # Delete feature layer     arcpy.Delete_management("lyr1")

View solution in original post

0 Kudos
2 Replies
curtvprice
MVP Esteemed Contributor
You can't make a layer from the layer you already have open with a cursor. A two-step process may work better for you to get the cursor out of the picture.

# Make feature layer of all points arcpy.MakeFeatureLayer_management("pyServiceConnection", "servLyr")  # Create list of unique object ids rows = arcpy.SearchCursor("servLyr") # Loop over points objIDs = list() for row in rows:     objIDs.append(row.OBJECTID) # make list unique objIDs = list(set(objIDs))  # Make and delete feature layer for each point one at a time for objID in objIDs:     where = "OBJECTID = {0}".format(objID)     arcpy.MakeFeatureLayer_management("servLyr", "lyr1", where)     # Delete feature layer     arcpy.Delete_management("lyr1")
0 Kudos
RobertMartin2
Occasional Contributor II
Thank you Curtis! This worked without a hitch.
0 Kudos