Executing geoprocessing tools from a script

Once you have determined the toolbox alias and then verified the accessibility of the tool based on your current license level, you are ready to add the execution of the tool to a script.

Getting ready

Now that you understand how to find the tools that are available and how to uniquely reference them, the next step is to put this together and execute a tool from a geoprocessing script. In this recipe, you can then execute the tool from your script.

How to do it…

  1. Open C:ArcpyBookCh5Crime_Ch5.mxd in ArcMap.
  2. Click on the Add Data button and add the EdgewoodSD.shp file to the table of contents from the c:ArcpyBookCh5 folder.
  3. If needed, turn off the Crime Density by School District and Burglaries in 2009 layers to get a better view of the EdgewoodSD layer. There is only one polygon feature in this file. It represents the Edgewood School District. Now, we're going to write a script that clips the Burglaries in 2009 features to this school district.
  4. Open the Python window in ArcMap.
  5. Import the arcpy module:
    import arcpy
  6. Create a variable that references the input feature class to be clipped:
    in_features = "c:/ArcpyBook/data/CityOfSanAntonio.gdb/Burglary"
  7. Create a variable that references the layer to be used for the clip:
    clip_features = "c:/ArcpyBook/Ch5/EdgewoodSD.shp"
  8. Create a variable that references the output feature class:
    out_feature_class = "c:/ArcpyBook/Ch5/ClpBurglary.shp"
  9. Execute the Clip tool from the Analysis Tools toolbox:
    arcpy.Clip_analysis(in_features,clip_features, out_feature_class)
  10. You can check your work by examining the c:ArcpyBookcodeCh5ExecuteGeoprocessingTools.py solution file.
  11. Run the script. The output feature class containing only those burglary points within the EdgewoodSD school district should be added to the data frame, as shown in the following screenshot:
    How to do it…

How it works…

The primary line of code of interest in this recipe is the final line that executes the Clip tool. Notice that we called this tool by specifying a syntax of Clip_analysis, which gives us a reference to the Clip tool in the Analysis Tools toolbox, which has an alias of analysis. We've also passed three parameters that reference the input feature class, clip feature class, and output feature class. I should point out that we hardcoded the paths to each of the datasets. This is not a good programming practice, but in this particular instance, I just wanted to illustrate how to execute a tool. A future chapter will show how you can remove the hardcoding in your scripts and make them much more versatile.

Most tools that you use will require paths to data sources. This path must be the same as the path reported on the ArcCatalog Location: toolbar, as shown in the following screenshot:

How it works…

Tools use ArcCatalog to find geographic data using an ArcCatalog path. This path is a string and is unique to each dataset. The path can include folder locations, database connections, or a URL. So, it is important to check the path using ArcCatalog before attempting to write Python scripts against the data. ArcSDE paths require special consideration. Many ArcSDE users do not have standardized connection names, which can cause issues when running models or scripts.

There's more...

Geoprocessing tools are organized in two ways. You can access tools as functions on arcpy or as modules matching the toolbox alias name. In the first case, when tools are accessible as functions from arcpy, they are called in the format that you followed in this recipe. The tool name is followed by an underscore and then the toolbox alias. In the second form, tools are called as functions of a module, which takes the name of the toolbox alias. Here, analysis is the toolbox alias, so it becomes a module. Clip is a function of this module and is called as follows:

arcpy.analysis.Clip(in_features,clip_features,out_feature_class)

Which method you use is really a matter of preference. They both accomplish the same thing, which is the execution of a geoprocessing tool.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset