Select to view content in your preferred language

Update Cursor Tool Returing Error

567
3
Jump to solution
03-28-2013 09:56 AM
RachelAlbritton1
Occasional Contributor
I've written a tool that loops through a parcel file, counts how many buffers intersect a parcel record within that file than updates an attribute field in the parcel layer with this number. The tool runs fine until it gets to the final record in the parcel layer then I get an error message that says

<type 'exceptions.NameError'>: name 'line' is not defined Failed to execute (RCW 3 Mile).



If I open up the layers attribute table, all the fields have been correctly updated so I don't understand why this error is occurring. Since the tool is part of a larger model I need to understand why this is happening sot the model can proceed forward.

I also tried running the tool independently (outside of modelbuilder) with the same results.

This is the code in the tool:

 #Select out each parcel record one by one and evalauate how many cluster buffers intersect with that parcel. #Update Parcel Attribute Table field three_mi with the # of buffers that intersect that parcel  SelectedParcelsFL=arcpy.GetParameterAsText(0) CbuffersFL=arcpy.GetParameterAsText(1)  #make sure that no records are currently selected in either slected parclesFL or cBuffersFL arcpy.AddMessage("Clearing selected parcel selections\n") arcpy.SelectLayerByAttribute_management(SelectedParcelsFL,"CLEAR_SELECTION") arcpy.AddMessage("Clearing selected buffer selections\n") arcpy.SelectLayerByAttribute_management(CbuffersFL,"CLEAR_SELECTION")  #Now begin 3-mile disperal buffer count print "updating parcels table with the number of 3-mile RCW dispersal buffers that intersect each parcel" arcpy.AddMessage("updating parcels table with the number of 3-mile RCW dispersal buffers that intersect each parcel\n")  for parcel in range(0,1092):     FID = "FID=%s" % (parcel)     arcpy.SelectLayerByAttribute_management(SelectedParcelsFL,"NEW_SELECTION",FID)     arcpy.SelectLayerByLocation_management(CbuffersFL,"INTERSECT",SelectedParcelsFL)     bufferCount = int(arcpy.GetCount_management(CbuffersFL).getOutput(0))     arcpy.AddMessage(str(FID)+" has "+str(bufferCount)+" RCW 3-mile dispersal buffers that intersect with it.")      #Update the three_mi field      uc=arcpy.UpdateCursor(SelectedParcelsFL)     for line in uc:         line.three_mi = bufferCount         uc.updateRow(line) #Actually changes the table values to buffer count     del line, uc          arcpy.SelectLayerByAttribute_management(SelectedParcelsFL,"CLEAR_SELECTION") print "updating parcels table with the number of 3-mile RCW dispersal buffers that intersect each parcel COMPLETED" arcpy.AddMessage("updating parcels table with the number of 3-mile RCW dispersal buffers that intersect each parcel COMPLETED")
0 Kudos
1 Solution

Accepted Solutions
markdenil
Frequent Contributor
Your cursor is working fine.
However, you then try to delete something that no longer exists, to wit: line

if you call each row in the cursor line ( for line in uc: )
then the cursor stops when line is no more
trying to then delete line
will cause an error: line is already gone.

just delete the cursor: uc


I aggree though, that Spatial Join is likely a better option.

View solution in original post

0 Kudos
3 Replies
DaleHoneycutt
Deactivated User
I don't have an answer about the cursor problem, but in looking at your task, it seems that you don't need cursors at all -- you could overlay the buffers and the parcel features using Spatial Join, Intersect, or Identity.  Spatial Join might be the most direct way -- it will give you a Join_Count attribute which would be the number of buffers that intersect a parcel. And it would be lighting fast compared to select by location for each individual parcel.  For an example to get started, see this blog post and scroll down to the section on Spatial Join.

Target features = parcels
Join features = buffers
Join operation = JOIN_ONE_TO_MANY
Match Option = (you have a choice here)
[INDENT]If you want buffers that just touch the boundary of a parcel to count, use INTERSECT[/INDENT]
[INDENT]If the buffer has to cross into a parcel, use WITHIN_A_DISTANCE with a small distance.  The blog explains this.[/INDENT]
0 Kudos
markdenil
Frequent Contributor
Your cursor is working fine.
However, you then try to delete something that no longer exists, to wit: line

if you call each row in the cursor line ( for line in uc: )
then the cursor stops when line is no more
trying to then delete line
will cause an error: line is already gone.

just delete the cursor: uc


I aggree though, that Spatial Join is likely a better option.
0 Kudos
RachelAlbritton1
Occasional Contributor
mdenil -
You're correct - my range statement was off - it needed to be one less. Thanks!
0 Kudos