GP学习(三)—How to run a geoprocessing tool
2016-07-15 15:41
441 查看
声明:仅做自己学习整理用,内容拷贝自ArcEngine SDK开发文档
Using the geoprocessing assembly
Executing a system tool
Executing a custom tool
Using the geoprocessor managed assembly
Executing a system tool
with managed assembly
Executing a custom
tool with managed assembly
Each geoprocessing tool has a fixed set of parameters that provide the tool with the information it needs for execution. Tools usually have input parameters that define the dataset or datasets that will 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 for 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.
When a tool is used in a program, its parameter values must be set correctly so it can execute when the program runs. The documentation of each tool clearly defines its parameters and properties. Once a valid
set of parameter values is provided, the tool is ready to be executed.
Parameters are specified as strings or objects. Strings are text values that uniquely identify a parameter value, such as a path to a dataset or a keyword. Most tool parameters can be specified as a simple string.
However, complex parameters, such as a spatial reference, can be 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.
You can run a geoprocessing tool by using the Geoprocessing library methods or by the geoprocessor managed assembly methods. For information about the basic differences between the two approaches, see Executing
tools. In both cases, call the Execute method
of the geoprocessor.
The Execute method uses a null reference instead of an ITrackCancel interface.
The ITrackCancel interface provides access to properties and methods that 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 with the following
examples.
The following are the generic steps to execute a tool:
Add a reference to ESRI.ArcGIS.Geoprocessing to your project. This is the only reference you need if you use the geoprocessing assembly.
Create the geoprocessor object.
Add the path to the custom toolbox if you are running a custom tool.
Create an IVariantArray and populate it with tool parameter values. The IVariantArray is available through the esriSystem library.
Call the Execute method on the geoprocessor.
The process is the same if you run a system tool or a custom tool.
properties so the call to the tool is easier to read.
[C#]
when you use IGeoProcessor2.Execute. As all system toolboxes are readily available to the geoprocessor, you do not need to add the toolbox to the geoprocessor. However, you must add the custom toolbox to the geoprocessorusing
the AddToolbox method.
The following code example shows how to execute the CalculateBestPath custom tool in the BestPath.tbx toolbox:
The only difference 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, see Essential
geoprocessing vocabulary in the ArcGIS Desktop User Help system.
[C#]
Always surround your code with try-catch blocks because the Execute method throws an exception if the tool fails to run.
Add a reference to ESRI.ArcGIS.Geoprocessor. You might also need to add the ESRI.ArcGIS.Geoprocessing assembly if you want to use, for example, the result object or list datasets.
Additionally, add a reference to the toolbox assembly to which the tool belongs. If you use more than one tool from different toolboxes, also add managed assemblies for those toolboxes.
Create the geoprocessor object.
Add the path to the custom toolbox if you are running a custom tool.
Create a tool process object and set the parameter values.
Call the Execute method on the geoprocessor.
[C#]
argument list that allows you to execute a tool by specifying the tool name, the parameters of the tool, and the ITrackCancel interface. First, add your custom toolbox to the geoprocessor using the AddToolbox method. See the following code example:
[C#]
The process is the same as executing a tool using the geoprocessing assembly except for the step of creating the geoprocessor.
You can generate a toolbox assembly for a custom toolbox using the integrated development environment (IDE) framework in Visual Studio .NET. To do so, use the ArcGIS Toolbox Reference dialog box. For more information, see ArcGIS
Toolbox Reference dialog box. Once an assembly is created, you can use it like a system toolbox assembly.
To run a tool in the background geoprocessing, use the ExecuteAsync method instead. For more information, see Running
a geoprocessing tool using background geoprocessing.
|
In this topic
Running a geoprocessing toolUsing the geoprocessing assembly
Executing a system tool
Executing a custom tool
Using the geoprocessor managed assembly
Executing a system tool
with managed assembly
Executing a custom
tool with managed assembly
Running a geoprocessing tool
Each geoprocessing tool has a fixed set of parameters that provide the tool with the information it needs for execution. Tools usually have input parameters that define the dataset or datasets that will typicallybe 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 for 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.
When a tool is used in a program, its parameter values must be set correctly so it can execute when the program runs. The documentation of each tool clearly defines its parameters and properties. Once a valid
set of parameter values is provided, the tool is ready to be executed.
Parameters are specified as strings or objects. Strings are text values that uniquely identify a parameter value, such as a path to a dataset or a keyword. Most tool parameters can be specified as a simple string.
However, complex parameters, such as a spatial reference, can be 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.
You can run a geoprocessing tool by using the Geoprocessing library methods or by the geoprocessor managed assembly methods. For information about the basic differences between the two approaches, see Executing
tools. In both cases, call the Execute method
of the geoprocessor.
The Execute method uses a null reference instead of an ITrackCancel interface.
The ITrackCancel interface provides access to properties and methods that 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 with the following
examples.
Using the geoprocessing assembly
The geoprocessing assembly is the Component Object Model (COM) interop of the Geoprocessing type library. 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 to ESRI.ArcGIS.Geoprocessing to your project. This is the only reference you need if you use the geoprocessing assembly.
Create the geoprocessor object.
Add the path to the custom toolbox if you are running a custom tool.
Create an IVariantArray and populate it with tool parameter values. The IVariantArray is available through the esriSystem library.
Call the Execute method on the geoprocessor.
The process is the same if you run a system tool or a custom tool.
Executing a system tool
The following code example shows the execution of the Buffer tool from the Analysis tools toolbox. The required parameters for the tool are defined. In this case, strings are used to define the input, output, and buffer distanceproperties so the call to the tool is easier to read.
[C#]
// Add references to esriSystem for licensing and IVariantArray. using ESRI.ArcGIS.esriSystem; // Add a reference to the geoprocessing namespace. using ESRI.ArcGIS.Geoprocessing; private static void RunBuffer() { // Create the geoprocessor. IGeoProcessor2 gp = new GeoProcessorClass(); // Create an IVariantArray to hold the parameter values. IVariantArray parameters = new VarArrayClass(); // Populate the variant array with parameter values. parameters.Add(@"D:\St_Johns\data.mdb\roads"); parameters.Add(@"D:\St_Johns\data.mdb\roads_Buffer"); parameters.Add("1000 Meters"); // Execute the tool. gp.Execute("Buffer_analysis", parameters, null); }
Executing a custom tool
In addition to using the existing tools and toolboxes provided by ESRI, you can also execute custom tools, such as model tools and script tools, that exist in custom toolboxes. The process is the same for system or custom toolswhen you use IGeoProcessor2.Execute. As all system toolboxes are readily available to the geoprocessor, you do not need to add the toolbox to the geoprocessor. However, you must add the custom toolbox to the geoprocessorusing
the AddToolbox method.
The following code example shows how to execute the CalculateBestPath custom tool in the BestPath.tbx toolbox:
The only difference 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, see Essential
geoprocessing vocabulary in the ArcGIS Desktop User 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"); // Execute the model tool by name. gp.Execute("CalculateBestPath", parameters, null); }
Always surround your code with try-catch blocks because the Execute method throws an exception if the tool fails to run.
Using the geoprocessor managed assembly
The following are the general steps to run a tool:Add a reference to ESRI.ArcGIS.Geoprocessor. You might also need to add the ESRI.ArcGIS.Geoprocessing assembly if you want to use, for example, the result object or list datasets.
Additionally, add a reference to the toolbox assembly to which the tool belongs. If you use more than one tool from different toolboxes, also add managed assemblies for those toolboxes.
Create the geoprocessor object.
Add the path to the custom toolbox if you are running a custom tool.
Create a tool process object and set the parameter values.
Call the Execute method on the geoprocessor.
Executing a system tool with managed assembly
In the following code example, the Buffer tool is executed with the same parameter values by using the managed assemblies:[C#]
// Add the geoprocessor namespace. using ESRI.ArcGIS.Geoprocessor; // Add the toolbox assembly. using ESRI.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"; // Execute the tool. GP.Execute(bufferTool, null); }
Executing a custom tool with managed assembly
Custom toolboxes do not have any managed assembly. Therefore, the simplest way to run a custom tool is by using IVariantArray and execute the tool by name. The Execute method of the geoprocessor is overloaded and has an additionalargument list that allows you to execute a tool by specifying the tool name, the parameters of the tool, and the ITrackCancel interface. First, add your custom toolbox to the geoprocessor using the AddToolbox method. See 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"); // Execute the model tool by name. GP.Execute("CalculateBestPath", parameters, null); }
The process is the same as executing a tool using the geoprocessing assembly except for the step of creating the geoprocessor.
You can generate a toolbox assembly for a custom toolbox using the integrated development environment (IDE) framework in Visual Studio .NET. To do so, use the ArcGIS Toolbox Reference dialog box. For more information, see ArcGIS
Toolbox Reference dialog box. Once an assembly is created, you can use it like a system toolbox assembly.
To run a tool in the background geoprocessing, use the ExecuteAsync method instead. For more information, see Running
a geoprocessing tool using background geoprocessing.
相关文章推荐
- BGP邻居协商过程
- BGP边界网关协议
- 基于ArcEngine+C#开发的空间数据管理平台架构设计及功能说明
- LP与GP的区别
- TIPTOP GP5.1 实现链接网页方法 ui.Interface.frontCall
- ArcEngine实现坐标转换和投影
- AE开发之PageLayoutControl
- AE开发之IMapFrame接口
- TOCControl代码实现图层名可编辑
- AE开发之之ILayer接口
- Dynamics GP report Note
- ArcEngine 专题图关键代码
- IDW Spatial Analyst(ESRI.ArcGIS.SpatialAnalystTools)ArcEngine中反距离加权插值方法
- ESRI.ArcGIS.RuntimeManager.Bind返回false解决方法
- AE各种小知识点
- ICoverageAnnotationFeature注记字符串内容获取
- arcengine镶嵌数据集的基本操作(C#)
- ArcEngine报0x80040228的错误
- GA/GP中的适应度函数