ArcObjects Library Reference  

copyfeatures

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 ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.AnalysisTools;

namespace copyfeatures
{
    class copyfeatures
    {
        static void Main(string[] args)
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);

            ///////////////////////////////////////////////////////////////////////////////////////////////////////////
            // 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:\gp\nfld.gdb\wells";
            makefeaturelayer.out_layer = "Wells_Lyr";
            RunTool(GP, makefeaturelayer, null);
            
            makefeaturelayer.in_features = @"C:\gp\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:\gp\nfld.gdb\wells_c";
            
            // 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, 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.Text
Imports ESRI.ArcGIS.Geoprocessor
Imports ESRI.ArcGIS.AnalysisTools
Imports ESRI.ArcGIS.DataManagementTools
Imports ESRI.ArcGIS.esriSystem


Namespace copyfeatures
	Friend Class copyfeatures
		Shared Sub Main(ByVal args As String())

            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)

			'/////////////////////////////////////////////////////////////////////////////////////////////////////////
			' 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:\gp\nfld.gdb\wells"
			makefeaturelayer.out_layer = "Wells_Lyr"
			RunTool(GP, makefeaturelayer, Nothing)

			makefeaturelayer.in_features = "C:\gp\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:\gp\nfld.gdb\wells_150"

            ' 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