Accessing Attributes in Feature Sets (Shapefiles)
In Review
Accessing attributes in feature setes (Shapefiles) is a common need in GIS scripting. The attributes can describe a wide variety of characteristics of the features in a feature set. These include measurements of the feature such as area, length, and perimiters. They can also include measurements of the objectus associated with the features. As an example in forestry, the attributes can contain heights, diameter at breat height (DBH), and canopy widths. For census data, the attributes will include the number of individuals in certain economic, ethnic, and other catagories.
1. Accessing Attributes with Search Cursors
Attributes, also known as fields, are accessed using "SearchCursors" in ArcGIS.
import arcpy TheShapefile="C:/ProjectsPython/OregonDams/oregon_dams.gdb/dams_large" # Create a search cursor based on the shapefile TheRows=arcpy.SearchCursor(TheShapefile) # Loop through each row in the attributes for TheRow in TheRows: TheValue=TheRow.getValue("damheight") print(TheValue)
That is all it takes! You can access numeric data, string data, and dates in attributes and then perform calcuations on them. You can also use "setValue()" to change the attributes in a shapefile. Try accessing various attributes in a shapefile now.
2. Examining All the Values in an Attribute Table
You can also go through all the values (cells) in a shapefiles attribute table by using the following code snippet. The "ListFields()" function will return a list of the fields in the attribute table for the shapefile. Then, we can iterate through all the rows in the table. Within each row we can then iterate through all the fields to get to each value in the table.
import arcpy TheShapefile="C:/ProjectsPython/Shapefiles/Plots_With_DougFir_7.shp" # Create a search cursor based on the shapefile TheRows=arcpy.SearchCursor(TheShapefile) # Get a list with all the fields in the shapefile TheFields = arcpy.ListFields(TheShapefile) # Loop through each row in the attributes for TheRow in TheRows: # Loops through each of the fields for each row for TheField in TheFields: # Get the name of the field from the field object TheFieldName=TheField.name # Use the field name to get the value TheValue=TheRow.getValue(TheFieldName) # Print the value print(TheField.name+": "+TheValue)
3. Inserting New Rows
You can insert a new row with the code below.
import arcpy TheShapefile="C:/Temp/10m-admin-0-countries.shp" # Create an insert cursor based on the shapefile TheRows=arcpy.InsertCursor(TheShapefile) # Insert the new row TheRow=TheRows.newRow() TheRows.insertRow(TheRow)
4. Deleting Rows
Deleting rows is quite a bit more complicated so I'm just going to refer you to the ArcMap help.
5. Updating Attributes
You can also go through all the fields (columns) in a shapefiles attribute table by using the following code snippet. This example users a country shapefile from the Natural Earth web site.
import arcpy TheShapefile="C:/Temp/10m-admin-0-countries.shp" # Create an update cursor based on the shapefile TheRows=arcpy.UpdateCursor(TheShapefile) # Update the row for TheRow in TheRows: if (TheRow.getValue("COUNTRY")==" "): # value is a single space (default for a new string value) TheRow.setValue("COUNTRY","test") TheRows.updateRow(TheRow)
Additional Resources
ArcGIS Documentation: Search Cursors
Notes to instructor
We need some good examples here of where these would be used.
- A tool to convert coordinates to meets and bounds (direction and distance?)
Checking for inappropriate language in string fields?
Finding keywords (species names) in string fields?