Thursday, June 20, 2024

Geometries in Python

In this week's assignment, we had to write a script that created a text file, used a Search Cursor to find information from a shape file, and then add this information to the file in a csv format.

This lab feels like it took me forever! I got the process done step by step, and often felt like trying to move forward to the next step undid the previous one. The loops within the Search Cursor threw me for a loop - pun intended. I was ready for the basic set up for the Search Cursor, as we’ve spent a good bit of time working on these during the last assignment. I doubled checked the attribute table for the data to make sure I had the correct phrasing of the name header. I borrowed quite a few of the codes from the exercise, changing them to make sense for the assignment. But it took some finagling to get the information to print out as instructed. I finally solved all of my issues by putting all of the information in one print command. It took a bit of tweaking. I read a lot about the .format as well, trying to figure it out. 

The next step was to populate the text document. I added a “f.write” command at the bottom of the script and it returned one line. Oh right – the cursor has run and it needs to be reset! After unsuccessfully trying to reset cursors last week, I figured the easiest thing was to write the text document while I printed everything in IDLE. So I moved this code on up into the Cursor and was able to get the following result.
Here's a sample of the final text document.

This is the code I created to write the document.



 


Tuesday, June 11, 2024

Exploring and Manipulating Data with Python

This week we really got into coding! The assignment included using python to look at and edit data. We had to write a code that created a new geodatabase, copied files over to it, sort data from a layer, and then print this information out in several ways. 


Here's a look at the script workflow.

Most of the code was pretty easy and straightforward as the exercise covered the basics well. I ran into issues with the search cursors. Creating them was easy enough but getting the data from the attribute table into the desired format was more tricky.

I had issues trying to print the results from the first search. The population is acting as an integer so it did not want to print when I create the code for this. I remembered issues with this situation from Module 2 and looked back at this code to see how I fixed it. I used a command to turn the integer into a string – str(). However, it still took me some playing around to figure out how to put this into the code. I tried it when I assigned the field to my variable pop, and then in several places around the print code before I finally sorted it out – str(row.getValue(pop)). 

Similarly, I created the search cursor for the dictionary easily enough but populating it, on the other hand, gave me trouble! I tried to reuse the search cursor settings but it only returns one city – Carlsbad, which is the last on the list. I finally figured out I needed to change the way that I was writing the code to add the keys and values to the dictionary. While {} creates a dictionary, I could add values in a simpler fashion – county_seats[key] = value. The most foolish part is I realized this was explained in the assignment instructions when I went to read back over them a little more thoroughly.

Overall, I feel like I'm getting the hang of coding and am enjoying the challenge of it.

Here's a look at the results of my code:
























Sunday, June 9, 2024

Geoprocessing with Python

This week, we worked on geoprocessing in ArcGIS Pro using Model Builder and Python scripting. We had to accomplish some very basic tasks, but use these methods for them, neither of which I've done before. 

I really like the Model Builder function in ArcGIS Pro. It’s nice to be able to draw things out in a way that creates the model but also makes the model (versus using the online tool to map things out and than doing all the geoprocessing). It took me a few minutes to sort out the specific tools as there are many different ways to accomplish the same results within ArcGIS. In the end, I used tools that best fit exactly what the assignment was asking me to do. For instance, it very clearly asked me to select out polygons and delete them. So instead of selecting out what I wanted left in the end and creating a new file, I figured out how to select by attribute and then delete these from the main feature layer.


The model I created with Model Builder

For the Python script, we had to have the script add XY coordinates to a shapefile, create a 1000 buffer around points, and then dissolve the buffer. I was able to just create a buffer that was dissolved so I only had two main processing part to my script. The scripting itself was a little intimidating to begin with, thinking about starting everything from scratch. But that’s never how coding goes! Specific language accomplishes specific tasks, so it’s just a matter of finding the language you need and conforming it to the files and tasks you want to accomplish.

Script success!

I'm really enjoying coding and look forward to what next week's assignment has in store for me.


Sunday, June 2, 2024

Python Debugging and Error Handling

This week, we covered debugging and error handling in codes. We had three different codes that we had to find and fix errors in.

For the first code, we had to fix two errors. I was able to spot the capitalization error before even trying to run this code. When I ran it at first, I realized that I had my data in an additional subfolder so I got an additional third error in the code. I moved the files into the main Data folder and things went better from there.


The second code was a little bit tricker as I didn’t know how many errors I might get. I was able to fix the bad file paths quickly but it took me a little bit longer to sort out m. I knew it was a variable that hadn’t been described but the solution. The for-in statements are interesting because I’m never sure exactly what Python just knows, or what its pulling from something from inside a file like a field name or map name. I will read up more on these!


In the third script, we weren't supposed to fix the error but isolate it with a try except statement so that the other portion of the code would work. This part gave me quite a time trying to figure out how much of the code to bypass. I definitely just kept trying it in different places. In the end, I realized the answer was there all along in the error message: Line 13. It was far more of part a than I expected! The indents also got tricky to sort out how many each line needed.

Here's what the flowchart of the code with the statement. Part A of the code tries to run the code in the script to print the layer names. However, if it gets an error, it skips this code and prints the error message instead.



GIS Portfolio

To show off all I have learned during my GIS Graduate Certificate program, I created an online portfolio. Click here to check it out.  The ...