Thursday, May 28, 2020

Debugging

Another week, another leap in understanding the world of GIS Programming.  This week, we were tasked with debugging three different scripts designed to provide information related projects in ArcGIS Pro.  The first script (as shown executed below) provided a printout of the fields in the park shapefile attribute table. In terms of programming, this first script just had some mismatching capitalization, font inconsistencies and an incorrect link to the folder with the shapefile. The fixes were rather obvious and easy to correct for without requiring additional assistance from the debugging tool.


The second script provided a printout of the names of all layers in the associated project in ArcGIS Pro.  There were a number of errors in this script, and to make it easier for myself, I used the debugging tool in Spyder to go through each line of code one by one, identify the issue and resolve, then move forward.
I found and corrected the following errors: A case error with the filepath definition. A missing y when referencing the arcpy catalog. An incorrect filepath for output, an incorrect file path for the project location. A missing e in spatialReference. The "for map in maps:" was incorrectly stated. and A misspelling of map within the code.  The debugging tool was a great help in processing this information quickly and easily.  The resulting code printed as shown below.


The final challenge was to use a try, except statement in order to successfully run a script without actually correcting the errors.  I still had to correct the generic filepaths so that I was actually calling on the correct data before running the code, but I corrected no other errors and simply used the following method to successfully run the code.  I input "try:" at the top of the code, indented the rest of the code in part A.  I then input the following except statement... 
 except Exception as e: 
     print(e)
at the bottom of the code and ran the code.  This resulted in the printout below.


The most fundamental things I have noticed so far in this class are the importance of spelling, capitalization, correct file paths for whatever project you are working on, and understanding catalog functions and how they can correctly and effectively be used. Attention to detail is key, and rushing through a coding process will not benefit anyone. Successful programming is akin to the old adage: “Measure twice, cut once.”

Sunday, May 24, 2020

While Loops and If Else Statements

In this lab, we were tasked with selecting and printing an item from a list, debugging a pre-written code for a dice game, using a while loop to create a random list of 20 numbers, selecting an unlucky number and printing how many times it showed up in the list using if else statements, and finally removing that number from the list using another while loop.  The results of my experience are shown below.



Part 1:

Create string variable consisting of full name
Split the name using a space as identifier
Print last name in list


Part 2:

Run the existing code to see what errors existing

Debug error, run again

Debug error, run again

Print successful results


Part 3:

Create empty list

Assign counter variable

While loop
     Generate random integer
     Append to list
     If statement to set list length to 20
          Break loop

Print list


Part 4:

Assign variable to unlucky number
Count method to see how many times number shows up in list

If statement to print if the number does not show up in the list
     Else statement to print if the number does show up, how many times it needs to be removed

While loop if z in lucky list
     Remove method to remove until the number no longer shows up in list

Print list


A few of the important things I learned from this lab are:

1 Keep it simple… If the code does not require additional steps, don’t add additional steps.

2 Very important to give a while loop the x+=1 command.

3 I was able to complete this task with a for loop before I was able to complete it with a while loop. And while I see the reasons for not using a for loop in general, I had a lot of trouble getting the while loop to work. I kept sticking with it and now it all seems rather simple. It did not feel that simple when I kept running into issues though. It makes for a very rewarding conclusion once it finally works.

4 I think one of the most important lessons for me is to not attempt too much code at once. Break the code down into subsequent parts and get it working as you go. I played around with having the 4th part of this assignment running before the 3rd part was completely successful. It didn’t necessarily hurt me to try, but I think you run the risk of information overload when trying to do too much at once.

Friday, May 15, 2020

Create Your Own File Path


In this lab exercise, we executed a pre-written .py file using the Spyder IDE in order to create our file folders for the GIS Programming class.  Instead of having to create eight basic folders with three folders in each of those, we simply ran a program and voila! The result of my code execution is shown below.


The lab for this week was pretty straightforward and gave me more confidence with the idea of running scripts to automate tasks. I have never used Python before so this was a great lesson to familiarize myself with the very basics of a few Python interfaces and feel the excitement of seeing how code is executed. When I first saw the code for creating new folders in the S:\, I have to admit I felt intimidated, but seeing it in action makes me want to understand it better so that I may create programs with similar, or even greater, capabilities.

I found the idea of pseudo code to be very helpful for laying out a plan to more successfully complete code. I will use this concept in the future as a way of organizing my thoughts before beginning to solve complex problems with code.