I am trying to publish a geoprocessing script from a script tool in python. The code runs great in ArcGIS but I am getting error messages when trying to publish the geoprocessing service.
Attachment 27179
Attachment 27179
Code:
#Import modules
import os, sys, arcpy, traceback, arcgisscripting
#Set Map Document
mxd = arcpy.mapping.MapDocument("Current")
#Set Overwrite Option
arcpy.env.overwriteOutput = True
gp = arcgisscripting.create(10.1)
try:
sCursor = arcpy.SearchCursor("Stewardship")
for row in sCursor:
if row.SequenceNumber:
arcpy.AddError("You can't run the tool in previous records. Please select your last digitized record and run the tool")
if not row.SequenceNumber:
#Sets parameters (attributes)
Status = gp.GetParameterAsText(0)
Office = gp.GetParameterAsText(1)
Forester = gp.GetParameterAsText(2)
DateStart = gp.GetParameterAsText(3)
PlanLength = gp.GetParameter(4)
PlanEQIP = gp.GetParameter(5)
# Create domains
# This is to send the code to the database but keep the description for the user input
StatusDomain = {'Completed': 'C', 'Pending': 'P'}
StatusCode = StatusDomain[Status]
officeDomain = {'Alpine': 'AL', 'Austin': 'AU', 'Carthage': 'CA', 'Corpus Christi': 'CC''}
officeCode= officeDomain[Office]
foresterDomain = {'Brittany Compton': 'bcompton', 'Brian Pope': 'bpope' }
foresterCode = foresterDomain[Forester]
UnderservedDomain = {'Yes': '1', 'No': '0'}
UnderservedCode = UnderservedDomain[Underserved]
del row, sCursor
# This is to send the data input to the database
with arcpy.da.UpdateCursor("Stewardship", ("Status", "Office", "Forester", "DateStart","PlanLength", "PlanEQIP", "RecipientLast", "RecipientFirst",
"MailAddress", "City" , "State", "ZipCode", "Phone", "Email", "Underserved")) as rows:
# row comes back as a tuple in the order specified here, so Office is row[0], Forester is row[1]
for row in rows:
row[0] = StatusCode
row[1] = officeCode
row[2] = foresterCode
rows.updateRow(row)
del row, rows
# This is to save only the year to the FFY field from the DateStart field
with arcpy.da.UpdateCursor("Stewardship", ("FFY")) as rows:
Datestarstr1 = str(DateStart)
arcpy.AddMessage(Datestarstr1)
if len(Datestarstr1) == 9:
# if for example 6/28/2013 or 10/4/2013
yearonly = DateStart[5:9]
# if for example 6/8/2013
if len(Datestarstr1) == 8:
yearonly = DateStart[4:8]
if len(Datestarstr1) > 9:
# if for example 10/10/2013
yearonly = DateStart[6:10]
arcpy.AddMessage(yearonly)
arcpy.AddMessage("TEST")
for row in rows:
row[0] = yearonly
rows.updateRow(row)
# This is to create centroids of the stewardship boundaries to select the county that contains the centroid (stewardship), to be used to create the PlanID
Countieslayer = 'Counties FL' # this is a string that represents the Counties feature layer
Stewardshiplayer = 'Stewardship FL' # this is a string that represents the Stewardship feature layer
Stewardshipcentroidslayer = 'Stewardship centroid FL' # this is a string that represents the Stewardship feature layer
if arcpy.Exists(Countieslayer):
arcpy.Delete_management(Countieslayer)
# make feature layer
arcpy.MakeFeatureLayer_management("Counties", "Countieslayer")
if arcpy.Exists(Stewardshiplayer):
arcpy.Delete_management(Stewardshiplayer)
# make feature layer
arcpy.MakeFeatureLayer_management("Stewardship", "Stewardshiplayer")
# I don't need to save the output to a folder, i can create a variable to hold a name and then create the layer after creating centroids
outstewardshipcentroids = "StewardshipCentroids2"
# Create centroid
arcpy.FeatureToPoint_management("Stewardshiplayer", outstewardshipcentroids,"INSIDE")
if arcpy.Exists(Stewardshipcentroidslayer):
arcpy.Delete_management(Stewardshipcentroidslayer)
# make feature layer
arcpy.MakeFeatureLayer_management(outstewardshipcentroids, "Stewardshipcentroidslayer")
arcpy.SelectLayerByLocation_management("Countieslayer", "CONTAINS", "Stewardshipcentroidslayer", "", "NEW_SELECTION")
countycode = tuple(arcpy.da.SearchCursor("Countieslayer", "FIPS_TXT"))[0][0]
urows = arcpy.da.UpdateCursor("StewardshipLayer", "County")
for urow in urows:
urow[0] = countycode
urows.updateRow(urow)
del urows, urow
# To add 1 to the year if month starts in October
import time
from datetime import datetime, date
#sim_date_counter = 1
with arcpy.da.UpdateCursor("Stewardshiplayer", ("FFY")) as rows:
for row in rows:
#get the value of the date
FFY = row[0]
FFYnumber = int(FFY)
#convert to string
DateStartstr = str(DateStart)
arcpy.AddMessage(DateStartstr)
# Get only the month
# To get only the first two characters:
DateStart2 = DateStartstr[0:2]
arcpy.AddMessage(DateStart2)
if "/" in DateStart2:
# if for example 6/28/2013
DateStart3 = DateStart[0:1]
FFYnumbercount = FFYnumber
arcpy.AddMessage(DateStart3)
arcpy.AddMessage(FFYnumbercount)
if "/" not in DateStart2:
# if for example 10/10/2013
DateStart4 = int(DateStart2)
FFYnumbercount = FFYnumber + 1
arcpy.AddMessage(DateStart4)
arcpy.AddMessage(FFYnumbercount)
row[0] = str(FFYnumbercount)
rows.updateRow(row)
# First we need to search by object id to it, it can be the highest, since there is only one selected
#maxValue3 = arcpy.SearchCursor("Stewardshiplayer", "", "", "", "OBJECTID D").next().getValue("OBJECTID")
trows = arcpy.SearchCursor("Stewardshiplayer")
for row in trows:
FFYtxt = row.getValue("FFY")
Countytxt = row.getValue("County")
arcpy.AddMessage("ffy:")
arcpy.AddMessage(FFYtxt)
del trows, row
# we list the layer to create a layer feature without a selection (because it is selected by the user)
lyr = arcpy.mapping.ListLayers(arcpy.mapping.MapDocument('CURRENT'), 'Stewardship')[0]
arcpy.MakeFeatureLayer_management(lyr.dataSource, "TMP")
# To get the total ammount of records, not only the selected
result = int(arcpy.GetCount_management("TMP").getOutput(0))
# write a query to get the county and year using the FFY and County of the object id of the Stewardship layer (the one that is selected)
query = "FFY" +"=" + "'"+ str(FFYtxt)+"'" + "AND " + "County" +"=" + "'"+ str(Countytxt)+"'"
arcpy.AddMessage(query)
# select all the records that have the FFYcounty the same as the FFYCounty of the hightest ObjectID
arcpy.SelectLayerByAttribute_management("TMP", "NEW_SELECTION", query )
result = int(arcpy.GetCount_management("TMP").getOutput(0))
# if there are more than 1 records with the same ffy and county
# select only the record witht the highest sequencenumber (sort first, then select)
if result > 1:
maxValue4 = arcpy.SearchCursor("TMP", "", "", "", "SequenceNumber D").next().getValue("SequenceNumber")
query = "SequenceNumber = "
arcpy.SelectLayerByAttribute_management("TMP", "SUBSET_SELECTION", "SequenceNumber = " + str(maxValue4))
result = int(arcpy.GetCount_management("TMP").getOutput(0))
rows = arcpy.SearchCursor("TMP")
for row in rows:
Seqnum = row.SequenceNumber
del rows, row
# If the sequence number is equal or higher than 1 it means there are other records with the same FFY and County, so we add 1 to the number
rows = arcpy.UpdateCursor("Stewardshiplayer")
# we make sure the Sequence number gets updated with the variable that is counting the sequence number
for row in rows:
row.Sequencenumber = Seqnum
rows.updateRow(row)
del rows, row
with arcpy.da.UpdateCursor("Stewardshiplayer", ("Sequencenumber")) as rows:
if Seqnum >= 1:
arcpy.AddMessage("Other records with the same FFY and County")
vsn = Seqnum + 1
else:
arcpy.AddMessage("No othe records with the same FFY and County")
vsn = 1
for row in rows:
#get the value of the date
row[0] = vsn
rows.updateRow(row)
# we Create the Plan ID concatenating the FFY, the county and the Sequence number, adding zeros in front of the sequence number if necessary to make the lenght always 3
with arcpy.da.UpdateCursor("Stewardship", ("PlanID")) as rowsffycounty2:
for row in rowsffycounty2:
row[0] = str(FFYtxt) + "-" + str(Countytxt) + "-" + str(vsn).zfill(3)
rowsffycounty2.updateRow(row)
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(msgs)
arcpy.AddError(pymsg)
print msgs
print pymsg
arcpy.AddMessage(arcpy.GetMessages(1))
print arcpy.GetMessages(1)