Terry's GIS Studies and Transition to a New Career

Showing posts with label While Loop. Show all posts
Showing posts with label While Loop. Show all posts

Friday, June 12, 2020

Module 5--Exploring and Manipulating Data

Well, I can guarantee that I am not quitting my day job. Though the lab started out easily, I got hung up on populating the dictionary. I probably spent 16 hours or more just one that one small step. After I finally figured out the issue, which was that by adding the update, get value statement for keys and values, and the key:value format statement within the for loop, everything printed out as required. I figured this out by commenting out lines of code and then adding new lines to see what happened. I eventually worked through the issues. Of course, viewing the student questions site on Canvas also assisted. Believe me, I contributed to the questions, but I also shared my solution.

The lab worked sequentially and built upon each step. Beginning with creating a new file geodatabase, I then assigned my feature classes to "fclist." I then copied these FCs to the new fGDB using a "for loop" (and also stripped out the .shp extension). Between each step, I learned how to add statements showing the date, time, and the amount of time the computer needed to complete the step. I also used "\n" to make sure lines were skipped for ease of viewing.

This screenshot shows the first several steps of the lab, to include the commentary: Create fGDB, copy Feature Classes, and creating a search cursor that returns county seats. The list of county seats is truncated to save space, but the code shows the information for all the county seats.
I then used a search cursor to sort out all the cities that were considered county seats in the feature column of the table. The only information I needed for this exercise was the name of the city, the feature, and the 2000 population.
Screenshot of the end of the county seat information. The dictionary is populated in the correct format {key:value}. Statements say how long it took, when the process finished, and the lab was complete.

Once I got the dictionary to print, the lab was complete. Though the lab was a challenge, I learned a lot.

A few words of advice:
--delete the row and cursor once you complete a loop.
--use \n so there is a line break.
--use print(arpy.GetMessage(count-1)) to get the date/time/process time for each line.
--don't forget the # symbol to add a comment.
--make sure ArcGIS Pro is not running at the same time as Spyder or you will likely get an error message.
--use the overwrite argument to reduce the amount of errors you receive saying that the fGDB exists (env.overwriteOutput=True).
--comment out lines of code (sometimes I put different numbers of # marks to keep myself straight) and try new lines. This way, you don't make a change and forget how to reverse it.










Monday, May 25, 2020

Module 2: Python Fundamentals

For this week's exercise and assignment, there were numerous operations broken down into four steps. These steps included creating a string variable, creating a list, creating loops and iterating variables, and removing specific variables.

In Step 1, I created a string of my full name,  split the string to separate my individual names, and then used the index function to return a specific name. It is important to note that one uses 0 to return the first name and -1 to return the last portion of a string.

Step 2 required me to determine errors in a random dice game. This required me to first import the random module, which is the first line of code. I then looked at the script provided in the assignment to find two errors. A few things to note in errors: Ensure your quotation marks are not confused with contractions as it will confuse the program and return an error, make sure the case (upper/lower) is consistent, especially when naming a variable, and watch indention.

Step 3 caused me quite a bit of problems, as I overthought everything. In this scenario, I had to create a loop using a while statement and then append numbers to it until I reached 20 separate numbers. Again, this script required me to import the random module as the first step. One lesson I learned is that if you want to break the loop, the break must have the same indention as the loop. Otherwise, Python will return an error. I also learned that there are many different ways to arrive at a break statement.

For Step 4, I used the numbers generated in Step 3 (or I could have added this script to the script in Step 3). I then chose an integer as an unlucky number, used the while loop to remove the unlucky number, and then created a new list. This was fairly straightforward and of no issue. However, the next logical step in the future could be to remove the unlucky number and replace it so that there were still 20 numbers in the list.

Below are the results of my script, which has the return of each script. The explanations are written above, so I will not repeat them.

Script for Module 2. This shows the return items for each step in the assignment as explained above.


Flow Chart for Step 4.