Friday, July 3, 2015

GIS Programming: Manipulating Spatial Data

Hello all,
     Welcome to this weeks post on manipulating spatial data using python code. This weeks activities revolve around multiple features of python. Specifically, working with lists, dictionaries, creating geodatabases, and the use of cursors in arcpy were all highlighted as primary objectives and focus areas this week. This weeks exercise culminated in an assignment script that creates a new geodatabase, copies a list of existing feature classes to the new geodatabase, and then builds a dictionary based on one of the feature classes, "cities.shp" using two attribute fields called NAME and POP_2000. These attributes were used as the key and values for the dictionary to be populated. A dictionary works off a key to provide a specific value in this case calling on a city would yield its resulting population information. Lets look at the finalized results for the script.


Stepping through the script from the results above we can see that the geodatabase is indeed created in a particular location, then used to have the various feature classes populated to it. Afterwards a specific set of cities are located. The cities layer houses information for the cities in the state of New Mexico. First we want to identify all of the cities that are the county seat for their respective counties. This is specified in a feature field within the cities attribute table. A Search Cursor function was performed on the cities dataset to locate all of these cities. The results were then utilized in a for loop to iterate over each city identified as a county seat and add it, and its population values into the dictionary. At the end of the code you can see that the dictionary entries are displayed showing both the key (city) and value (population). Another aspect of the script which is somewhat obvious looking at the results is that each key process had a Get Messages function applied to show the overall completion status of each segment. Additionally, most of the items generated a printed message showing that it was being started.
The most difficult portion of this weeks lab was using the search cursor result to  populate the dictionary. In the end I realized that the cursor variable which is being used as an object in the for loop mentioned above could be used by itself without any additional variables. I tried to over complicate the dictionary by specifying different variables for both the key and value and putting them together. This was because I hadnt yet realized I already had the solution present. Thankfully further investigation with pseudocode breaking down each step that needs to be taken provided more insight into the answer for successfully written code. This was the biggest of the many lessons learned in this weeks lab. Please stay tuned for further exploration with arcpy and python.

No comments:

Post a Comment