Available with Spatial Analyst license.
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 tools in the ArcGIS Spatial Analyst extension. Casting a raster allows the raster dataset to be easily queried for many properties.
- To create a Raster object, specify a layer name or path and dataset name as shown in the following example.
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.
rasObject = Raster("C:/Data/elevation")
- 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")
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")
Where the data is saved depends on what you enter in the save method and what workspace environments you have set.
- When the full path, with dataset name, is specified, then this location is where the permanent data will be saved.
- If only a dataset name is specified, then the location of the saved data is determined by the geoprocessing workspace environments.
- If the scratch or the current workspace is set, then the saved data will be saved to the location of the set workspace.
- If both the current workspace and scratch workspace are set, then the saved data will be saved to the current workspace.
- If no workspace is set, an error will be returned.
- If you simply want to persist the data where it is with its default name, then call the save method without specifying a name, as shown in the example below:
- The save method supports all raster formats supported by Spatial Analyst. In the example below the raster save method is used to save data to a file geodatabase raster and to a Tiff format raster.
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