About the Executing geoprocessing tools Sample
[C#]
copyfeatures.cs
/*
* copyfeatures.cs : This C# sample uses the Geoprocessor class in conjunction with geoprocessing tools classes to
* execute a series of geoprocessing tools. This sample will extract features to a new feature class based on a
* location and an attribute query.
*/
using System;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.AnalysisTools;
namespace copyfeatures
{
class copyfeatures
{
[STAThread]
static void Main(string[] args)
{
if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine))
{
if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop))
{
System.Windows.Forms.MessageBox.Show("This application could not load the correct version of ArcGIS.");
return;
}
}
LicenseInitializer aoLicenseInitializer = new LicenseInitializer();
if (!aoLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine, esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced },
new esriLicenseExtensionCode[] { esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork }))
{
System.Windows.Forms.MessageBox.Show("This application could not initialize with the correct ArcGIS license and will shutdown. LicenseMessage: " + aoLicenseInitializer.LicenseMessage());
aoLicenseInitializer.ShutdownApplication();
return;
}
// Run the geoprocessing code
SelectFeaturesAndRunCopyFeatures();
aoLicenseInitializer.ShutdownApplication();
}
private static void SelectFeaturesAndRunCopyFeatures()
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 1: Make feature layers using the MakeFeatureLayer tool for the inputs to the SelectByLocation tool.
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialize the Geoprocessor
Geoprocessor GP = new Geoprocessor();
// Initialize the MakeFeatureLayer tool
MakeFeatureLayer makefeaturelayer = new MakeFeatureLayer();
makefeaturelayer.in_features = @"C:\data\nfld.gdb\wells";
makefeaturelayer.out_layer = "Wells_Lyr";
RunTool(GP, makefeaturelayer, null);
makefeaturelayer.in_features = @"C:\data\nfld.gdb\bedrock";
makefeaturelayer.out_layer = "bedrock_Lyr";
RunTool(GP, makefeaturelayer, null);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 2: Execute SelectLayerByLocation using the feature layers to select all wells that intersect the bedrock geology.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialize the SelectLayerByLocation tool
SelectLayerByLocation SelectByLocation = new SelectLayerByLocation();
SelectByLocation.in_layer = "Wells_Lyr";
SelectByLocation.select_features = "bedrock_Lyr";
SelectByLocation.overlap_type = "INTERSECT";
RunTool(GP, SelectByLocation, null);
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 3: Execute SelectLayerByAttribute to select all wells that have a well yield > 150 L/min.
/////////////////////////////////////////////////////////////////////////////////////////////////
// Initialize the SelectLayerByAttribute tool
SelectLayerByAttribute SelectByAttribute = new SelectLayerByAttribute();
SelectByAttribute.in_layer_or_view = "Wells_Lyr";
SelectByAttribute.selection_type = "NEW_SELECTION";
SelectByAttribute.where_clause = "WELL_YIELD > 150";
RunTool(GP, SelectByAttribute, null);
////////////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 4: Execute CopyFeatures tool to create a new feature class of wells with well yield > 150 L/min.
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialize the CopyFeatures tool
CopyFeatures CopyFeatures = new CopyFeatures();
CopyFeatures.in_features = "Wells_Lyr";
CopyFeatures.out_feature_class = @"C:\data\nfld.gdb\high_yield_wells";
RunTool(GP, CopyFeatures, null);
}
private static void RunTool(Geoprocessor geoprocessor, IGPProcess process, ITrackCancel TC)
{
// 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);
}
}
// Function for returning the tool messages.
private static void ReturnMessages(Geoprocessor gp)
{
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
Console.WriteLine(gp.GetMessage(Count));
}
}
}
}
}
[Visual Basic .NET]
copyfeatures.vb
'
'* copyfeatures.cs : This VB.NET sample uses the Geoprocessor class in conjunction with geoprocessing tools classes to
'* execute a series of geoprocessing tools. This sample will extract features to a new feature class based on a
'* location and an attribute query.
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Windows.Forms
Imports System.Text
Imports ESRI.ArcGIS.Geoprocessor
Imports ESRI.ArcGIS.AnalysisTools
Imports ESRI.ArcGIS.DataManagementTools
Imports ESRI.ArcGIS.esriSystem
Namespace copyfeatures
Friend Class copyfeatures
<STAThread()> _
Shared Sub Main(ByVal args As String())
If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine)) Then
If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)) Then
System.Windows.Forms.MessageBox.Show("This application could not load the correct version of ArcGIS.")
End If
End If
Dim aoLicenseInitializer As LicenseInitializer
aoLicenseInitializer = New LicenseInitializer
'ESRI License Initializer generated code.
If (Not aoLicenseInitializer.InitializeApplication(New esriLicenseProductCode() {esriLicenseProductCode.esriLicenseProductCodeEngine, esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced}, _
New esriLicenseExtensionCode() {esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork})) Then
System.Windows.Forms.MessageBox.Show("This application could not initialize with the correct ArcGIS license and will shutdown. LicenseMessage: " + aoLicenseInitializer.LicenseMessage())
aoLicenseInitializer.ShutdownApplication()
Return
End If
' Run the geoprocessing code
SelectFeaturesAndRunCopyFeatures()
' Shutdown application
aoLicenseInitializer.ShutdownApplication()
End Sub
Private Shared Sub SelectFeaturesAndRunCopyFeatures()
'/////////////////////////////////////////////////////////////////////////////////////////////////////////
' STEP 1: Make feature layers using the MakeFeatureLayer tool for the inputs to the SelectByLocation tool.
'/////////////////////////////////////////////////////////////////////////////////////////////////////////
' Initialize the Geoprocessor
Dim GP As Geoprocessor = New Geoprocessor
' Set the OverwriteOutput setting to True
GP.OverwriteOutput = True
' Initialize the MakeFeatureLayer tool
Dim makefeaturelayer As MakeFeatureLayer = New MakeFeatureLayer()
makefeaturelayer.in_features = "C:\data\nfld.gdb\wells"
makefeaturelayer.out_layer = "Wells_Lyr"
RunTool(GP, makefeaturelayer, Nothing)
makefeaturelayer.in_features = "C:\data\nfld.gdb\bedrock"
makefeaturelayer.out_layer = "bedrock_Lyr"
RunTool(GP, makefeaturelayer, Nothing)
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' STEP 2: Execute SelectLayerByLocation using the feature layers to select all wells that intersect the bedrock geology.
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' Initialize the SelectLayerByLocation tool
Dim SelectByLocation As SelectLayerByLocation = New SelectLayerByLocation()
SelectByLocation.in_layer = "Wells_Lyr"
SelectByLocation.select_features = "bedrock_Lyr"
SelectByLocation.overlap_type = "INTERSECT"
RunTool(GP, SelectByLocation, Nothing)
'///////////////////////////////////////////////////////////////////////////////////////////////
' STEP 3: Execute SelectLayerByAttribute to select all wells that have a well yield > 150 L/min.
'///////////////////////////////////////////////////////////////////////////////////////////////
' Initialize the SelectLayerByAttribute tool
Dim SelectByAttribute As SelectLayerByAttribute = New SelectLayerByAttribute()
SelectByAttribute.in_layer_or_view = "Wells_Lyr"
SelectByAttribute.selection_type = "NEW_SELECTION"
SelectByAttribute.where_clause = "WELL_YIELD > 150"
RunTool(GP, SelectByAttribute, Nothing)
'//////////////////////////////////////////////////////////////////////////////////////////////////////
' STEP 4: Execute CopyFeatures tool to create a new feature class of wells with well yield > 150 L/min.
'//////////////////////////////////////////////////////////////////////////////////////////////////////
' Initialize the CopyFeatures tool
Dim copy_features As ESRI.ArcGIS.DataManagementTools.CopyFeatures = New ESRI.ArcGIS.DataManagementTools.CopyFeatures()
copy_features.in_features = "Wells_Lyr"
copy_features.out_feature_class = "C:\data\nfld.gdb\high_yield_wells"
'' Set the output Coordinate System environment
'GP.SetEnvironmentValue("outputCoordinateSystem", "C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems\Projected Coordinate Systems\UTM\NAD 1983\NAD 1983 UTM Zone 21N.prj")
RunTool(GP, copy_features, Nothing)
End Sub
Private Shared Sub RunTool(ByVal geoprocessor As Geoprocessor, ByVal process As IGPProcess, ByVal TC As ITrackCancel)
' Set the overwrite output option to true
geoprocessor.OverwriteOutput = True
Try
geoprocessor.Execute(process, Nothing)
ReturnMessages(geoprocessor)
Catch err As Exception
Console.WriteLine(err.Message)
ReturnMessages(geoprocessor)
End Try
End Sub
' Function for returning the tool messages.
Private Shared Sub ReturnMessages(ByVal gp As Geoprocessor)
' Print out the messages from tool executions
Dim Count As Integer
If gp.MessageCount > 0 Then
For Count = 0 To gp.MessageCount - 1
Console.WriteLine(gp.GetMessage(Count))
Next
End If
End Sub
End Class
End Namespace