Select to view content in your preferred language

Script tool for Geoprocessing Service

878
6
08-03-2010 06:29 AM
AdrianHughes
Emerging Contributor
Does anyone have any examples of script tools used in an ArcGIS server geoprocessing service? I guess they have to be written differently to script tools in the desktop environment but can't find any documentation on them
0 Kudos
6 Replies
KevinHibma
Esri Regular Contributor
Does anyone have any examples of script tools used in an ArcGIS server geoprocessing service? I guess they have to be written differently to script tools in the desktop environment but can't find any documentation on them


Aside from watching your input/output parameters (being supported and how you reference them)...the scripts aren't really any different.

Do you have a workflow or script that you're having problems with?
0 Kudos
AdrianHughes
Emerging Contributor
I've read about the limitations in inputs and outputs. I had a script that worked fine in the desktop environment but gives a very ambiguous error on the server. The message in server manager is simply Error:Failed. Event viewer does not shed anymore light either.

I still get the error with the very simplified script below. Perhaps its as you say to do with referencing the parameters. Is it correct to use sys.argv[]? My inputs are just two feature sets and a couple of strings


import sys, string, os, arcgisscripting


def InitialSetting():
  
    try:
        global gp
        gp = arcgisscripting.create(9.3)
        gp.Addtoolbox(r"C:\Program Files\ArcGIS\ArcToolBox\Toolboxes\Data Management Tools.tbx")

    except:
        print "error"

        sys.exit(0)
    else:
        global Printboth
        def Printboth(msg):
            print msg
            gp.addmessage(msg)
      
        Printboth("Importing ArcGIS scripting object successful")        
           
InitialSetting()

gp.overwriteoutput = 1

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")


# Script parameters
Method = sys.argv[1]
Binomial = sys.argv[2]
Known_Location_Points = sys.argv[3]
Inferred_Species_Range = sys.argv[4]
Hydroshed_Layer =  "Hydrosheds_Taiwan"
0 Kudos
AndrewChapkowski
Esri Regular Contributor
The web help has multiple of published GP service examples: http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Guide_to_the_geoprocessing_service_exa...

I would also use gp.getparameter() or gp.getparameterastext() to get your inputs instead of sys.argv. You shouldn't have to reference the toolboxes either. 

In general when writing scripts I try to follow this format:
import sys
import os
import traceback
import inspect
import arcgisscripting

class ErrorHandling(object):        
    def trace(self):
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        # script name + line number
        line = tbinfo.split(", ")[1]
        filename = inspect.getfile( inspect.currentframe() )
        # Get Python syntax error
        #
        synerror = traceback.format_exc().splitlines()[-1]
        return line, filename, synerror


if __name__ == '__main__':
    try:
        gp = arcgisscripting.create(9.3)
        gp.overwriteoutput = 1
        #
        #!!!!! place python logic here !!!!!
        #
    except arcgisscripting.ExecuteError:
        # Return GEOPROCESSING specific errors #
        EH = ErrorHandling()
        
        line, filename, err = EH.trace()
        gp.adderror("Geoprocessing error on " + line + " of " + filename + " :")
        for msg in range(0, gp.MessageCount):
            if gp.GetSeverity(msg) == 2:
                gp.AddReturnMessage(msg)
        gp.AddError(gp.GetMessages(2))
        
    except:
        # Return any PYTHON or system specific errors #
        EH = ErrorHandling()
        line, filename, err = EH.trace()
        gp.AddError("Python error on " + line + " of " + filename + " : with error - " + err)
    finally:
        del gp



Hope this helps.
0 Kudos
AdrianHughes
Emerging Contributor
Hi

I've seen those examples but none of them use script tools. I'm still getting the same error (Error:Failed) for the very simple code below. Is there any way of getting a more meaningful error message

import sys, string, os, arcgisscripting

  
gp = arcgisscripting.create(9.3)
gp.overwriteoutput = 1



# Script parameters
Method = gp.GetParameterAsText(0)
Binomial = gp.GetParameterAsText(1)
Known_Location_Points = gp.GetParameter(2)
Inferred_Species_Range = gp.GetParameter(3)
Hydroshed_Layer =  "Hydrosheds_Taiwan"
0 Kudos
AndrewChapkowski
Esri Regular Contributor
Did you enable messaging on your service, this will give you the error messages from the server? To do that, you'll need to stop the service and check the show messages check box in catalog.

Do you have your script/toolbox saved in a location where the server can access it?  For example,
I place my scripts and toolboxes in a directory inside the \\computer-name\arcgisserver folder.  Then I publish my script from the UNC referenced name. 
Another consideration, is to reference the script by UNC path name. 

Attached is a toolbox that contains a script that can be published to server, hopefully this will help.
0 Kudos
AdrianHughes
Emerging Contributor
I didn't have messaging enabled. Thanks, thats helped a lot and I've now got a basic script to work.
0 Kudos