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:



Anonymous said...

How do I use wild cards to exclude certain featureclasses from a list. Examples shown only show how to select using wildcards not exclude.

eg. exclude all FC's that contain VW (WA_HER_STE_LN_VW).


Andrew said...

Wildcards only find information, and don't exclude them. You can do additional filter using conditional statements to prevent the use of specific names of datasets. Try using the string function find(). It should return a -1 if the value is not present.