bpstewar

Publishing a synchronous python script as a geoprocessing service

Discussion created by bpstewar on May 18, 2012
This might belong in the Server forums, but I thought I would try here first.

I have a python script that performs a basic Zonal Statistics as Table. I publish it as a GP service, however, when the tool is published as synchronous tool, it hangs, despite the fact the it successfully runs the tool (I can see the output on the server). When I run it asynchronously, it works fine. Any ideas?

# ---------------------------------------------------------------------------
# Zonal stats with raster filter
# Benjamin P. Stewart, April 2012
# Purpose: Run a zonal stats calculation based on an input feature set
#    and a raster dataset chosen by a string input
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
from arcpy.sa import *

# Check out any necessary licenses
spatialAnalystExists = arcpy.CheckOutExtension("spatial")
print(spatialAnalystExists)

arcpy.AddMessage("Spatial Analyst License is " + spatialAnalystExists)
arcpy.env.overwriteOutput = True

Feature_Set = arcpy.GetParameterAsText(0) #arcpy.FeatureSet(r"\\sdnarc\ArcData\SAWI\Tools\zonalStats.gdb\testPoly") #
#desc = arcpy.gp.describe(Feature_Set)
#for f in desc.fields:
#    arcpy.AddMessage(f.Name)

arcpy.AddMessage(arcpy.env.scratchWorkspace)
   
if Feature_Set == '#' or not Feature_Set:
    Feature_Set = arcpy.FeatureSet(r"\\sdnarc\ArcData\SAWI\Tools\zonalStats.gdb\testPoly")

chosenFile = arcpy.GetParameterAsText(1)
if chosenFile == '#' or not chosenFile:
    chosenFile = "GDP"

#The input raster needs to be chosen based on a dropdown box and a dictionary
inputDict = {'Annual Precipitation':r'\\sdnarc\ArcData\SAWI\baseDataGanges\ANNPRCP.img',
             'Annual Temperature':r'\\sdnarc\ArcData\SAWI\baseDataGanges\ANNTEMP.img',
             'Dominant Soil Type':r'\\sdnarc\ArcData\SAWI\baseDataGanges\DominantSoilType.img',
             'Droughts PE':r'\\sdnarc\ArcData\SAWI\baseDataGanges\Droughts_PE.img',
             'Floods Freq':r'\\sdnarc\ArcData\SAWI\baseDataGanges\Floods_Freq.img',
             'Floods PE':r'\\sdnarc\ArcData\SAWI\baseDataGanges\Floods_PE.img',
             'Floods Risk':r'\\sdnarc\ArcData\SAWI\baseDataGanges\Floods_Risk.img',
             'GDP':r'\\sdnarc\ArcData\SAWI\baseDataGanges\GDP.img',
             'Irrigated Areas':r'\\sdnarc\ArcData\SAWI\baseDataGanges\GIAM.img',
             'Rainfed Areas':r'\\sdnarc\ArcData\SAWI\baseDataGanges\GMRCA.img',
             'LC2009':r'\\sdnarc\ArcData\SAWI\baseDataGanges\LC2009.img'}

inputRaster = inputDict[chosenFile]
             
#Currently, we'll just make one output file, but, really, this needs to be managed better
interimZonal = r"%s\interimZonal" % arcpy.env.scratchWorkspace # provide a default value if unspecified
arcpy.gp.addmessage("Processing %s as %s" % (chosenFile, inputRaster))

# Process: Zonal Statistics as Table
ext = arcpy.CheckExtension("spatial")
arcpy.AddMessage("Starting Zonal Stats: Spatial Analyst is %s" % str(ext))
result = ZonalStatisticsAsTable(Feature_Set, "testID", inputRaster, interimZonal, "DATA", "ALL")
arcpy.AddMessage("Successfully calculated zonal stats")

#Read results and push into record set
arcpy.SetParameter(2, arcpy.RecordSet(interimZonal))

Outcomes