Friday, July 3, 2015

Module 7 - Explore and Manipulate Spatial Data

This week in GISProgramming I learned just how important attention to detail can be. I also learned that the longer you stare a the computer screen, the easier it is to overlook little details so sometimes walking away is a good thing. The exercise and lab for the week included creating a new geodatabase and using lists, loops, cursors and dictionaries to manipulate spatial data used in ArcGIS. I went back and forth between ArcMap and PythonWin to accomplish the tasks for the week and find to my coding mistakes.

The first task in the lab was to create a new file geodatabase and fill it with existing shapefiles that we downloaded earlier. This part of the lab was fairly simple, especially since we have access to the arcpy module. I imported the arccpy module and set the geoprocessing environment before I created the database first. Then I used a for loop to copy the existing shapefiles from the sharedrive into the geodatabase.  The most important thing to remember in this section was that geodatabases are filled with feature classes not shapefiles so the file names couldn't include the .shp extension when they were copied. This was accomplished with the arcpy.Describe (basename) function.

Then came the most difficult section in my opinion - creating the cursor search. I spent a lot more time on this section than I should have because of a minor spelling mistake. The task was to search a layer filled with city data for cities with a particular feature and then return the name of the city as well as the population. My first problem was figuring out how to properly enter the field names from the attribute table as parameters for the field_name section of the cursor. That was just a major over-thinking issue and a comma with one set of brackets solved everything. The other problem I had was with the syntax of my where clause. I must have ran the code 20 times with small changes all with no results printing. Finally I looked at the query builder in ArcMap to sort through that particular field and I found that I made a word plural that was not plural in the attribute table (county seat vs county seats). As soon as I fixed that spelling error the cursor search worked just fine.

The last part of the lab was to create a empty dictionary and fill it with the city name and population values returned by the cursor search. I used another for loop to accomplish this using the items from row 0 in my cursor search as the keys in the dictionary and the items from row 1 as the values for the dictionary. I was extra careful about spelling and syntax at this point so I was able to create the dictionary pretty quickly. Once the code ran properly I added print statements throughout to show which steps had been completed. The final results are in the screenshot below.
Module 7 - Geodatabase and Dictionary Creation in PythonWin


No comments:

Post a Comment