The geoprocessor object has multiple List methods one can use to retrieve lists of items that it is aware of. Below are examples of the most commonly encountered ones: ListWorkspaces(), ListTables(), ListRasters(), ListFeatureClasses(), ListDatasets() and ListFields().
ListWorkspaces()
The purpose of ListWorkspaces() is to provide the user with a listing of the types of workspaces that ArcGIS can make use of. The command is simple and accepts two parameters, a wildcard, and the type of workspace one is using. Below is a listing of the parameters:
| wildcard | OPTIONAL. A combination of the star character (*) and any number of characters to help limit results. For example, listing all workspaces with the word nature anywhere in the name would use a wildcard “*nature*”. |
| Workspace Type | OPTIONAL. Limits the type of workspaces that are returned. Possible values are:
|
In order to execute the command, you also need to have your workspace defined. This may sounds a bit contradictory, as you are trying to find workspaces, but what this does is identify an initial location on the disk to start from.
Sample ListWorkspaces() Code
import arcgisscripting
gp = arcgisscripting.create(9.3) # We are using the 9.3 version here
gp.workspace = "mydatafolder" # The original location the search begins at
accessWorkspaces = gp.ListWorkspaces("*", "Access") # Return all personal geodatabases type workspaces
coveraWorkspaces = gp.ListWorkspaces("*", "Coverage") # Return all coverages type workspaces
fileGDBWorkspaces = gp.ListWorkspaces("*", "FileGDB") # Return all file geodatabases type workspaces
folderWorkspaces = gp.ListWorkspaces("city*", "Folder") # Return all shapefile folders starting with the word "city"
sdeWorkspaces = gp.ListWorkspaces("*", "SDE") # Return all SDE type workspaces
allWorkspaces = gp.ListWorkspaces("*", "ALL") # Return all workspaces of all types
Please note that if you are using an earlier version of the geoprocessor (versions 9.2 and below), there are some slight changes on how you access the result of these commands. In 9.3 and above, results are Python Lists, while in earlier versions the results are a geoprocessing enumeration.
ListTables()
The purpose of ListTabless() is to provide the user with a listing of tables ina workspace. The command is simple and accepts two parameters, a wildcard, and the type of table one is seeking. Below is a listing of the parameters:
| wildcard | OPTIONAL. A combination of the star character (*) and any number of characters to help limit results. For example, listing all tables with the word event anywhere in the name would use a wildcard “*event*”. |
| Table Type | OPTIONAL. Limits the type of tables that are returned. Possible values are:
|
In order to execute the command, you also need to have your workspace defined.
Sample ListTables() Code
import arcgisscripting
gp = arcgisscripting.create(9.3) # We are using the 9.3 version here
gp.workspace = "mydatafolder" # The original location the search begins at
dbfTables = gp.ListTables("*", "dBase") # Return all dBase tables
infoTables = gp.ListTables("*", "INFO") # Return all INFO tables
allTables = gp.ListTables("*", "ALL") # Return all tables. This list is equal with the concatenation of the two above lists.
Please note that if you are using an earlier version of the geoprocessor (versions 9.2 and below), there are some slight changes on how you access the result of these commands. In 9.3 and above, results are Python Lists, while in earlier versions the results are a geoprocessing enumeration.
ListRasters()
The purpose of ListRasters() is to provide the user with a listing of raster type datasets in a workspace. The command is simple and accepts two parameters, a wildcard, and the type of raster file one is seeking. Below is a listing of the parameters:
| wildcard | OPTIONAL. A combination of the star character (*) and any number of characters to help limit results. For example, listing all rasters with the word dem anywhere in the name would use a wildcard “*dem*”. |
| Raster Type | OPTIONAL. Limits the type of raster files that are returned. Possible values are:
|
In order to execute the command, you also need to have your workspace defined.
Sample ListRasters() Code
import arcgisscripting
gp = arcgisscripting.create(9.3) # We are using the 9.3 version here
gp.workspace = "mydatafolder" # The original location the search begins at
bmpFiles = gp.ListRasters("*", "BMP") # Return only BMP files
gifFiles = gp.ListRasters("*", "GIF") # Return only GIF files
imgFiles = gp.ListRasters("*", "IMG") # Return only IMG files
jp2Files = gp.ListRasters("*", "JP2") # Return only JP2 files
jpgFiles = gp.ListRasters("*", "JPG") # Return only JPG files
pngFiles = gp.ListRasters("*", "PNG") # Return only PNG files
tiffFiles = gp.ListRasters("*", "TIFF") # Return only TIFF files
gridFiles = gp.ListRasters("*", "GRID") # Return only GRID files
allFiles = gp.ListRasters() # Returns all raster file type, since optional parameters not provided.
Please note that if you are using an earlier version of the geoprocessor (versions 9.2 and below), there are some slight changes on how you access the result of these commands. In 9.3 and above, results are Python Lists, while in earlier versions the results are a geoprocessing enumeration.
ListFeatureClasses()
The purpose of ListFeatureClasses() is to provide the user with a listing of feature classes in a workspace. The command is simple and accepts two parameters, a wildcard, and the type of feature class one is seeking. Below is a listing of the parameters:
| wildcard | OPTIONAL. A combination of the star character (*) and any number of characters to help limit results. For example, listing all feature classes ending with the “fc” characters in their name would use a wildcard “*fc”. |
| Feature Class Type | OPTIONAL. Limits the type of feature classes that are returned. Possible values are:
|
In order to execute the command, you also need to have your workspace defined.
Sample ListFeatureClasses() Code
import arcgisscripting
gp = arcgisscripting.create(9.3) # We are using the 9.3 version here
gp.workspace = "mydatafolder" # The original location the search begins at
pointFCs = gp.ListFeatureClasses("*", "POINT") # Return only Point feature classes
lineFCs = gp.ListFeatureClasses("*", "LINE") # Return only Line feature classes
polygonFCs = gp.ListFeatureClasses("*", "POLYGON") # Return only Polygon feature classes
# Similarly, change the type to one of the supported types listed above
Please note that if you are using an earlier version of the geoprocessor (versions 9.2 and below), there are some slight changes on how you access the result of these commands. In 9.3 and above, results are Python Lists, while in earlier versions the results are a geoprocessing enumeration.
ListDatasets()
The purpose of ListDatasets() is to provide the user with a listing of the datasets in a workspace that ArcGIS can make use of. The command is simple and accepts two parameters, a wildcard, and the type of dataset one is using. Below is a listing of the parameters:
| wildcard | OPTIONAL. A combination of the star character (*) and any number of characters to help limit results. For example, listing all datasets that begin with the word “my” would use a wildcard “my*”. |
| Workspace Type | OPTIONAL. Limits the type of datasets that are returned. Possible values are:
|
In order to execute the command, you also need to have your workspace defined.
Sample ListDatasets() Code
import arcgisscripting
gp = arcgisscripting.create(9.3) # We are using the 9.3 version here
gp.workspace = "myPersonalDB.mdb" # The original location the search begins at - a geodatabse
pgdbDatasets = gp.ListDatasets("*", "Feature") # Return all datasets of type Feature within a personal geodatabases
# Similarly, changing the workspace and type of dataset, one can retrieve a list of all datasets
Please note that if you are using an earlier version of the geoprocessor (versions 9.2 and below), there are some slight changes on how you access the result of these commands. In 9.3 and above, results are Python Lists, while in earlier versions the results are a geoprocessing enumeration.
ListFields()
The purpose of ListFields() is to provide the user with a listing of the fields within a table, shapefile or feature class that ArcGIS can make use of. The command is simple and accepts three parameters, an input table/shapefile/feature class, a wildcard, and the type of field one is trying to list. Below is a listing of the parameters:
| Input Table | REQUIRED: The input file to be used to read fields. Possible types of files accepted are:
|
| wildcard | OPTIONAL. A combination of the star character (*) and any number of characters to help limit results. For example, listing all fields that begin with the word “Shape” would use a wildcard “Shape*”. |
| Field Type | OPTIONAL. Limits the type of workspaces that are returned. Possible values are:
|
In order to execute the command, you also need to have your workspace defined. This may sounds a bit contradictory, as you are trying to find workspaces, but what this does is identify an initial location on the disk to start from.
Sample ListFields() Code
import arcgisscripting
gp = arcgisscripting.create(9.3) # We are using the 9.3 version here
gp.workspace = "mydatafolder" # The original location the search begins at
fileToRead = "myShape.shp" # resides in the mydatafolder workspace
allFields = gp.ListFields(fileToRead, "*", "ALL") # List all fields in the fileToRead table
geoFields = gp.ListFields(fileToRead, "*", "Geometry") # List geometry fields in the fileToRead table
# Similarly, you can search and return any specific fields you need. This is often done when asked to add a field, to verify it does not exist.
fieldToAdd = "Name" # Give our field a name
# Check if the field exists
if len(gp.ListFields(fileToRead, fieldToAdd, "ALL)) == 0:
# If the field does not exist, the length of the returned list is zero
# Within this loop, we know the fields does not exist, so we need to add it
gp.AddField(fileToRead, fieldToAdd, "TEXT", "#", "#", "256")
# The above line adds a string field called "Name" with 256 characters maximum length
Please note that if you are using an earlier version of the geoprocessor (versions 9.2 and below), there are some slight changes on how you access the result of these commands. In 9.3 and above, results are Python Lists, while in earlier versions the results are a geoprocessing enumeration.




(1 votes, average: 5.00 out of 5)Related posts: