Using the Describe() function to return descriptive information about a feature class

All datasets contain information that is descriptive in nature. For example, a feature class has a name, shape type, spatial reference, and so on. This information can be valuable to your scripts when you are seeking specific information before continuing with further processing in the script. For example, you might want to perform a buffer only on polyline feature classes instead of points or polygons. Using the Describe() function, you can obtain basic descriptive information about any dataset. You can think of this information as metadata.

Getting ready

The Describe() function provides you with the ability to get basic information about datasets. These datasets could include feature classes, tables, ArcInfo coverages, layer files, workspaces, rasters, and so on. A Describe object is returned and contains specific properties, based on the data type being described. Properties on the Describe object are organized into property groups and all datasets fall into at least one property group. For example, performing Describe() against a geodatabase would return the GDB FeatureClass, FeatureClass, Table, and Dataset property groups. Each of these property groups contains specific properties that can be examined.

The Describe() function accepts a string parameter, which is a pointer to a datasource. In the following code example, we pass a feature class that is contained within a file geodatabase. The function returns a Describe object that contains a set of dynamic properties called property groups. We can then access these various properties as we have done in this case by simply printing out the properties using the print function:

arcpy.env.workspace = "c:/ArcpyBook/Ch9/CityOfSanAntonio.gdb"
desc = arcpy.Describe("Schools")
print("The feature type is: " + desc.featureType)
The feature type is: Simple
print("The shape type is: " + desc.shapeType)
The shape type is: Polygon
print("The name is: " + desc.name)
The name is: Schools
print("The path to the data is: " + desc.path)
The path to the data is: c:/ArcpyBook/Ch9/CityOfSanAntonio.gdb

All datasets, regardless of their type, contain a default set of properties located on the Describe object. These are read-only properties. Some of the more commonly used properties include dataType, catalogPath, name, path, and file.

In this recipe, you will write a script that obtains descriptive information about a feature class using the Describe() function.

How to do it…

Follow these steps to learn how to obtain descriptive information about a feature class:

  1. Open IDLE and create a new script window.
  2. Save the script as C:ArcpyBookCh9DescribeFeatureClass.py.
  3. Import the arcpy module:
    import arcpy
  4. Set the workspace:
    arcpy.env.workspace = "C:/ArcpyBook/data/CityOfSanAntonio.gdb"
  5. Start a try block:
    try:
  6. Call the Describe() function on the Burglary feature class and print out the shape type:
    descFC = arcpy.Describe("Burglary")
    print("The shape type is: " + descFC.ShapeType)
  7. Get a list of fields in the feature class and print out the name, type, and length of each:
    flds = descFC.fields
    for fld in flds:
        print("Field: " + fld.name)
        print("Type: " + fld.type
        print("Length: " + str(fld.length))
  8. Get the geographic extent of the feature class and print out the coordinates that define the extent:
    ext = descFC.extent
    print("XMin: %f" % (ext.XMin))
    print("YMin: %f" % (ext.YMin))
    print("XMax: %f" % (ext.XMax))
    print("YMax: %f" % (ext.YMax))
  9. Add the Exception block:
    except Exception as e:
        print(e.message)
  10. The entire script should appear as follows:
    import arcpy
    arcpy.env.workspace = "c:/ArcpyBook/data/CityOfSanAntonio.gdb"
    try:
        descFC = arcpy.Describe("Burglary")
        print("The shape type is: " + descFC.ShapeType)
        flds = descFC.fields
        for fld in flds:
            print("Field: " + fld.name)
            print("Type: " + fld.type)
            print("Length: " + str(fld.length))
        ext = descFC.extent
        print("XMin: %f" % (ext.XMin))
        print("YMin: %f" % (ext.YMin))
        print("XMax: %f" % (ext.XMax))
        print("YMax: %f" % (ext.YMax))
    except:
        print(arcpy.GetMessages())
  11. You can check your work by examining the C:ArcpyBookcodeCh9DescribeFeatureClass.py solution file.
  12. Save and run the script. You should see the following output:
    The shape type is: Point
    Field: OBJECTID
    Type: OID
    Length: 4
    Field: Shape
    Type: Geometry
    Length: 0
    Field: CASE
    Type: String
    Length: 11
    Field: LOCATION
    Type: String
    Length: 40
    .....
    .....
    XMin: -103.518030
    YMin: -6.145758
    XMax: -98.243208
    YMax: 29.676404
    

How it works…

Performing a Describe() against a feature class, which we have done in this script, returns a FeatureClass property group along with access to the Table and Dataset property groups, respectively. In addition to returning a FeatureClass property group, you also have access to a Table properties group.

The Table property group is important primarily because it gives you access to the fields in a standalone table or feature class. You can also access any indexes on the table or feature class through this property group. The Fields property in Table Properties returns a Python list containing one Field object for each field in the feature class. Each field has a number of read-only properties including the name, alias, length, type, scale, precision, and so on. The most obviously useful properties are name and type. In this script, we printed out the field name, type, and length. Note the use of a Python for loop to process each field in the Python list.

Finally, we printed out the geographic extent of the layer through the use of the Extent object, returned by the extent property in the Dataset property group. The Dataset property group contains a number of useful properties. Perhaps, the most used properties include extent and spatialReference, as many geoprocessing tools and scripts require this information at some point during execution. You can also obtain the datasetType and versioning information along with several other properties.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset