Geographic processing-Chapter 4: Geographic processing script
Section 1 ArcGIS script In geographical processing, scripts can be used to complete automatic tasks, such as data conversion or Geodatabase production. These scripts can be run in the script application or added to the toolbox. You can use any COM scripting language, such as Python, Perl, jscloud, or VBScript. Any model created in modelbuilder can be converted into a script file. Why script? The script is similar to the model. You can link the processing process and run these processes in a certain order. Like the model, scripts can be used to build functions that are not available in ArcGIS Desktop System Tools. For example, you can create custom batch processing. What script software does ArcGIS desktop support? Each script software program has its own language, some of which can be used to write scripts running on the ArcGIS Desktop processing tool. These script languages include python, Perl, VBScript, and JScript. In this section, we focus on the Python language, because it is installed along with the ArcGIS desktop software installation. In addition, the Python language has many advantages, it supports running in different operating system environments. The following table describes the representation of the same two lines of code in different scripting languages. The two codes are used to check an element class named "roads_clip" and delete it if it is found. From the table, we can see that what is different is the syntax, but the process is the same. Script Language example code Python if GP. exists ("roads_clip "): GP. Delete ("roads_clip ") VBScript if GP. exists ("roads_clip") then GP. Delete ("roads_clip ") JScript if (GP. exists ("roads_clip ")) GP. Delete ("roads_clip "); |
You can also use scripts to process data based on certain conditions. For example, you can use a script to check whether the input data is a line type or a polygon element class, and then process the data. In the following example, you can also see how to cyclically work in the script.
You can view the script examples of any geographic processing tool contained in ArcGIS desktop environment. In arctoolbox, right-click the system tool and select help. In the ArcGIS Desktop Help system, expand the script syntax section to see the script syntax of the tool.
For example, the "add XY coordinates" tool provides a syntax description for the script, explains the meaning of each parameter, and how to use this tool in the script, 4-1.
Script profiling
Even if you have never written a line of code, you may also need to use scripts written by others. But remember, you need to have available licenses and extensions. You can also execute any system tool in the script. What does it mean? That is to say, if you want to use some functions, such as ArcInfo, ArcView, and extended functions, such as tools in 3D analyst. These can be seen from the three product-level table of ArcGIS desktop Geographic processing in Chapter 2. If you want to use some tools, you must have a corresponding level of ArcGIS license and an extended license.
Let's get down to the point. Next we will use an example to explain the Python script language. There are several parts of Python's geographic processing syntax, including:
(1) Comment
In python, any line of code can be annotated using the # flag, which is similar to other programming languages. The following code:
(# Outputs (#-----------------------------------------------------------------------------------
# Clipall. py
# ESRI Python script example
# Created on: 25/3/2007
# Description: clips all of the feature classes in a workspace
# And puts the clipped feature classes in a specified workspace
)
(2) Standard Code
A geographic processing function that starts with Python code is required and requires at least two lines of code:
# Import system modules
Import win32com. Client
And
# Create the geoprocessor object
Gp = win32com. Client. Dispatch ("esrigeoprocessing. gpdispatch.1 ")
(There are some changes in arcgis9.2. You can use the following code to replace these two lines:
# Import system modules
Import arcgisscripting
# Create the geoprocessor object
Gp = arcgisscripting. Create ()
)
Example of the geographic processing code in the script
(1) script Standard Code
Figure 4-2 shows the standard code for the python geographic processing script including the annotation:
Other specific modules to be imported are sys and OS. Syntax:
Import win32com. Client, sys, OS
(2) script arguments
It is also called system arguments. This Code allows users to interact with scripts and can specify values for variables, such as output workspace.
Figure 4-3 below shows the code and comments of the three script arguments:
These script arguments must follow certain requirements to ensure that the script runs successfully. As shown in the figure, this is a tool for running the cropping function, sys. argv [1] indicates the workspace or working path of the input data, sys. argv [2] indicates the elements used for cropping, sys. argv [3] indicates the output space. These three parameters are consistent with the parameters used in the clip tool dialog window in arctoolbox.
(3) Environment Settings
That is, the settings required for running the script are applied to the entire processing process, such as the default workspace. The following is an environment-setting syntax and comment:
(# Set the current Workspace
GP. workspace = inputws
)
You can use environment settings in the script. In this case, you can ignore the environment settings in arctoolbox.
(4) Tools
In the script, you only need to specify the Tool Name and toolkit alias and tool parameters, then any system tool can be run in the script code program.
4-4 types of code (with comments) use the "cropping" tool to cycle a series of element classes. Can you find the cropping tool on the graph?
We noticed that the cropping tool includes GP. prefix and the script syntax used by this tool, such as 4-5:
In the above syntax, we can see that for any tool, you must first specify the Toolbox where the tool is located, that is, GP. the Section shown in the prefix, such as the clip tool, is in the analysis toolbox. In arctoolbox, each tool has an alias of 4-6.
Section 2 use the script Tool
Section 2 how to use scripts in the ArcGIS Environment
You can run the script in an independent script environment, add the script to the toolbox, run the script in the dialog box, or add the script to the model to run the script.
If you run the script as an independent program, you do not need to open any ArcGIS Desktop Application to complete the geographic processing operation. For example, you can run a script in the script editor, such as pythonwin, or in the Command window.
When you run the geographic processing script as an independent program, consider the following:
1. ArcGIS Desktop license
2. Permission to use geographic processing in scripts
3. python is installed (if you are using a Python script)
4. Access the data and workspace references set in the script.
If you need to run a script in the ArcGIS desktop application and use the script as a system tool or model, you must add the script to the toolbox and run it as a script tool.
Add a script to arctoolbox. You can add the script by right-clicking Add à script in an existing toolbox or a new toolbox. We use an instance as a reference to crop and output all elements in the entire folder through some elements. Code 4-3 and 4-4 are shown. In arctoolbox, set parameters according to the wizard, including the name displayed in the tool dialog box and the input data type, as shown in Figure 4-7. Figure 4-8 shows the generated Tool dialog box. If we need to use a help system, we can use document editor as mentioned in the model builder chapter to implement a help system that is the same as the system tool. This facilitates tool sharing. These added script tools can also be used as system tools. You can run the dialog box directly or use them in model builder, the configuration can be published as a geographical processing service through ArcGIS Server and shared over the Internet.