Sunday 30 April 2017

Setting Output Parameter in ArcPy for Server Tool


I have published the following script tool (which is wrapped in a model, i.e. just the script tool and all of its parameters) to ArcGIS Server 10. The GP service succeeds when run, but I cannot determine how to access the final output of the GP service. The result is a RecordSet object, and I can view it in the arcgisjobs/job-id/ directory when viewing it on the server.


However, I cannot figure out how to access the result via the REST API. As you can see, the script tool doesn't have an output parameter. When viewing the GP service via the REST Directory there are only the two inputs. I have tried using SetParameterAsText and a Result object and adding an output parameter to both the ST and the model but without any success. So, the question is how do I modify the script and/or script tool/model properties to designate an output parameter so that it can be accessed via the REST API and subsequently consumed in a web application?


I have read through this related question but it has not helped.


The REST Endpoint: http://atlas.utc.edu/ArcGIS/rest/services/RRI/DE/GPServer/Decision%20Engine


The Python script:


#Import ArcPy site package module

import arcpy
from arcpy import env
from arcpy import Extent
from arcpy import Raster
from arcpy import RecordSet
from arcpy.sa import ZonalStatisticsAsTable
from time import gmtime, strftime

# Check out ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Set Environment Workspace
ws = env.workspace = WORKSPACE PATH

# Set Environment Raster Cell Size
cellSize = env.cellSize = 10
# Set Environment Processing Extent
processingExtent = Extent(602560.330672585, 3827530.65305605, 746490.330672585, 3966810.65305605)

environProcessingExtent = env.extent = processingExtent


# Strings representing rasters in the workspace
rasters = arcpy.GetParameterAsText(0).split(";")
# The dataset that defines the zones
inZoneData = arcpy.GetParameterAsText(1)

# Determine Zone Dataset
if inZoneData == "Block Group":
zoneField = "Value"
zoneData = ws + "\\ToolData\\Data.gdb\\BG_Ras"
elif inZoneData == "Census Tract":

zoneField = "TRACTCE10"
zoneData = "\\ToolData\\Data.gdb\\CT_Ras"
else:
zoneField = ""

addedRaster = Raster(ws + "\\ToolData\\Data.gdb\\constantRaster")

# Process each raster string
for raster in rasters:
addedRaster = addedRaster + Raster(ws + "\\ToolData\\Data.gdb\\" + raster)


# Execute Zonal Statistics
zonalTable = RecordSet(ZonalStatisticsAsTable(zoneData, zoneField, addedRaster, "%SCRATCHWORKSPACE%\\zontalTable", "DATA", "MAXIMUM")).save("%SCRATCHWORKSPACE%\\rszt")

Some relevant images?


The Toolbox


Script Tool Parameters


Script Tool Parameters


Model Tool Parameters


Model Tool Parameters



Thanks.



Answer



This worked for me:



  1. Set an output parameter of type RecordSet on the Script Tool and republish.

  2. Push your zonalStats into an in_memory/table. Or, if you need to write them out for whatever reason, copy the rows into an in_memory/table after you're done with the sa.


  3. SetOutputParameter in arcpy to be the Recordset.


    import arcpy
    foo="in_memory/foo"

    arcpy.management.CopyRows("C:/foo.dbf",foo)
    arcpy.SetParameter(0, arcpy.RecordSet(foo))

    ##Note that this also works
    #r = arcpy.CreateObject("RecordSet")
    #r.load(foo)
    #arcpy.SetParameter(0, r)


Your url to the resource is going to follow this pattern:

http://[nameofserver]/ArcGIS/rest/services/[NameOfGPToolBox]/GPServer/[NameOfGPScript]/jobs/[job-id]/results/[nameOfOutputParameter]


There might also be trouble with your virtual directory mapping to your arcgisserver/arcgisjobs directory.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...