Saturday, June 13, 2020

Basic Tasks in ArcGIS with Python

This week, we created a geodatabase, copied shapefiles from a folder into the geodatabase, created a searchCursor with a SQL query and a for loop to retrieve the populations of each city listed as a county seat in New Mexico, and then created a dictionary of those values and printed the dictionary.  The results from my code are pasted below:







I had some difficulty when printing the “POP_2000” field in the section where we were listing the County Seat statistics. I kept getting the error that POP_2000 must be string and the logic of where to place the ‘str’ to convert this object from an integer puzzled me for a bit. In hindsight, it was a very simple fix, but I struggled for a while with that. I ultimately realized that the correct location was before the row.getValue function as follows:
print("Population circa 2000: " + str(row.getValue(field2)) + "\n")

I also had issues with finding the correct way to efficiently get the searchCursor set up so that I would be able to retrieve only the cities listed as a “County Seat”. I debated creating a copy and deleting fields so that it could only print the existent fields, but knew it was not the best route. I then realized the SQL query was all too obvious. I used the following line of code to select only the County Seats:
cursor = arcpy.SearchCursor(fc,'"FEATURE" = \'County Seat\'')

My final major hurdle in this experience was in getting the dictionary updated with all of the county seats and their respective population. I kept getting the “NAME” and “POP_2000” as the only populated fields in my dictionary. Again, the solution was very logical and I finally realized the need to input the entire ‘row.getValue’ + the correct field in my update function in order to iterate and populate the dictionary as desired. That line of code resulted as follows:
for row in cursor:
     dictionary.update({row.getValue(field):row.getValue(field2)})


In summation, I found this lab to be the most challenging and most rewarding so far. I am really starting to see how the code fits together and how it gives us the ability to complete complex tasks by stacking the Legos together. The answers are all very logical, but it can be difficult to see the obvious errors until you have experienced them a few times first hand.  The basics of importing catalogs, assigning proper variables and setting the work environment are key to being able to dig into the most difficult problems you are trying to solve. Without the fundamentals in place, one can get bogged down in simple mistakes and issues. A good foundation is paramount.

No comments:

Post a Comment