Exercise Use Python To Create Buffers Around Forest Roads
Exercise Use Python To Create Buffers Around Forest Roads
In this course, you will use Python to create treatment areas for invasive plant species within the San Juan
National Forest in the state of Colorado. Part of this process will be to define areas in which chemical and
non-chemical treatments may be used within the forest.
In this exercise, you will create treatment areas for invasive plant species by creating polygons around the
forest roads. These polygons will define the non-chemical treatment areas.
Note: PyCharm is available as a free download. If you do not have PyCharm installed, the first course
exercise provides instructions for downloading the appropriate version.
To complete the exercise, you must download the data. If you have already downloaded and
installed the data, continue to the next step.
If necessary, sign in to ArcGIS Pro using your ArcGIS Online organizational account.
Note: If you have configured ArcGIS Pro to start without a project template or with a default
project, you will not see the Start page. On the Project tab, click Open, and then click
Open Another Project.
Browse to C:\EsriTraining\PythonGP\Data.
1/10
24/10/22, 9:18 Exercise: Use Python to create buffers around forest roads Print Window More informationMore information
In this step, you will become familiar with the San Juan National Forest data that you will work with
in this course.
This folder contains all of the data that you will need for this course.
If necessary, in the map view, click the San Juan map tab to activate it.
Remind me how
From the Bookmarks menu, choose Invasive Plants.
Your map shows a portion of the San Juan National Forest and some of the surrounding
communities. You will notice the location of the invasive plants and their proximity to roads and
water features. You will use Python to create buffer zones around roads to determine the extent
where treatment is needed in the invasive plant areas.
In this step, you will examine the data that will be used by your script.
In the Contents pane, right-click the BufferDistance table and choose Open.
2/10
24/10/22, 9:18 Exercise: Use Python to create buffers around forest roads Print Window More informationMore information
This table is the completed table of buffer distances that will be used to create buffer polygons
around the road features.
Note: This data is for training purposes only, so if you are prompted with a warning about
compatibility, click Yes.
In this step, you will begin your Python script. For the exercises in this course, you will be using
PyCharm for your IDE.
More information
After the application opens, you will open a project folder and create a new script.
Note: If the PyCharm application is already open, you can open the folder from the File
menu.
If you receive a warning message, check the box Trust Projects in C:\EsriTraining\PythonGP,
and then click Trust Project.
Now you will verify that you have set the Python Interpreter for the project. If you do not have the
interpreter set, you will get a warning message and the script will not run.
In the Settings dialog box, expand Project: Scripts, and then click Python Interpreter.
In the dialog box, at the top right, click the configure gear , and then choose Add.
3/10
24/10/22, 9:18 Exercise: Use Python to create buffers around forest roads Print Window More informationMore information
Note: If you do not see an ArcGIS folder in your Program Files folder, ArcGIS may have been
installed in your Users folder instead. Browse to C:\Users\
<Your_Username>\AppData\Local\Programs\ArcGIS\Pro\bin\Python\envs\arcgispro-
py3\python.exe. (On some systems, the AppData folder is hidden; to reveal the folder, on
the toolbar, click the Show Or Hide Hidden Files And Folders button.
Now that you have set the correct Python interpreter, you can create your script.
In the New Python File dialog box, name the file BufferRoads.py, and then press Enter.
Now you will open the Python Console. The Python Console is the area that shows the results of
your script and provides a place for testing code snippets.
Before entering any Python code, you will import ArcPy in the Python Console in PyCharm. This
window appears below the BufferRoads.py script window. By importing ArcPy first, you will make
PyCharm aware of the ArcPy libraries.
In the Python Console, type import arcpy, and then press Enter.
In the BufferRoads.py script window, on the first line, type import arcpy, and then
press Enter.
1 import arcpy
4/10
24/10/22, 9:18 Exercise: Use Python to create buffers around forest roads Print Window More informationMore information
Because you are writing and running your script outside of ArcGIS, you must include this
statement to access ArcGIS Python functionality.
Documentation is important when writing scripts, especially if you want to share your scripts with
others who may not be familiar with your code. You will add comments to begin each major part
of your script.
In the script window, on the next line, type # Set geoprocessing environments,
and then press Enter.
1 import arcpy
Type arcpy.e.
Press the Tab key to add the env class to the statement.
1 import arcpy
3 arcpy.env
Tip: You can also double-click an item in the list to add it to the script.
1 import arcpy
3 arcpy.env.workspace = r"C:\EsriTraining\PythonGP\Data\SanJuan.gdb"
Note: Paths in Python are string values, so make sure to enclose the path in quotation marks.
Press Enter.
5/10
24/10/22, 9:18 Exercise: Use Python to create buffers around forest roads Print Window More informationMore information
1 import arcpy
2 # Set geoprocessing environments
3 arcpy.env.workspace = r"C:\EsriTraining\PythonGP\Data\SanJuan.gdb"
4 arcpy.env.overwriteOutput = True
The overwriteOutput parameter controls whether tools will automatically overwrite any existing
output when your script is run. When set to True, tools will execute and overwrite the output
dataset. When set to False, existing outputs will not be overwritten, and the tool will return an
error.
Tip: Because overwriteOutput is a Boolean data type, you could also type 1 for True or 0 for
False.
The next section of your script will join the BufferDistance table to the Roads feature class. Before
executing the join, you will create four variables that will store the parameters used by the
arcpy.Join function.
In the next line of the script, type # Set parameters used to join the
BufferDistance table to the Roads feature class.
This comment indicates that you are setting the parameters for the Join function.
In the Python Console, type import arcpy, and then press Enter.
? What are the four required parameters for the JoinField function?
In the script, create four variables with their values as shown in the following table.
inFeatures Roads
6/10
24/10/22, 9:18 Exercise: Use Python to create buffers around forest roads Print Window More informationMore information
inField ROUTE_TYPE
joinTable BufferDistance
joinField ROUTE_TYPE
1 import arcpy
4 arcpy.env.overwriteOutput = True
5 # Set parameters used to join the BufferDistance table to the Roads feature class
6 inFeatures = "Roads"
7 inField = "ROUTE_TYPE"
8 joinTable = "BufferDistance"
9 joinField = "ROUTE_TYPE"
Next, you will write the code to complete the join. First, you will add a comment to the script that
indicates that the script will create the join.
In the next line of the script, type # Join table to feature class, and then
press Enter.
Type arcpy.Join and add the JoinField_management function from the list.
Note: The name of the function is JoinField_management. The name of the toolbox in which
the function is stored will appear after the function name in your script.
1 import arcpy
3 arcpy.env.workspace = r"C:\EsriTraining\PythonGP\Data\SanJuan.gdb"
4 arcpy.env.overwriteOutput = True
5 # Set parameters used to join the BufferDistance table to the Roads feature class
6 inFeatures = "Roads"
7 inField = "ROUTE_TYPE"
8 joinTable = "BufferDistance"
9 joinField = "ROUTE_TYPE"
10 # Join table to feature class
Tip: Use the function usage in the Python Console to help you enter the variables in the
correct order.
7/10
24/10/22, 9:18 Exercise: Use Python to create buffers around forest roads Print Window More informationMore information
Now you are ready to buffer the roads using the joined DISTANCE attribute. Before you execute
the Buffer function, you will store the parameters as variables.
In the next line of script, type # Set parameters used to buffer Roads
feature class.
In the Python console, use the arcpy.Usage function to find the parameters for the Buffer
function.
Tip: You can also refer to Buffer (Analysis) on ArcGIS Pro Help.
Use the following table to add the two needed variables for the Buffer function.
outBuffers RoadBuffers
buffField DISTANCE
14 buffField = "DISTANCE"
In the next line of the script, type # Buffer the roads based on DISTANCE
attribute.
Write the code to buffer the Roads using the DISTANCE attribute.
13 outBuffers = "RoadBuffers"
14 buffField = "DISTANCE"
8/10
24/10/22, 9:18 Exercise: Use Python to create buffers around forest roads Print Window More informationMore information
Tip: To help you enter the variables in the correct order, use the arcpy.Usage function in the
Python Console or refer to Buffer on ArcGIS Pro Help.
You are now ready to run your script. PyCharm has autosave functionality, so it is not necessary to
save your script before running it.
Note: Make sure that ArcGIS Pro is closed before you run your script. If ArcGIS Pro is open,
the geodatabase will be locked and your Python script will not have write access.
Your script will take a few moments to run. When PyCharm is finished, you should see the
following message at the bottom of the PyCharm window.
More information
Now that your roads buffer is complete, you will look at the results. While there are ways to add
results to a map using a script, it is not done by default like it is when using Python window or
Python script tools within the ArcGIS Pro interface. You will work with these in later exercises. In
this exercise, you will confirm the results of your Python code by manually bringing the resulting
data into ArcGIS Pro.
Drag the RoadBuffers feature class into the San Juan map.
9/10
24/10/22, 9:18 Exercise: Use Python to create buffers around forest roads Print Window More informationMore information
Note: Your colors may be different from the colors in the graphic.
You will notice that the widths around the roads are varied. This variance is based on the
ROUTE_TYPE attribute.
If you are continuing to the next exercise, leave PyCharm open; otherwise, exit PyCharm.
10/10