Arcobjects Geoprocessing Procedure:
1. Create a geoprocessor object and set its properties.
2. Set the appropriate values for the tool’s parameters.
3. Execute a tool using the geoprocessor object’s Execute() method.
In order to create a geoprocessor object, the IGeoProcessor2 interface can be used. This interface
is defined in the geoprocessing library of ArcObjects and is implemented by the GeoProcessor
CoClass.
IGeoProcessor2 gp = new GeoProcessorClass();
//add the result of geoprocessing as a new layer to Map
gp.AddOutputsToMap = true;
//if output of geoprocessing exists before the execution of tool
//it will be overwritten
gp.OverwriteOutput = true;
Assume that you want to create Thiessen polygons for the cities FeatureClass to create proximal
zones for all cities. The proximal zones represent full areas where any location inside the zone is
closer to its associated city than any other city. Based on the Create Thiessen Polygons reference
page, you have to provide at least an input point FeatureLayer and the path to the output
FeatureClass.
In order to create and set each parameter, the IVariantArray interface of the System library must
be used.
IVariantArray parameters = new VarArrayClass();
Each parameter has to be added to the IVariantArray interface in the exact order that is specified
on the tool’s reference page.
//in_features
parameters.Add(@"D:\DataFolder\fileGDB.gdb\cities");
//out_feature_class
parameters.Add(@"D:\DataFolder\fileGDB.gdb\citiesThiessen");
//fields_to_copy(Optional)
parameters.Add("ALL");
As it is illustrated on the Create Thiessen Polygons reference page, the third parameter is optional.
You can simply not add any value to IVariantArray or, as shown in the preceding code, you can
provide an appropriate value for the optional parameter. You can skip the optional parameter
using an empty string as input to IVariantArray’s Add() method. For example, look at the
reference page of the Buffer tool; you can see that three of the seven available parameters are
mandatory. The following code demonstrates how to skip the fourth parameter and specify the
fifth parameter:
//1-in_features
parameters.Add(@"D:\test.gdb\cities");
//2-out_feature_class
parameters.Add(@"D:\test.gdb\citiesBuffer");
//3-buffer_distance_or_field
parameters.Add("50 kilometers");
//4-line_side(Optional)
parameters.Add("");
//5-line_end_type(Optional)
parameters.Add("ROUND");
//6 &7 there is no need to provide empty string
//for the rest of parameters since you don't want to set them
After setting all the required parameters, all you need to run a tool is to call the Execute() method
of the geoprocessor object. The Execute() method solicits the name of the tool and its parameters.
gp.Execute("CreateThiessenPolygons_analysis", parameters, null);
The following code shows the complete code for this example. In order to run the code, you need to
add references to the Geoprocessing and System libraries of ArcObjects:
IGeoProcessor2 gp = new GeoProcessorClass();
//add the result of geoprocessing as a new layer to Map
gp.AddOutputsToMap = true;
//if output of geoprocessing exists before the execution of tool
//it will be overwritten
gp.OverwriteOutput = true;
IVariantArray parameters = new VarArrayClass();
//in_features
parameters.Add(@"D:\DataFolder\fileGDB.gdb\cities");
//out_feature_class
parameters.Add(@"D:\DataFolder\fileGDB.gdb\citiesThiessen");
//fields_to_copy(Optional)
parameters.Add("ALL");
//or parameters.Add("");
gp.Execute("CreateThiessenPolygons_analysis", parameters, null);
The Geoprocessing library of ArcObjects is accessible through the ESRI.ArcGIS.Geoprocessing
namespace. This library contains a few hundred types which can be used to run and manage tools
and GIS workfl ows. You learned earlier in this section that IGeoProcessor2 is the main interface of
this library and the easiest way to run a geoprocessing tool is to call its Execute() method.
However, using IGeoProcessor2 is not the only approach to run a geoprocessing tool or model.
There are some managed assemblies created by Esri to performing geoprocessing in a managed
way. A managed way means there is a native .NET assembly (the Geoprocessor assembly) that is
a wrapper for some types in the Geoprocessing library of ArcObjects, and there are other .NET
assemblies for each system toolbox.
These native .NET assemblies provide an even easier way to run a system tool. In general, the
procedure for running a tool using the Geoprocessor-managed assembly is the same as running a tool
No comments:
Post a Comment