Monday, June 3, 2024

GIS 5103 Module 3 - Python Debugging

 



Python scripting is a language of its own, and not using the proper spelling, spacing, and punctuation can make it impossible for the computer to execute the code, exactly like the same mistakes in any written language can render interpretation impossible to a human reader. The goal of Module 3 of GIS Programming was to debug errors in codes that were provided to us, and ultimately enable the computer to successfully interpret the scripts. The assignment was broken down into three parts, with a separate script for each, and the results of each can be referenced in the top image shown above. 

The first was a simple script that was written to type out the fields of an attribute table of a specific feature class. All errors were syntax errors, which include spelling, punctuation, and indentation mistakes. Working through errors one at a time is definitely the most efficient route to debugging any script; I learned this the hard way. When I had initially opened the file, I zealously jumped into the debugging process, changing this error and that error without running the code. In the midst of this haphazard methodology, I managed to change something in the code that did not need to be changed, and I was unable to successfully debug the code. After recopying the original script, I ran the code, fixed the given error, ran the code, fixed the new error, and repeated this process until the script ran. This took approximately three minutes compared to the ten minutes that were wasted by my initial technique. As shown in the top picture, the output was a list of all the fields included in this particular feature class's attribute table.

The second part of this assignment was very similar to the first part; I used the same method of running the code, fixing the error, and repeating until the script ran. This process was also very quick, and probably took less that ten minutes total. As shown in the top picture, the output was a message stating the Projected Coordinate System of the ArcGIS project and a list of all layers that were contained in the project.

Finally, we were tasked with handling a totally different type of error: a run-time error [also called an exception]. While a syntax error will prevent the code from running at all, an exception will allow the code to run, but produces an error that prevents the code from running successfully. In this scenario, we were provided a script that was broken down into two parts; the first section of code contained an error that prevented the second part from running. Instead of correcting the error in the script [a missing parameter that is required by the mapping function], we were required to get the code to run, producing an error message for the first part of the script and successfully running the second part; to accomplish this, we learned how to employ try / except statements. As shown in the bottom image, the flowchart can be followed to see how the script functions. First, the script begins, and the arcpy module is imported [this allows Python to interact with ArcGIS]. Next, a try statement is added to see if the portion of the code declaring variables contains an error. If it does throw an exception, an error message is produced, and the program continues on to the second half of the script. If no exception were to occur, the script would cycle through the entire lyrlist of the project, searching for a feature class named "parks". If a feature class is named "parks", the layer name is printed, the layer's visibility is turned on, and the layer's labeling is also made visible. Finally, the project is saved, the variables declared in the script are deleted, and the second half of the script is executed. If no feature class is named "parks", the script jumps to the lines commanding the project to be saved and the variables to be deleted, and then the interpreter proceeds to the second half of the code. The results of this script can be viewed in the top picture. 

This assignment was a great opportunity to dive deeper into the commands and functions of this programming language, and to familiarize ourselves with the particular syntax that must be used while writing Python scripts. I am still no where near a Python guru, but I am happy with the knowledge I am obtaining through this class on a weekly basis. 

No comments:

Post a Comment

GIS 5935 Module 2.2 - Surface Interpolation

  Post in progress - please check back soon...