ArcGIS – ArcGIS Geoprocessing Service Output Location Guide

arcgis-10.3arcgis-javascript-apiarcgis-servergeoprocessing-service

I am publishing an ArcGIS geoprocessing service using a Python script tool and can't seem to figure out the correct output path to use. I would like to share the results as a map service, which means that, according to this documentation, I should not use the "in_memory/output_file" scheme.

So, my script writes to a permanent location on my server:

output_path = r"\\server\path\to\data\geodatabase.gdb" 
output_layer = os.path.join(output_path, "Result")
arcpy.SetParameterAsText(1, output_layer)

But there are some issues that seem to remain.

  1. Although the tool runs successfully in ArcMap, it fails after its published on the server with an error that says the output layer already exists.

  2. Assuming I set the geoprocessing environments to overwrite the output, is this the behavior I want? What if there are multiple instances of the geoprocessing tool running… will they not conflict with each other when trying to read/write?

If I understand correctly, a new folder is created for each job, something like:

c:\arcgisserver\directories\arcgisjobs\%service%_gpserver\%jobid%\

I just can't figure out how I can define my output file to be placed there so that it can get pushed up in a temporary map service result.

Best Answer

Use the arcpy.env.scratchGDB instead - this will create those C:\arcgisserver\directories\arcgisjobs\scriptgp_gpserver\j002461f550fc4658a08da‌​c98c7bd6672 gdb for each job. Use the SetParameter, not SetParameterAsText.

The source script tool:

import arcpy
import os

in_fc = r"C:\GIS\ags_data\pub_data.gdb\_small"
output_path = r"C:\GIS\ags_data\pub_data.gdb"#arcpy.env.scratchGDB

output_layer = arcpy.Buffer_analysis(in_features=in_fc,
                                     out_feature_class=os.path.join(output_path, "Result"),
                                     buffer_distance_or_field="10 Meters")

out_fl = arcpy.MakeFeatureLayer_management(output_layer,"OutBuffers")
arcpy.SetParameter(1, out_fl)

Notice the Feature Layer object created as the output - this is what you want to pass further, for instance, in web apps (you can test the service in Web App Builder - handy to test the GP tasks). So the script tool in the toolbox will have an output (derived) parameter of Feature Layer type.

The published script tool (can be found at C:\arcgisserver\directories\arcgissystem\arcgisinput\Script_good.GPServer\extracted\v101\ags_data):

# Esri start of added imports
import sys, os, arcpy
# Esri end of added imports

# Esri start of added variables
g_ESRI_variable_1 = u'C:\\GIS\\ags_data\\pub_data.gdb\\_small'
g_ESRI_variable_2 = u'C:\\GIS\\ags_data\\pub_data.gdb'
g_ESRI_variable_3 = u'10 Meters'
g_ESRI_variable_4 = u'OutBuffers'
# Esri end of added variables

import arcpy
import os

in_fc = g_ESRI_variable_1
output_path = g_ESRI_variable_2

output_layer = arcpy.Buffer_analysis(in_features=in_fc,
                                     out_feature_class=os.path.join(output_path, "Result"),
                                     buffer_distance_or_field=g_ESRI_variable_3)

out_fl = arcpy.MakeFeatureLayer_management(output_layer,g_ESRI_variable_4)
arcpy.SetParameter(1, out_fl)

Now you need to edit the output_path to be arcpy.env.scratchGDB in the published script. No need to restart the service.

Replace this row:

output_path = g_ESRI_variable_2

with this row:

output_path = arcpy.env.scratchGDB

When you run the published task in ArcMap, a layer will be added with the symbology you've specified right before the publishing. You will also see the scratch gdb created for each job (with the actual buffer polygons created).