Masthead

Accessing Attributes in Feature Sets

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.

Examples

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.

1. Accessing Attributes with Search Cursors

Attributes, also known as fields, are accessed using "SearchCursors" in ArcGIS. The example below accesses the name and population from the Natural Earth countries shapefile.

import arcpy

TheShapefile="C:\\Temp6\\Inputs\\ne_110m_admin_0_countries.shp"

# Create a search cursor based on the shapefile

TheRows=arcpy.SearchCursor(TheShapefile)

# Loop through each row in the attributes

for TheRow in TheRows:
   TheName=TheRow.getValue("ADMIN")
   ThePopulation=TheRow.getValue("POP_EST")
   print(TheName+": "+format(ThePopulation))

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.

2. Examining All the Values in an Attribute Table (optional)

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:\\Temp6\\Inputs\\ne_110m_admin_0_countries.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+": "+format(TheValue))

3. Inserting New Rows (optional)

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 (optional)

Deleting rows is quite a bit more complicated so I'm just going to refer you to the ArcMap help.

Delete Rows (Data Management)

5. Updating Attributes (optional)

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

 

© Copyright 2018 HSU - All rights reserved.