I have the following script. Sometimes it runs and sometimes it does not. I think it might be the search cursor causing problems, but I am not sure. Other people, who do not have GIS background knowledge will be running this scipt so I need it to work all the time, not just some of the time. The script works fine as long as I run my scripts one after another and don't touch anything. If I remove everything from the table of contents and add it again, the error occurs. Does anyone have any suggestions how to fix it or what to do so that it always runs?I really appriciate the help. Sorry the script is a little long:
# ---------------------------------------------------------------------------
# NeighbourhoodSelect.py
# Created on: 2011-04-21 12:32:34.00000
# (generated by ArcGIS/ModelBuilder)
# Description: This script asks the user to input the geodatabase, Neighbourhood feature class
# and the CalcPop feature class. A GUI appears which asks the user to select a
# neighbourhood. Once the neighbbourhood is selected, the attributes from the CalcPop feature
# class are joined to the selected neighbourhood and a new feature class, containing only the
# the information for that neighbourhood is created.
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy, sys, os
from Tkinter import *
Title = "Neighbourhoods"
arcpy.env.workspace = sys.argv[1]
# Local variables:
Layer = sys.argv[2]
out_select = "selection"
Parcel_fc = sys.argv[3]
SelField = "NeighbourhoodName"
arcpy.env.overwriteOutput = True
#Create a GUI containing a listbox and buttons
class Application (Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid()
self.createWidgets(master)
def createWidgets(self, master):
self.yScroll1 = Scrollbar ( self, orient=VERTICAL )
self.yScroll1.grid ( row=0, column=1, sticky=N+S )
self.stList = Listbox (self, yscrollcommand=self.yScroll1.set)
self.stList.grid(row=0, column=0, sticky=EW)
self.yScroll1["command"] = self.stList.yview
#Populate list with choices in ascending order
mxd=arcpy.mapping.MapDocument("C:\\Users\\me\\Desktop\\Project\\TEST\\POP.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == Layer:
break
SelTable = lyr.dataSource
rows = arcpy.SearchCursor(SelTable, "", "", SelField, SelField + " A")
oldVal = ""
for row in rows:
newVal = row.getValue(SelField)
#only add value to Listbox if it is not a duplicate
if newVal <> oldVal:
self.stList.insert( END, row.getValue(SelField) )
oldVal = newVal
#add selection and quit buttons
self.selButton1=Button(self,text='Submit', command=self.selectNeighbourhood)
self.selButton1.grid(row=0,column=2, sticky=N+ EW)
self.quitButton = Button(self, text='Exit', command=master.destroy)
self.quitButton.grid(row=0,column=2, sticky= S+EW)
def selectNeighbourhood(self):
sel = self.stList.curselection()
try:
myFeature = self.stList.get(sel[0])
where_clause = "\"" + SelField + "\" = " + "'" + str(myFeature) + "'"
arcpy.Select_analysis(Layer, out_select, where_clause)
print where_clause
myFeature = myFeature.replace(' ', '_')
# Process: Intersect
inFeatures = [out_select, Parcel_fc]
neighbourhoods_Intersect = myFeature
arcpy.Intersect_analysis(inFeatures, neighbourhoods_Intersect, "ALL", "", "INPUT")
arcpy.AddMessage("Intersect Complete")
NI_lyr = '"' + neighbourhoods_Intersect + '"'
arcpy.MakeFeatureLayer_management(neighbourhoods_Intersect, NI_lyr)
arcpy.AddMessage("layer created")
arcpy.Delete_management(out_select)
arcpy.env.overwriteOutput = True
#stats = myFeature
unique_name = arcpy.CreateUniqueName(neighbourhoods_Intersect)
# Process: Summary Statistics
arcpy.Statistics_analysis(neighbourhoods_Intersect, unique_name, "CurrentPop SUM", "CurrentPop")
# Process: Add Field
arcpy.AddField_management(unique_name, "Total_Population", "DOUBLE", "", "", "", "", "NULLABLE", "REQUIRED", "")
arcpy.AddMessage("Field is Added")
rows = arcpy.SearchCursor(unique_name, "", "", "SUM_CurrentPop")
NewList= []
for row in rows:
row = row.getValue("SUM_CurrentPop")
if row != None:
print row
NewList.append(row)
total = sum(NewList)
print total
arcpy.CalculateField_management(unique_name, "Total_Population", total, "PYTHON")
arcpy.AddMessage("All processes are completed. Please close GUI")
except Exception, e:
tb = sys.exc_info()[2]
print "Line %i" % tb.tb_lineno
print e.message
root = Tk()
app = Application(master=root)
app.master.title(Title)
app.mainloop()