Geoprocessing Service from Python Toolbox to create a web map?

Discussion created by ionarawilson on Aug 30, 2013
Latest reply on Aug 30, 2013 by ionarawilson
Hi guys, I have created a python toolbox for 8 tools. One of the tools calculates a plan id based on the county where the polygon is drawn and the year from a date entered by the user.

I would like to create a web app with all these tools. My web app already has editing capabilities and the feature service created for these tools can be already be edited in the web app.

But I need to calculate that PlanID field (the field is already in the database), so I am guessing I need to convert my toolbox script to a geoprocessing service.

If so, how can I do that, since I already have the editor part working in the web app, so I don't need that part of the script to enter the data, just the part that after getting the data, it calculates the planid based on the county where the polygon created is. Here is the python tool script ( I deleted some parts like some of the parameters and messages so it would fit here)
What would I need to change in my code for it to work? Can you please take a look at the python code and give me some advice on what to do? Any idea, or general idea on where to go from here would be awesome! The javascript code is in the following post.
Thank you!!!

class Stewardship(object):
    def getParameterInfo(self):
        """Define parameter definitions"""

        param1 = arcpy.Parameter(
        displayName = "Forester",
        name = "Forester",
        datatype = "String",
        parameterType = "Required",
        direction = "Input"


        params = [param0, param1, param2, param3, param4, param5, param6, param7, param8, param9, 

param10, param11, param12, param13, param14]
        return params
    def execute(self, parameters, messages):
        Status =  parameters[2].valueAsText
        DateStart =  parameters[3].valueAsText

# 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

# 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)

            if len(Datestarstr1) == 9:
            # if for example 6/28/2013 or 10/4/2013
                yearonly = DateStart[5:9]

            if len(Datestarstr1) == 8:
                yearonly = DateStart[4:8]
            if len(Datestarstr1) > 9:
             # if for example 10/10/2013
                yearonly = DateStart[6:10]

            for row in rows:
                row[0] = yearonly


# 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 

        Stewardshipcentroidslayer = 'Stewardship centroid FL'  # this is a string that represents the 

Stewardship feature layer
        if arcpy.Exists(Countieslayer):
            # make feature layer
        arcpy.MakeFeatureLayer_management("Counties", "Countieslayer")

        if arcpy.Exists(Stewardshiplayer):
            # make feature layer
        arcpy.MakeFeatureLayer_management("Stewardship", "Stewardshiplayer")

create the layer after creating centroids
        outstewardshipcentroids = "StewardshipCentroids2"
        if arcpy.Exists(outstewardshipcentroids):
            # make feature layer
        # Create centroid
        arcpy.FeatureToPoint_management("Stewardshiplayer",  outstewardshipcentroids,"INSIDE")

        if arcpy.Exists("Stewardshipcentroidslayer"):
            # make feature layer
        arcpy.MakeFeatureLayer_management(outstewardshipcentroids, "Stewardshipcentroidslayer")

        arcpy.SelectLayerByLocation_management("Countieslayer", "CONTAINS", outstewardshipcentroids, "", 


    # To get the code of the county from the counties layer to send it to the County field in 

        # Takes the first row of the cursor (there is only one object in the county centroid).
        # The first index is the first row, the next [0] index is the first attribute, which there is 

only one (FIPS_TXT)
        countycode = tuple(arcpy.da.SearchCursor("Countieslayer", "FIPS_TXT"))[0][0]
        urows = arcpy.da.UpdateCursor("StewardshipLayer", "County")
        for urow in urows:
            urow[0] = countycode

        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)
            # Get only the month
            # To get only the first two characters:
            DateStart2 = DateStartstr[0:2]
            if "/" in DateStart2:
                        # if for example 6/28/2013
                    DateStart3 = DateStart[0:1]
                    FFYnumbercount = FFYnumber
            if "/" not in DateStart2:
                 # if for example 10/10/2013
                DateStart4 = int(DateStart2)
                FFYnumbercount = FFYnumber + 1

            row[0] = str(FFYnumbercount)

        trows = arcpy.SearchCursor("Stewardshiplayer")

        for row in trows:

                   FFYtxt = row.getValue("FFY")
                   Countytxt = row.getValue("County")
        del trows, row

    # we list the layer to create a layer feature without a selection (because it is selected by the 

        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)+"'"

        # select all the records that have the FFYcounty the same as the FFYCounty of the hightest 

        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

            query = "SequenceNumber = "
            arcpy.SelectLayerByAttribute_management("TMP",  "SUBSET_SELECTION", "SequenceNumber = " + 

            result = int(arcpy.GetCount_management("TMP").getOutput(0))

    #  to get the sequence number
        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")

        with arcpy.da.UpdateCursor("Stewardshiplayer", ("Sequencenumber")) as rows:

            if Seqnum >= 1:
                    vsn = Seqnum + 1
                    vsn = 1
            for row in rows:

            #get the value of the date

               row[0] = vsn

    # 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)