scratchworkspace for geoprocessing task

8983
6
Jump to solution
07-16-2014 02:08 PM
DorothyMortenson
Occasional Contributor II

Hello,

I am trying to create a geoprocessing task that will result in a pdf file.  The geoprocessing task kicks off a python script and does stuff, then generates this report as a pdf.

I'm having a little trouble knowing what to specifically do with the scratchworkspace.

Here is what I've tried:

attempt 1:

Put the scratchworkspace in the python scripts. Did this a couple of ways:

arcpy.env.scratchWorkspace = '%scratchworkspace%'

(result: doesn't seem to do anything)

and

myfile   = "C:\\arcgisserver\\directories\\arcgisoutput\\SpecialProcess\\myfile.pdf"

( works when I run the python script by itself. Works when I run the script from a tool. Does not work as a geoprocessing service)

Attempt 2 :

Put a parameter called output_location in the tool.

tried as a data type of  folder, tried as a workspace.

Direction = output

Default = %scratchworkspace%

When i run it, it says that the output_location already exists and won't run.

Attempt 3:

Don't do anything for the output_location

In the Environment Settings, under workspace, under Scratch Workspace - entered %scratchworkspace%

I know this is just picky and needs to be set just perfectly to work.  Can anyone spell it out for me please?

Thank you.

Dorothy

1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III

If you are happy to leave the PDF in the Jobs scratch folder then you can use the following line to create the output path:

fn = r"{0}\well_map.pdf".format(arcpy.env.scratchFolder)

Also, make sure not to change the scratch workspace in your script:

# You should be able to remove this line when running on the server:

arcpy.env.scratchWorkspace  = '%scratchworkspace%'   (is this in quotes?)

Let ArcGIS Server control the scratch directory and it will use a new Job directory each time the process runs.

Hope this helps.

Owen

View solution in original post

0 Kudos
6 Replies
AnthonyGiles
Frequent Contributor

Dorothy,

i have always used the in_memory workspace. This automatically creates the files for output in the jobs folder on the server under a folder with the job id.

ArcGIS Help 10.1

0 Kudos
OwenEarley
Occasional Contributor III

Hi Dorothy,

When running a GP service on the server, the scratch workspace is created under a job directory. For example:

http://[server]/arcgis/rest/directories/arcgisjobs/lotexport_gpserver/j52e4e3ec1ff741e9aad19b8263615232/scratch/WebMap_12345.pdf 

You will notice that in the path a directory with a Global ID (j52e4e3ec1ff741e9aad19b8263615232) is created to ensure the output is unique.

If you want to control where the file is created then you can export the PDF into this job directory then copy it into your target directory using python, for example:

arcpy.mapping.ExportToPDF(mxd,fn,resolution=self.dpi,image_compression="JPEG",jpeg_compression_quality=90)

shutil.copyfile(fn, r"{0}\{1}.pdf".format(outdir, key))

return r"{0}/{1}.pdf".format(baseurl,key)

# Notes:

# mxd = map document

# fn = output filename (include the arcpy.env.scratchWorkspace path)

# outdir = a path on the server for the output PDFs

# baseurl = the virtual path to the outdir

# key = unique file name

Just make sure that your output PDF name is also unique. You can then return the URL to the PDF as the geoprocessing result.

You may also need to implement some kind of regular spring cleaning for this directory. Scheduled tasks work great for this.

Owen

DorothyMortenson
Occasional Contributor II

I'm afraid I'm still a little thick. I get what both of you are saying, I'm just not quite sure how to specify the workspace and file. I'm fine with the pdf staying in the scratchworkspace. I'd also be ok if it was in memory. 

Please help me with the following:

arcpy.env.scratchWorkspace  = '%scratchworkspace%'   (is this in quotes?)

mxd      = "C:\\mystuff\\ArcMap\\well_map_template.mxd"

fn          = arcpy.env.scratchWorkspace  +   "well_map.pdf"  (this is the line I'm most confused on how to write)

arcpy.mapping.ExportToPDF(mxd, fn, resolution=self.dpi,image_compression="JPEG",jpeg_compression_quality=90)

0 Kudos
OwenEarley
Occasional Contributor III

If you are happy to leave the PDF in the Jobs scratch folder then you can use the following line to create the output path:

fn = r"{0}\well_map.pdf".format(arcpy.env.scratchFolder)

Also, make sure not to change the scratch workspace in your script:

# You should be able to remove this line when running on the server:

arcpy.env.scratchWorkspace  = '%scratchworkspace%'   (is this in quotes?)

Let ArcGIS Server control the scratch directory and it will use a new Job directory each time the process runs.

Hope this helps.

Owen

0 Kudos
DorothyMortenson
Occasional Contributor II

Yeah! Nice one, and a nice way to top of the end of the week.

GeoNZ
by
Occasional Contributor

Owen,

This still isn't working as intended (despite having set it up successfully previously)

The output PDF is created in the correct output scratch folder fine using fn = r"{0}\well_map.pdf".format(arcpy.env.scratchFolder)

But setting output parameter to fn results in an error through the geoprocessing widget:

A test message I put in the code says the output path to fn is C:\Users\arcgis\Appdata\Local\Temp\geoprocessors\*name*\*ID*\scratch\well_map.pdf

...I don't think that's a problem itself as a similar message resolves in a same way for output featureclasses which draw fine as a map service (note: I've checked the directories of the server, none are set to a local path)

Looking at the network calls/response through IE developer tools or similar, the link to the file is OK and I can click on that and successfully open it.

It's just that whatever I try I cannot get the output parameter in the geoprocessing widget to successfully set to the link- It errors out with no messages (i.e. not a script problem).

Any ideas?

0 Kudos