Monday, June 17, 2024

GIS 5103 Module 5 - Exploring and Manipulating Data

GIS Programming, Module 5 allowed us to take the knowledge obtained throughout the four previous modules and apply them into realistic ArcGIS scenarios. There was a great deal of application, even more learning, and probably more trial-and-error than I would like to admit. This module forced us to dive deeper into Python scripting, gaining more confidence to work with dictionaries, SQL [Structured Query Language] queries, and Search Cursors. I will not admit to being a Python professional yet, but I am proud to say that I successfully applied this knowledge at work, creating four separate scripts that automate specific tasks, shaving off the total amount of time it takes to perform my job.

For this particular Module, our required script was to perform the following tasks: create a file geodatabase, copy feature classes into the newly created geodatabase, perform a Search Cursor on the feature class named 'cities' [finding all county seats in the state of New Mexico], using a Search Cursor to create a list of all these queried county seats, their classification, and their population [as of the 2000 census]. Finally, the script was to extract the names and the populations of each county seat and insert them into a blank dictionary that was created. 

As shown in the flowchart below, this is the most complex script that has been created in this class, performing numerous functions throughout its execution. Following along the path of the flowchart, the script begins by importing the ArcPy module and setting the current working environment. After this has been accomplished and a few variables have been set, the file geodatabase was created in the S:\GISProgramming\Module5\Results\ folder. Next, the script iterates through a loop, copying each feature class from the data folder into the geodatabase.


After the geodatabase has been created, a Search Cursor was run on the 'cities' feature class, looking for all cities whose feature attribute equaled 'County Seat'. It is worth mentioning here, that there are three different types of Cursor functions that can be executed through scripts in ArcGIS: Search, Update, and Insert. Search Cursors are used to search out records with no ability to write over the existing attributes. Update Cursors are used to change / overwrite existing attributes of feature classes. And Insert Cursors are used to insert new rows into the attribute table, essentially creating new features within that particular feature class. 

The first time I worked through this code, it was a cut and paste job from the exercise and the code worked fine. However, our text states that arcpy.SearchCursor() is only included with ArcGIS for backwards compatibility purposes, and that the newly adopted arcpy.da.SearchCursor() function was highly recommended moving forward. [Zandbergen, 2020. p. 248] While the functions themselves are similar, I experienced quite a few errors while trying to integrate the SQL query into the parameters of the arcpy.da.SearchCursor() function. Multiple online searches, in addition to the textbook, provided ample support to work through the issues that occurred during this overhaul. 

Lastly, the script creates an empty dictionary and populates the keys of the dictionary with the county seat's name and populates the value of the key with the 2020 census population count. There were no major issues in this part of the script, but I would recommend keeping one thing in mind. Cursors only move in a forward position, so if the script iterates through a For loop, the cursor needs to be returned back to the beginning row of the attribute table, especially if the data is going to be searched subsequently. There are a couple methods to remedy this situation, and neither are complicated; this is just a point worth mentioning. The results of the script can be viewed in the screenshots below.


Ultimately, this Module provided the most realistic applications in Python scripting for ArcGIS, and I am ecstatic that I was apply this knowledge to create scripts that automate redundant tasks at work. Even after this class concludes, I will continue to develop more Python scripts that enable me to be more productive and efficient at my career.

Source:
     Zandbergen, P.A. (2020). Python Scripting for ArcGIS Pro. Esri Press.

No comments:

Post a Comment

GIS 5935 Module 2.2 - Surface Interpolation

  Post in progress - please check back soon...