Wednesday, June 1, 2022

Programming in GIS - Debugging and Error Handling

 

Shapefile Fields Forever: Fixing errors in a script that gave us details about the different fields listed in the attribute table of a shapefile.

For the second module in our GIS Programming course we focused on strategies for debugging and handling errors in the code. I am proud to say for this section, I was finally able to apply my years of experience in, ah, navigating errors in Python. This module was also eye-opening for how difficult it is for my eyes to naturally see spelling errors in the Python script. I’m not sure if it’s the font, or the expectation that code doesn’t look like complete English, but I was surprised by the errors that it took me an entire error code to catch. Thank you Spyder Console for the assist.

In truth though, while some of the basic strategies we read and heard about for this module were fairly simple: try each line to isolate the error, try to work around the error. The tools presented and number of formalized work-arounds were actually extremely useful, and I am so glad we covered these topics this early in the course. I wish I’d had these tools for every time in the past that I’ve attempted to make a task smoother with Python.   

What’s In a Layer?: The second script we edited is one I can easily see myself recycling. Once the errors were corrected, it returns the spatial reference and layers found on a given map. 

Third try: Is The Charm! Our third edited script used statements to isolate and work around an error without removing it from the code

Using try: except: to work around an error is such a neat application and loophole [not a loop] for managing errors in Python. I think I would benefit from more practice identifying exactly where best to put the except statement, but I can see it being a vital tool for working around errors, as well as identifying them in a readable way.

Flowcharts:

I had to switch the program I was using to make flowcharts, unfortunately it means my charts are much shorter than before. This may be a good change now though to stop me from typing out every line in long blocks of code. 

Script 1 without its Stop


Unfortunately the new flowchart platform outright prevents me from using enough lines to display the for loops in Script 2 correctly. This is only a rough approximation of the flow, because there should be lines looping back to both the for statements and the eventual termination in both of the loops.



Script 3, focusing on the try statement in Part A
Lessons Learned:
  • You can use debugger and IPython to run (step) through line by line more easily than highlighting ad running individual lines
  • Don't forget to stop the Debugger tool when you want to run changes
  • When in doubt, check your spelling and capitalization
  • Generally, the except should go before the outcome, but after the end of any loops
  • Python usually does tell you what's gone wrong, and if it doesn't - IPython will

No comments:

Post a Comment

GIS Portfolio

 As a final assignment at the end of my time with University of West Florida, I have built a GIS portfolio StoryMap. The final product is em...