Accessing attributes in feature sets (Shapefiles and Raster tables) 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 perimeters. They can also include measurements of the objects associated with the features. As an example in forestry, the attributes can contain heights, diameter at breast height (DBH), and canopy widths. For census data, the attributes will include the number of individuals in certain economic, ethnic, and other categories.
Below are some common examples of calculations for attributes:
Remember, most folks are not as proficient at using ArcGIS as you are (or soon will be) so creating custom tools help with processes that are repeated over and over.
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 calculations on them. You can also use "setValue()" to change the attributes in a shapefile. Try accessing various attributes in a shapefile now.
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)
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)
Deleting rows is quite a bit more complicated so I'm just going to refer you to the ArcMap help.
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)
ArcGIS Documentation: Search Cursors
© Copyright 2018 HSU - All rights reserved.