In this topic How to run a geoprocessing tool (this place is carefully sorted out.) )Each geoprocessing tool has a fixed set of parameters this provide with the tool it information for needs. Tools usually have input parameters that define the dataset or datasets that would typically be used to generate new output Data. Parameters have the following important properties:Name-each tool parameter has a unique name.Type-the type of data expected, such as a feature class, Integer, string, and raster.Required-either A value must be provided to a parameter, or it is optional. Each tool has a documentation page known as A tool reference page. For more information about parameters, see interpreting a tool reference page. (Interpreting: explanation. This page describes how to quickly locate the tool, find the tool name and tool parameter information, and be sure to understand the information on this topic so that you can use the ArcGIS tool well. In fact, when you use the ArcMap System tool, in the tool's help document, you can find the tool name, alias, parameters, and some of the Python code. When a tool was used in a, its parameter values must being set correctly so it can execute as the program runs. The documentation of the tool clearly defines its parameters and properties. Once a valid set of parameter values is provided, the tool are ready to be executed. Parameters are specified as strings or objects. Strings are text values that is uniquely identify a parameter value, such as a path to a dataset or a keyword. Most tool parameters can is specified as a simple string. However, complex parameters, such as a spatial reference, can is easier to specify with an object. Each tool has its own paraMeter types. To get complete information on a particular Tool, review the tool reference page. Interpreting a tool Reference page explains how to read a tool reference page and extract information to use in. NET. The difference between the two methods: Geoprocessing & geoprocessor You can run a geoprocessing tool by using the geoprocessing library me Thods or by the Geoprocessor managed assembly methods. For information about the basic differences between the two approaches, seeexecuting tools. In both cases, the execute method of the geoprocessor is called. Cancellation is usually set to null the Execute method uses a null reference instead of An itrackcancel interface. The Itrackcancel interface provides access to properties and methods this determine if a cancellation has been executed by The user and also allows developers to specify what actions constitute a cancellation. Both approaches are elaborated (elaborated below) with the following examples. Using The geoprocessing assembly (is a COM interop assembly) The geoprocessing assembly is the Component Object Model (COM) interop of the Geoprocessing type library (what is COM Interop. COM interop looks like a link between COM and. NET, a bridge. The "Execute method" of the IGeoProcessor2 interface of the library is used to run A tool. The following are the generic steps to execute a tool:add a reference toESRI.ArcGIS.Geoprocessingto your project. This is the reference for you need if your use the Geoprocessing assembly. Create Thegeoprocessor Object (  via the IGeoProcessor2 interface; Note: P is uppercase and the 2nd interface). Add the path to the custom Toolbox if you are running a custom tool. Create an Ivariantarray and populate (assembled on board, filled in) it with tool parameter values. The Ivariantarray is available through the Esrisystem library (parameter values are set by Ivariantarray). Call The execute Methodon the geoprocessor. The process is the same if you run a system tool or a custom tool. Make sure that the fill parameters are set in the correct order: Make sure you maintain the orders of parameters as specified in tool reference page when Popul AtinG The variant array. Follow The syntax section of the tool reference page ("at the bottom"), it shows the correct ordering of parameters . If you want to skip optional parameters, you only need to populate them with an empty string, such as "": if you are want to skip optional parameter, just add a empty string to the variant array in Correct order. For example, if a tool ' s third, fourth and fifth parameters are optional and your want to the fifth Meter, then you have to pass empty strings to the third and fourth parameters the To maintain correct. If you fill in an empty string, the handler uses the default value: Passing an empty string instructs the "tool to" to "use" of that parameter. The correct parameter setting order needs to refer to tool's help Page instead of the tool ' dialog box, which is important: Do not follow the tool ' s dialog box to get the orders of Paramete Rs Follow the tool ' s help page (browse to interpreting a tool reference page link on the end). Parameters are arranged on a tool dialog box by ' Display order ' not by the ' actual order. executing a system tool The following code example shows the execution of the Buffer tool from the Analysis Toolbox. The required parameters for the tool are defined. In this case, strings are used to define the input, output, and buffer distance properties, so the call to the tool is EAS Ier to read. [C #]
Using System;
Using System.Threading;
ADD references to Esrisystem for licensing and Ivariantarray.
Using ESRI. Arcgis.esrisystem;
ADD a reference to the Geoprocessing namespace.
Using ESRI. arcgis.geoprocessing;
Call this method from your main.
private static void Runbuffer ()
{
//Create the geoprocessor.
IGeoProcessor2 GP = new Geoprocessorclass (); Note that the left side of the interface, but also to note the name of the right class suffix with a class.
This place is not the same as geoprocessor. Gp.
Overwriteoutput = true;
Igeoprocessorresult result = new Geoprocessorresultclass ();
Create a Variant array to hold the parameter values.
Ivariantarray parameters = new Vararrayclass ();
Object SEv = null;
try {//Populate the variant array with parameter values. Parameters.
ADD (@ "C:\data\california.gdb\cities"); Parameters.
ADD (@ "C:\data\california.gdb\cities_buff"); Parameters.
Add ("1000 Meters"); Execute the tool. "
Buffer_analysis "This string can be found in the syntax description in the feature description below ArcGIS Help, which is the function name .... result = GP.
Execute ("Buffer_analysis", parameters, NULL);
Wait until the execution completes. while (result.
Status = = esrijobstatus.esrijobexecuting) thread.sleep (1000);
Wait for 1 second.
Print geoprocessring messages. Console.WriteLine (GP.
Getmessages (ref sev));
catch (Exception ex){//Print a generic exception message. Console.WriteLine (ex.
message);
Print geoprocessing execution error messages. Console.WriteLine (GP.
Getmessages (ref sev)); }
}
Executing a custom tool In addition to using the existing tools and toolboxes provided by Esri, can also execute custom tools, such as Model T Ools and script tools, that exist in custom toolboxes. The process is the same for system or custom tools while you use Igeoprocessor2.execute. As all system toolboxes are readily available to the geoprocessor, you don't need to add the toolbox. However, you must add the custom Toolbox to the Geoprocessor using the Addtoolbox method. The following code example shows how to execute the Calculatebestpath custom tool in the BESTPATH.TBX toolbox:the only di Fference between running a system tool and a custom tool is adding the custom toolbox to the Geoprocessor. For more information on system and custom tools, you'll essential geoprocessing vocabulary in the ArcGIS Desktop Help system. [C #]
public void samplecalculatebestpathtoolgping ()
{
//Initialize the geoprocessor.
IGeoProcessor2 GP = new Geoprocessorclass ();
ADD the Bestpath Toolbox.
Gp. Addtoolbox (@ "C:\SANDIEGO\BESTPATH.TBX");
Generate the array of parameters.
Ivariantarray parameters = new Vararrayclass ();
Parameters. ADD (@ "C:\SanDiego\source.shp");
Parameters. ADD (@ "C:\SanDiego\destination.shp");
Parameters. ADD (@ "C:\SanDiego\bestpath.shp");
Object SEv = null;
Try
{
//Execute the Model tool by name.
Gp. Execute ("Calculatebestpath", parameters, null);
Console.WriteLine (GP. Getmessages (ref sev));
catch (Exception ex)
{
//Print geoprocessing execution error messages.
Console.WriteLine (GP. Getmessages (ref sev));
}
Always Surround your code with Try-catch blocks, because the Execute method throws a exception if the tool to run.
Using the Geoprocessor managed assembly (is a managed assembly) The following are the general steps to run a Tool:add a reference toESRI.ArcGIS.Geoprocessor. (If you need to use the results of the tool execution, you can also refer to ESRI.A Rcgis. Geoprocessing:you may also need to add the ESRI. Arcgis.geoprocessing assembly If you are want to use, for example, the result object or List datasets.) Additionally, add areference to the Toolbox assembly to which the tool belongs. If you are more than one tool from different toolboxes, also add managed assemblies for those. Create Thegeoprocessor Object (Note that it is through the Geoprocessor class, which is what we normally call a class instantiation of an object.) Note: P is lowercase). Add the path to the custom Toolbox if you are running a custom tool. Create a tool process object and set the parameter values (parameter values are set directly to the specific tool). Call "Execute" on the geoprocessor.
executing a system tool with managed assembly (I found that this is the case with a lot of my usual: geoprocessor) In the following code example, the Buffer tool are executed with the same values by using the parameter managed : [C #]
//Add the Geoprocessor namespace. Using ESRI.
Arcgis.geoprocessor; ADD the Toolbox assembly. You need to refer to the assembly using ESRI where the tool is located.
Arcgis.analysistools;
public void Samplebuffertool () {//Create the geoprocessor.
Geoprocessor GP = new Geoprocessor ();
Create the tool process object. ESRI. ArcGIS.AnalysisTools.Buffer Buffertool = new ESRI.
ArcGIS.AnalysisTools.Buffer ();
Set parameter values.
Buffertool.in_features = @ "D:\St_Johns\data.mdb\roads";
Buffertool.out_feature_class = @ "D:\St_Johns\data.mdb\roads_Buffer";
Buffertool.buffer_distance_or_field = "Distance";
Object SEv = null;
try {//Execute the tool. Gp.
Execute (Buffertool, NULL); Console.WriteLine (GP.
Getmessages (ref sev));
The catch (Exception ex) {//Print geoprocessing execution error messages. Console.WriteLine (GP.
Getmessages (ref sev)); }
}
Executing a custom tool with managed assembly Custom toolboxes do not have any managed assembly. Therefore, the simplest way to run a custom tool are by using Ivariantarray and executing the tool by name. The Execute method, the Geoprocessor is overloaded and has, additional argument list that allows to execute a tool By specifying the tool name, the parameters of the tool, and the Itrackcancel interface. The Your custom toolbox to the Geoprocessor using the Addtoolbox method. The following code example: [C #]
public void Samplecalculatebestpathtool () {//Initialize the geoprocessor.
Geoprocessor GP = new Geoprocessor ();
ADD the Bestpath Toolbox. Gp.
Addtoolbox (@ "C:\SANDIEGO\BESTPATH.TBX");
Generate the array of parameters.
Ivariantarray parameters = new Vararrayclass (); Parameters.
ADD (@ "C:\SanDiego\source.shp"); Parameters.
ADD (@ "C:\SanDiego\destination.shp"); Parameters.
ADD (@ "C:\SanDiego\bestpath.shp");
Object SEv = null;
try {//Execute the Model tool by name. Gp.
Execute ("Calculatebestpath", parameters, NULL); Console.WriteLine (GP.
Getmessages (ref sev));
The catch (Exception ex) {//Print geoprocessing execution error messages. Console.WriteLine (GP.
Getmessages (ref sev)); }
}
The process of executing a custom tool is same whether your use the Geoprocessing assembly or the Geoprocessor managed mbly. The only difference are in creating the geoprocessor. Can generate a toolbox assembly for a custom toolboxusing the integrated development Environment (IDE) framework in Vi Sual Studio. NET. To did, use the ArcGIS Toolbox Reference dialog box. For more information, you'll be Toolbox Reference dialog box in ArcGIS (this is the recommended way to package your custom tool items.) ). Once an assembly is created, your can use it in the same way as a system Toolbox assembly. To run a tool in the background geoprocessing and use the Executeasync method instead. For more information, Running a geoprocessing tool using background geoprocessing.
igpprocess Interface (Just an interface, from the word process to see igpprocess is a verbal interface)
private static void Runtool (Geoprocessor geoprocessor,igpprocess process, Itrackcancel TC)
{//geoprocessor is a noun, a subject; igpprocess is a verb interface, a predicate ....
Set the Overwrite output option to True
Geoprocessor. Overwriteoutput = true;
Execute the tool
Try
{
Geoprocessor. Execute (process, NULL);
Returnmessages (Geoprocessor);
}
catch (Exception err)
{
Console.WriteLine (Err. message);
Returnmessages (Geoprocessor);
}
}
Geo-processed environment settings
In ArcMap below the Operation Toolbox inside the tool, sometimes will set some environment variable, in AE inside how will do it.
(1) If you know the relevant interface of your operation, you can use Irasteranalysisenvironment, the following code:
Ilocalop plocalop = new Rasterlocalopclass ();
iworkspacefactory penvwf = new Rasterworkspacefactoryclass ();