Wednesday, July 8, 2015

Cursors and Dictionaries


This week we investigated ways to explore and manipulate spatial data.  The Describe function allows the user to discover what data exists as well as to examine the properties of that data.  Tuples and dictionaries were introduced.  We then went on to learn how to manipulate data by using cursors to access and iterate over rows in a table, or to insert or delete records.  One use of the SearchCursor function we practiced in the lab was in creating a SQL statement, which in our case gave us the name and population of all county seats in New Mexico.  We used this table to create a dictionary of keys (cities that were county seats) and values (their population).  One other topic this week was working with text, which was not part of the lab.

This screenshot shows the results of a script which created a geodatabase, copied data (shapefiles) into that geodatabase, selected the name, feature and population fields of the cities feature class, then narrowed that down to just those cities which were county seats.  The last part of the script created a dictionary and populated it with this information and printed it.

It took a while to get the correct syntax for the SQL statement, since the correct type and number of brackets and quotation marks is critical.  However, I got stuck on the part of the code where the dictionary was to print the name and population.  Finally, with some excellent tips and guidance, I was able to make it work.

No comments:

Post a Comment