4

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.

  • Why can't just use arcpy.env.scratchGDB in the desktop side Python script instead editing the server side script once uploaded? I'm having troubles in in these things in this days geonet.esri.com/thread/193356-scratch-gdb-on-server – user781065 Apr 19 '17 at 12:48
  • I responded to your post on geonet. However, on this platform (gis.stackexchange) we don't use an answer (like you posted above) to ask questions. You should either post a new question or add a comment. – KHibma Apr 19 '17 at 13:40
4

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

  • Thanks for the detailed explanation. I did see your blog post (tereshenkov.wordpress.com/2015/04/07/…) but didn't understand fully. – Duncan Rager Mar 3 '16 at 18:02
  • 1
    No problem at all. Hope this works out fine. I strongly suggest staying away from ModelBuilder when publishing GP services, stick to Python... – Alex Tereshenkov Mar 3 '16 at 18:06
  • Curious about one more thing: if my output is not a layer to be shown on a map service, perhaps it is a .txt, .csv, json, or .pdf file, would the output path be the same? I've seen some examples that use arcpy.env.scratchFolder... is this correct? Does this scratch folder sit next to the scratch geodatabase in the project folder? – Duncan Rager Mar 9 '16 at 21:17
  • You are correct, Duncan. The scratchFolder is right beside the scratchGDB in the jobs folder for the specific service – Alex Tereshenkov Mar 10 '16 at 11:10
  • @AlexTereshenkov Is there a reason why you recommend that (1) the output parameter should be of Feature Layer type instead of Feature Class and (2) the output should be sent to SetParameter() instead of SetParameterAsText()? – Francesco Tonini Mar 9 '17 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.