![]() |
Getting results from a geoprocessing tool
The geoprocessor always returns the output values of a tool when it is executed. However, depending on whether the tool is executed with a 9.3-version geoprocessor or a 9.2-version geoprocessor makes a difference in what you get back and how it is used. Users familiar with geoprocessing server tools will recall that server tools returned result objects at ArcGIS 9.2. The advantage of a result object is that you can maintain information about the execution of tools, including messages, parameters, and output. These results can be maintained even after several other tools have been run. With a 9.2-geoprocessor, a result is typically returned as a string, but can also be returns as a numeric type, or Boolean depending on the tool used. With a 9.2-version geoprocessor, information about the execution of a tool is accessed directly from the geoprocessor object and is only accessible until you run another tool. The following example shows how to get the output from a result object following the execution of a geoprocessing tool with a 9.3-version geoprocessor.# Create a 9.3-version geoprocessor
#
import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.Workspace = "D:/St_Johns/data.gdb"
# A result object is returned from a tool using a 9.3-version geoprocessor
#
result = gp.Clip_analysis("roads","urban_area","urban_roads")
# To return the output value, use the result object's GetOutput method. An index
# value is used to represent which output, as most tools including Clip have only a
# single output parameter, this will usually be 0
#
print result.GetOutput(0)
![The result object properties The result object properties](https://www.cnblogs.com/gisoracle/admin/mk:@MSITStore:C:/Program%20Files%20(x86)/ArcGIS/help/automating_your_work_with_scripts.chm::/gp_result_object.jpg)
Status | Returns the status of the job on the server.
|
ResultID | Returns the result ID. |
MessageCount | Returns the number of messages. |
MaxSeverity | Returns the maximum severity. The returned severity can be 0 (no errors/warnings raised), 1 (warnings raised), or 2 (errors raised). |
OutputCount | Returns the number of outputs. |
GetMessages(severity) | The type of messages to be returned. 0=message, 1=warning, 2=error. Not specifying a value returns all message types. |
GetMessage(index) | Returns a specific message. |
GetSeverity(index) | Returns the severity of a specific message. |
GetInput(index) | Returns a given input. If a record set or raster data object, a RecordSet or RasterData object is returned. |
GetOutput(index) | Returns a given output. If a record set or raster data object, a RecordSet or RasterData object is returned. |
GetMapImageUrl(ParameterList, Height, Width, Resolution) | Get map service image for a given output. |
Cancel() | Cancel the job on the server. |
# Create a 9.3-version geoprocessor
#
import arcgisscripting
gp = arcgisscripting.create(9.3)
roads = "D:/base/data.gdb/roads"
# A result object is returned from a tool using a 9.3-version geoprocessor
#
result = gp.GetCount(roads)
# GetCount is used to determine the number of records. Since GetOutput always
# returns a Unicode string, we must convert it to a integer using Python's built-in int() function
# if we want to use the value as an integer
#
count = int(result.GetOutput(0))
if count > 0:
gp.Delete(roads)
A 9.2-version geoprocessor returns the output values of the tool when it is executed. Typically, this is the path to the output dataset produced or updated by the tool, but it can be other value types, such as a number or Boolean. If an output is a multivalue parameter, the values are returned in a string, which consists of multiple strings separated by a semicolon. Output values are always returned to the script from the geoprocessor. Return values are necessary when a tool has no output dataset and has an output scalar value, such as an integer or Boolean. They are also helpful when working with multiuser databases, since output names are qualified with a user's name and table space when outputs are written into the database. The following two examples, demonstrate how to get a returned value back from a tool that outputs a feature class name, and a tool that outputs a double-precision number.
# Create a 9.2-version geoprocessor
#
import arcgisscripting
gp = arcgisscripting.create()
gp.Workspace = "D:/St_Johns/data.gdb"
outputFC = gp.Clip_analysis("roads","urban_area","urban_roads")
# Print output feature class name D:/St_Johns/data.gdb/urban_roads
#
print outputFC
Return value
# Create a 9.2-version geoprocessor
#
import arcgisscripting
gp = arcgisscripting.create()
gridIndex = gp.CalculateDefaultGridIndex("D:/St_Johns/data.gdb/urban_roads")
# Print return value, which is a double precision number
#
print gridIndex
Like other geoprocessing tools, geoprocessing server tools have a fixed set of parameters that provide the tool with the information it needs for execution. Unlike other tools (core, model, script), when using server tools in a script, the output must be deliberately retrieved by the user instead of automatically by the execution of the tool. The result object is how a user interacts with a job on a remote server.
In the following example, the GetParameterValue method 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's Save method.
a featureset object is created by using
import arcgisscripting
import time
gp = arcgisscripting.create(9.3)
# Add a toolbox from a server
#
gp.AddToolbox("http://flame7/arcgis/services;GP/BufferByVal")
# Use GetParameterValue to get a featureset object with the default schema of the
# first parameter of the tool 'bufferpoints'
#
inFeatureSet = gp.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 = gp.BufferPoints(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.GetOutput(0)
outFeatSet.Save("c:/temp/base.gdb/towers_buffer")
Getting a raster result
Raster results are returned as Tagged Image File Format (TIFF) format. 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 arcgisscripting, time
gp = arcgisscripting.create(9.3)
# Set the scratchworkspace to a folder.
#
gp.ScratchWorkspace = "c:/temp/rasteroutput"
# Add a toolbox from a server
#
gp.AddToolBox("http://flame7/arcgis/services;SlopeByVal")
dem = "c:/dems/k_ne_g"
# Run a server tool named RunSlope
#
result = gp.RunSlope(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.GetOutput(0)
Getting a map image
With asynchronous jobs, the user can choose to get a map image that symbolizes the results in Joint Photographic Experts Group (JPEG) format. When large output record sets are expected back from the server, this option takes much less time.
import arcgisscripting
import time
import urllib
gp = arcgisscripting.create(9.3)
# Add a toolbox from a server
#
gp.addtoolbox("http://flame7/arcgis/services;GP/BufferByVal")
# Use GetParameterValue to get a featureset object with the default schema of the
# first parameter of the tool 'bufferpoints'
#
inFeatureSet = gp.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 = gp.BufferPoints(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 module's urlretrieve method to copy the image locally
#
urllib.urlretrieve(mapimage, "c:/base/road_buffer.jpg)