Creating a layer in Geoprocessing Service causes an error

2714
3
Jump to solution
07-30-2013 08:53 AM
NilsBabel
Frequent Contributor
I have some logic in my Geoprocessing service that creates a layer in order to add it to a map.

1.        mxd = arcpy.mapping.MapDocument(map_document)
2.        df = arcpy.mapping.ListDataFrames(mxd)[0]
3.        arcpy.management.MakeFeatureLayer(out_zone, "layer") #out_zone is an in_memory fc
4.        buffer_layer = arcpy.mapping.Layer("layer") #this doesn't work in the gp service
5.        arcpy.mapping.AddLayer(df, buffer_layer, "TOP")

This works just fine in the script tool from ArcMap.  It publishes successfully as well.  But when I run the GP service I get an error on line 4 above.  I have some traceback error reporting and it says the line that is failing is:

buffer_layer = arcpy.mapping.Layer(g_Esri_variable_3)

The error I get is Object: CreateObject layer invalid data source

So my question is what is g_Esri_variable_3?  What is ArcGIS Server doing differently than the desktop script tool?  Any suggestions on fixing this?
0 Kudos
1 Solution

Accepted Solutions
NilsBabel
Frequent Contributor
I figured out a workaround.  I saved the layer to a layer file.  Then created the layer object from the layer file.  Still don't know why the original script works in desktop but not server.

View solution in original post

0 Kudos
3 Replies
NilsBabel
Frequent Contributor
I figured out a workaround.  I saved the layer to a layer file.  Then created the layer object from the layer file.  Still don't know why the original script works in desktop but not server.
0 Kudos
TerryGustafson
Frequent Contributor

I'm having the same issue and I think its because the layer in in the "GPInMemoryWorkspace" and not on a disk.

0 Kudos
JonMorris2
Frequent Contributor

When publishing to server, certain variables are substituted for g_ESRI_variable_x and declared at the top of the script. Not sure why server needs to do this - some of the variables in my scripts are just field names, like g_ESRI_variable_4 = u'F_AREA'. You can see the amended script in the arcgissystem folder on the server, e.g. D:\arcgisserver\directories\arcgissystem\arcgisinput\LFP\lfp01Setup.GPServer\extracted\v101\_python\LFP_01_Setup.py

I think the problem in your script may be to do with the current path. The help doc says that the Layer constructor needs

A string that includes the full path and file name of an existing layer (.lyr) file.

Your script doesn't contain a path, so maybe the working folder is different when running on the server. Try specifying the full path for "layer" in lines 3 and 4.

0 Kudos