Aug 19

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:

  • Access: Only personal geodatabases are returned.
  • Coverage: Only coverage workspaces are returned.
  • FileGDB: Only file geodatabases are returned.
  • Folder: Only shapefile workspaces are returned.
  • SDE: Only SDE databases are returned.
  • ALL: Returns all above types of workspaces.

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:

  • dBase: Only dBase (.dbf) tables are returned.
  • INFO: Only INFO tables are returned.
  • ALL: Returns all above types of tables.

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:

  • BMP: Only bitmap image files are returned.
  • GIF: Only Graphic Interchange Format files are returned.
  • IMG: Only ERDAS IMAGINE files are returned.
  • JP2: Only JPEG 2000 raster image files are returned.
  • JPG: Only JPEG raster image files are returned.
  • PNG: Only Portable Network Graphics image files are returned.
  • TIFF: Only Tagged Image File Format files are returned.
  • GRID: Only ESRI GRID raster fles are returned.
  • ALL: Returns all above types of raster.

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:

  • ANNOTATION: Only annotation feature classes are returned.
  • ARC: Only arc feature classes are returned.
  • DIMENSION: Only dimension feature classes are returned.
  • LABEL: Only label feature classes are returned.
  • LINE: Only line feature classes are returned.
  • NODE: Only node feature classes are returned.
  • POINT: Only point feature classes are returned.
  • POLYGON: Only polygon feature classes are returned.
  • REGION: Only region feature classes are returned.
  • ROUTE:  Only route feature classes are returned.
  • TIC: Only tic feature classes are returned.

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:

  • Feature: Geodatabase or Coverage datasets, depending on the type of workspace.
  • TIN: Only trianular irregular network datasets are returned..
  • Raster: Only raster datasets are returned.
  • CAD: Only CAD type datasets are returned.
  • ALL: Returns all above types of datasets.

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:

  • Table (dBase, INFO)
  • Shapefile (.shp)
  • Feature Class (any of the feature classes shown above in ListFeatureClasses)
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:

  • SmallInteger: Only fields of Small Integer type are returned.
  • Integer: Only fields of Integer type are returned.
  • Single: Only fields of Single precision floating point are returned.
  • Double: Only fields of Double precision floating point are returned.
  • String: Only fields of String type are returned.
  • Date: Only fields of Date type are returned.
  • OID: Only fields of OID type are returned (internal Object IDentifier number).
  • Geometry: Only fields of Geometry type are returned.
  • BLOB:  Only fields of Binary Large OBject are returned.
  • ALL: Returns all above types of fields.

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 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Related posts:

  1. Geoprocessing Iteration with Python
  2. Python Geoprocessing in ArcGIS 9.2 vs. 9.3
  3. Understanding the Geoprocessor Programming Model part 3
  4. Understanding the Geoprocessor Programming Model part 2
  5. Loading the Geoprocessor Safely

Leave a Reply

preload preload preload
Easy AdSense by Unreal