Geoprocessing Python Scripts: How to work with directories and scratch?

1278
2
Jump to solution
12-01-2013 08:36 AM
AndrewBlakey
New Contributor III
I'm having some frustrating times with a GP script I'm trying to publish to my Ubuntu ArcGIS Server 10.2 machine.

The script works perfectly locally and publishes just fine, but when trying to use it in ArcGIS Desktop, it throws an immediate error:

Error executing tool.:  Traceback (most recent call last):   File "Z:\net\SITEHOST\gisdata\arcgisserver\directories\arcgissystem\arcgisinput\autoTransectFour.GPServer\extracted\v101\rq\atw.py", line 69, in <module>     arcpy.AddMessage(str(arcpy.Describe(g_ESRI_variable_2).extent) + " of the waterbodyBuffer")   File "Z:\arcgis\server\arcpy\arcpy\__init__.py", line 1234, in Describe     return gp.describe(value)   File "Z:\arcgis\server\arcpy\arcpy\geoprocessing\_base.py", line 374, in describe     self._gp.Describe(*gp_fixargs(args, True))) IOError: "%scratchFolder%\waterbodyBuffer" does not exist



I'm not including the whole script as it doesn't get far and the error seems to do with how I manage workspaces.

Imports import arcpy import gc import csv from arcpy import env from math import radians, tan, cos, fabs  ### INPUTS / OUTPUTS ### waterbodyPoly = arcpy.GetParameterAsText(0) transSpacing = float(arcpy.GetParameterAsText(1)) shorelineBuffer = float(arcpy.GetParameterAsText(2))   outTransect = arcpy.GetParameterAsText(3) outPoints = arcpy.GetParameterAsText(4) angleType = arcpy.GetParameterAsText(5) pathLines = arcpy.GetParameterAsText(6)  #arcpy.env.workspace = ws   ### VARIABLES ### kfPerimeter = shorelineBuffer - (transSpacing / 2) sRef = arcpy.Describe(waterbodyPoly).spatialReference  # Get start point from geometry boatStartPoint = arcpy.Point()  # Create Empty FCs arcpy.CreateFeatureclass_management(arcpy.env.scratchWorkspace, pathLines, "POLYLINE", spatial_reference = sRef) arcpy.AddMessage(str(arcpy.Describe(waterbodyPoly).extent) + " of the waterbodyPoly")  # Buffer Waterbody inwards by shorelineBuffer arcpy.Buffer_analysis(waterbodyPoly,"waterbodyBuffer",0.0 - shorelineBuffer) arcpy.AddMessage(str(arcpy.Describe("waterbodyBuffer").extent) + " of the waterbodyBuffer")  # 


There's a lot of bits and pieces in the ArcGIS help pages online for these things, but there's nothing I've found that very explicitly discusses how to structure and publish PYTHON scripts.

Links to better documentation or some pointers/tips/suggestions would be greatly appreciated!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KevinHibma
Esri Regular Contributor
This is the topic you'll want to read regarding scripts you write for use in a GP Service:
http://resources.arcgis.com/en/help/main/10.2/#/Authoring_geoprocessing_tasks_with_Python_scripts/00...
In regards to scratchFolder, scratchGDB, I'd start with this blog: http://blogs.esri.com/esri/arcgis/2012/10/19/the-new-scratchgdb-and-scratchfolder-environments/
(It has links to specific topics in the help as well)

When you say it works locally and publishes fine but throws an error in Desktop, do you mean when you run the published service, that is throwing the error?

I think I see what's happened. This is the line which has confused the publishing:
arcpy.Buffer_analysis(waterbodyPoly,"waterbodyBuffer",0.0 - shorelineBuffer)


In your script you've told Buffer, the output will be called "waterbodyBuffer". I'm not entirely certain what happened when you ran this on Desktop, but it must have made output of some sort. Perhaps a shapefile?
So, when the script gets published, decisions are made based on your script and your environment.
From the error it states: IOError: "%scratchFolder%\waterbodyBuffer" does not exist
I can tell that the publishing thought you wanted a shapefile, because it tried to place that output into a folder. However, you don't have .shp. So the output wont be valid.

The fix is easy enough. In your original script, I'd do this:

# Buffer Waterbody inwards by shorelineBuffer waterbodyBuffer = "in_memory\\waterbody" arcpy.Buffer_analysis(waterbodyPoly,waterbodyBuffer,0.0 - shorelineBuffer) arcpy.AddMessage(str(arcpy.Describe(waterbodyBuffer).extent) + " of the waterbodyBuffer") #


Noting that I turned your string "waterbodyBuffer" into a variable which points to in_memory\\waterbody and where it was used in Buffer and described, made that point to the variable.

View solution in original post

0 Kudos
2 Replies
KevinHibma
Esri Regular Contributor
This is the topic you'll want to read regarding scripts you write for use in a GP Service:
http://resources.arcgis.com/en/help/main/10.2/#/Authoring_geoprocessing_tasks_with_Python_scripts/00...
In regards to scratchFolder, scratchGDB, I'd start with this blog: http://blogs.esri.com/esri/arcgis/2012/10/19/the-new-scratchgdb-and-scratchfolder-environments/
(It has links to specific topics in the help as well)

When you say it works locally and publishes fine but throws an error in Desktop, do you mean when you run the published service, that is throwing the error?

I think I see what's happened. This is the line which has confused the publishing:
arcpy.Buffer_analysis(waterbodyPoly,"waterbodyBuffer",0.0 - shorelineBuffer)


In your script you've told Buffer, the output will be called "waterbodyBuffer". I'm not entirely certain what happened when you ran this on Desktop, but it must have made output of some sort. Perhaps a shapefile?
So, when the script gets published, decisions are made based on your script and your environment.
From the error it states: IOError: "%scratchFolder%\waterbodyBuffer" does not exist
I can tell that the publishing thought you wanted a shapefile, because it tried to place that output into a folder. However, you don't have .shp. So the output wont be valid.

The fix is easy enough. In your original script, I'd do this:

# Buffer Waterbody inwards by shorelineBuffer waterbodyBuffer = "in_memory\\waterbody" arcpy.Buffer_analysis(waterbodyPoly,waterbodyBuffer,0.0 - shorelineBuffer) arcpy.AddMessage(str(arcpy.Describe(waterbodyBuffer).extent) + " of the waterbodyBuffer") #


Noting that I turned your string "waterbodyBuffer" into a variable which points to in_memory\\waterbody and where it was used in Buffer and described, made that point to the variable.
0 Kudos
AndrewBlakey
New Contributor III
Hi Kevin:

This is all incredibly helpful. Thank you very much.  While my script does contain errors, your changes have pushed the bar down another 10 or so lines.  This is also another consideration when writing/changing my code to work properly.

Thanks for the blog post as well. I didn't see that one before.

It's great to begin a Monday morning with a plethora of possibility for fixing a very frustrating weekend error. 😃
0 Kudos