Working with Raster objects

Raster objects can be used as input to and are the primary output of Map Algebra expressions. When executing a Map Algebra expression that uses operators, the inputs must be either Raster objects or constants. Raster outputs from Map Algebra expressions are always temporary, but can be saved by calling the save method on the Raster object. Raster objects can be queried to easily access the referenced dataset properties.

Creating a Raster object

Raster objects are created either by casting a raster dataset or as output from Spatial Analyst tools. Casting a raster allows the raster dataset to be easily queried for many properties.

  1. To create a Raster object, specify a layer name or path and dataset name as shown in the following example.
    rasObject = Raster("C:/Data/elevation")
    In the above statement, the elevation raster dataset's properties are now available through the resulting Raster object (rasObject). For a list of properties on a Raster object, see A complete list of Raster object properties.
  2. Raster objects are created as left-hand output from Spatial Analyst tools.
    # rasObject is a Raster object pointing to a temporary 
    #   raster dataset
    rasObject = Slope("C:/Data/elevation") 
    NoteNote:

    Tools outside the Spatial Analyst toolset do not output Raster objects.

Raster save method

The raster associated with the Raster object can be saved by using the save method.

Spatial Analyst tools create temporary outputs. These outputs can be saved by using the Raster object's save method. In the example below the temporary output from the Slope tool is saved to a specified output folder.

outraster = Slope("C:/Data/elevation")
outraster.save("C:/output/sloperaster")

Working with raster properties

When a raster is cast as a Raster object, it is easy to query properties from the dataset. All Raster object properties are read-only. Querying for a Raster property returns a string, a number, or an object. Raster properties can be used in many ways, including as input into a tool parameter, or to set an environment setting (as shown below).

from arcpy import env
from arcpy.sa import *
outraster = Raster("C:/Data/studyarea")
myextent = outraster.extent

# Modify myextent as necessary for your workflow and use it to set the extent environment
env.extent = myextent
NoteNote:

The raster properties available off of the Raster object are a combination of properties also accessible through the Describe function (Raster Dataset Properties) and from the Get Raster Properties tool.

Related Topics


6/28/2013