- Programming ArcGIS 10.1 with Python Cookbook
- Eric Pimpler
- 661字
- 2021-07-30 17:29:54
Executing tools from a script
As an ArcGIS user, you have almost certainly used the many available tools in ArcToolbox to accomplish your geoprocessing tasks. Some examples include Clip, Buffer, Feature Class to Feature Class, Add Field, and many more. Your scripts can execute any of the tools found in ArcToolbox. Remember that the tools available to you as a programmer are dependent upon the license level of ArcGIS Destkop that you are using. These tasks can be automated through the creation of a Python script that executes these tools programmatically.
How to do it…
- Follow these steps to learn how to execute a geoprocessing tool from your script. Open c
:\ArcpyBook\Ch2\TravisCounty.mxd
with ArcMap. - Open the Python window.
- Import the
arcpy
package:import arcpy
- Set the workspace. We haven't discussed the
env
class yet. Environment settings for ArcGIS are exposed as properties of thisenv
class, which is a part ofarcpy
. One of the properties of theenv
class is workspace, which defines the current working directory for data input and output. In this case, it will be the directory where we'll write the output dataset from our tool:arcpy.env.workspace = "c:/ArcpyBook/data/"
- We're going to use the Buffer tool from the Analysis Tools toolbox to buffer the Streams layer seen in the active data frame in ArcMap. Open ArcToolbox and find this tool, as shown in the following screenshot:
- Double-click on the Buffer tool to display the interface shown in the following screenshot. Most tools have one or more input parameters that must be supplied for the tool to execute. Whether you're running the tool from the user interface or from a Python script, you must always supply these required parameters:
- Close the Buffer tool.
- Execute the Buffer tool in the Python window. Use the code-completion feature of the Python window to help you as well as the tool help displayed in the window on the right.
This will buffer the Streams layer by 50 meters to create a new
Streams_Buff
polygon layer:arcpy.Buffer_analysis("Streams", "Streams_Buff", "50 Meters")
- Use your ArcMap zoom and pan tools to get a better look at the output dataset, as shown in the following screenshot:
How it works…
All geoprocessing tools available to your script are defined as dynamic functions from the main arcpy
object. Each tool that you execute from a script must follow a specific syntax that first defines the tool name, followed by an underscore, and then the alias for the toolbox name. In our example, the Buffer tool is located in the Analysis Tools toolbox, which has an alias of analysis
. This is done because it is possible for more than one tool to have the same name. A unique reference for each tool is generated using the syntax <toolname>_<toolbox_alias>
.
Getting the toolbox alias is easy in ArcGIS Desktop. Find the toolbox associated with the tool and right-click on the toolbox name. Select Properties. In the Properties dialog box that is displayed, find the Alias textbox. You will see the following alias when referring to a particular tool in your geoprocessing scripts:

In addition to the dynamic functions that represent geoprocessing tools, there are many additional functions available on the arcpy
class that are not geoprocessing tools. Functions for creating cursors, listing datasets, describing datasets, working with environment settings, messaging, and many others are provided. We'll cover many of these functions as we move through the book.
There's more…
Geoprocessing workflows often require multiple steps that require the use of one or more geoprocessing tools. You can more efficiently and effectively develop scripts by first creating an outline for your script. This outline will help you consider the task at hand and identify the geoprocessing tools that will be used. Outlines don't have to be complex undertakings. You can simply draw a diagram of the workflow and then implement your code based on this workflow. The point is to do some planning ahead of time before you actually start coding.
- Boost.Asio C++ Network Programming(Second Edition)
- Mastering JavaScript Functional Programming
- JavaScript+jQuery網(wǎng)頁特效設計任務驅(qū)動教程(第2版)
- 動手玩轉(zhuǎn)Scratch3.0編程:人工智能科創(chuàng)教育指南
- RTC程序設計:實時音視頻權威指南
- Functional Kotlin
- 小程序開發(fā)原理與實戰(zhàn)
- Modern JavaScript Applications
- Unity UI Cookbook
- 深入分布式緩存:從原理到實踐
- C語言程序設計
- OpenCV 4計算機視覺項目實戰(zhàn)(原書第2版)
- OpenResty完全開發(fā)指南:構建百萬級別并發(fā)的Web應用
- Extending Unity with Editor Scripting
- OpenCV 3計算機視覺:Python語言實現(xiàn)(原書第2版)