Getting Started with Geoprocessing and ArcObjects in .NET
Continued from page 39
the ArcGIS Resource Center (resources.arcgis.com) and “Executing geoprocessing tools”, both available from the ArcGIS Resource Center for Geoprocessing. Listing 6 is a program that performs basic geoprocessing tasks on feature classes in the ArkansasData file geodatabase. This listing is verbosely commented, so I won’t go into the details. Essentially, we go into the streams polyline feature classes, select all the perennial streams that intersect the city limits polygons, clip those streams to the city limits, sum up the stream lengths for each new feature class, and write those results to a text file. If you need to process every feature class in your geodatabase, you could employ the code in Listing 4 and use it to dynamically fetch the list of feature classes for iteration at run time instead of explicitly listing your inputs as is done in Listing 6 on lines 27–28. Working with ArcSDE So far we have worked with file geodatabases, but what about ArcSDE (both Enterprise and Workgroup)? Let’s dig a little deeper into using ArcObjects in .NET by connecting to instances of Enterprise ArcSDE running on SQL Server (2005, in this case) and Workgroup ArcSDE running on SQL Server Express 2008. (Apologies to those working in Oracle shops: I don’t have access to an Oracle ArcSDE instance to test against.) For more information on connecting to geodatabases (including Oracle ArcSDE), see the article “How to connect to a geodatabase” available at the ArcGIS Resource Center for Geoprocessing. Connecting to ArcSDE generally entails setting up your connection properties, using those properties to open up a workspace within the geodatabase, then opening a feature dataset within that workspace. Listing 8 shows how to connect to Enterprise ArcSDE on SQL Server 2005 and Workgroup ArcSDE on SQL Server Express 2008. Note the similarities in the connection properties between Enterprise and Workgroup ArcSDE. Also note that opening up the database for either instance uses the same code—only different connection properties. If you are unsure of any of the connection properties you need, ask your database administrator. Working with ArcGIS Server The Esri.ArcGIS.Server assembly exposes hundreds of methods we can use to interact with ArcGIS Server. Using ArcObjects, we can connect to ArcGIS Server and get a list of running services. Listing 9 is a simple example showing how to connect to an ArcGIS Server instance and return a list containing each service along with its status (as illustrated in Listing 10). Keep in mind that the account this process runs on must have the required privileges to access your ArcGIS Server instance. A script written by Sam Berg of Esri, called AGSSOM, is an excellent example of how to use ArcObjects to more fully interact with ArcGIS Server. AGSSOM can be found on the Esri ArcScripts site (arcscripts.esri.com/details.asp?dbid=16293). Documentation, Help, and Additional Resources Esri provides a wealth of information to help you build .NET applications. The Esri Resource Center for Geoprocessing (resources.esri. com/geoprocessing) is your one-stop shop for searching for reference materials, forum postings, and sample code. The Developer Resources section will provide you with the ArcObjects API reference, the geoprocessing tool reference, and the Geoprocessing Object Model Diagram (OMD). Of course, the ArcGIS Engine Forums are always a good place to search for help and sample code. Lastly, the .NET Framework Developer Center on the Microsoft Developer Network (MSDN) (msdn.microsoft.com/en-us/library/aa139615.aspx) is a great place to find reference materials for anything related to general .NET development. Conclusion There is no denying that the .NET platform is complex and daunting when viewed by a beginning programmer. That said, I truly hope that the samples provided here have shown that creating simple geoprocessing programs with .NET is within your reach. No matter the data format used—from shapefiles to file geodatabases—and whether you are working with ArcSDE or ArcGIS Server, the Esri geoprocessor and .NET are a powerful and well-tested combination that can automate both simple and incredibly complex processes. For more information, contact Chad Cooper GIS Analyst Southwestern Energy Company chad_cooper@swn.com About the Author Chad Cooper, who has been working in GIS for seven years in technician and analyst positions, has worked with Southwestern Energy Company for the last five years. Cooper holds a bachelor’s degree from Mississippi State University and a master’s degree from the University of Arkansas, both in geology. He realized he could both have fun and earn a living in working with GIS. A huge fan of the Python programming language for several years, Cooper wrote his first .NET program two years ago. Cooper lives in Texas with his beautiful wife and two handsome, lively young boys. In his free time, he enjoys fishing with his boys, photography, and occasionally riding a bike.
40 ArcUser Spring 2010
www.esri.com