Saturday 23 November 2019

arcgis desktop - Using arcpy.SetParameter() with Python Script Tool published as Geoprocessing Service?


I have a script that needs to be published to ArcGIS Server as a geoprocessing service. This script computes a set of records in table form. I need to set an output parameter so that it returns the table (recordset).


So, to make sure I have a table (not a feature class), I am using arcpy.TableToTable_conversion(), outputting the table to the in_memory workspace. So, I know I have a table. I know that it has records, as I can perform an arcpy.GetCount and get the number of records.



I have the output parameter defined. Here's what it looks like:


arcpy.SetParameter(2, theTable)

No matter what I try this does not work. In my toolbox, the output parameter is set as a RecordSet. I've tried supplying a recordset object, the path to the in-memory feature class, and many other ideas.


How do you properly handle outputting a table to a recordset output using SetParameter for ArcGIS server?



Answer



In my experience (and I'm more than happy to be corrected on this) when you build a geoprocessing service the argument you give for the output is not the object itself, but a reference to the object (the file path).


The data type just tells server how to interpret the data depending on how you run the tool (within an app it might automatically display the record-set, from SOAP you might get the record set as a data stream (though I'm not sure) and through REST you'll just get a link).


Basically you need to write your table to this file - ArcGIS server will automatically prepend a folder/jobid path to the front based on your output folder from when you added the model to server. e.g.




  1. Your output folder on the server might be C:/arcgisserver/arcgisoutput

  2. So on the server this folder becomes /arcgisoutput

  3. Then your output for your toolbox might be /arcgisoutput/tabletoolbox

  4. Your value for the output might be %scratchworkspace%/tablename.csv


Note the %scratchworkspace% allows ArcGIS server to sub in the output directories. See Key Concepts for Geoprocessing Services in the ESRI help.


So the server autotmatically creates a job id (UUID), makes a folder for you in output, and prepends this to your output argument. Write your data there and when you get it back from arcgis server you'll get a href link like: http://server_name/arcgisoutput/tabletoolbox/job_id/tablename.csv


You should now be able to access the file however you like (use urllib2.urlopen(path).read() in Python as a quick shortcut).


Hope all this is clear! -H


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