Select to view content in your preferred language

Issue with script tool

2964
4
Jump to solution
04-26-2013 01:38 PM
by Anonymous User
Not applicable
Original User: davidisraelitt

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!

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...')
0 Kudos
1 Solution

Accepted Solutions
TimDonoyou
Regular Contributor
hi there,

at the end of the script i would delete all your defined variables so that none are kept in memory within your arcmap session.

you could also try using an update cursor rather than calculate field

hope this helps

Tim

View solution in original post

0 Kudos
4 Replies
by Anonymous User
Not applicable
Original User: scoggins

Have you tried

arcpy.env.overwriteOutput = True

?

Put that at the beginning of the code and you'll overwrite the data you've already written with new or the same values.
0 Kudos
by Anonymous User
Not applicable
Original User: davidisraelitt

Have you tried

arcpy.env.overwriteOutput = True

?

Put that at the beginning of the code and you'll overwrite the data you've already written with new or the same values.



I placed this above the part of my code where I define the parameters. Still no luck...
0 Kudos
by Anonymous User
Not applicable
Original User: davidisraelitt

Bumping this back to the top.... Issue still exists...
0 Kudos
TimDonoyou
Regular Contributor
hi there,

at the end of the script i would delete all your defined variables so that none are kept in memory within your arcmap session.

you could also try using an update cursor rather than calculate field

hope this helps

Tim
0 Kudos