Special
its green color? Hover over it. Nothing, right? That’s because you just removed its reference. Now add Esri.ArcGIS.Geoprocessor. before Geoprocessor, so you have this:
static void Main(string[] args) { Esri.ArcGIS.Geoprocessor.Geoprocessor gp = new Esri.ArcGIS.Geoprocessor.Geoprocessor(); }
You now have effectively fully qualified the entire namespace and told Visual Studio where to look for the Geoprocessor class. You can use whichever method you choose. However, the first method, employing the using directive, seems to be the preferred method and is the method I will employ in the examples here. Next, add a new line and type gp.;. You should get an AutoComplete list with all the Geoprocessor methods that are now available to you. Pretty nice, eh? Now that we have a Geoprocessor object available to us, let’s put it to use on a small, simple file geodatabase of Arkansas data that I assembled from Geostor (geostor.arkansas.gov) and the U.S. Geological Survey National Hydrography Dataset (nhd.usgs.gov). Listing 4 goes into every geodatabase in C:\Data and prints the feature class names to standard output. This is not very useful by itself, but getting access to datasets is crucial to virtually all geoprocessing tasks. There are several different ways to execute your program. To run your program from within Visual Studio (also known as “debugging”), go to Debug > Step Into or press the F11 key. This will start your program in the Main() method. Once there, press F10 to step through your code. Notice that a command prompt window gets launched, and any Console.Writeline() statements get printed to this. Another way is to build your application (Ctrl + Shift + B or Build menu > Build Solution) to an executable file (.exe), open a command prompt window and navigate to the Debug directory where your compiled executable is stored by default. (My Debug directory is C:\VS\ArcUserArticle\ Geoprocessing\GpExample\GpExample\bin\Debug) Run the executable. Listing 5 shows how to run the executable (by navigating to the directory, typing the executable’s name, then pressing Enter) and the results you will get. Compiling an executable and running it from the command line is a common method of running .NET console programs. You can easily schedule processes this way through Windows Scheduled Tasks or an enterprise scheduler. Performing Basic Geoprocessing Tasks Now that we have created a Geoprocessor object, set up a workspace
Figure 1: Adding assembly references to your project on our file geodatabase, and iterated through the feature classes in the geodatabase, let’s see how to set up and perform some basic geoprocessing tasks with ArcToolbox tools. First, it is important to note that any geoprocessing tool available to you in ArcToolbox is available to you programmatically through .NET—all you need to do to get access to a tool is add a reference to its assembly, or toolbox. To do this, in the Solution Explorer in Visual Studio, right-click the References folder of your project and select Add Reference. Scroll down until you start seeing Esri assemblies. What you get should resemble Figure 1. The assembly Esri.ArcGIS.DataManagementTools is the assembly that houses all the tools available to us in the Data Management toolbox in ArcToolbox. To expose assemblies for use in your solution, you would add references to Esri. ArcGIS.Geoprocessor, Esri.ArcGIS.Geoprocessing, Esri.ArcGIS. DataManagementTools, and Esri.ArcGIS.AnalysisTools, then add them using directives for each assembly to your program file. To illustrate how to programmatically set up tool inputs and outputs, let’s perform some relatively simple geoprocessing tasks— selecting, clipping, buffering, and exporting—on our file geodatabase feature classes. Along the way, we’ll also do some logging to a text file. For more information on accessing the geoprocessor from C#, see the post “Overview of accessing geoprocessing functionality” at Continued on page 40
www.esri.com
ArcUser Spring 2010 39