官术网_书友最值得收藏!

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…

  1. Follow these steps to learn how to execute a geoprocessing tool from your script. Open c:\ArcpyBook\Ch2\TravisCounty.mxd with ArcMap.
  2. Open the Python window.
  3. Import the arcpy package:
    import arcpy
  4. Set the workspace. We haven't discussed the env class yet. Environment settings for ArcGIS are exposed as properties of this env class, which is a part of arcpy. One of the properties of the env 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/"
  5. 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:
    How to do it…
  6. 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:
    How to do it…
  7. Close the Buffer tool.
  8. 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.
    How to do it…

    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")
  9. Use your ArcMap zoom and pan tools to get a better look at the output dataset, as shown in the following screenshot:
    How to do it…

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:

How it works…

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.

主站蜘蛛池模板: 息烽县| 望城县| 通河县| 五指山市| 磐石市| 崇文区| 屯昌县| 大同县| 梓潼县| 塘沽区| 砀山县| 景泰县| 万载县| 涿鹿县| 县级市| 昔阳县| 四川省| 汉阴县| 太湖县| 贵定县| 青龙| 额济纳旗| 德州市| 临邑县| 德州市| 剑川县| 石河子市| 阿荣旗| 多伦县| 沁水县| 沧源| 晴隆县| 隆安县| 石台县| 淄博市| 泰来县| 云林县| 吉木萨尔县| 霸州市| 安陆市| 韶关市|