Available with Spatial Analyst license.
Raster objects can be used as input to, and are the primary output of, map algebra expressions. When running 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 you can save them by calling the save method on the Raster object. You can query Raster objects to quickly access the referenced dataset properties.
Create 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 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 this statement, the elevation raster dataset's properties are now available through the resulting Raster object (rasObject).
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 toolbox do not output Raster objects.
Raster save method
You can save the raster associated with the Raster object by using the save method.
Spatial Analyst tools create temporary outputs. You can save these outputs by using the Raster object's save method. In the following example, 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, this location is where the permanent data will be saved.
- If only a dataset name is specified, the location of the saved data is determined by the geoprocessing workspace environments.
- If the scratch or the current workspace is set, the saved data will be saved to the location of the set workspace.
- If both the current workspace and scratch workspace are set, the saved data will be saved to the current workspace.
- If no workspace is set, an error will be returned.
- To persist the data where it is with its default name, call the save method without specifying a name, as shown in the following example:
- The save method supports all output raster data formats supported by Spatial Analyst. In the following example, the raster save method is used to save data to a file geodatabase raster and to a TIFF format raster.
Work with raster properties
When a raster is cast as a Raster object, you can query properties from the dataset. All Raster object properties are read-only. Querying 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 follows:
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