Wednesday, June 12, 2024

Module 5- Exploring and Manipulating Data- GISProgramming

 This week we learned how to check for, describe and list data, create and populate a geodatabase with existing data in other folder, how to use a SQL statement within a python script using cursors, we reviewed lists and dictionaries and populated a dictionary from a selected feature class using a cursor.

Create and populate a new geodatabase

We began by creating a new geodatabase in our results folder and populating it with shapefile data provided in our data folder.  During this step I ran into some mistakes with my files early on. For both the creation of my geodatabase and the next step of populating my geodatabase I left out a “/” in my output. This led to me initially create a geodatabase named “resultsbnl26” in my module 5 folder as opposed to a geodatabase named bnl26 in my results folder. After I corrected this error, while populating my geodatabase I wound up creating a single shapefile named bnl26 to my results folder and nothing in my geodatabase folder. I realized quickly just how important that slash is.

Create a Search Cursor

We then were tasked to create a Search Cursor for the cities layer and to retrieve and print the name, population and feature of all cities that were listed as a “County Seat”. I created my cursor, ran my “for” loop and immediately had no issues printing my “name” statement but I ran into several snags trying to print the population. I kept getting an error that the program couldn’t concatenate an integer, so I tried converting it to a string, but I was not able to. I spent awhile trouble shooting and trying different methods of formatting my print statement but finally I had to set a separate variable to get the value of the population and then set that variable as a string instead of putting the getValue method within the print function. During this troubleshooting process I had a moment where I falsely thought I had figured out the population issue when, instead of an error, I got a syntax issue on the next line that wouldn’t allow my code to begin to run. I spent awhile wrongly troubleshooting that line (which was my feature print statement) until I finally removed it altogether to see if that fixed the issue. It simply moved the syntax error to the next line so at that point I knew that I still had an error in the previous line with my population print statement. This was a good reminder that sometimes the errors are not what or where we think they are.

During this section I also struggled to figure out how to use the newline character outside of a string since my previous experience with it had been inside a string and most of the options I was seeing online were within a string. I finally realized I needed to “+” it within the print statement rather than trying to tack it onto the end of my variable.

Create and populated a dictionary from feature class data

Our final task was to create and populate a dictionary that contained the names and population of each county seat. This task was my biggest struggle since we had only done the required tasks separately so far. I created my blank dictionary and I knew I needed a “for” loop and to somehow use the search cursor again. For some reason I tried to set my “for” loop as “for county in county_seats” (county_seats was my dictionary), and had a second “for” loop for the search cursor within the first for loop but realized that the first for loop was unnecessary. I knew how to update the dictionary but I kept accidentally populating my dictionary with [NAME : Pop_2000] (the titles of the rows) instead of getting the individual features names. At this point I realized I needed to add the getValue method to the line of code somehow, but I kept trying to add it within the “update” dictionary statement. I realized that I was making the same mistake I did earlier during the previous task and that I should set key and value variables that used the getValue method and then use those variables within the dictionary and it populated smoothly after that. I did run into a repeat of an issue I had earlier in the term where instead of printing off my final list it printed each iteration. I accidentally printed each iteration of my dictionary as it ran the “for” loop. This was easily corrected by adjusting the indent.

After I had completed all my tasks I went in and added the necessary print statements and retrieved messages for each task.

Here are the many screenshots of my final output as well as the flowchart I created based on my script.

 





Final thoughts

It was amazing to see the progress we have made in such a short time. I have really enjoyed learning to code, and I am disappointed that we are coming to the end of the term (only one week left). I am actively looking for ways to get more practice with these tools and I am hoping I can utilize them in my next few classes. One issue I am running into is that since it is all so new to me, I keep forgetting what options are even available to me. Another issue is discerning what variables to set for each task but this week I felt a lot more confident in my ability to “figure it out” when I hit a snag. I noticed that when I stepped away for a bit and came back to the task at hand fresh, I was able to solve my issues more quickly. This is a weakness of mine because I get very hyper focused on things I perceive as a puzzle and I don’t want to step away, but I am actively choosing to find a task I can do that forces me to take my mind off the “puzzle” of whatever error I am struggling with so that I can come back and see the code more clearly.

 



No comments:

Post a Comment

GIS Portfolio

 We were tasked to create a GIS portfolio for our internship program. It was a great opportunity to put organize the work I have been doing....