Wednesday, June 19, 2019

GIS Programming - Module 5 Lab

Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:27:44) [MSC v.1900 64 bit (AMD64)] on win32

>>> print ("Hello Everyone!") 

Hello Everyone!

For this weeks lab in GIS Programming, we have been working with data exploration and manipulation using Python in both ArcGIS Pro and Spyder. For this weeks lab, I was assigned to create a multi-part script that carried out several data tasks. First, I was to create a new file geodatabase. Then, I had to copy the original data into the new file geodatabase. Once the data had been copied into the new file geodatabase, I was tasked with creating a search cursor using a SQL query 'where' where clause that returned and printed 3 pieces of information, the name, feature type, and population from the year 2000 of cities that all had a feature type of 'County Seat'. Finally, I took the information from my search cursor and constructed a python dictionary that when printed shows the cities that met the SQL query search criteria in the format of 'City' : Population format. To break things down further I have created a pseudocode block to conceptualize my process.

Start
>>> Create new file geodatabase (fGDB) named mod5.gdb for data storage. Print result messages.
>>> Create a feature class list and copy features from data to new fGDB (use basename property to remove the .shp extension for compatibility). Print result messages.
>>> Create a search cursor with a SQL 'where' clause and for loop to select cities that have a feature type of 'county seat'. Print results in the form of clustered messages (City name, Feature Type, and Population from 2000).
>>> Create a new dictionary with a for loop that when printed returns a compiled dictionary list of all the cities and their populations that meet the search cursor criteria (ex. 'Townsville : 5000'). Print result messages and newly created dictionary.
Stop

To see my code in action, I have clustered the results together and will explain each section below. I decided that separating each running portion with a new line of '*' would make the results easier to read.


The picture above shows the results of my script being run. The first section of the code successfully creates a new file geodatabase (fGDB) and lets the user know that the process was started and completed successfully with the start and finish time. The second portion of the code copies the eight shapefiles over to the newly created fGDB. For this portion of the code, it was essential that I used the basename property when copying the features over as shapefiles are not compatible with file geodatabases. By using the basename property, 'airports.shp' now becomes 'airports' and is able to be copied to the new fGDB as a feature class. As you can see, the script prints start and finish messages after each feature is copied and lets the user know the task has been successfully completed. The third portion of the code is where I implement my search cursor. As you can see, a message displays showing that the search cursor process has started. By incorporating a 'where' clause SQL statement, the search cursor returned only the name, feature type, and population for the year 2000 of the cities with a feature type of 'County Seat' as shown in the results. Once again, a message prints informing the user the task has been completed successfully. Finally, the script informs the user that a new dictionary named county seats is being created and upon successful completion prints the runtime message and the completed dictionary in the {'City' : Population}format with city names being the dictionary keys and population number being the dictionary values. At the end of the script, a message prints informing the user that the script has finished running in its entirety. 

Overall I really enjoyed this weeks lab and am so happy with the progress I have been making. One hangup this lab gave me was with the creation of my dictionary. I found that whenever I tried to use the required loop to populate my empty dictionary, it would only ever print the empty dictionary. To solve this issue, I called my search cursor a second time and the dictionary appended itself perfectly. Thank you for tuning in this week and I look forward to sharing my future work with you!

No comments:

Post a Comment