executetool\copyfeatures.vb
Executing geoprocessing tools
executetool\copyfeatures.vb
' Copyright 2008 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
' 

'
 
'* copyfeatures.cs : This VB.NET sample uses the Geoprocessor class in conjunction with geoprocessing tools classes to
 '* execute a series of 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())
      '/////////////////////////////////////////////////////////////////////////////////////////////////////////
      ' STEP 1: Make feature layers using the MakeFeatureLayer tool for the inputs to the SelectByLocation tool.
      '/////////////////////////////////////////////////////////////////////////////////////////////////////////

      ' Intialize the Geoprocessor 
            Dim GP As Geoprocessor = New Geoprocessor

            ' Set the OverwriteOutput setting to True
            GP.OverwriteOutput = True

      ' Intialize 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 theb bedrock geololgy.
      '/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      ' Intialize 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.
      '///////////////////////////////////////////////////////////////////////////////////////////////

      ' Intialize 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.
      '//////////////////////////////////////////////////////////////////////////////////////////////////////

      ' Intialize 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\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