Use geoprocessing services in Python

Like other geoprocessing tools, geoprocessing server tools have a fixed set of parameters that provide the tool with the information it needs for execution. When using asynchronous server tools in a script, the output can be retrieved by the Result's getOutput method.

Tip:

The IsSynchronous function can be used to determine whether a tool is run synchronously or asynchronously. When a tool is synchronous, the results are automatically returned, but no other action may be taken until the tool has completed.

In the following example, the GetParameterValue function is used to get a FeatureSet object from a server tool. This FeatureSet object contains the schema of the tool's input parameter. The FeatureSet object is then loaded with a feature class, and the server tool is executed on the server. The script ends by using the Result object's getOutput method to get the tool output's, which is then saved locally by using the FeatureSet save method.

import arcpy
import time

# Add a toolbox from a server
arcpy.ImportToolbox("http://flame7/arcgis/services;GP/BufferByVal", "mytools")

# Use GetParameterValue to get a featureset object with the default 
#   schema of the first parameter of the tool 'bufferpoints'
inFeatureSet = arcpy.GetParameterValue("bufferpoints", 0)

# Load a shapefile into the featureset
inFeatureSet.load("c:/base/roads.shp")

# Run a server tool named BufferPoints with featureset created above
result = arcpy.BufferPoints_mytools(inFeatureSet, "5 feet")

# Check the status of the result object every 0.2 seconds until it has a value 
#    of 4 (succeeded) or greater
while result.status < 4:
    time.sleep(0.2)

# Get the output FeatureSet back from the server and save to a local geodatabase
outFeatSet = result[0]
outFeatSet.save("c:/temp/base.gdb/towers_buffer")

Getting a raster result from a server tool

Raster results are returned as Tagged Image File Format (TIFF). By default, when using getOutput, the TIFF is written to your system's TEMP folder. To control the location of the TIFF, set the scratchWorkspace environment to a folder.

import arcpy
import time

# Set the scratchworkspace to a folder.
arcpy.env.scratchWorkspace = "c:/temp/rasteroutput"

# Add a toolbox from a server
arcpy.ImportToolbox("http://flame7/arcgis/services;SlopeByVal", "mytools")

dem = "c:/dems/k_ne_g"

# Run a server tool named RunSlope
result = arcpy.RunSlope_mytools(dem)

# Check the status of the result object every 0.2 seconds until it has a value 
#    of 4 (succeeded) or greater
while result.status < 4:
    print result.status
    time.sleep(0.2)

# Raster output will be written to the scratchworkspace
outTIFF = result[0]

Getting a map image

Geoprocessing services can have a result map service to create a digital map image of task results. Digital maps contain visual representations of geographic datasets that communicate information. Digital maps are transported across the web as images (such as a .jpeg). A map image, byte for byte, contains far more human-interpretable information than raw features in a feature class. Map images are also manageable—they are easily compressed, they can be tiled into manageable chunks, and there are established methods for transporting and viewing them across the web.

Map images are created by an ArcGIS Server map service and are the result of publishing an ArcMap document (.mxd). Because of the characteristics of a map image, you may want to create one for the results of your geoprocessing task and transport the image across the web rather than transporting the result dataset or datasets. Geoprocessing services can have a result map service used by ArcGIS Server to create map images of your output data.

import arcpy
import time
from urllib.request import urlretrieve

# Add a toolbox from a server
arcpy.ImportToolbox("http://flame7/arcgis/services;GP/BufferByVal", "mytools")

# Use GetParameterValue to get a featureset object with the default schema of the 
#   first parameter of the tool 'bufferpoints'
inFeatureSet = arcpy.GetParameterValue("bufferpoints", 0)

# Load a shapefile into the featureset
inFeatureSet.load("c:/base/roads.shp")

# Run a server tool named BufferPoints with featureset created above
result = arcpy.BufferPoints_mytools(inFeatureSet, "5 feet")

# Check the status of the result object every 0.2 seconds until it has a value 
#   of 4 (succeeded) or greater
while result.status < 4:
    time.sleep(0.2)
    print(result.status)

# Return a map service
mapimage = result.getMapImageURL(0)

# Use Python's urllib.request's urlretrieve method to copy the image locally
urlretrieve(mapimage, "c:/base/road_buffer.jpg")