Module 5 - Explore and Manipulate Data
Module 5 explored using and manipulating data in ArcGIS Notebook and IDLE. A cities location shapefile was provided that included the city name, classification, and the population density in the attribute information. A list of tasks was assigned and needed to be completed by writing a Python script.
- The first task was to create a new geodatabase and copy all the shapefiles into the new geodatabase. The names of the new files in the created geodatabase needed to use the basename property from the arcpy.Describe() function. Therefore, the file name would be something like cities.shp and not cities.shp.shp.
- The second task was to create a Search Cursor for the cities feature class, in which the cursor should only retrieve the !NAME!, !FEATURE!, and !POP_2000! fields only for the cities classified as 'County Seats'.
- The last task was to create a dictionary of the city name and population in 2000. To do this, the script format below was added under the for row in cursor script group, in which the key was the !NAME! and !POP_2000! fields of the Count Seat cities:
<dictionary variable>[<key>] = <value>
or
<dictionary variable>.update({<key>:<value>})
4. The script also needed to include start and process complete messages using the script below:
count = arcpy.GetMessageCount()
print (arcpy.GetMessage(count-1))
Screenshots of the final printed results after running the final script are included:
I ran into several issues when creating this script. For example, I needed to use the define a cursor variable, that uses the arcpy.SearchCursor() function. Then, in order to take the resulting city names and populations of the only County Seat cities as the input key value for populating the dictionary, I used the getValue () method under the for row in cursor group. Creating one variable that lists all three attributes was not successful for me, although I am sure there is another, simpler solution. However, the solution I made was to
define the three values separately and cast each getValue() output as a string using the str() method in the print statement for the County Seat names, classification, and population in 2000. One thing to note is that the del row and del cursor is also needed at the end of the script to release the editing lock on the data after using the arcpy.SearchCursor() function. Also, using the overwriteOutput feature is needed to run the script multiple times and overwrite created files from the previous run without getting error. However, if there is still an error message even when using the overwriteOutput feature, then it could mean the
shared lock is preventing the output from being overwritten, most likely
because ArcGIS Pro is open in another tab. Closing AcrGIS Pro and running in IDLE is a possible solution.
Comments
Post a Comment