0% found this document useful (0 votes)
8 views6 pages

Exercise - Solve Scripting Errors

The document outlines an exercise for a GIS analyst in San Diego to resolve scripting errors in a Python script designed to create development buffers along streams. The exercise involves identifying and fixing syntax errors, handling exceptions, and ensuring the script runs successfully to create 100-foot, 500-foot, and 1000-foot buffers. After debugging, the script successfully generates the required buffers, which can be used for other distances or locations.

Uploaded by

shakeel_ncp9818
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Exercise - Solve Scripting Errors

The document outlines an exercise for a GIS analyst in San Diego to resolve scripting errors in a Python script designed to create development buffers along streams. The exercise involves identifying and fixing syntax errors, handling exceptions, and ensuring the script runs successfully to create 100-foot, 500-foot, and 1000-foot buffers. After debugging, the script successfully generates the required buffers, which can be used for other distances or locations.

Uploaded by

shakeel_ncp9818
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

6/16/2020 Exercise: Solve scripting errors Print Window Other methods for handling exceptions

Exercise: Solve scripting errors

San Diego's Natural Resources Department needs to create 100-foot, 500-foot, and 1000-foot
development buffers along streams in San Diego. As the GIS analyst, you have been provided a script to
automate this task.

When you run the script, you will notice several errors. Using what you have learned about handling
errors in Python, you will solve these scripting errors so that you can run the script successfully.

Estimated completion time: 15 minutes

To complete exercises, you need the following:


ArcGIS Desktop 10.7 (Basic, Standard, or Advanced)
PythonWin 2.7

ArcMap users: PythonWin 2.7 is available as a free download. If you do not have PythonWin installed, the
first course exercise provides instructions for downloading the appropriate version.

- Step 1: Download the data

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

- Step 2: Open the script

Before running the buffering script, you will review it in PythonWin in case you need to make any
edits.

Open PythonWin.

Browse to C:\EsriTraining\PythEveryone\HandleErrors and open [Link].

Review the pseudocode to understand the script workflow.

The script creates a list of three buffering distances and loops (or iterates) through the list to use
the distance as the parameter for the Buffer tool. Each output feature class has a unique name of
SD_Stream and the buffer distance.

Now that you have reviewed the code, you will run it.

Run the script.

You will notice that the script does not run. There is a Failed To Run Script - Syntax Error message
at the bottom of PythonWin, and your cursor moves to the end of line 2.

1/6
6/16/2020 Exercise: Solve scripting errors Print Window Other methods for handling exceptions

PythonWin's error message informs you that there is a syntax error. Without this error
? message, how would you know that Python encountered a syntax error and not an
exception?

- Step 3: Handle the syntax errors

Your next step is to address the syntax errors to ensure that the script will run.

When you click the Run button or the Check button in PythonWin, the program looks for
syntax errors. If a syntax error is detected, the cursor moves to the approximate line of code that
contains the error.

Review line 2 of the script.

? What is missing from this line of code?

On line 2, move your cursor to the end of the path.

Add a quotation mark (") at the end of the line.

Click the Check button to scan the script for additional syntax errors.

A Failed To Check message appears at the bottom of PythonWin, and the cursor moves to line 7 in
the script.

Review line 7 of the script.

? What is missing from this line of code?

On line 7, after buffDists, add a colon (:).

Click the Check button .

A message appears at the bottom of PythonWin, indicating that the check was successful and
there are no other syntax errors.

Run the script.

The script begins to run, and then it fails. An error message appears in the Interactive window.

? Python has encountered an exception. How do you know that Python encountered an
exception and not a syntax error?

2/6
6/16/2020 Exercise: Solve scripting errors Print Window Other methods for handling exceptions

- Step 4: Handle the exceptions using pseudocode

You need to address the script exceptions to ensure that the script successfully executes.

Exceptions often provide helpful information (the code line where the script failed, explanation of
the error, and so on) to help you resolve the error.

Review the error and the script pseudocode and corresponding code.

? What step is missing from the script?

Move your cursor to the beginning of the first line of script.

Add code to import the ArcPy site package.

import arcpy

Press Enter.

Step 4a: Handle the exceptions using pseudocode.

Run the script.

An error message appears in the Interactive window.

- Step 5: Handle the exceptions using print

Because the error occurs within the for-in statement, you will add a print statement to try to
determine which item in the buffDists list is causing the error.

Move your cursor to the end of the for statement and press Enter.

3/6
6/16/2020 Exercise: Solve scripting errors Print Window Other methods for handling exceptions

Add a line of code that will print the buff value with a string statement that says "is
processing."

print (str(buff) + " is processing")

Step 5a: Handle the exceptions using print.

Move your cursor to the end of the buffer_analysis method and press Enter.

The following line is now indented.

Clear the Interactive window.

Run the script.

The Interactive window shows the print statement for the first list item (100) before the error
message. This statement indicates that the error occurs when processing the first list item, 100, as
the buffer distance parameter.

Review the full error message.

? What additional information might explain why the Buffer function errors with 100 as the
buffer distance?

Start ArcMap.

Browse to ..\EsriTraining\PythEveryone\HandleErrors and open [Link].

In the Catalog window, browse to ..\EsriTraining\PythEveryone\HandleErrors.

Note: If you do not see the Catalog tab, you can click the Catalog button on the Standard
toolbar.

Expand [Link].

Step 5b: Handle the


exceptions using
print.

4/6
6/16/2020 Exercise: Solve scripting errors Print Window Other methods for handling exceptions

SD_Stream100 and SD_Stream500 feature classes already exist. Because the script cannot
overwrite existing feature classes, it is failing.

In the Catalog window, delete SD_Stream100 and SD_Stream500.

Other methods for handling exceptions

Minimize ArcMap, and return to PythonWin.

Clear the Interactive window, and then run the script.

Step 5c: Handle the exceptions using print.

Each print statement appears in the Interactive window, and an exit code 0 message appears at
the bottom of PythonWin, which indicates that the script has successfully executed.

- Step 6: Check the results

Your last step is to check that the script results are what you expected.

Restore ArcMap.

In the Catalog window, refresh the HandleErrors folder.

Expand [Link].

Step 6a: Check the


results.

The SD_Stream100, SD_Stream500, and SD_Stream1000 feature classes have been added to the
San Diego geodatabase. You will add them to the map to ensure that they were created correctly.

Add SD_Stream100, SD_Stream500, and SD_Stream1000 to the map document.

If necessary, reorder the map layers so that the new feature classes are listed from smallest
to largest buffer size.

5/6
6/16/2020 Exercise: Solve scripting errors Print Window Other methods for handling exceptions

Step 6b: Check the results.

Zoom to the Check Results bookmark.

Remind me how
From the Bookmarks menu, choose Check results.

Step 6c: Check the results.


Appearance of colors and basemap may vary.

The script has successfully created 100-foot, 500-foot, and 1000-foot buffers around the streams
feature classes for the Natural Resource department. The script has been debugged and is ready
to use with other buffering distances or locations.

Exit ArcMap without saving your changes.

Exit PythonWin.

6/6

You might also like