Arcpy
Arcpy is a module, which contains the geoprocessing commands that can be used in a Python script. The arcpy module is included with the installation of Esri ArcMap Desktop and thus not a free library of commands. When using the Python window inside of ArcMap the module is automatically loaded, this will be discussed in a future module. When working with the IDE outside of ArcMap the module must be loaded to complete the geoprocessing commands. Code created in an IDE can also be used inside of ArcMap without the need for modifications.
The arcpy module should be loaded as one of the first lines in any script. Loading a module is accomplished by using the import statement with the module. Hint: if the user loads the arcpy module in immediate mode prior to beginning a script, it will provide suggestions on geoprocessing commands while writing the script. Note: all the letters in arcpy are lower case, but in some writings, capitalization is used.
The arcpy module should be loaded as one of the first lines in any script. Loading a module is accomplished by using the import statement with the module. Hint: if the user loads the arcpy module in immediate mode prior to beginning a script, it will provide suggestions on geoprocessing commands while writing the script. Note: all the letters in arcpy are lower case, but in some writings, capitalization is used.
Format
To use tools from the arcpy module, an understanding the structure of the Esri ArcToolbox is useful. In general, the formatting of a tool is as follows:
arcpy.tool_location(properties)
The final three parameters are optional and thus do not have to be included, if one of these commands is used then commas are required so that the proper command is applied. The Union tool is located in the analysis toolbox.
arcpy.tool_location(properties)
- tool represents the name of the tool
- location is the toolbox which contains the tool
- properties are the inputted parameters for the functionality of the tool.
- For example:
The final three parameters are optional and thus do not have to be included, if one of these commands is used then commas are required so that the proper command is applied. The Union tool is located in the analysis toolbox.
The data symbol to the left will appear at any location in which data is required to complete the technical skill lesson or assignment. Data used in the technical skill lessons are provided so the learner can follow the processes being demonstrated and can be downloaded. All data is provided in a single compressed file folder for the entire course. For technical skill assignments, generally, the learner will need to locate data from external sources, in some cases it can be the same data set used in the technical skill lessons.
|
Buffer
Create the code that will produce a 25-mile buffer around college campuses in Kentucky.
The code in Figure 1 will be discussed line by line, the comments will be omitted and only the functional part of the script will be examined.
The code in Figure 1 will be discussed line by line, the comments will be omitted and only the functional part of the script will be examined.
- To load the arcpy module, use the import arcpy command; this should always be one of the first functional lines of code.
- A variable called featureInput has a pathway defined; this is the location of the data. Before the pathway is defined note that the letter ‘r’ is placed in the script. The ‘r’ is used to inform the software that standard pathway functions are being used, note it is placed outside of the quotation marks so it is not part of the string. The pathway shown in Figure 1 is to the author’s data location and will be different on the learner’s computer. The data is coming from a geodatabase.
- A variable called featureOutput is used to define the saving pathway, to the same geodatabase.
- A variable called radius is defined to be 25 miles. This is a hard coded distance and would require a modification of the code to change the buffer radius.
- The buffer command is located in the analysis toolbox.
- The three variables are used as the input to the buffer command. The variables could have been directly entered in the command line instead. The same script can be used for other operations just by making minor edits. If the pathways and radius had been directly entered, quotation marks would be required similarly as used to define the variables.
- Four additional parameters were not used in the Buffer command.
- line_side expects FULL, LEFT, RIGHT, OUTSIDE_ONLY (default value)
- line_end_type expects ROUND (default value) FLAT
- dissolve_option expects NONE (default value), ALL, LIST
- dissolve_field – lists of fields from the input to dissolve the output
- since each of the parameters were after the first three parameters and they were all ignored they can be omitted in the coding, if one of them was used such as dissolve_option, then leading sets of commas would be required.
Environment (env)
The env command is part of arcpy, the purpose of the environment statement is to set the environment and thus eliminate the need for individual pathways, used in the buffer example. Had the buffer example used the env command, the complete pathway to the input and output would not have needed to be fully specified.
The environment command, many times, is the second line of functional script after importing the arcpy module. The format can be seen in Figure 2. It is part of the arcpy module so the from statement is used in conjunction with the import statement. The command that is then used from the arcpy env module is workspace. The pathway defined in Figure 2 is to a geodatabase that contains multiple files that will be used. Note: the ‘r’ serves the same function as noted earlier. In the clip command, the function of the env statement will be explored.
The environment command, many times, is the second line of functional script after importing the arcpy module. The format can be seen in Figure 2. It is part of the arcpy module so the from statement is used in conjunction with the import statement. The command that is then used from the arcpy env module is workspace. The pathway defined in Figure 2 is to a geodatabase that contains multiple files that will be used. Note: the ‘r’ serves the same function as noted earlier. In the clip command, the function of the env statement will be explored.