Wednesday 22 January 2020

arcpy - RESTful Geoprocessing Service Request returns empty Result object without errors


This is a bit difficult for anyone here to test as our ArcGIS Server 10.2 instance is behind a firewall. I'll try to present all the necessary information, but feel free to ask for more and I'll adjust the question accordingly.


I have a Geoprocessing Service hosted on ArcGIS Server. The service is a very simple Python script (below) that takes in a string of Hydrologic Unit Codes (in this case sub-watersheds [HUC8 level]). The script takes this string, splits it and compares against a table of species occurrence by watershed. This data is compiled into a Python dictionary and returned as JSON with the json module.


I'm accessing this service via an ajax request from a web mapping app, but for simplicity's sake I'll leave the JavaScript side out unless it's really necessary.


Python


import arcpy, json


# files
hucs = r'S:\ES\ES 2007 Efiles\PROP-500 Maps and Photography\PROP-540 Geographical Information System (GIS) Data files\MegaPetition.gdb\HUCs'
species = r'S:\ES\ES 2007 Efiles\PROP-500 Maps and Photography\PROP-540 Geographical Information System (GIS) Data files\MegaPetition.gdb\CandidatesHUC'
data = {}
Watersheds = arcpy.GetParameterAsText(0)
Watersheds = Watersheds.split(",")

cursor = arcpy.SearchCursor(hucs)


for row in cursor:
for watershed in Watersheds:
if row.HUC_CODE == watershed:
currentWatershed = {}
speciesList = []
arcpy.AddMessage("Working on " + row.HUC_NAME + "...")

whereClause = "HUC8_CD = '" + watershed + "'"
speciesCursor = arcpy.SearchCursor(species, whereClause)
arcpy.AddMessage(" Contains " + str(row.NumPetitionedSp) + " At-Risk Species.")


for animal in speciesCursor:
speciesList.append(animal.GCOMNAME)
currentWatershed = {'attributes': [watershed, row.NumPetitionedSp],
'species': speciesList}
data[row.HUC_NAME] = currentWatershed

print json.dumps(data)
del row, cursor


Perhaps I don't understand how the script gets the string of watersheds from the request so that I can use it in my script. With a Script Tool I use arcpy.GetParameterAsText(0), but that doesn't seem to apply here.


I've tried to execute this script through the ArcGIS Server GP Service Page, but I still get an empty result:


RESTful Request Generates the following URL: http://server-address/arcgis/rest/services/NatureServe/AtRiskGPService/GPServer/NatureServe%20At%20Risk%20GP%20Service/execute?Watersheds=06040003%2C06040002%2C06030003%2C06030002%2C06030005&env%3AoutSR=&env%3AprocessSR=&returnZ=false&returnM=false&f=pjson


With the following Response:


Empty Response


Update!


I changed the messaging option for the service from Error to Info, so I can see that the script is taking in the watershed parameter and processing it. It looks like it's just not sending the response back as part of the Result object.


The Result object is created by running a GP tool (ex. result = arcpy.GetCount_management(in_table)). Is there any way I can add my own data to the Result object if I'm not actually running any GP tools? If not I guess I need to send the JSON through a message instead.



Answer



You have no outputs to your tool. You want to return this:



print json.dumps(data)

That wont return anything when run as a tool: neither to the results messages, nor as an output. You need to add a new, derived string parameter to your tool properties then this code at the end:


arcpy.SetParameterAsText(1, json.dumps(data))

Alternatively you could just:


arcpy.AddMessage(json.dumps(data))

But then you'd have to have your webapp loop through the output messages and parse.


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...