Friday, June 12, 2020

Exploring & Manipulating Data

The focus of this week's assignment was to use python to create a geodatabase, populate it with copied files, and then learn how to use the search cursor function and how dictionaries operate.

Creating a file geodatabase uses arcpy's CreateFileGDB_management function.  The parameters are the output location of the fGDB and the name of it.
Code displaying that the fGDB was made.

Next was the matter of populating the fGDB.  We already had the files we already needed elsewhere and it is easy to copy each one over with a loop.  First, I made a list and populated it with arcpy.ListFeatureClasses().  Then for each feature class in the list I had it iterated over in a loop.  Within the loop  I used arcpy.CopyFeatures_management().  The first parameter was the feature class the loop was currently on, then the second parameter was made by concatenating the output environment path with the fGDB name and then using arcpy's describe function to get the basename of the file added on at the end.

Each feature class being copied over to the new fGDB.
 Now that the files have all been copied over, we have to change the workspace to the fGDB.  The next task is to iterate over all the county seats in the city feature class.  This is done by creating a search cursor.  A search cursor takes the parameters of a feature class and then a SQL query.  For this query I specified that the feature must be equal to 'County Seat'.  As it goes through the loop the program prints the name, feature type, and the population of the city in the year 2000.
The successful output of the search cursor.
Lastly, we learned the basic functionality of python dictionaries.  I started by creating a blank dictionary and then populating it with the same search cursor as was created in the previous step.  With the loop established, just one more line of code assigns a key based off of the city name and the population count as the corresponding value.

The creation and contents of the dictionary.

The flow chart showing the list of steps the code runs through from creation of the fGDB to the dictionary output.

No comments:

Post a Comment