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.
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.
C:ArcpyBookCh5Crime_Ch5.mxd
in ArcMap.EdgewoodSD.shp
file to the table of contents from the c:ArcpyBookCh5
folder.arcpy
module:import arcpy
in_features = "c:/ArcpyBook/data/CityOfSanAntonio.gdb/Burglary"
clip_features = "c:/ArcpyBook/Ch5/EdgewoodSD.shp"
out_feature_class = "c:/ArcpyBook/Ch5/ClpBurglary.shp"
arcpy.Clip_analysis(in_features,clip_features, out_feature_class)
c:ArcpyBookcodeCh5ExecuteGeoprocessingTools.py
solution file.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:
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.
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.