Using the Describe() function to return descriptive information about a raster image

Raster files also contain descriptive information that can be returned by the Describe() function.

Getting ready

A raster dataset can also be described through the use of the Describe() function. In this recipe, you will describe a raster dataset by returning its extent and spatial reference. The Describe() function contains a reference to the general purpose Dataset properties group and also contains a reference to the SpatialReference object for the dataset. The SpatialReference object can then be used to get detailed spatial reference information for the dataset.

How to do it…

Follow these steps to learn how to obtain descriptive information about a raster image file.

  1. Open IDLE and create a new script window.
  2. Save the script as C:ArcpyBookCh9DescribeRaster.py.
  3. Import the arcpy module:
    import arcpy
  4. Set the workspace:
    arcpy.env.workspace = "C:/ArcpyBook/data "
  5. Start a try block:
    try:
  6. Call the Describe() function on a raster dataset:
    descRaster = arcpy.Describe("AUSTIN_EAST_NW.sid")
  7. Get the extent of the raster dataset and print it out:
    ext = descRaster.extent
    print("XMin: %f" % (ext.XMin))
    print("YMin: %f" % (ext.YMin))
    print("XMax: %f" % (ext.XMax))
    print("YMax: %f" % (ext.YMax))
  8. Get a reference to the SpatialReference object and print it out:
    sr = descRaster.SpatialReference
    print(sr.name)
    print(sr.type)
  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"
    try:
        descRaster = arcpy.Describe("AUSTIN_EAST_NW.sid")
        ext = descRaster.extent
        print("XMin: %f" % (ext.XMin))
        print("YMin: %f" % (ext.YMin))
        print("XMax: %f" % (ext.XMax))
        print("YMax: %f" % (ext.YMax))
    
        sr = descRaster.SpatialReference
        print(sr.name)
        print(sr.type)
    except Exception as e:
        print(e.message)
  11. You can check your work by examining the C:ArcpyBookcodeCh9DescribeRaster.py solution file.
  12. Save and run the script. You should see the following output:
    XMin: 3111134.862457
    YMin: 10086853.262238
    XMax: 3131385.723907
    YMax: 10110047.019228
    NAD83_Texas_Central
    Projected
    

How it works…

This recipe is very similar to previous recipes. The difference is that we're using the Describe() function against a raster dataset instead of a vector feature class. In both cases, we've returned the geographic extent of the datasets using the extent object. However, in the script, we've also obtained the SpatialReference object for the raster dataset and printed out information about this object including its name and type.

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

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