How to run a geoprocessing tool

How to run a geoprocessing tool

In this topic


 

Running a geoprocessing tool

Each geoprocessing tool has a fixed set of parameters that provides 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 several important properties:
 
  • Name—Each tool parameter has a unique name.
  • Type—The type of data expected, such as feature class, integer, string, and raster.
  • Required—Either a value must be provided for a parameter or it is optional.
 
When a tool is used in a program, its parameter values must be correctly set so it can execute when the program is run. The documentation of each tool clearly defines its parameters and properties. Once a valid set of parameter values are provided, the tool is ready to be executed.
 
Parameters are specified either 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, may be easier to specify with an object. In the following code example, the required parameters for the Buffer tool are defined. In this case, strings are used to define the input, output, and buffer distance properties of Buffer so the call to the tool is easier to read.
 
The following Execute method uses 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.
 

[C#]
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.AnalysisTools;

public void SampleBufferTool()
{

  // Initialize the geoprocessor. 
  Geoprocessor GP = new Geoprocessor();

  ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new
    ESRI.ArcGIS.AnalysisTools.Buffer();

  bufferTool.in_features = @"D:\St_Johns\data.mdb\roads_Buffer";
  bufferTool.out_feature_class = @"D:\St_Johns\data.mdb\roads";
  bufferTool.buffer_distance_or_field = "distance";

  GP.Execute(bufferTool, null);

}

[VB.NET]
Imports ESRI.ArcGIS.Geoprocessor
Imports ESRI.ArcGIS.AnalysisTools

Public Sub SampleBufferTool()
    
    ' Initialize the geoprocessor.
    Dim GP As Geoprocessor = New Geoprocessor()
    
    Dim bufferTool As ESRI.ArcGIS.AnalysisTools.Buffer = New ESRI.ArcGIS.AnalysisTools.Buffer()
    
    bufferTool.in_features = "D:\St_Johns\data.mdb\roads_Buffer"
    bufferTool.out_feature_class = "D:\St_Johns\data.mdb\roads"
    bufferTool.buffer_distance_or_field = "distance"
    
    GP.Execute(bufferTool, Nothing)
    
End Sub
Toolbox names and namespaces
The following table shows the system toolbox names and namespaces:
 
Toolbox names Namespaces
3D Analyst tools ESRI.ArcGIS.Analyst3DTools
Analysis tools ESRI.ArcGIS.AnalysisTools
Conversion tools ESRI.ArcGIS.ConversionTools
Data Management tools ESRI.ArcGIS.DataManagementTools
Cartography tools ESRI.ArcGIS.CartographyTools
Coverage tools ESRI.ArcGIS.CoverageTools
Geocoding tools ESRI.ArcGIS.GeocodingTools
Geostatistical Analyst tools ESRI.ArcGIS.GeostatisticalAnalystTools
Linear Referencing tools ESRI.ArcGIS.LinearReferencingAnalystTools
Multidimension tools ESRI.ArcGIS.MultidimensionTools
Network Analyst tools ESRI.ArcGIS.NetworkAnalystTools
Samples ESRI.ArcGIS.SamplesTools
Spatial Analyst tools ESRI.ArcGIS.SpatialAnalystTools
Spatial Statistics tools ESRI.ArcGIS.SpatialStatisticsTools
 

Running custom geoprocessing tools

In addition to using the existing tools and toolboxes provided by ESRI, it is also possible to execute your custom tools, such as model tools and script tools, which exist in custom toolboxes. Using the integrated development environment (IDE) framework built-in Visual Studio .NET, you can generate a geoprocessing assembly to represent any custom toolbox. To do so, use the ArcGIS Toolbox Reference dialog box. 
 

Executing a tool by name

It is not a prerequisite to generate a geoprocessing assembly to represent your custom toolbox. There is an alternative way to use the Execute method on the geoprocessor. The Execute method is overloaded and has an additional argument list that allows you to execute a tool by specifying the tool name, the parameters of the tool, and the ITrackCancel interface.
 
The following is an example of executing the CalculateBestPath model tool, which is located in the BestPath toolbox:
 

[C#]
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;

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);

}

[VB.NET]
Imports ESRI.ArcGIS.Geoprocessor
Imports ESRI.ArcGIS.esriSystem

Public Sub SampleCalculateBestPathTool()
    
    ' Initialize the geoprocessor.
    Dim GP As Geoprocessor = New Geoprocessor()
    
    ' Add the BestPath toolbox.
    GP.AddToolbox("C:\SanDiego\BestPath.tbx")
    
    ' Generate the array of parameters.
    Dim parameters As IVariantArray = 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, Nothing)
    
End Sub

posted @ 2014-09-04 13:32  之远  阅读(200)  评论(0编辑  收藏  举报