GIS5103 Module 3: Debug & Error Handling
Switching from the heat to the onslaught of rain recently has made me sick again—you could say I've caught a... bug!
Speaking of catching a bug, aside from the common cold I've been experiencing, we learned about common Python and geoprocessing errors, as well as general debugging and error handling in Python scripts this week. Troubleshooting can be headache-inducing, but at the same time, oh so rewarding when you can overcome them. Python thankfully makes this process as painless as possible. Again, I'm reminded of how kind Python and its editors can be in identifying what causes a script to fail to run or to break in the midst of a run.
This week, we were tasked with three different pre-made scripts this time. For two of the scripts, we were able to fix some errors and ensure they ran. The final and third script, however, required us to use try-except statements to bypass erroneous sections of Part A while still allowing Part B to run.
| Script 1 - Feature Class Field Names. Prints out all the names of each field found in the "parks.shp" file. |
The first script covered correcting a few minor errors with the script. IDLE was my preferred editor, and getting more comfortable with the different error messages was nice. I'm noticing less anxiety whenever I see red text. Reframing it as approaching red, erroneous-like text with an open mind of "check this interesting thing out" has been helpful. Honestly, it's helped me overcome some perfectionism and encouraged more trial and error in my work.
| Script 2 - Project Spatial Ref & Layers. Prints out the ArcGIS Pro project's spatial reference and its layers. |
As shown above, the second script was another fixing errors. It prints out the project's spatial reference, NAD 83 State Plane Texas Central FIPS 4203 Feet. It also pulled the layer names from the map, which included parks, bike_routes, facilities, and county. This one helped me notice errors when it comes to functions in particular, reminding me about case sensitivity.
| Script 3 - Parks Try-Except Statement. Implemented a try-except statement to bypass any errors in Part A of the script and lets Part B run successfully after. |
| Script 3 Flowchart. |
The last script was fun to wrap my head around. Putting in a try-except statement made me think of using a net to cast to catch an error. So, I tried thinking of the try-except statement as a trap block to case potential erroneous parts of the script in Part A. I chose to not include the initial print statement to ensure that Part A was attempting to run. I also wanted to make my exception statement have an additional error text when printed, so I did add an f-string to that clause. I like using f-strings and like how they look when I read back a script, personally.
For fun and to challenge myself, I also wanted to fix the script to see if I could get it running. Definitely having Script 2 and also seeing Part B completed with no errors gives you quite the foundation and leg-up on fixing Part A. With a bit of adjustment and switching the placement of the Part A success print statement (due to it not printing when I wanted it to), I was able to get it running as you can see below.
| Script 3 - Parks Working. Prints out the parks layer name if present in project's map, then prints success statement. If not, it would have printed the previous error message. |
Now, future troubleshooting doesn't seem so daunting to face after more exposure (therapy). Bring on the scripting scaries!
Comments
Post a Comment