Describing data in a geoprocessing script |
|
Release 9.1
Last modified September 14, 2005 |
|
Geoprocessing tools work with all types of data, such as geodatabase feature classes, shapefiles, rasters, tables, topologies, and networks. Each piece of data has properties that may be used to control the flow of a script or the parameters of a tool. For example, the output feature type of an intersect operation is dependent on the type of data being intersected. When the Intersect tool is run within a script on a list of input datasets, it must be able to determine the data types used so the correct output type can be set.
Using the geoprocessor's Describe method, a dataset's properties may be determined and used to make decisions.
Learn more about the Describe method
Describe creates what is known as a Dispatch object. Its properties are dynamic, depending on what data type is described. These properties come from the data elements defined by the geoprocessing COM framework. See the Data Elements Object Diagram found in the ArcGIS Developer Help system for more information. The output of Describe is an object containing properties, such as data type, fields, indices, and so on. Different dataset types have different properties, so the object created by Describe changes its properties depending on what is being described.
# Import COM Dispatch and create geoprocessor from win32com.client import Dispatch GP = Dispatch("esriGeoprocessing.GpDispatch.1") # Describe feature class desc = GP.Describe("D:/St_Johns/data.mdb/roads") type = desc.FeatureType
The fields or indexes property of a table or feature class is exposed as an enumeration. In this case, the enumeration may contain field or index objects, consisting of properties for each field or index. The ListFields and ListIndexes methods may be used to create the same enumerations or limit their contents. The following example shows how to create an enumeration of fields and how to loop through the contents to find a specific field.
# Import COM Dispatch and create geoprocessor from win32com.client import Dispatch GP = Dispatch("esriGeoprocessing.GpDispatch.1") # Describe feature class fc = "D:/St_Johns/data.mdb/roads" desc = GP.Describe(fc) fields = desc.Fields field = fields.next() while field: if field.Name == "Flag": # Set the value for the field and exit loop GP.CalculateField(fc, "Flag", "1") break field = fields.next()
Geographic datasets, such as feature classes, coverages, and rasters, have a spatial reference, which defines a dataset's coordinate system, x,y domain, m domain, and z domain. Each part of the spatial reference has a number of properties, especially the coordinate system, which defines what map projection options are used to define horizontal coordinates. All this information is available from the spatial reference property, which is actually another object containing a number of properties.
Learn more about the CreateObject method
# Describe feature class fc = "D:/St_Johns/data.mdb/roads" desc = GP.Describe(fc) # Get the spatial reference SR = desc.SpatialReference # Check if the feature class is in projected space if SR.Type == "Projected": GP.Copy(fc,"D:/St_Johns/data.mdb/UTM")
Some properties are composed of a set of values. The tolerances of a coverage or the connection properties of a workspace are examples of this. Property sets have named properties that can be called from the property set itself. In the example below, the tolerances of a coverage are printed to the standard output:
from win32com.client import Dispatch GP = Dispatch("esriGeoprocessing.GpDispatch.1") desc = GP.Describe("D:/St_Johns/freshwater") covTols = desc.tolerances print covTols.Fuzzy print covTols.Dangle print covTols.TicMatch print covTols.Edit print covTols.NodeSnap print covTols.Weed print covTols.Grain print covTols.Snap
Scripts often use paths to data, which may be problematic if the data being referenced does not exist. Data may be deleted or moved between executions of a script, which will cause errors if the path is used as a geoprocessing tool parameter. If there is a possibility of a referenced dataset not existing during a script's execution, the geoprocessor's Exists method should be used.
Learn more about the Exists method
The function simply returns a Boolean value for the existence of a dataset or object at the time of execution. Objects, such as a cursor, spatial reference, or any other object managed by the geoprocessor, may also be used as input to Exists. Exists will work with any type of data available in ArcCatalog or with any system file or folder. An ArcCatalog path must be used for this and any other method of the geoprocessor when referring to GIS data. If the data resides in an enterprise geodatabase, the name must be fully qualified.
GP.Workspace = "D:/St_Johns/data.mdb" # Clip roads feature class if it exists fc = "D:/St_Johns/data.mdb/roads" if GP.Exists(fc): GP.clip_analysis(fc,"urban_area","urban_roads")