You are on page 1of 11

Exercise: Use Python in ArcGIS

The city of Providence, Rhode Island is increasing their infrastructure to improve local economy.
They have received a map of industrial buildings and highways for the county of Providence. They
have received schools and sewer line data for the state of Rhode Island. As the GIS manager, you
have been asked to finish the map by completing the following tasks:
Clip the schools and sewer line data using the Providence county border.
Change the map labels for the highways layer.

You will use Python's ArcGIS functionality to complete these tasks.

Estimated completion time: 15 minutes

Step 1: Download the data

To complete the exercise, you need to download the data. If you have already downloaded and
installed the data, continue to the next step.

Step 2: Open an ArcMap document

First, you will open the map document provided by the city of Providence.

Open ArcMap.

In the Catalog window, navigate to your C:\Student\PythEveryone10_1\PythonInArcGIS


folder.

Double-click Providence.mxd to open it.

Close the Catalog window—you will not need it for this exercise.

Step 2a: Open an ArcMap document.


You will use Python to clip and re-label the layers of this map document.

Step 3: Clip a layer

Now that the map is set up, your next step is to use the Clip tool to clip the schools feature
class using the Providence boundary.

In the Standard toolbar, click the ArcToolbox button .

In the ArcToolbox window, expand Analysis Tools > Extract.

Double-click Clip.

In the Clip window, for Input Features, choose RI_Schools.

For Clip Features, choose Providence.

For Output Feature Class, click the Browse button .

Navigate to your C:\Student\PythEveryone10_1\PythonInArcGIS\RhodeIsland.gdb.

For Name, type Schools.


Step 3a: C lip a layer.

Click Save.

Step 3b: C lip a layer.

Click OK.

Close the Clip tool when it's complete.

Step 3c: C lip a layer.


A new layer named Schools is added to the map document.

Instead of repeating these same steps to clip the sewer data, you will use Python to repeat this
task.

Step 4: Copy Python snippet

There are several ways to use Python to re-run the Clip tool. In this step, you will use a Python
snippet.

In ArcMap, from the Geoprocessing menu, choose Results.

In the Results window, expand Current Session.

Right-click Clip, and then choose Copy As Python Snippet (as shown).

Close the Results window.

Close the ArcToolbox window.

If necessary, open the Python window in ArcMap.

In the Python window, right-click and choose Paste.


Step 4a: C opy Python snippet.

The Python Snippet translates the Clip tool into Python code—in particular, a function. The
syntax begins with the site package (arcpy), then is followed by the function (Clip_analysis). The
function parameters are the same parameters as the tool (Input Features, Clip Features, Output
Feature Class, and XY Tolerance).

Note: When you want to skip an optional parameter or keep its default setting, you
can exclude it from the Python syntax, or add an empty set of quotation marks (" ")
or a pound sign ("#"). In this case, the Python snippet uses a pound sign to
indicate that the XY Tolerance is empty.

Step 5: Edit Python snippet

The Python Snippet needs to be edited to execute Clip on the sewer feature class.

Which function parameter(s) need to be edited?

Input Features

Clip Features

Output Feature Class

XY Tolerance

In the Python window, replace "RI_Schools"with "RI_Sewers".

Note: Because you are still working with feature layers in your map, you do not
need to define the full path of RI_Sewers. If you were working outside ArcMap or
with a feature class that was not a feature layer, you would need to define the full
path for this parameter
"C:/Student/PythEveryone10_1/PythonInArcGIS/RhodeIsland.gdb/RI_Sewers".
Change the feature class name of the Output Feature Class parameter to Sewers.

Step 5a: Edit Python snippet.

You do not need to change the full path of the Output Feature Class because you want to save
the Output Feature Class to the same geodatabase as your other data. This is why you've only
changed the feature class name.

Click at the end of the line of code, outside the closing parenthesis.

Step 5b: Edit Python snippet.

Press Enter twice.


Step 5c: Edit Python snippet.

The Clip_analysis function executes and the new Sewers layer is added to the map document.

Close the Python window.

Step 6: Update labels

The next task is to update the highway labels. First, you will reformat the map document to
better see the labels.

Remove the RI_Schools and RI_Sewers layers.

Move the Schools layer below the Ind buildings layer.

Move the Sewers layer below the Highways layer.

Step 6a: Update labels.


From the Bookmarks menu, choose Providence.

Step 6b: Update labels.

The highway labels currently show the highway number. You will use Python to manipulate the
label expression to display the word Highway before the highway number (e.g., Highway 101).

In the Table of Contents, right-click Highways and choose Properties.

In the Layer Properties dialog box, click the Labels tab.

In the Text String section, click Expression.

The label expression allows you to alter how a label is displayed, without having to change the
field values in the attribute table. Currently, the Highways label expression is [HWY_SYMBOL],
which represents each of the highway numbers.

Using Python, how would you concatenate "Highway " with the [HWY_SYMBOL]
field?

In the Label Expression dialog box:


For Parser, choose Python.
Concatenate the text "Highway "with [HWY_SYMBOL].
Step 6c: Update labels.

Click Verify.

In the Expression Verification dialog box, confirm that the Sample Label reads Highway
100.

Click OK to close the Expression Verification and Label Expression dialog boxes.

Click OK to close the Layer Properties dialog box, which will update the label expression.

Step 6d: Update labels.


The highway labels now include the word Highway.

Step 7: Set up map layout

Now that the labels have been updated, your final step is to set up the map layout.

First, you need to add a map layer that will serve as a geographic reference for those viewing the
map.

On the Standard toolbar, click the arrow next to the Add Data button and choose Add
Basemap.

Step 7a: Set up map layout.

The Basemap Gallery displays basemaps that are available on ArcGIS Online.

Select the Light Gray Canvas basemap, and then click Add.

Step 7b: Set up map layout.

Your map document displays the new basemap layer below the other map layers.

Below the map, click the Layout View button .


Step 7c: Set up map layout.

The map document is complete and ready for the city of Providence to use for their infrastructure
planning project.

From the File menu, choose Save As.

Save your completed map document in your


C:\Student\PythEveryone10_1\PythonInArcGIS folder as MyProvidence.mxd.

Close ArcMap.

You might also like