Wednesday, June 5, 2024

GIS Programming - Module 3 - Debugging and Error Handling

Module 3 for GIS Programming focused on the debugging and error handling of Python scripts. We were introduced to common error messages and how to implement debugging procedures. The lecture and exercise materials built upon this foundation providing us some instruction and practice on how to interpret script error messages while recognizing and correcting syntax errors and exceptions so that scripts run successfully.

The first script we were tasked with contained two syntax errors. Syntax errors are similar to making typographical errors or saying you are grammatically incorrect. Some common issues related to syntax errors are misspellings, lack of punctuation, wrong use of upper/lowercase letters, and indentation issues. This first script when successfully run prints out the names of all fields in the attribute table of the ArcGIS Pro Project we were working with.  Below is a screenshot of my script output after correcting the two syntax issues.


The second script contained several errors/exceptions. These included issues with the undefined filepaths, forward and back slash issues, misspelled variables, and other issues mostly related to misspellings. These types of named exceptions present with messages including names such as NameError, AttributeError, and TypeError. Ultimately I was able to get the script to run, however I was receiving an OSError. After doing a quick Google search for OSError in ArcGIS Pro I found an ESRI discussion post saying that this could likely be due to having the project open and running. I then closed the project and ran the script.  the script ran successfully without any error messages. When successfully run the script prints out the names of all the layers in the ArcGIS Pro Project. Below is a screenshot of the output after all the errors/exceptions are corrected.



The third script involved running a script even though it contained errors. This is done by modifying the script by adding a try-except statement. The challenge here was deciding where the try-except statements need to be placed in order to make the script run successfully despite containing errors. When used, a try-except statement can catch and handle exceptions, thus avoiding a runtime error and sudden termination of a script. The script contained two different parts where Part A returns an error message but continues to run Part B printing out the name, data source, and spatial reference of each layer. Below is a screenshot of the script output after successfully placing the try-except statements as well as a flowchart depicting the steps I took in the process.







After feeling overwhelmed and defeated by last week's module I was very concerned coming into this week. However, after spending some extra time trying to get a grasp on things this module went much better than I anticipated. Hopefully, I can continue to move forward and build upon what I have learned.










No comments:

Post a Comment

Applications in GIS - Module 6 - Suitability & Least Cost Analysis

In Module 6, we learned about Suitability and Least Cost Path Analysis. We were introduced to performing suitability analysis using both vec...