Using Geoprocessor


In this topic


Running tools

Working with the Geoprocessor object provides a consistent developer experience for running tools that exist in toolboxes that are available locally or on the server as geoprocessing services. When working with published server tools, Geoprocessor does not require a server context. However, unlike the GPServer object, the Geoprocessor object does not provide data type information for tools or models explicitly. To obtain information about tools or model parameters, ArcObjects such as IToolboxWorkspace, IGPToolbox, IGPTool, and IGPParameter should be used.

Executing tools

With Geoprocessor, tools are executed synchronously by calling the execute() method. Geoprocessor does not provide the ability to submit a job to the server and query for its status. Hence, results of a geoprocessing operation using Geoprocessor are not stored in a folder on the server, but can be retrieved using IGeoprocessorResult.

Geoprocessor object workflow

The following is the workflow consuming published geoprocessing services using the Geoprocessor object.
  1. Initialize Geoprocessor as shown in the following sample code.
[Java]
//Initialize license.
EngineInitializer.initializeEngine();
AoInitialize ao = new AoInitialize();
ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
//Initialize geoprocessor.
GeoProcessor geoprocessor = new GeoProcessor(); //no server context is required
  1. Add the server toolbox as shown in the following sample code.
[Java]
String serverToolbox = 
    "http://<server name>:<port num>/arcgis/services;<geoprocessing service name>";
//Add the server toolbox.
geoprocessor.addToolbox(serverToolbox);
  1. Retrieve the toolbox and tool information as shown in the following sample code.
[Java]
//Retrieve the toolbox name.
IGpEnumList toolboxList = geoprocessor.listToolboxes("BufferToolbox*"); 
    //Use a wildcard.
String toolboxName = toolboxList.next();
//Retrieve the tool name.
IGpEnumList toolsList = geoprocessor.listTools("Buffer*"); 
    //Use a wildcard. The name of the tool used here is BufferModel.
String toolName = toolsList.next(); //Iteration might be required here.
  1. Initialize the tool parameters. In the following sample code, the geoprocessing service that is being consumed contains a Buffer tool. A Buffer tool has two inputs, feature class and distance, and one output, feature class. The input feature class parameter is initialized here by a RecordSet object. This RecordSet is created from a feature class that exists on a local disk.  A RecordSet can also be created by using ArcObjects such as ITableIRecordSetInit, or ICursor objects.
[Java]
//Initialize the input feature class parameter by creating a RecordSet object from an input feature class on local disk.
String pathToInputFC = "...";
RecordSet recordSet = new RecordSet();
IGPUtilities gpUtil = new GPUtilities();
IFeatureClass ifc = gpUtil.openFeatureClassFromString(pathToInputFC);
FeatureClass fc = new FeatureClass(ifc);
recordSet.setSourceTable(fc, null);
GPFeatureRecordSetLayer inputFC = new GPFeatureRecordSetLayer();
gpRecordSet.setRecordSetByRef(recordSet);
//Initialize the distance parameter.
String distance = "10 Feet";
//Initialize the output feature class parameter to a location.
String outputFC = "...";
//Create an array of parameters to be used by execute().
IVariantArray parameters = new VarArray();
parameters.add(inputFC);
parameters.add(outputFC);
parameters.add(distance);
  1. Execute the tool. Clear any messages that the Geoprocessor might have and execute the tool. The execute() method returns the GeoprocessorResult object as shown in the following sample code.
[Java]
geoprocessor.clearMessages();
IGeoProcessorResult geoprocessorResult = geoprocessor.execute(toolName, parameters,
    null);
  1. Retrieve the result as shown in the following sample code. After execution, retrieve and print all messages and obtain reference to the result.
[Java]
IGPMessages messages = geoprocessorResult.getResultMessages();
int messageCount = messages.getCount();
for (int i = 0; i < messageCount; i++){
    System.out.println(messages.getMessage(i).getDescription());
}

IGPValue outputValue = geoprocessorResult.getOutput(0);
String outputFCName = outputValue.getAsText();
System.out.println("\nCheck Output FC at " + outputFCName);
Using GPUtilities, the result can also be converted to an IFeatureClass and used for
    further operations. 
/**Create a feature class out of the result.
GPUtilities gpUtil = new GPUtilities(); 
IFeatureClass ifc = gpUtil.openFeatureClassFromString(outputFCName);
FeatureClass fc = new FeatureClass(ifc);
.....
.....
 */
For additional information on geoprocessing, see "Publishing Geoprocessing services" and "Publishing Map services" in the Publishing section of the ArcGIS Manager documentation.


See Also:

Geoprocessing
Managing input and output
Using GPServer