Background:
I have forest data stored as points and polygons. The points are the location of sample plots, and the polygons are management stands. There is a field, "SID", in the attribute table of the points that corresponds to the polygon that contains each point.
The purpose of this script tool is to update the "SID" field in the point file after the polygons have been moved (because the plot is now in a new stand). For points not within a polygon, I've set it up so they get "flagged".
Problem:
The script will only work properly one time unless I close/reopen the mxd or start/end an edit session. The tool will run a second time, but will not perform any field recalculations or throw up an error message.
Is there something I am overlooking?
Thanks in advance for your help!
I have forest data stored as points and polygons. The points are the location of sample plots, and the polygons are management stands. There is a field, "SID", in the attribute table of the points that corresponds to the polygon that contains each point.
The purpose of this script tool is to update the "SID" field in the point file after the polygons have been moved (because the plot is now in a new stand). For points not within a polygon, I've set it up so they get "flagged".
Problem:
The script will only work properly one time unless I close/reopen the mxd or start/end an edit session. The tool will run a second time, but will not perform any field recalculations or throw up an error message.
Is there something I am overlooking?
Thanks in advance for your help!
Code:
import arcpy
from arcpy import env
arcpy.AddMessage('Identifying parameters')
stand = arcpy.GetParameterAsText(0) #stand polygon data
sid = arcpy.GetParameterAsText(1) #unique stand identifier text field from stand layer
pts = arcpy.GetParameterAsText(2) #plot points data
p_sid = arcpy.GetParameterAsText(3) #unique stand identifier text field from point layer
arcpy.AddMessage('Parameters identified.')
env.workspace = pts
#adds each SID to a list and looks for repeats of an SID
arcpy.AddMessage('Identifying stands...')
cursor1 = arcpy.da.SearchCursor(stand, [sid])
list1 = []
t = 0
for row in cursor1:
global t
step1 = str(row)
step2 = step1.split("'")[1]
if list1.count(str(step2))==0:
list1.append(step2)
t+=1
if t == 1:
arcpy.AddMessage("Identified "+str(t)+" stand...")
else:
arcpy.AddMessage("Identified "+str(t)+" stands...")
else:
arcpy.AddMessage('!!!!Error: Stand ID (SID) "'+str(step2)+'" occurs '+str(list1.count(step2)+1)+' times!!!!')
import sys
sys.exit(0)
del cursor1
#For each SID, selects points within the stand and updates the SID of those points
arcpy.AddMessage('Assigning stand ID (SID) to points...')
if len(list1) > 0:
for row in list1:
arcpy.SelectLayerByAttribute_management(stand, "NEW_SELECTION", str(sid)+"='"+str(row)+"'")
arcpy.SelectLayerByLocation_management(pts, "WITHIN", stand,)
arcpy.CalculateField_management(pts, p_sid, '"'+str(row)+'"', "PYTHON")
arcpy.AddMessage('Flagging points outside of stands...')
arcpy.SelectLayerByAttribute_management(stand, "NEW_SELECTION", '"OBJECTID" >= 0')
arcpy.SelectLayerByAttribute_management(pts, "NEW_SELECTION", '"OBJECTID" >= 0')
arcpy.SelectLayerByLocation_management(pts, "WITHIN", stand, "", "REMOVE_FROM_SELECTION")
arcpy.CalculateField_management(pts, p_sid, '"FLAGGED_" + !'+str(p_sid)+'!', "PYTHON")
arcpy.SelectLayerByAttribute_management(pts, "NEW_SELECTION", '"OBJECTID" < 0')
arcpy.SelectLayerByAttribute_management(stand, "NEW_SELECTION", '"OBJECTID" < 0')
else:
arcpy.AddMessage('Issue...')