Thursday, May 5, 2011

Listing Data with ArcPy

ArcPy has a number of functions built into the package that perform list operations.  From the web help here is the complete list at ArcGIS 10.
The results of each of the above function is a python list containing the data you want.  A list in scripting can contain any data type.  To access the data, use looping to iterate through the list and get the values.

Example: Listing Feature Classes in a Workspace

import arcpy
from arcpy import env

# Set the workspace. List all of the feature classes that start with 'G'
env.workspace = r"c:\temp\data.gdb"
fcs = arcpy.ListFeatureClasses("G*")

The results of this is a list of feature classes in the env.workspace path. The variable fcs is an array of paths to each feature class begining with the letter G.

Example: Using List Results

# Set the workspace. List all of the TIFF files
env.workspace= r"c:\temp\images"

# Gets a list of raster data (TIF)
for tiff in arcpy.ListRasters("*", "TIF"):
print tiff

Here we can a simple process that takes the list data and prints the results back to the user. The only returned results are of type Tagged Image File Format. If no images of that file type are found, then an empty array is returned.

List Function Type Keywords
The keywords for the list functions return what is returned to the user in the list.  Here is the list of key words as found in the ArcGIS 10 help documentation: